UNPKG

@openproject/primer-view-components

Version:

ViewComponents of the Primer Design System for OpenProject

63 lines (62 loc) 3.99 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _ActionMenuFocusZoneStack_instances, _ActionMenuFocusZoneStack_stack, _ActionMenuFocusZoneStack_setupFocusZone, _ActionMenuFocusZoneStack_validItemRoles_get; import { FocusKeys, focusTrap, focusZone } from '@primer/behaviors'; import { ActionMenuElement } from './action_menu_element'; export class ActionMenuFocusZoneStack { constructor() { _ActionMenuFocusZoneStack_instances.add(this); _ActionMenuFocusZoneStack_stack.set(this, void 0); __classPrivateFieldSet(this, _ActionMenuFocusZoneStack_stack, [], "f"); } get current() { return __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f")[__classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").length - 1]; } push(next, options = { trapFocus: true }) { const { trapFocus } = options; __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").push({ element: next, abortController: __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_instances, "m", _ActionMenuFocusZoneStack_setupFocusZone).call(this, next, trapFocus) }); } pop(target) { if (target) { while (__classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").length > 0 && this.current?.element !== target) { const entry = __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").pop(); entry?.abortController?.abort(); } } const entry = __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").pop(); entry?.abortController?.abort(); } elementIsMenuItem(element) { return __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_instances, "a", _ActionMenuFocusZoneStack_validItemRoles_get).includes(element.getAttribute('role') || ''); } get isEmpty() { return __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").length === 0; } } _ActionMenuFocusZoneStack_stack = new WeakMap(), _ActionMenuFocusZoneStack_instances = new WeakSet(), _ActionMenuFocusZoneStack_setupFocusZone = function _ActionMenuFocusZoneStack_setupFocusZone(containerEl, trapFocus) { const focusZoneAbortController = focusZone(containerEl, { bindKeys: FocusKeys.ArrowVertical | FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd | FocusKeys.Backspace, focusOutBehavior: 'wrap', focusableElementFilter: (element) => { return this.elementIsMenuItem(element) && element.closest('anchored-position') === containerEl; }, }); if (trapFocus) { const { signal: focusZoneSignal } = focusZoneAbortController; return focusTrap(containerEl, undefined, focusZoneSignal); } else { return focusZoneAbortController; } }, _ActionMenuFocusZoneStack_validItemRoles_get = function _ActionMenuFocusZoneStack_validItemRoles_get() { return ActionMenuElement.validItemRoles; };