UMD Web Utilities Library
    Preparing search index...

    Function trapFocus

    • Traps focus within an element for accessibility

      Prevents focus from leaving the specified element when Tab is pressed. Essential for modal dialogs and other components that require focus containment per WCAG 2.1.

      Parameters

      • element: {
            action: (event: KeyboardEvent) => void;
            element: HTMLElement | Element;
            shadowDomContext?: HTMLElement | null;
        }

        The element to trap focus within

      Returns () => void

      Cleanup function to remove event listeners

      const cleanup = trapFocus({
      element: modalElement,
      action: (event) => {
      // Close modal when Escape is pressed
      modal.close();
      },
      shadowDomContext: shadowRoot
      });

      // Later, cleanup when modal closes
      cleanup();