@wordpress/components
Version:
UI components for WordPress.
170 lines (169 loc) • 14.4 kB
JavaScript
import clsx from "clsx";
import * as Ariakit from "@ariakit/react";
import { forwardRef, useCallback, useContext, useMemo } from "@wordpress/element";
import { Context } from "./context.mjs";
import { jsx } 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/menu/style.module.scss
if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("b4b0159e1b", "._31a59c2dbce0a799__menu{outline:2px solid transparent!important;position:relative;z-index:1000000}._077aa31da0bb3dd3__menu-surface{background-color:var(--wp-components-color-background,var(--wpds-color-background-surface-neutral-strong,#fff));border-radius:4px;box-shadow:0 0 0 1px var(--wp-components-color-gray-300,var(--wpds-color-stroke-surface-neutral,#dbdbdb)),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);box-sizing:border-box;display:grid;grid-template-columns:minmax(0,max-content) 1fr;grid-template-rows:auto;max-height:var(--popover-available-height);max-width:320px;min-width:160px;overflow:auto;overscroll-behavior:contain;padding:4px}._077aa31da0bb3dd3__menu-surface:where(._22378a645f656cff__is-toolbar){box-shadow:0 0 0 1px var(--wp-components-color-foreground,var(--wpds-color-foreground-content-neutral,#1e1e1e))}@media not (prefers-reduced-motion){.ac0cc0c0c59279e3__menu-motion-root{transition-duration:var(--wpds-motion-duration-md,.2s),var(--wpds-motion-duration-sm,.1s);transition-property:transform,opacity;transition-timing-function:var(--wpds-motion-easing-expressive,cubic-bezier(.25,0,0,1)),linear;will-change:transform,opacity}.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])){opacity:0}.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])):has(>._077aa31da0bb3dd3__menu-surface[data-enter]){opacity:1}.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])):has(>._077aa31da0bb3dd3__menu-surface[data-side=bottom]){transform:translateY(-4px)}.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])):has(>._077aa31da0bb3dd3__menu-surface[data-side=top]){transform:translateY(4px)}.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])):has(>._077aa31da0bb3dd3__menu-surface[data-side=left]){transform:translateX(4px)}.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])):has(>._077aa31da0bb3dd3__menu-surface[data-side=right]){transform:translateX(-4px)}.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])):has(>._077aa31da0bb3dd3__menu-surface[data-enter][data-side=bottom]),.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])):has(>._077aa31da0bb3dd3__menu-surface[data-enter][data-side=top]){transform:translateY(0)}.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])):has(>._077aa31da0bb3dd3__menu-surface[data-enter][data-side=left]),.ac0cc0c0c59279e3__menu-motion-root:not(:has(>._077aa31da0bb3dd3__menu-surface[data-submenu])):has(>._077aa31da0bb3dd3__menu-surface[data-enter][data-side=right]){transform:translateX(0)}}.c4951959c100036c__item{all:unset;align-items:center;box-sizing:border-box;cursor:var(--wpds-cursor-control,pointer);display:grid;grid-column:1/-1;grid-template-columns:minmax(0,max-content) 1fr;min-height:32px;position:relative}@supports (grid-template-columns:subgrid){.c4951959c100036c__item{grid-template-columns:subgrid}}.c4951959c100036c__item{border-radius:2px;color:var(--wpds-color-foreground-interactive-neutral,#1e1e1e);font-family:inherit;font-size:13px;font-weight:400;line-height:20px;outline:none;padding-block:4px;padding-inline:12px;scroll-margin:4px;user-select:none}.c4951959c100036c__item[aria-disabled=true]{color:var(--wpds-color-foreground-interactive-neutral-disabled,#8b8b8b)}.c4951959c100036c__item[aria-disabled=true]:focus-visible{outline:var(--wpds-border-width-focus,var(--wp-admin-border-width-focus,2px)) solid var(--focus-color,var(--wpds-color-stroke-focus,var(--wp-admin-theme-color,#3858e9)));outline-offset:var(--wpds-border-width-focus,var(--wp-admin-border-width-focus,2px));outline-offset:0}.c4951959c100036c__item:is(:focus,[data-focus]):not([aria-disabled=true]){background-color:var(--wpds-color-background-interactive-brand-strong-active,color-mix(in oklch,var(--wp-admin-theme-color,#3858e9) 93%,#000));box-shadow:none;color:var(--wpds-color-foreground-interactive-brand-strong-active,#fff);outline:none}@media (forced-colors:active){.c4951959c100036c__item:is(:focus,[data-focus]):not([aria-disabled=true]){background-color:Highlight;color:HighlightText;forced-color-adjust:none}}.c4951959c100036c__item[aria-expanded=true]:not(:focus,[data-focus],[aria-disabled=true]){background-color:var(--wpds-color-background-interactive-neutral-weak-active,#ececec)}@media (forced-colors:active){.c4951959c100036c__item[aria-expanded=true]:not(:focus,[data-focus],[aria-disabled=true]){outline:var(--wpds-border-width-xs,1px) solid GrayText;outline-offset:0}}.c4951959c100036c__item svg{fill:currentColor}._41f98a86b82184ef__item-prefix-wrapper{grid-column:1}.acb29546fad46aaa__checkbox-item>._41f98a86b82184ef__item-prefix-wrapper,.d2cbe69ad4d19f43__radio-item>._41f98a86b82184ef__item-prefix-wrapper{min-width:24px}._41f98a86b82184ef__item-prefix-wrapper:not(:empty),.acb29546fad46aaa__checkbox-item>._41f98a86b82184ef__item-prefix-wrapper,.d2cbe69ad4d19f43__radio-item>._41f98a86b82184ef__item-prefix-wrapper{margin-inline-end:8px}._41f98a86b82184ef__item-prefix-wrapper{align-items:center;color:var(--wpds-color-foreground-content-neutral-weak,#6e6e6e);display:flex;justify-content:center}[aria-disabled=true]>._41f98a86b82184ef__item-prefix-wrapper,[aria-expanded=true]>._41f98a86b82184ef__item-prefix-wrapper,[data-active-item]>._41f98a86b82184ef__item-prefix-wrapper{color:inherit}.e7fd4ab32f9106a5__item-content-wrapper{align-items:center;display:flex;gap:12px;grid-column:2;justify-content:space-between;pointer-events:none}.ea40d404caa71b2c__item-children-wrapper{display:inline-flex;flex:1;flex-direction:column;gap:4px}.d41f4d062cbdad1e__item-suffix-wrapper{align-items:center;color:var(--wpds-color-foreground-content-neutral-weak,#6e6e6e);display:flex;flex:0 1 fit-content;gap:12px;justify-content:center;min-width:0;width:fit-content}[aria-disabled=true] :not(._31a59c2dbce0a799__menu) .d41f4d062cbdad1e__item-suffix-wrapper,[aria-expanded=true] :not(._31a59c2dbce0a799__menu) .d41f4d062cbdad1e__item-suffix-wrapper,[data-active-item] :not(._31a59c2dbce0a799__menu) .d41f4d062cbdad1e__item-suffix-wrapper{color:inherit}.eb571cbfb460189a__group{display:contents}.e25f5d435960d0b3__group-label{grid-column:1/-1;padding-block-end:8px;padding-block-start:12px;padding-inline:12px}._3828638d0a3398f1__separator{background-color:var(--wp-components-color-gray-200,var(--wpds-color-stroke-surface-neutral,#dbdbdb));border:none;grid-column:1/-1;height:1px;margin-block:8px;margin-inline:12px;outline:2px solid transparent}._3828638d0a3398f1__separator:where(._22378a645f656cff__is-toolbar){background-color:var(--wp-components-color-foreground,var(--wpds-color-foreground-content-neutral,#1e1e1e))}.d39f208de155ce87__submenu-chevron-icon{transform:scaleX(1);width:6px}.d39f208de155ce87__submenu-chevron-icon:where(._6fca907fdb3bbbc4__is-rtl){transform:scaleX(-1)}._8fbc5626d232d92a__item-label{color:inherit;font-size:13px;line-height:20px}._463a4649c81edf39__item-help-text{color:var(--wpds-color-foreground-content-neutral-weak,#6e6e6e);font-size:12px;line-height:16px;overflow-wrap:anywhere}[aria-disabled=true] :not(._31a59c2dbce0a799__menu) ._463a4649c81edf39__item-help-text,[aria-expanded=true] :not(._31a59c2dbce0a799__menu) ._463a4649c81edf39__item-help-text,[data-active-item] :not(._31a59c2dbce0a799__menu) ._463a4649c81edf39__item-help-text{color:inherit}");
var style_module_default = {
"menu": "_31a59c2dbce0a799__menu",
"menu-surface": "_077aa31da0bb3dd3__menu-surface",
"is-toolbar": "_22378a645f656cff__is-toolbar",
"menu-motion-root": "ac0cc0c0c59279e3__menu-motion-root",
"item": "c4951959c100036c__item",
"item-prefix-wrapper": "_41f98a86b82184ef__item-prefix-wrapper",
"checkbox-item": "acb29546fad46aaa__checkbox-item",
"radio-item": "d2cbe69ad4d19f43__radio-item",
"item-content-wrapper": "e7fd4ab32f9106a5__item-content-wrapper",
"item-children-wrapper": "ea40d404caa71b2c__item-children-wrapper",
"item-suffix-wrapper": "d41f4d062cbdad1e__item-suffix-wrapper",
"group": "eb571cbfb460189a__group",
"group-label": "e25f5d435960d0b3__group-label",
"separator": "_3828638d0a3398f1__separator",
"submenu-chevron-icon": "d39f208de155ce87__submenu-chevron-icon",
"is-rtl": "_6fca907fdb3bbbc4__is-rtl",
"item-label": "_8fbc5626d232d92a__item-label",
"item-help-text": "_463a4649c81edf39__item-help-text"
};
// packages/components/src/menu/popover.tsx
const Popover = forwardRef(function UnforwardedPopover({ gutter, shift, modal = true, ...otherProps }, ref) {
const menuContext = useContext(Context);
const appliedPlacementSide = Ariakit.useStoreState(menuContext?.store, "currentPlacement")?.split("-")[0];
const hideOnEscape = useCallback((event) => {
event.preventDefault();
return true;
}, []);
const computedDirection = Ariakit.useStoreState(menuContext?.store, "rtl") ? "rtl" : "ltr";
const wrapperProps = useMemo(() => ({
dir: computedDirection,
style: { direction: computedDirection }
}), [computedDirection]);
if (!menuContext?.store) throw new Error("Menu.Popover can only be rendered inside a Menu component");
const renderMenu = useCallback((htmlProps) => /* @__PURE__ */ jsx("div", {
className: style_module_default["menu-motion-root"],
children: /* @__PURE__ */ jsx("div", {
...htmlProps,
className: clsx(style_module_default["menu-surface"], htmlProps.className, { [style_module_default["is-toolbar"]]: menuContext.variant === "toolbar" })
})
}), [menuContext.variant]);
return /* @__PURE__ */ jsx(Ariakit.Menu, {
...otherProps,
className: clsx(style_module_default.menu, otherProps.className),
ref,
modal,
store: menuContext.store,
gutter: gutter ?? (menuContext.store.parent ? 0 : 8),
shift: shift ?? (menuContext.store.parent ? -4 : 0),
hideOnHoverOutside: false,
"data-side": appliedPlacementSide,
"data-submenu": !!menuContext.store.parent || void 0,
wrapperProps,
hideOnEscape,
unmountOnHide: true,
render: renderMenu
});
});
export { Popover };
//# sourceMappingURL=popover.mjs.map