UNPKG

@wordpress/interactivity

Version:

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

40 lines (36 loc) 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = exports.getRegionRootFragment = void 0; var _preact = require("preact"); var _vdom = require("./vdom"); var _utils = require("./utils"); var _constants = require("./constants"); /** * External dependencies */ /** * Internal dependencies */ // Keep the same root fragment for each interactive region node. const regionRootFragments = new WeakMap(); const getRegionRootFragment = region => { if (!regionRootFragments.has(region)) { regionRootFragments.set(region, (0, _utils.createRootFragment)(region.parentElement, region)); } return regionRootFragments.get(region); }; // Initialize the router with the initial DOM. exports.getRegionRootFragment = getRegionRootFragment; const init = async () => { document.querySelectorAll(`[data-${_constants.directivePrefix}-interactive]`).forEach(node => { if (!_vdom.hydratedIslands.has(node)) { const fragment = getRegionRootFragment(node); const vdom = (0, _vdom.toVdom)(node); (0, _preact.hydrate)(vdom, fragment); } }); }; exports.init = init; //# sourceMappingURL=init.js.map