[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/navigation/ -> view.js (source)

   1  import * as __WEBPACK_EXTERNAL_MODULE__wordpress_interactivity_8e89b257__ from "@wordpress/interactivity";
   2  /******/ // The require scope
   3  /******/ var __webpack_require__ = {};
   4  /******/ 
   5  /************************************************************************/
   6  /******/ /* webpack/runtime/define property getters */
   7  /******/ (() => {
   8  /******/     // define getter functions for harmony exports
   9  /******/     __webpack_require__.d = (exports, definition) => {
  10  /******/         for(var key in definition) {
  11  /******/             if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  12  /******/                 Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  13  /******/             }
  14  /******/         }
  15  /******/     };
  16  /******/ })();
  17  /******/ 
  18  /******/ /* webpack/runtime/hasOwnProperty shorthand */
  19  /******/ (() => {
  20  /******/     __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
  21  /******/ })();
  22  /******/ 
  23  /************************************************************************/
  24  var __webpack_exports__ = {};
  25  
  26  ;// CONCATENATED MODULE: external "@wordpress/interactivity"
  27  var x = (y) => {
  28      var x = {}; __webpack_require__.d(x, y); return x
  29  } 
  30  var y = (x) => (() => (x))
  31  const interactivity_namespaceObject = x({ ["getContext"]: () => (__WEBPACK_EXTERNAL_MODULE__wordpress_interactivity_8e89b257__.getContext), ["getElement"]: () => (__WEBPACK_EXTERNAL_MODULE__wordpress_interactivity_8e89b257__.getElement), ["store"]: () => (__WEBPACK_EXTERNAL_MODULE__wordpress_interactivity_8e89b257__.store) });
  32  ;// CONCATENATED MODULE: ./node_modules/@wordpress/block-library/build-module/navigation/view.js
  33  /**
  34   * WordPress dependencies
  35   */
  36  
  37  const focusableSelectors = ['a[href]', 'input:not([disabled]):not([type="hidden"]):not([aria-hidden])', 'select:not([disabled]):not([aria-hidden])', 'textarea:not([disabled]):not([aria-hidden])', 'button:not([disabled]):not([aria-hidden])', '[contenteditable]', '[tabindex]:not([tabindex^="-"])'];
  38  
  39  // This is a fix for Safari in iOS/iPadOS. Without it, Safari doesn't focus out
  40  // when the user taps in the body. It can be removed once we add an overlay to
  41  // capture the clicks, instead of relying on the focusout event.
  42  document.addEventListener('click', () => {});
  43  const {
  44    state,
  45    actions
  46  } = (0,interactivity_namespaceObject.store)('core/navigation', {
  47    state: {
  48      get roleAttribute() {
  49        const ctx = (0,interactivity_namespaceObject.getContext)();
  50        return ctx.type === 'overlay' && state.isMenuOpen ? 'dialog' : null;
  51      },
  52      get ariaModal() {
  53        const ctx = (0,interactivity_namespaceObject.getContext)();
  54        return ctx.type === 'overlay' && state.isMenuOpen ? 'true' : null;
  55      },
  56      get ariaLabel() {
  57        const ctx = (0,interactivity_namespaceObject.getContext)();
  58        return ctx.type === 'overlay' && state.isMenuOpen ? ctx.ariaLabel : null;
  59      },
  60      get isMenuOpen() {
  61        // The menu is opened if either `click`, `hover` or `focus` is true.
  62        return Object.values(state.menuOpenedBy).filter(Boolean).length > 0;
  63      },
  64      get menuOpenedBy() {
  65        const ctx = (0,interactivity_namespaceObject.getContext)();
  66        return ctx.type === 'overlay' ? ctx.overlayOpenedBy : ctx.submenuOpenedBy;
  67      }
  68    },
  69    actions: {
  70      openMenuOnHover() {
  71        const {
  72          type,
  73          overlayOpenedBy
  74        } = (0,interactivity_namespaceObject.getContext)();
  75        if (type === 'submenu' &&
  76        // Only open on hover if the overlay is closed.
  77        Object.values(overlayOpenedBy || {}).filter(Boolean).length === 0) {
  78          actions.openMenu('hover');
  79        }
  80      },
  81      closeMenuOnHover() {
  82        const {
  83          type,
  84          overlayOpenedBy
  85        } = (0,interactivity_namespaceObject.getContext)();
  86        if (type === 'submenu' &&
  87        // Only close on hover if the overlay is closed.
  88        Object.values(overlayOpenedBy || {}).filter(Boolean).length === 0) {
  89          actions.closeMenu('hover');
  90        }
  91      },
  92      openMenuOnClick() {
  93        const ctx = (0,interactivity_namespaceObject.getContext)();
  94        const {
  95          ref
  96        } = (0,interactivity_namespaceObject.getElement)();
  97        ctx.previousFocus = ref;
  98        actions.openMenu('click');
  99      },
 100      closeMenuOnClick() {
 101        actions.closeMenu('click');
 102        actions.closeMenu('focus');
 103      },
 104      openMenuOnFocus() {
 105        actions.openMenu('focus');
 106      },
 107      toggleMenuOnClick() {
 108        const ctx = (0,interactivity_namespaceObject.getContext)();
 109        const {
 110          ref
 111        } = (0,interactivity_namespaceObject.getElement)();
 112        // Safari won't send focus to the clicked element, so we need to manually place it: https://bugs.webkit.org/show_bug.cgi?id=22261
 113        if (window.document.activeElement !== ref) ref.focus();
 114        const {
 115          menuOpenedBy
 116        } = state;
 117        if (menuOpenedBy.click || menuOpenedBy.focus) {
 118          actions.closeMenu('click');
 119          actions.closeMenu('focus');
 120        } else {
 121          ctx.previousFocus = ref;
 122          actions.openMenu('click');
 123        }
 124      },
 125      handleMenuKeydown(event) {
 126        const {
 127          type,
 128          firstFocusableElement,
 129          lastFocusableElement
 130        } = (0,interactivity_namespaceObject.getContext)();
 131        if (state.menuOpenedBy.click) {
 132          // If Escape close the menu.
 133          if (event?.key === 'Escape') {
 134            actions.closeMenu('click');
 135            actions.closeMenu('focus');
 136            return;
 137          }
 138  
 139          // Trap focus if it is an overlay (main menu).
 140          if (type === 'overlay' && event.key === 'Tab') {
 141            // If shift + tab it change the direction.
 142            if (event.shiftKey && window.document.activeElement === firstFocusableElement) {
 143              event.preventDefault();
 144              lastFocusableElement.focus();
 145            } else if (!event.shiftKey && window.document.activeElement === lastFocusableElement) {
 146              event.preventDefault();
 147              firstFocusableElement.focus();
 148            }
 149          }
 150        }
 151      },
 152      handleMenuFocusout(event) {
 153        const {
 154          modal,
 155          type
 156        } = (0,interactivity_namespaceObject.getContext)();
 157        // If focus is outside modal, and in the document, close menu
 158        // event.target === The element losing focus
 159        // event.relatedTarget === The element receiving focus (if any)
 160        // When focusout is outsite the document,
 161        // `window.document.activeElement` doesn't change.
 162  
 163        // The event.relatedTarget is null when something outside the navigation menu is clicked. This is only necessary for Safari.
 164        if (event.relatedTarget === null || !modal?.contains(event.relatedTarget) && event.target !== window.document.activeElement && type === 'submenu') {
 165          actions.closeMenu('click');
 166          actions.closeMenu('focus');
 167        }
 168      },
 169      openMenu(menuOpenedOn = 'click') {
 170        const {
 171          type
 172        } = (0,interactivity_namespaceObject.getContext)();
 173        state.menuOpenedBy[menuOpenedOn] = true;
 174        if (type === 'overlay') {
 175          // Add a `has-modal-open` class to the <html> root.
 176          document.documentElement.classList.add('has-modal-open');
 177        }
 178      },
 179      closeMenu(menuClosedOn = 'click') {
 180        const ctx = (0,interactivity_namespaceObject.getContext)();
 181        state.menuOpenedBy[menuClosedOn] = false;
 182        // Check if the menu is still open or not.
 183        if (!state.isMenuOpen) {
 184          if (ctx.modal?.contains(window.document.activeElement)) {
 185            ctx.previousFocus?.focus();
 186          }
 187          ctx.modal = null;
 188          ctx.previousFocus = null;
 189          if (ctx.type === 'overlay') {
 190            document.documentElement.classList.remove('has-modal-open');
 191          }
 192        }
 193      }
 194    },
 195    callbacks: {
 196      initMenu() {
 197        const ctx = (0,interactivity_namespaceObject.getContext)();
 198        const {
 199          ref
 200        } = (0,interactivity_namespaceObject.getElement)();
 201        if (state.isMenuOpen) {
 202          const focusableElements = ref.querySelectorAll(focusableSelectors);
 203          ctx.modal = ref;
 204          ctx.firstFocusableElement = focusableElements[0];
 205          ctx.lastFocusableElement = focusableElements[focusableElements.length - 1];
 206        }
 207      },
 208      focusFirstElement() {
 209        const {
 210          ref
 211        } = (0,interactivity_namespaceObject.getElement)();
 212        if (state.isMenuOpen) {
 213          const focusableElements = ref.querySelectorAll(focusableSelectors);
 214          focusableElements?.[0]?.focus();
 215        }
 216      }
 217    }
 218  }, {
 219    lock: true
 220  });
 221  


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref