@wordpress/components
Version:
UI components for WordPress.
202 lines (201 loc) • 10.5 kB
JavaScript
import clsx from "clsx";
import { useEvent } from "@wordpress/compose";
import { useCallback, useEffect, useLayoutEffect, useRef } from "@wordpress/element";
import { useToolsPanelContext } from "../context.mjs";
import { useContextSystem } from "../../context/index.mjs";
// packages/style-runtime/src/index.ts
const STYLE_HASH_ATTRIBUTE = "data-wp-hash";
/**
* Returns the shared style runtime registry.
*
* The registry is stored on `globalThis` so separately bundled copies of this
* package can coordinate through the same document and style maps.
*
* @return The shared runtime registry.
*/
function getRuntime() {
const globalScope = globalThis;
if (globalScope.__wpStyleRuntime) return globalScope.__wpStyleRuntime;
globalScope.__wpStyleRuntime = {
documents: /* @__PURE__ */ new Map(),
styles: /* @__PURE__ */ new Map(),
injectedStyles: /* @__PURE__ */ new WeakMap()
};
if (typeof document !== "undefined") registerDocument(document);
return globalScope.__wpStyleRuntime;
}
/**
* Checks whether a document already contains a style tag for a hash.
*
* @param targetDocument Document to inspect.
* @param hash Stable hash for the transformed CSS.
*
* @return Whether the style hash already exists in the document.
*/
function documentContainsStyleHash(targetDocument, hash) {
if (!targetDocument.head) return false;
for (const style of targetDocument.head.querySelectorAll(`style[${STYLE_HASH_ATTRIBUTE}]`)) if (style.getAttribute(STYLE_HASH_ATTRIBUTE) === hash) return true;
return false;
}
/**
* Injects a registered style into a document, unless that document already
* contains a style tag for the same hash.
*
* @param targetDocument Document to inject the style into.
* @param hash Stable hash for the transformed CSS.
* @param css CSS text to inject.
*/
function injectStyle(targetDocument, hash, css) {
if (!targetDocument.head) return;
const runtime = getRuntime();
let injectedStyles = runtime.injectedStyles.get(targetDocument);
if (!injectedStyles) {
injectedStyles = /* @__PURE__ */ new Set();
runtime.injectedStyles.set(targetDocument, injectedStyles);
}
if (injectedStyles.has(hash)) return;
if (documentContainsStyleHash(targetDocument, hash)) {
injectedStyles.add(hash);
return;
}
const style = targetDocument.createElement("style");
style.setAttribute(STYLE_HASH_ATTRIBUTE, hash);
style.appendChild(targetDocument.createTextNode(css));
targetDocument.head.appendChild(style);
injectedStyles.add(hash);
}
/**
* Registers a document as a style injection target.
*
* Existing registered styles are replayed into the document immediately.
* Documents are reference-counted so multiple providers can safely register the
* same document without one cleanup removing it while another registration is
* still active.
*
* @param targetDocument Document to receive registered styles.
* @return Cleanup function that unregisters this document registration.
*/
function registerDocument(targetDocument) {
const runtime = getRuntime();
runtime.documents.set(targetDocument, (runtime.documents.get(targetDocument) ?? 0) + 1);
for (const [hash, css] of runtime.styles) injectStyle(targetDocument, hash, css);
return () => {
const count = runtime.documents.get(targetDocument);
if (count === void 0) return;
if (count <= 1) {
runtime.documents.delete(targetDocument);
return;
}
runtime.documents.set(targetDocument, count - 1);
};
}
/**
* Registers a style and injects it into all registered documents.
*
* The hash is used as the deduplication key, so calling this repeatedly with
* the same hash will not add duplicate style tags to a document.
* Registered styles are retained for the lifetime of the page so they can be
* replayed into documents that are registered later.
*
* @param hash Stable hash for the transformed CSS.
* @param css CSS text to inject.
*/
function registerStyle(hash, css) {
const runtime = getRuntime();
runtime.styles.set(hash, css);
for (const targetDocument of runtime.documents.keys()) injectStyle(targetDocument, hash, css);
}
// packages/components/src/tools-panel/style.module.scss
if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("2b5d59eda1", "._6c1192219f33c902__tools-panel{border-top:1px solid #ddd;margin-top:-1px;padding:16px}.cd15a9c80fb6cbd4__tools-panel-with-inner-wrapper>div:not(:first-of-type){column-gap:var(--wpds-dimension-gap-lg,16px);display:grid;grid-column:1/-1;grid-template-columns:repeat(2,minmax(0,1fr));row-gap:var(--wpds-dimension-gap-lg,16px)}.e58228332b88343f__tools-panel-hidden-inner-wrapper>div:not(:first-of-type){display:none}._00ef83981f008ed1__tools-panel-header{gap:8px;grid-column:1/-1}._00ef83981f008ed1__tools-panel-header .components-dropdown-menu{line-height:0;margin:-4px 0}._00ef83981f008ed1__tools-panel-header._00ef83981f008ed1__tools-panel-header._00ef83981f008ed1__tools-panel-header._00ef83981f008ed1__tools-panel-header .components-dropdown-menu__toggle{min-width:24px;padding:0}._07c5a099c6314de5__tools-panel-heading._07c5a099c6314de5__tools-panel-heading{font-size:inherit;line-height:normal;margin:0}._900a3554bc5023e1__tools-panel-item{grid-column:1/-1}._900a3554bc5023e1__tools-panel-item>div,._900a3554bc5023e1__tools-panel-item>fieldset{margin-bottom:0;max-width:100%;padding-bottom:0}._900a3554bc5023e1__tools-panel-item .components-base-control__help,._900a3554bc5023e1__tools-panel-item._900a3554bc5023e1__tools-panel-item .components-base-control,._900a3554bc5023e1__tools-panel-item._900a3554bc5023e1__tools-panel-item .components-base-control .components-base-control__field:last-child{margin-bottom:0}._175ccc0d8111dddf__tools-panel-item-placeholder{display:none}._4193bdc190e9fde6__dropdown-menu{min-width:200px}.d094cd83a4e22156__reset-label{color:var(--wp-components-color-accent-darker-10,var(--wp-admin-theme-color-darker-10,#2145e6));font-size:var(--wpds-typography-font-size-xs,11px);font-weight:var(--wpds-typography-font-weight-emphasis,600);line-height:1.4;margin-inline-start:12px;text-transform:uppercase}._3de4c27602641134__default-controls-item{color:#1e1e1e}._3de4c27602641134__default-controls-item:focus .d094cd83a4e22156__reset-label{color:inherit}._3de4c27602641134__default-controls-item._3de4c27602641134__default-controls-item[aria-disabled=true]{color:#757575;opacity:1}._3de4c27602641134__default-controls-item._3de4c27602641134__default-controls-item[aria-disabled=true]:hover{color:#757575}._3de4c27602641134__default-controls-item._3de4c27602641134__default-controls-item[aria-disabled=true] .d094cd83a4e22156__reset-label{opacity:.3}");
var style_module_default = {
"tools-panel": "_6c1192219f33c902__tools-panel",
"tools-panel-with-inner-wrapper": "cd15a9c80fb6cbd4__tools-panel-with-inner-wrapper",
"tools-panel-hidden-inner-wrapper": "e58228332b88343f__tools-panel-hidden-inner-wrapper",
"tools-panel-header": "_00ef83981f008ed1__tools-panel-header",
"tools-panel-heading": "_07c5a099c6314de5__tools-panel-heading",
"tools-panel-item": "_900a3554bc5023e1__tools-panel-item",
"tools-panel-item-placeholder": "_175ccc0d8111dddf__tools-panel-item-placeholder",
"dropdown-menu": "_4193bdc190e9fde6__dropdown-menu",
"reset-label": "d094cd83a4e22156__reset-label",
"default-controls-item": "_3de4c27602641134__default-controls-item"
};
// packages/components/src/tools-panel/tools-panel-item/hook.ts
const noop = () => {};
function useToolsPanelItem(props) {
const { className, defaultShown, hasValue, isShownByDefault = false, label, panelId, resetAllFilter = noop, onDeselect, onSelect, onShownChange, ...otherProps } = useContextSystem(props, "ToolsPanelItem");
const { panelId: currentPanelId, menuItems, registerPanelItem, deregisterPanelItem, flagItemCustomization, isResetting, shouldRenderPlaceholderItems: shouldRenderPlaceholder, firstDisplayedItem, lastDisplayedItem, __experimentalFirstVisibleItemClass, __experimentalLastVisibleItemClass } = useToolsPanelContext();
const hasValueCallback = useCallback(hasValue, [panelId]);
const resetAllFilterCallback = useEvent(resetAllFilter);
const defaultShownRef = useRef(defaultShown);
useLayoutEffect(() => {
defaultShownRef.current = defaultShown;
});
const onShownChangeCallback = useEvent(onShownChange);
const hasMatchingPanel = currentPanelId === panelId || currentPanelId === null;
useLayoutEffect(() => {
if (!hasMatchingPanel) return;
const item = {
defaultShown: defaultShownRef.current,
hasValue: hasValueCallback,
isShownByDefault,
label,
onShownChange: onShownChangeCallback,
panelId,
resetAllFilter: resetAllFilterCallback
};
registerPanelItem(item);
return () => {
deregisterPanelItem(label, item);
};
}, [
deregisterPanelItem,
hasMatchingPanel,
hasValueCallback,
isShownByDefault,
label,
onShownChangeCallback,
panelId,
registerPanelItem,
resetAllFilterCallback
]);
const isMenuItemChecked = menuItems?.[isShownByDefault ? "default" : "optional"]?.[label];
const isRegistered = isMenuItemChecked !== void 0;
const wasMenuItemCheckedRef = useRef(void 0);
const isValueSet = hasValue();
useLayoutEffect(() => {
if (!hasMatchingPanel || !isShownByDefault && !isValueSet) return;
flagItemCustomization(isValueSet, label);
}, [
hasMatchingPanel,
isValueSet,
label,
flagItemCustomization,
isShownByDefault
]);
useEffect(() => {
const wasMenuItemChecked = wasMenuItemCheckedRef.current;
wasMenuItemCheckedRef.current = isMenuItemChecked;
if (!isRegistered || !(wasMenuItemChecked !== void 0) || isResetting || !hasMatchingPanel) return;
if (isMenuItemChecked && !isValueSet && !wasMenuItemChecked) onSelect?.();
if (!isMenuItemChecked && isValueSet && wasMenuItemChecked) onDeselect?.();
}, [
hasMatchingPanel,
isMenuItemChecked,
isRegistered,
isResetting,
isValueSet,
onSelect,
onDeselect
]);
const isShown = isShownByDefault ? isRegistered : isMenuItemChecked;
const shouldApplyPlaceholderStyles = shouldRenderPlaceholder && !isShown;
const classes = clsx(style_module_default["tools-panel-item"], shouldApplyPlaceholderStyles && style_module_default["tools-panel-item-placeholder"], !shouldApplyPlaceholderStyles && className, firstDisplayedItem === label && __experimentalFirstVisibleItemClass, lastDisplayedItem === label && __experimentalLastVisibleItemClass);
return {
...otherProps,
isShown,
shouldRenderPlaceholder,
className: classes
};
}
export { useToolsPanelItem };
//# sourceMappingURL=hook.mjs.map