UNPKG

@wordpress/components

Version:
171 lines (170 loc) • 7.9 kB
import clsx from "clsx"; import { parseQuantityAndUnitFromRawValue } from "../../unit-control/utils.mjs"; import { useContextSystem } from "../../context/index.mjs"; // 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/border-control/style.module.scss if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("8d5e6dadfe", ".a30bb709a06fd05e__border-control{border:0;box-sizing:border-box;margin:0;padding:0}.a30bb709a06fd05e__border-control *,.a30bb709a06fd05e__border-control :after,.a30bb709a06fd05e__border-control :before{box-sizing:inherit}.d3d859b731dccbfb__inner-wrapper{height:40px}.d3d859b731dccbfb__inner-wrapper .components-unit-control{flex:1 1 40%}.d3d859b731dccbfb__inner-wrapper.d3d859b731dccbfb__inner-wrapper .components-unit-control__select.components-unit-control__select{min-height:0}.b2cbcfebb0c7fd64__inner-wrapper-has-width .components-unit-control{flex:0 0 auto}.ade28937fa5efb6b__dropdown{background:#fff}.ade28937fa5efb6b__dropdown.ade28937fa5efb6b__dropdown>button{align-items:center;aspect-ratio:1;border:var(--wpds-border-width-xs,1px) solid var(--wpds-color-stroke-interactive-neutral,#8b8b8b);border-end-end-radius:0;border-end-start-radius:2px;border-start-end-radius:0;border-start-start-radius:2px;display:flex;justify-content:center;padding:0}.d18ca87ef6d01b00__color-indicator-wrapper{border:2px solid transparent;border-radius:9999px;height:24px;padding:2px;width:24px}.d18ca87ef6d01b00__color-indicator-wrapper>span{background:linear-gradient(-45deg,transparent 48%,rgba(0,0,0,.2) 0,rgba(0,0,0,.2) 52%,transparent 0);height:16px;width:16px}.caef52158116adc9__popover-controls{width:228px}._1e07e31d4454e0ef__reset-button-wrapper{display:flex;justify-content:flex-end;margin-block-start:12px}._9009293e6f3cf978__slider{flex:1 1 60%;margin-inline-end:12px}"); var style_module_default = { "border-control": "a30bb709a06fd05e__border-control", "inner-wrapper": "d3d859b731dccbfb__inner-wrapper", "inner-wrapper-has-width": "b2cbcfebb0c7fd64__inner-wrapper-has-width", "dropdown": "ade28937fa5efb6b__dropdown", "color-indicator-wrapper": "d18ca87ef6d01b00__color-indicator-wrapper", "popover-controls": "caef52158116adc9__popover-controls", "reset-button-wrapper": "_1e07e31d4454e0ef__reset-button-wrapper", "slider": "_9009293e6f3cf978__slider" }; // packages/components/src/border-control/border-control-dropdown/hook.ts function useBorderControlDropdown(props) { const { border, className, colors = [], enableAlpha = false, enableStyle = true, onChange, previousStyleSelection, __experimentalIsRenderedInSidebar = false, ...otherProps } = useContextSystem(props, "BorderControlDropdown"); const [widthValue] = parseQuantityAndUnitFromRawValue(border?.width); const hasZeroWidth = widthValue === 0; const onColorChange = (color) => { const style = border?.style === "none" ? previousStyleSelection : border?.style; const width = hasZeroWidth && !!color ? "1px" : border?.width; onChange({ color, style, width }); }; const onStyleChange = (style) => { const width = hasZeroWidth && !!style ? "1px" : border?.width; onChange({ ...border, style, width }); }; const onReset = () => { onChange({ ...border, color: void 0, style: void 0 }); }; const classes = clsx(style_module_default.dropdown, className); const { color: indicatorColor, style: indicatorStyle } = border || {}; return { ...otherProps, border, className: classes, colors, enableAlpha, enableStyle, indicatorWrapperClassName: style_module_default["color-indicator-wrapper"], indicatorWrapperStyle: indicatorStyle ? { borderStyle: indicatorStyle === "none" ? "solid" : indicatorStyle, borderColor: indicatorColor || (indicatorStyle !== "none" ? "var(--wpds-color-stroke-surface-neutral, #dbdbdb)" : void 0) } : void 0, onColorChange, onStyleChange, onReset, popoverControlsClassName: style_module_default["popover-controls"], resetButtonWrapperClassName: style_module_default["reset-button-wrapper"], __experimentalIsRenderedInSidebar }; } export { useBorderControlDropdown }; //# sourceMappingURL=hook.mjs.map