UNPKG

@wordpress/components

Version:
224 lines (223 loc) • 10.9 kB
import clsx from "clsx"; import * as Ariakit from "@ariakit/react"; import { useInstanceId } from "@wordpress/compose"; import { useLayoutEffect, useRef } from "@wordpress/element"; import { contextConnect, useContextSystem } from "../../context/index.mjs"; import { useToggleGroupControlContext } from "../context.mjs"; import Tooltip from "../../tooltip/index.mjs"; import { Fragment, 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/toggle-group-control/toggle-group-control-option-base/style.module.scss if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("948258ee0d", "._7ae5235f569593ce__label{display:inline-flex;max-width:100%;min-width:0;position:relative}._6a074ff4965d9b00__label-block{flex:1}._03bb63f30642884a__button{fill:currentColor;align-items:center;appearance:none;background:transparent;border:none;border-radius:var(--wpds-border-radius-xs,1px);color:var(--wp-components-color-gray-700,var(--wpds-color-foreground-content-neutral-weak,#6e6e6e));cursor:var(--wpds-cursor-control,pointer);display:flex;font-family:inherit;height:100%;justify-content:center;line-height:1;outline:none;padding:0 12px;position:relative;text-align:center}@media not (prefers-reduced-motion){._03bb63f30642884a__button{transition:color .16s linear,font-weight 60ms linear}}._03bb63f30642884a__button{user-select:none;width:100%;z-index:2}._03bb63f30642884a__button::-moz-focus-inner{border:0}._03bb63f30642884a__button[aria-disabled=true],._03bb63f30642884a__button[disabled]{cursor:default;opacity:.4}._03bb63f30642884a__button:hover:not([disabled]):not([aria-disabled=true]){color:var(--wp-components-color-foreground,var(--wpds-color-foreground-content-neutral,#1e1e1e))}.ac9075d03c1b34df__is-deselectable:focus{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))}.ac9075d03c1b34df__is-deselectable:focus[aria-pressed=false]{background:var(--wp-components-color-background,var(--wpds-color-background-surface-neutral-strong,#fff));box-shadow:0 0 0 2px var(--wp-components-color-background,var(--wpds-color-background-surface-neutral-strong,#fff))}._921855fe3287b40b__is-icon{aspect-ratio:1;height:38px;padding:0}._7c486b7afa4f8d6b__is-pressed{color:var(--wp-components-color-foreground,var(--wpds-color-foreground-content-neutral,#1e1e1e));font-weight:var(--wpds-typography-font-weight-emphasis,600)}._1e09c75637263e67__button-content{display:flex;font-size:var(--wpds-typography-font-size-md,13px);line-height:1}._1e09c75637263e67__button-content[data-label]{align-items:center;flex-direction:column}._1e09c75637263e67__button-content[data-label]:after{content:attr(data-label)/\"\";font-weight:var(--wpds-typography-font-weight-emphasis,600);height:0;overflow:hidden;visibility:hidden}"); var style_module_default = { "label": "_7ae5235f569593ce__label", "label-block": "_6a074ff4965d9b00__label-block", "button": "_03bb63f30642884a__button", "is-deselectable": "ac9075d03c1b34df__is-deselectable", "is-icon": "_921855fe3287b40b__is-icon", "is-pressed": "_7c486b7afa4f8d6b__is-pressed", "button-content": "_1e09c75637263e67__button-content" }; // packages/components/src/toggle-group-control/toggle-group-control-option-base/component.tsx const WithToolTip = ({ showTooltip, text, children }) => { if (showTooltip && text) return /* @__PURE__ */ jsx(Tooltip, { text, placement: "top", children }); return /* @__PURE__ */ jsx(Fragment, { children }); }; function ToggleGroupControlOptionBase(props, forwardedRef) { const toggleGroupControlContext = useToggleGroupControlContext(); const id = useInstanceId(ToggleGroupControlOptionBase, toggleGroupControlContext.baseId || "toggle-group-control-option-base"); const buttonProps = useContextSystem({ ...props, id }, "ToggleGroupControlOptionBase"); const { isBlock = false, isDeselectable = false } = toggleGroupControlContext; const { className, isIcon = false, value, children, showTooltip = false, disabled, ...otherButtonProps } = buttonProps; const isOptionDisabled = Boolean(toggleGroupControlContext.disabled || disabled); const isPressed = toggleGroupControlContext.value === value; const labelClasses = clsx(style_module_default.label, isBlock && style_module_default["label-block"]); const itemClasses = clsx(style_module_default.button, { [style_module_default["is-deselectable"]]: isDeselectable, [style_module_default["is-icon"]]: isIcon, [style_module_default["is-pressed"]]: isPressed }, className); const buttonOnClick = () => { if (isDeselectable && isPressed) toggleGroupControlContext.setValue(void 0); else toggleGroupControlContext.setValue(value); }; const commonProps = { ...otherButtonProps, className: itemClasses, "data-value": value, ref: forwardedRef }; const buttonContentProps = { className: style_module_default["button-content"], "data-label": typeof children === "string" ? children : void 0 }; const labelRef = useRef(null); useLayoutEffect(() => { if (isPressed && labelRef.current) toggleGroupControlContext.setSelectedElement(labelRef.current); }, [isPressed, toggleGroupControlContext]); return /* @__PURE__ */ jsx("div", { ref: labelRef, className: labelClasses, children: /* @__PURE__ */ jsx(WithToolTip, { showTooltip, text: otherButtonProps["aria-label"], children: isDeselectable ? /* @__PURE__ */ jsx("button", { ...commonProps, disabled: isOptionDisabled, "aria-pressed": isPressed, type: "button", onClick: buttonOnClick, children: /* @__PURE__ */ jsx("div", { ...buttonContentProps, children }) }) : /* @__PURE__ */ jsx(Ariakit.Radio, { disabled, onFocusVisible: () => { if (!(toggleGroupControlContext.value === null || toggleGroupControlContext.value === "") || toggleGroupControlContext.activeItemIsNotFirstItem?.()) toggleGroupControlContext.setValue(value); }, render: /* @__PURE__ */ jsx("button", { type: "button", ...commonProps }), value, children: /* @__PURE__ */ jsx("div", { ...buttonContentProps, children }) }) }) }); } /** * `ToggleGroupControlOptionBase` is a form component and is meant to be used as an internal, * generic component for any children of `ToggleGroupControl`. * * @example * ```jsx * import { * __experimentalToggleGroupControl as ToggleGroupControl, * __experimentalToggleGroupControlOptionBase as ToggleGroupControlOptionBase, * } from '@wordpress/components'; * * function Example() { * return ( * <ToggleGroupControl label="my label" value="vertical" isBlock> * <ToggleGroupControlOption value="horizontal" label="Horizontal" /> * <ToggleGroupControlOption value="vertical" label="Vertical" /> * </ToggleGroupControl> * ); * } * ``` */ const ConnectedToggleGroupControlOptionBase = contextConnect(ToggleGroupControlOptionBase, "ToggleGroupControlOptionBase"); export { ConnectedToggleGroupControlOptionBase as default }; //# sourceMappingURL=component.mjs.map