import { LitElement, html, css } from 'lit'; export class RocketDrawer extends LitElement { static properties = { open: { type: Boolean, reflect: true }, }; constructor() { super(); this.open = false; } connectedCallback() { super.connectedCallback(); this._onDocClick = this._onDocClick.bind(this); this._onKeyDown = this._onKeyDown.bind(this); document.addEventListener('click', this._onDocClick, { capture: true }); document.addEventListener('keydown', this._onKeyDown); } disconnectedCallback() { document.removeEventListener('click', this._onDocClick, { capture: true }); document.removeEventListener('keydown', this._onKeyDown); super.disconnectedCallback(); } /** * @param {KeyboardEvent} ev */ _onKeyDown(ev) { if (ev.key === 'Escape' && this.open) { this.close(); } } /** * @param {Map} changedProperties */ updated(changedProperties) { // skip the initial render so the drawer never steals focus on page load if (!changedProperties.has('open') || changedProperties.get('open') === undefined) { return; } if (this.open) { this.shadowRoot?.getElementById('content')?.focus(); } else { const invokerSlot = /** @type {HTMLSlotElement | null} */ ( this.shadowRoot?.querySelector('slot[name="invoker"]') ?? null ); const assigned = /** @type {HTMLElement | undefined} */ (invokerSlot?.assignedElements()[0]); const fallback = /** @type {HTMLElement | null} */ ( this.shadowRoot?.querySelector('.burger-menu') ?? null ); (assigned ?? fallback)?.focus(); } } render() { return html`
`; } // Close when clicking anywhere outside the dialog or the invoker /** * @param {any} ev */ _onDocClick(ev) { if (!this.open) return; const path = ev.composedPath(); const dlg = this.shadowRoot?.getElementById('rd-dialog'); const invoker = this.shadowRoot?.querySelector('slot[name="invoker"]'); const clickInsideDialog = path.includes(dlg); const clickOnInvoker = path.includes(invoker) || path.some((/** @type {{ assignedSlot: Element | null | undefined; }} */ n) => { if (n?.assignedSlot && n.assignedSlot === invoker) return true; return false; }); if (!clickInsideDialog && !clickOnInvoker) this.close(); } close() { this.open = false; } toggle() { this.open = !this.open; } #iconBurger() { return html` `; } #iconClose() { return html` `; } static styles = css` :host { display: block; --rd-text-1: #2c3e50; --rd-ease-3: cubic-bezier(0.25, 0, 0.3, 1); --rd-target-width: 80%; } * { box-sizing: border-box; } .burger-menu { background: none; border: none; padding: 0; margin: 0; line-height: 0; cursor: pointer; color: var(--rd-text-1); position: relative; z-index: 101; } .icon { width: 2.3rem; display: block; transition: transform 0.2s ease; } .burger-menu[aria-expanded='true'] .icon { transform: rotate(90deg); } dialog { display: block; position: fixed; top: 0; right: 0; left: auto; height: 100vh; width: 0; margin: 0; padding: 0; border: none; color: var(--rd-text-1); box-shadow: -4px 0 16px rgba(0, 0, 0, 0.35); z-index: 100; overflow: hidden; opacity: 0; pointer-events: none; /* visibility keeps the closed drawer out of tab order and the a11y tree */ visibility: hidden; transition: width 0.35s var(--rd-ease-3), opacity 0.35s var(--rd-ease-3), visibility 0s var(--rd-ease-3) 0.35s; will-change: width; } dialog[open] { width: var(--rd-target-width); opacity: 1; pointer-events: auto; visibility: visible; transition: width 0.35s var(--rd-ease-3), opacity 0.35s var(--rd-ease-3), visibility 0s; } #content { height: 100%; padding: 1rem; max-width: var(--rd-target-width); overflow-y: auto; } @media (prefers-reduced-motion: reduce) { dialog, dialog::backdrop { transition: none; } } .sr-only { position: absolute; left: -10000px; top: auto; width: 1px; height: 1px; overflow: hidden; } `; }