UNPKG

@wordpress/interactivity

Version:

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

36 lines (33 loc) 953 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 ); } } ); };