@wordpress/components
Version:
UI components for WordPress.
210 lines (209 loc) • 11.9 kB
JavaScript
import * as Ariakit from "@ariakit/react";
import clsx from "clsx";
import { useInstanceId } from "@wordpress/compose";
import { __, sprintf } from "@wordpress/i18n";
import CustomSelect from "./custom-select.mjs";
import CustomSelectItem from "./item.mjs";
import { VisuallyHidden } from "../visually-hidden/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/custom-select-control/style.module.scss
if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("8cba2555a7", ".c86872ee1cb9e7a8__select{background-color:var(--wp-components-color-background,var(--wpds-color-background-surface-neutral-strong,#fff));border:none;color:var(--wp-components-color-foreground,var(--wpds-color-foreground-content-neutral,#1e1e1e));cursor:var(--wpds-cursor-control,pointer);display:block;font-family:inherit;font-size:16px;text-align:start;user-select:none;width:100%}.c86872ee1cb9e7a8__select[data-focus-visible]{outline:none}@media (min-width:600px){.c86872ee1cb9e7a8__select{font-size:13px}.c86872ee1cb9e7a8__select:where(._10ec0acea2294241__is-small){font-size:11px}}.c86872ee1cb9e7a8__select{min-height:40px;padding-inline-end:30px;padding-inline-start:12px}._8a233b1d560cadb8__item{padding-inline-end:6px;padding-inline-start:12px}.c86872ee1cb9e7a8__select:where(._10ec0acea2294241__is-small){min-height:24px;padding-inline-end:26px;padding-inline-start:8px}._8a233b1d560cadb8__item:where(._10ec0acea2294241__is-small){padding-inline-end:2px;padding-inline-start:8px}@keyframes _8d94f6fd4a6ce793__slide-down{0%{transform:translateY(-4px)}to{transform:translateY(0)}}@keyframes da97d4d9bb44af86__fade-in{0%{opacity:0}to{opacity:1}}._20aa5f6b569ea540__popover{background-color:var(--wp-components-color-background,var(--wpds-color-background-surface-neutral-strong,#fff));border:1px solid var(--wp-components-color-foreground,var(--wpds-color-foreground-content-neutral,#1e1e1e));border-radius:2px;box-shadow:0 2px 3px rgba(0,0,0,.05),0 4px 5px rgba(0,0,0,.04),0 12px 12px rgba(0,0,0,.03),0 16px 16px rgba(0,0,0,.02);display:flex;flex-direction:column;max-height:min(var(--popover-available-height,400px),400px);min-width:min-content;overflow:auto;overscroll-behavior:contain;z-index:1000000}@media not (prefers-reduced-motion){._20aa5f6b569ea540__popover[data-open]{animation-duration:var(--wpds-motion-duration-md,.2s),var(--wpds-motion-duration-sm,.1s);animation-name:_8d94f6fd4a6ce793__slide-down,da97d4d9bb44af86__fade-in;animation-timing-function:var(--wpds-motion-easing-expressive,cubic-bezier(.25,0,0,1)),linear;will-change:transform,opacity}}._20aa5f6b569ea540__popover[data-focus-visible]{outline:none}._8a233b1d560cadb8__item{align-items:center;cursor:default;display:flex;font-size:13px;justify-content:space-between;line-height:28px;padding-block:8px;scroll-margin:4px;user-select:none}._8a233b1d560cadb8__item[data-active-item]{background-color:var(--wpds-color-background-interactive-brand-strong-active,color-mix(in oklch,var(--wp-admin-theme-color,#3858e9) 93%,#000))!important;box-shadow:none;color:var(--wpds-color-foreground-interactive-brand-strong-active,#fff)!important;outline:none}@media (forced-colors:active){._8a233b1d560cadb8__item[data-active-item]{background-color:Highlight!important;color:HighlightText!important;forced-color-adjust:none}}._8a233b1d560cadb8__item[data-active-item] ._4d4c463903ab100e__item-hint{color:inherit}._1269a6e87d37dd5f__selected-hint-wrapper{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}._8f33c4a5c5296bbd__selected-hint{color:var(--wpds-color-foreground-content-neutral-weak,#6e6e6e);margin-inline-start:8px}._073e20cd513cc127__hint-wrapper{align-items:center;column-gap:16px;display:flex;flex:1;flex-wrap:wrap;justify-content:space-between}._4d4c463903ab100e__item-hint{color:var(--wpds-color-foreground-content-neutral-weak,#6e6e6e);line-height:1.4;margin-block:4px;padding-inline-end:4px;text-align:initial}.a05e4151cabd0b1c__item-check{fill:currentColor;align-items:center;align-self:start;display:flex;font-size:0;margin-block-start:2px;margin-inline-start:8px}._073e20cd513cc127__hint-wrapper~.a05e4151cabd0b1c__item-check,.a05e4151cabd0b1c__item-check:not(:empty){font-size:24px}");
var style_module_default = {
"select": "c86872ee1cb9e7a8__select",
"is-small": "_10ec0acea2294241__is-small",
"item": "_8a233b1d560cadb8__item",
"popover": "_20aa5f6b569ea540__popover",
"slide-down": "_8d94f6fd4a6ce793__slide-down",
"fade-in": "da97d4d9bb44af86__fade-in",
"item-hint": "_4d4c463903ab100e__item-hint",
"selected-hint-wrapper": "_1269a6e87d37dd5f__selected-hint-wrapper",
"selected-hint": "_8f33c4a5c5296bbd__selected-hint",
"hint-wrapper": "_073e20cd513cc127__hint-wrapper",
"item-check": "a05e4151cabd0b1c__item-check"
};
// packages/components/src/custom-select-control/index.tsx
function useDeprecatedProps({ __experimentalShowSelectedHint, ...otherProps }) {
return {
showSelectedHint: __experimentalShowSelectedHint,
...otherProps
};
}
function applyOptionDeprecations({ __experimentalHint, ...rest }) {
return {
hint: __experimentalHint,
...rest
};
}
function getDescribedBy(currentName, describedBy) {
if (describedBy) return describedBy;
return sprintf(__("Currently selected: %s"), currentName);
}
/**
* `CustomSelectControl` is a dropdown for selecting a single option from a
* list, with support for custom styling.
*/
function CustomSelectControl(props) {
const { __next40pxDefaultSize: _next40pxDefaultSize, describedBy, options, onChange, size = "default", value, className: classNameProp, showSelectedHint = false, ...restProps } = useDeprecatedProps(props);
const descriptionId = useInstanceId(CustomSelectControl, "custom-select-control__description");
const store = Ariakit.useSelectStore({
async setValue(nextValue) {
const nextOption = options.find((item) => item.key === nextValue);
if (!onChange || !nextOption) return;
await Promise.resolve();
const state = store.getState();
const changeObject = {
highlightedIndex: state.renderedItems.findIndex((item) => item.value === nextValue),
inputValue: "",
isOpen: state.open,
selectedItem: nextOption,
type: ""
};
onChange(changeObject);
},
value: value?.key,
defaultValue: options[0]?.key
});
const children = options.map(applyOptionDeprecations).map(({ name, key, hint, style, className }) => {
const withHint = /* @__PURE__ */ jsxs("div", {
className: style_module_default["hint-wrapper"],
children: [/* @__PURE__ */ jsx("span", { children: name }), /* @__PURE__ */ jsx("span", {
className: clsx(style_module_default["item-hint"], "components-custom-select-control__item-hint"),
children: hint
})]
});
return /* @__PURE__ */ jsx(CustomSelectItem, {
value: key,
children: hint ? withHint : name,
style,
className: clsx(className, "components-custom-select-control__item", { "has-hint": hint })
}, key);
});
const currentValue = Ariakit.useStoreState(store, "value");
const selectedOption = options?.map(applyOptionDeprecations)?.find(({ key }) => currentValue === key) ?? options[0];
const renderSelectedValue = () => {
if (!showSelectedHint || !selectedOption.hint) return selectedOption?.name;
return /* @__PURE__ */ jsxs("div", {
className: style_module_default["selected-hint-wrapper"],
children: [selectedOption?.name, /* @__PURE__ */ jsx("span", {
className: clsx(style_module_default["selected-hint"], "components-custom-select-control__hint"),
children: selectedOption?.hint
})]
});
};
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(CustomSelect, {
"aria-describedby": descriptionId,
renderSelectedValue,
size,
store,
className: clsx("components-custom-select-control", classNameProp),
...restProps,
children
}), /* @__PURE__ */ jsx(VisuallyHidden, { children: /* @__PURE__ */ jsx("span", {
id: descriptionId,
children: getDescribedBy(selectedOption?.name, describedBy)
}) })] });
}
export { CustomSelectControl as default };
//# sourceMappingURL=index.mjs.map