~cytrogen/masto-fe

ref: 496fd5987ecb67ac51888fde30b7fb76cf829edf masto-fe/app/javascript/flavours/glitch/packs/settings.js -rw-r--r-- 1.1 KiB
496fd598 — Zoë Bijl [bugfix] correct import of `@rails/ujs` / fix lint issues (#92) 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import 'packs/public-path';
import Rails  from '@rails/ujs';

import loadKeyboardExtensions from 'flavours/glitch/load_keyboard_extensions';
import { loadPolyfills } from 'flavours/glitch/polyfills';
import 'cocoon-js-vanilla';

function main() {
  const toggleSidebar = () => {
    const sidebar = document.querySelector('.sidebar ul');
    const toggleButton = document.querySelector('.sidebar__toggle__icon');

    if (sidebar.classList.contains('visible')) {
      document.body.style.overflow = null;
      toggleButton.setAttribute('aria-expanded', 'false');
    } else {
      document.body.style.overflow = 'hidden';
      toggleButton.setAttribute('aria-expanded', 'true');
    }

    toggleButton.classList.toggle('active');
    sidebar.classList.toggle('visible');
  };

  Rails.delegate(document, '.sidebar__toggle__icon', 'click', () => {
    toggleSidebar();
  });

  Rails.delegate(document, '.sidebar__toggle__icon', 'keydown', e => {
    if (e.key === ' ' || e.key === 'Enter') {
      e.preventDefault();
      toggleSidebar();
    }
  });
}

loadPolyfills()
  .then(main)
  .then(loadKeyboardExtensions)
  .catch(error => {
    console.error(error);
  });