UNPKG

@wordpress/interactivity-router

Version:

Package that exposes state and actions from the `core/router` store, part of the Interactivity API.

43 lines (40 loc) 2.36 kB
/* wp:polyfill */ "use strict"; function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } // Check if the link is valid for client-side navigation. const isValidLink = ref => ref && ref instanceof window.HTMLAnchorElement && ref.href && (!ref.target || ref.target === '_self') && ref.origin === window.location.origin && !ref.pathname.startsWith('/wp-admin') && !ref.pathname.startsWith('/wp-login.php') && !ref.getAttribute('href').startsWith('#') && !new URL(ref.href).searchParams.has('_wpnonce'); // Check if the event is valid for client-side navigation. const isValidEvent = event => event && event.button === 0 && // Left clicks only. !event.metaKey && // Open in new tab (Mac). !event.ctrlKey && // Open in new tab (Windows). !event.altKey && // Download. !event.shiftKey && !event.defaultPrevented; // Navigate on click. document.addEventListener('click', async event => { const ref = event.target.closest('a'); if (isValidLink(ref) && isValidEvent(event)) { event.preventDefault(); const { actions } = await Promise.resolve().then(() => _interopRequireWildcard(require('@wordpress/interactivity-router'))); actions.navigate(ref.href); } }); // Prefetch on hover. document.addEventListener('mouseenter', async event => { if (event.target?.nodeName === 'A') { const ref = event.target.closest('a'); if (isValidLink(ref) && isValidEvent(event)) { const { actions } = await Promise.resolve().then(() => _interopRequireWildcard(require('@wordpress/interactivity-router'))); actions.prefetch(ref.href); } } }, true); //# sourceMappingURL=full-page.js.map