UNPKG

@wordpress/interactivity

Version:

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

31 lines (29 loc) 949 B
/** * External dependencies */ import { hydrate } from 'preact'; /** * Internal dependencies */ import { toVdom, hydratedIslands } from './vdom'; import { createRootFragment } from './utils'; import { directivePrefix } from './constants'; // Keep the same root fragment for each interactive region node. const regionRootFragments = new WeakMap(); export const getRegionRootFragment = region => { if (!regionRootFragments.has(region)) { regionRootFragments.set(region, createRootFragment(region.parentElement, region)); } return regionRootFragments.get(region); }; // Initialize the router with the initial DOM. export const init = async () => { document.querySelectorAll(`[data-${directivePrefix}-interactive]`).forEach(node => { if (!hydratedIslands.has(node)) { const fragment = getRegionRootFragment(node); const vdom = toVdom(node); hydrate(vdom, fragment); } }); }; //# sourceMappingURL=init.js.map