layout.js 1.4 KB

1234567891011121314151617181920212223242526272829
  1. /* Navigation can wrap, and HTMX replaces the body on boosted navigation. */
  2. (() => {
  3. let observer;
  4. function observeLayout() {
  5. observer?.disconnect();
  6. const header = document.querySelector('.gc-app-header');
  7. const context = document.querySelector('.gc-case-context');
  8. const measure = () => {
  9. document.documentElement.style.setProperty('--gc-header-height', `${header?.getBoundingClientRect().height || 0}px`);
  10. document.documentElement.style.setProperty('--gc-context-height', `${context?.getBoundingClientRect().height || 0}px`);
  11. };
  12. measure();
  13. observer = new ResizeObserver(measure);
  14. if (header) observer.observe(header);
  15. if (context) observer.observe(context);
  16. }
  17. // A label dispatches a second click to its checkbox. Use checked state once.
  18. document.addEventListener('change', event => {
  19. const input = event.target;
  20. if (!input.matches('input[data-gc-collapse]')) return;
  21. const panel = document.getElementById(input.getAttribute('aria-controls'));
  22. if (panel) panel.classList.toggle('show', input.checked);
  23. input.setAttribute('aria-expanded', String(input.checked));
  24. });
  25. document.addEventListener('DOMContentLoaded', observeLayout);
  26. document.addEventListener('htmx:load', observeLayout);
  27. document.addEventListener('htmx:historyRestore', observeLayout);
  28. window.addEventListener('pageshow', observeLayout);
  29. })();