UNPKG

@wordpress/components

Version:
228 lines (227 loc) • 11.5 kB
import { speak } from "@wordpress/a11y"; import { check, moreVertical, plus } from "@wordpress/icons"; import { __, _x, sprintf } from "@wordpress/i18n"; import DropdownMenu from "../../dropdown-menu/index.mjs"; import MenuGroup from "../../menu-group/index.mjs"; import MenuItem from "../../menu-item/index.mjs"; import { HStack } from "../../h-stack/index.mjs"; import { Heading } from "../../heading/index.mjs"; import { useToolsPanelHeader } from "./hook.mjs"; import { contextConnect } from "../../context/index.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // 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-header/component.tsx const DefaultControlsGroup = ({ itemClassName, items, toggleItem }) => { if (!items.length) return null; const resetSuffix = /* @__PURE__ */ jsx("span", { "aria-hidden": true, className: style_module_default["reset-label"], children: __("Reset") }); return /* @__PURE__ */ jsx(Fragment, { children: items.map(([label, hasValue]) => { if (hasValue) return /* @__PURE__ */ jsx(MenuItem, { className: itemClassName, role: "menuitem", label: sprintf(__("%s, reset to default"), label), onClick: () => { toggleItem(label); speak(sprintf(__("%s reset to default"), label), "assertive"); }, suffix: resetSuffix, children: label }, label); return /* @__PURE__ */ jsx(MenuItem, { icon: check, className: itemClassName, role: "menuitemcheckbox", isSelected: true, "aria-disabled": true, children: label }, label); }) }); }; const OptionalControlsGroup = ({ items, toggleItem }) => { if (!items.length) return null; return /* @__PURE__ */ jsx(Fragment, { children: items.map(([label, isSelected]) => { const itemLabel = isSelected ? sprintf(__("%s, hide and reset"), label) : sprintf(_x("%s, show", "input control"), label); return /* @__PURE__ */ jsx(MenuItem, { icon: isSelected ? check : null, isSelected, label: itemLabel, onClick: () => { if (isSelected) speak(sprintf(__("%s hidden and reset to default"), label), "assertive"); else speak(sprintf(__("%s is now visible"), label), "assertive"); toggleItem(label); }, role: "menuitemcheckbox", children: label }, label); }) }); }; const ToolsPanelHeader = (props, forwardedRef) => { const { areAllOptionalControlsHidden, defaultControlsItemClassName, dropdownMenuClassName, hasMenuItems, headingClassName, headingLevel = 2, label: labelText, menuItems, resetAll, toggleItem, dropdownMenuProps, ...headerProps } = useToolsPanelHeader(props); if (!labelText) return null; const defaultItems = Object.entries(menuItems?.default || {}); const optionalItems = Object.entries(menuItems?.optional || {}); const dropDownMenuIcon = areAllOptionalControlsHidden ? plus : moreVertical; const dropDownMenuLabelText = sprintf(_x("%s options", "Button label to reveal tool panel options"), labelText); const dropdownMenuDescriptionText = areAllOptionalControlsHidden ? __("All options are currently hidden") : void 0; const canResetAll = [...defaultItems, ...optionalItems].some(([, isSelected]) => isSelected); return /* @__PURE__ */ jsxs(HStack, { ...headerProps, ref: forwardedRef, children: [/* @__PURE__ */ jsx(Heading, { level: headingLevel, className: headingClassName, children: labelText }), hasMenuItems && /* @__PURE__ */ jsx(DropdownMenu, { ...dropdownMenuProps, icon: dropDownMenuIcon, label: dropDownMenuLabelText, menuProps: { className: dropdownMenuClassName }, toggleProps: { size: "small", description: dropdownMenuDescriptionText }, children: () => /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(MenuGroup, { label: labelText, children: [/* @__PURE__ */ jsx(DefaultControlsGroup, { items: defaultItems, toggleItem, itemClassName: defaultControlsItemClassName }), /* @__PURE__ */ jsx(OptionalControlsGroup, { items: optionalItems, toggleItem })] }), /* @__PURE__ */ jsx(MenuGroup, { children: /* @__PURE__ */ jsx(MenuItem, { "aria-disabled": !canResetAll, variant: "tertiary", onClick: () => { if (canResetAll) { resetAll(); speak(__("All options reset"), "assertive"); } }, children: __("Reset all") }) })] }) })] }); }; const ConnectedToolsPanelHeader = contextConnect(ToolsPanelHeader, "ToolsPanelHeader"); export { ConnectedToolsPanelHeader as default }; //# sourceMappingURL=component.mjs.map