UNPKG

@wordpress/interactivity

Version:

Package that provides a standard and simple way to handle the frontend interactivity of Gutenberg blocks.

8 lines (7 loc) 5.28 kB
{ "version": 3, "sources": ["../src/index.ts"], "sourcesContent": ["if ( globalThis.SCRIPT_DEBUG ) {\n\tawait import( 'preact/debug' );\n}\n\n/**\n * External dependencies\n */\nimport { h, cloneElement, render } from 'preact';\nimport { batch, effect } from '@preact/signals';\n\n/**\n * Internal dependencies\n */\nimport registerDirectives, { routerRegions } from './directives';\nimport {\n\tinitialVdomPromise,\n\thydrateRegions,\n\tgetRegionRootFragment,\n} from './hydration';\nimport { toVdom } from './vdom';\nimport { directive } from './hooks';\nimport { getNamespace } from './namespaces';\nimport { parseServerData, populateServerData } from './store';\nimport { proxifyState } from './proxies';\nimport {\n\tdeepReadOnly,\n\tnavigationSignal,\n\tonDOMReady,\n\tsessionId,\n\twarn,\n} from './utils';\n\nexport {\n\tstore,\n\tgetConfig,\n\tgetServerState,\n\ttype AsyncAction,\n\ttype TypeYield,\n} from './store';\nexport { getContext, getServerContext, getElement } from './scopes';\nexport {\n\twithScope,\n\tuseWatch,\n\tuseInit,\n\tuseEffect,\n\tuseLayoutEffect,\n\tuseCallback,\n\tuseMemo,\n\tsplitTask,\n\twithSyncEvent,\n} from './utils';\n\nexport { useState, useRef } from 'preact/hooks';\n\n/**\n * Subscribes to changes in any signal accessed inside the callback, re-running\n * the callback whenever those signals change. Returns a cleanup function to\n * stop watching.\n *\n * @example\n * ```js\n * const unwatch = watch( () => {\n * console.log( state.counter );\n * } );\n *\n * // Later, to stop watching:\n * unwatch();\n * ```\n */\nexport const watch = effect;\n\nconst requiredConsent =\n\t'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress.';\n\nexport const privateApis = (\n\tlock: 'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress.'\n): any => {\n\tif ( lock === requiredConsent ) {\n\t\treturn {\n\t\t\tgetRegionRootFragment,\n\t\t\tinitialVdomPromise,\n\t\t\ttoVdom,\n\t\t\tdirective,\n\t\t\tgetNamespace,\n\t\t\th,\n\t\t\tcloneElement,\n\t\t\trender,\n\t\t\tproxifyState,\n\t\t\tparseServerData,\n\t\t\tpopulateServerData,\n\t\t\tbatch,\n\t\t\trouterRegions,\n\t\t\tdeepReadOnly,\n\t\t\tnavigationSignal,\n\t\t\tsessionId,\n\t\t\twarn,\n\t\t};\n\t}\n\n\tthrow new Error( 'Forbidden access.' );\n};\n\n// Parses and populates the initial state and config. All the core directives\n// are registered at this point as well.\npopulateServerData( parseServerData() );\nregisterDirectives();\n\n// Hydrates all interactive regions when `DOMContentLoaded` is dispatched, or as\n// soon as the `@wordpress/interactivity` module is evaluated in the case that\n// the event was already dispatched. This ensures synchronous modules had the\n// opportunity to register their stores before hydration takes place. For\n// asynchronous modules, or modules importing this module asynchronously, this\n// cannot be guaranteed.\nonDOMReady( hydrateRegions );\n\n// Tag the current history entry with the session ID so that, within the same\n// session, all entries share the same ID and back/forward works normally.\nwindow.history.replaceState(\n\t{ ...window.history.state, wpInteractivityId: sessionId },\n\t''\n);\n\n// When the browser fires `popstate` for a history entry that was created in a\n// different session (i.e., before a full page reload), force a reload so the\n// server can render the correct content. Without this, the URL would change but\n// the page content would remain stale because the interactivity router \u2014 which\n// handles client-side navigations \u2014 might not be loaded yet.\n//\n// Some `popstate` events (e.g., anchor/fragment navigations like\n// clicking `<a href=\"#section\">`) have `null` state. These are\n// same-document navigations and must NOT trigger a reload \u2014 the browser\n// should just scroll to the target element as normal.\nwindow.addEventListener( 'popstate', ( event ) => {\n\tif (\n\t\tevent.state !== null &&\n\t\tevent.state?.wpInteractivityId !== sessionId\n\t) {\n\t\twindow.location.reload();\n\t}\n} );\n"], "mappings": ";AAOA,SAAS,GAAG,cAAc,cAAc;AACxC,SAAS,OAAO,cAAc;AAK9B,OAAO,sBAAsB,qBAAqB;AAClD;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,cAAc;AACvB,SAAS,iBAAi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