@wordpress/components
Version:
UI components for WordPress.
257 lines (256 loc) • 14.1 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let _wordpress_a11y = require("@wordpress/a11y");
let _wordpress_icons = require("@wordpress/icons");
let _wordpress_i18n = require("@wordpress/i18n");
let ______dropdown_menu_index_tsx = require("../../dropdown-menu/index.cjs");
______dropdown_menu_index_tsx = __toESM(______dropdown_menu_index_tsx);
let ______menu_group_index_tsx = require("../../menu-group/index.cjs");
______menu_group_index_tsx = __toESM(______menu_group_index_tsx);
let ______menu_item_index_tsx = require("../../menu-item/index.cjs");
______menu_item_index_tsx = __toESM(______menu_item_index_tsx);
let ______h_stack_index_ts = require("../../h-stack/index.cjs");
let ______heading_index_ts = require("../../heading/index.cjs");
let __hook_ts = require("./hook.cjs");
let ______context_index_ts = require("../../context/index.cjs");
let react_jsx_runtime = require("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__ */ (0, react_jsx_runtime.jsx)("span", {
"aria-hidden": true,
className: style_module_default["reset-label"],
children: (0, _wordpress_i18n.__)("Reset")
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: items.map(([label, hasValue]) => {
if (hasValue) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______menu_item_index_tsx.default, {
className: itemClassName,
role: "menuitem",
label: (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("%s, reset to default"), label),
onClick: () => {
toggleItem(label);
(0, _wordpress_a11y.speak)((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("%s reset to default"), label), "assertive");
},
suffix: resetSuffix,
children: label
}, label);
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______menu_item_index_tsx.default, {
icon: _wordpress_icons.check,
className: itemClassName,
role: "menuitemcheckbox",
isSelected: true,
"aria-disabled": true,
children: label
}, label);
}) });
};
const OptionalControlsGroup = ({ items, toggleItem }) => {
if (!items.length) return null;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: items.map(([label, isSelected]) => {
const itemLabel = isSelected ? (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("%s, hide and reset"), label) : (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n._x)("%s, show", "input control"), label);
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______menu_item_index_tsx.default, {
icon: isSelected ? _wordpress_icons.check : null,
isSelected,
label: itemLabel,
onClick: () => {
if (isSelected) (0, _wordpress_a11y.speak)((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("%s hidden and reset to default"), label), "assertive");
else (0, _wordpress_a11y.speak)((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("%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 } = (0, __hook_ts.useToolsPanelHeader)(props);
if (!labelText) return null;
const defaultItems = Object.entries(menuItems?.default || {});
const optionalItems = Object.entries(menuItems?.optional || {});
const dropDownMenuIcon = areAllOptionalControlsHidden ? _wordpress_icons.plus : _wordpress_icons.moreVertical;
const dropDownMenuLabelText = (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n._x)("%s options", "Button label to reveal tool panel options"), labelText);
const dropdownMenuDescriptionText = areAllOptionalControlsHidden ? (0, _wordpress_i18n.__)("All options are currently hidden") : void 0;
const canResetAll = [...defaultItems, ...optionalItems].some(([, isSelected]) => isSelected);
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(______h_stack_index_ts.HStack, {
...headerProps,
ref: forwardedRef,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(______heading_index_ts.Heading, {
level: headingLevel,
className: headingClassName,
children: labelText
}), hasMenuItems && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______dropdown_menu_index_tsx.default, {
...dropdownMenuProps,
icon: dropDownMenuIcon,
label: dropDownMenuLabelText,
menuProps: { className: dropdownMenuClassName },
toggleProps: {
size: "small",
description: dropdownMenuDescriptionText
},
children: () => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(______menu_group_index_tsx.default, {
label: labelText,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultControlsGroup, {
items: defaultItems,
toggleItem,
itemClassName: defaultControlsItemClassName
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(OptionalControlsGroup, {
items: optionalItems,
toggleItem
})]
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______menu_group_index_tsx.default, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______menu_item_index_tsx.default, {
"aria-disabled": !canResetAll,
variant: "tertiary",
onClick: () => {
if (canResetAll) {
resetAll();
(0, _wordpress_a11y.speak)((0, _wordpress_i18n.__)("All options reset"), "assertive");
}
},
children: (0, _wordpress_i18n.__)("Reset all")
}) })] })
})]
});
};
const ConnectedToolsPanelHeader = (0, ______context_index_ts.contextConnect)(ToolsPanelHeader, "ToolsPanelHeader");
exports.default = ConnectedToolsPanelHeader;
//# sourceMappingURL=component.cjs.map