UNPKG

@wordpress/components

Version:
215 lines (214 loc) • 15.4 kB
import { isRTL } from "@wordpress/i18n"; import clsx from "clsx"; import { getCSSWideKeyword } from "./utils.mjs"; import RangeMark from "./mark.mjs"; import { PolymorphicElement } from "../utils/polymorphic-element.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; import { createElement } from "react"; // 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/range-control/style.module.scss if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("4d5e856f6b", "._6a1e01adf97c4762__root{-webkit-tap-highlight-color:transparent;align-items:center;display:flex;justify-content:flex-start;min-height:40px;padding:0;position:relative;touch-action:none;width:100%}._6a1e01adf97c4762__root:where(._2495fc3509946926__is-compact){min-height:32px}._811431d1b36d2890__wrapper{--_wp-components-range-control-color:initial;color:var(--_wp-components-range-control-color);display:block;flex:1;height:30px;min-height:30px;position:relative;width:100%}._2bab18807adcbaf7__default-color{color:var(--wp-components-color-accent,var(--wp-admin-theme-color,#3858e9))}._325bd79ef1e3c6cc__before-icon,._4c163a0fe307fef3__after-icon{display:flex;margin-top:4px}:where(._6a1e01adf97c4762__root:not(._4e480ef72e95c007__is-rtl)) ._325bd79ef1e3c6cc__before-icon{margin-right:6px}:where(._6a1e01adf97c4762__root._4e480ef72e95c007__is-rtl) ._325bd79ef1e3c6cc__before-icon{margin-left:6px}:where(._6a1e01adf97c4762__root:not(._4e480ef72e95c007__is-rtl)) ._4c163a0fe307fef3__after-icon{margin-left:6px}:where(._6a1e01adf97c4762__root._4e480ef72e95c007__is-rtl) ._4c163a0fe307fef3__after-icon{margin-right:6px}._7f1276e73f11c614__rail{--_wp-components-range-control-rail-color:initial;background:var(--_wp-components-range-control-rail-color,var(--wpds-color-background-track-neutral,#dbdbdb));border-radius:9999px;display:block;height:4px;left:0;margin-top:13px;pointer-events:none;position:absolute;right:0;top:0}._7f1276e73f11c614__rail:where(._42c8b908ae7f250e__is-disabled){background:var(--wp-components-color-gray-100,var(--wpds-color-background-surface-neutral,#fcfcfc))}._404ee8010545d96e__track{--_wp-components-range-control-track-color:initial;background:var(--_wp-components-range-control-track-color);border-radius:9999px;display:block;height:4px;margin-top:13px;pointer-events:none;position:absolute;top:0}._404ee8010545d96e__track:where(._42c8b908ae7f250e__is-disabled){background:var(--wp-components-color-gray-400,var(--wpds-color-stroke-interactive-neutral,#8b8b8b))}@media not (prefers-reduced-motion){.is-marked ._404ee8010545d96e__track{transition:width .1s ease}}.cc773bc17fc1e078__marks{display:block;margin-top:17px;pointer-events:none;position:relative;user-select:none;width:100%}.f723c0a62c1430b7__mark{background-color:var(--wp-components-color-background,var(--wpds-color-background-surface-neutral-strong,#fff));height:4px;left:0;position:absolute;top:-4px;transform:translateX(-50%);width:2px;z-index:1}.d32fdc2beb8f564b__mark-label{color:var(--wp-components-color-gray-300,var(--wpds-color-stroke-surface-neutral,#dbdbdb));font-size:11px;position:absolute;top:8px;transform:translateX(-50%);white-space:nowrap}.d32fdc2beb8f564b__mark-label:where(:not(._4e480ef72e95c007__is-rtl)){left:0}.d32fdc2beb8f564b__mark-label:where(._4e480ef72e95c007__is-rtl){right:0;transform:translateX(50%)}.d32fdc2beb8f564b__mark-label:where(.a76ff600efef8300__is-filled){color:var(--wp-components-color-gray-700,var(--wpds-color-foreground-content-neutral-weak,#6e6e6e))}._4e662f841e7693d5__thumb-wrapper{align-items:center;background:var(--wp-components-color-accent,var(--wp-admin-theme-color,#3858e9));border-radius:50%;display:flex;height:12px;justify-content:center;margin-top:9px;outline:0;pointer-events:none;position:absolute;top:0;user-select:none;width:12px;z-index:3}._4e662f841e7693d5__thumb-wrapper:where(._42c8b908ae7f250e__is-disabled){background:var(--wp-components-color-gray-400,var(--wpds-color-stroke-interactive-neutral,#8b8b8b))}._4e662f841e7693d5__thumb-wrapper{transform:translateX(4.5px)}:where(._6a1e01adf97c4762__root:not(._4e480ef72e95c007__is-rtl)) ._4e662f841e7693d5__thumb-wrapper{margin-left:-10px}:where(._6a1e01adf97c4762__root._4e480ef72e95c007__is-rtl) ._4e662f841e7693d5__thumb-wrapper{margin-right:-10px;transform:translateX(-4.5px)}@media not (prefers-reduced-motion){.is-marked ._4e662f841e7693d5__thumb-wrapper{transition:left .1s ease}}.c291efee9339b385__thumb{align-items:center;background:var(--wp-components-color-accent,var(--wp-admin-theme-color,#3858e9));border-radius:50%;box-shadow:0 1px 1px rgba(0,0,0,.03),0 1px 2px rgba(0,0,0,.02),0 3px 3px rgba(0,0,0,.02),0 4px 4px rgba(0,0,0,.01);height:100%;outline:0;position:absolute;user-select:none;width:100%}.c291efee9339b385__thumb:where(._42c8b908ae7f250e__is-disabled){background:var(--wp-components-color-gray-400,var(--wpds-color-stroke-interactive-neutral,#8b8b8b))}.c291efee9339b385__thumb:where(._4c40282bc4b85f01__is-focused){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))}._9f9d2d76949b6e97__input{box-sizing:border-box;cursor:var(--wpds-cursor-control,pointer);display:block;height:100%;left:0;margin:0 -6px;opacity:0;outline:none;position:absolute;right:0;top:0;width:calc(100% + 12px)}.e5e0e57fa03659d3__tooltip{background:rgba(0,0,0,.8);border-radius:2px;color:#fff;display:none;font-size:12px;line-height:1.4;min-width:32px;opacity:0;padding:4px 8px;pointer-events:none;position:absolute;text-align:center;transform:translateX(-50%);user-select:none}.e5e0e57fa03659d3__tooltip:where(._4e480ef72e95c007__is-rtl){transform:translateX(50%)}.e5e0e57fa03659d3__tooltip:where(._3d76f75f23a3b86b__is-visible){display:inline-block;opacity:1}.e5e0e57fa03659d3__tooltip:where(:not(.a2c1548960f4840f__is-bottom)){top:-80%}.e5e0e57fa03659d3__tooltip:where(.a2c1548960f4840f__is-bottom){bottom:-80%}@media not (prefers-reduced-motion){.e5e0e57fa03659d3__tooltip{transition:opacity .12s ease,display allow-discrete .12s ease}}@starting-style{.e5e0e57fa03659d3__tooltip{opacity:0}}.ce0552937139bab7__input-number{display:inline-block;font-size:13px;margin-top:0}:where(._6a1e01adf97c4762__root:not(._4e480ef72e95c007__is-rtl)) .ce0552937139bab7__input-number{margin-left:16px!important}:where(._6a1e01adf97c4762__root._4e480ef72e95c007__is-rtl) .ce0552937139bab7__input-number{margin-right:16px!important}._17a230bac5de238b__action-right{display:block;margin-top:0}:where(._6a1e01adf97c4762__root:not(._4e480ef72e95c007__is-rtl)) ._17a230bac5de238b__action-right{margin-left:8px}:where(._6a1e01adf97c4762__root._4e480ef72e95c007__is-rtl) ._17a230bac5de238b__action-right{margin-right:8px}._17a230bac5de238b__action-right button,._17a230bac5de238b__action-right button.is-small{height:30px;margin-left:0;min-height:30px}._21ecef0ddd6b30cc__color-inherit{color:inherit}._09208cae4be0d86a__background-inherit{background:inherit}._0912537971ff850a__color-initial{color:initial}._2725e6ff88306d8d__background-initial{background:initial}._5ced90e174221615__color-unset{color:unset}.d26df97a68a49058__background-unset{background:unset}._6ffdbff3c2cce7b1__color-revert{color:revert}._39937b8ec4d503f0__background-revert{background:revert}._7f42d7000a4d1141__color-revert-layer{color:revert-layer}._869689f01bfa7a3a__background-revert-layer{background:revert-layer}@media (forced-colors:active){._7f1276e73f11c614__rail{background:GrayText}._404ee8010545d96e__track,._4e662f841e7693d5__thumb-wrapper,.c291efee9339b385__thumb{background:CanvasText}._404ee8010545d96e__track:where(._42c8b908ae7f250e__is-disabled),._4e662f841e7693d5__thumb-wrapper:where(._42c8b908ae7f250e__is-disabled),.c291efee9339b385__thumb:where(._42c8b908ae7f250e__is-disabled){background:GrayText}}"); var style_module_default = { "root": "_6a1e01adf97c4762__root", "is-compact": "_2495fc3509946926__is-compact", "wrapper": "_811431d1b36d2890__wrapper", "default-color": "_2bab18807adcbaf7__default-color", "before-icon": "_325bd79ef1e3c6cc__before-icon", "after-icon": "_4c163a0fe307fef3__after-icon", "is-rtl": "_4e480ef72e95c007__is-rtl", "rail": "_7f1276e73f11c614__rail", "is-disabled": "_42c8b908ae7f250e__is-disabled", "track": "_404ee8010545d96e__track", "marks": "cc773bc17fc1e078__marks", "mark": "f723c0a62c1430b7__mark", "mark-label": "d32fdc2beb8f564b__mark-label", "is-filled": "a76ff600efef8300__is-filled", "thumb-wrapper": "_4e662f841e7693d5__thumb-wrapper", "thumb": "c291efee9339b385__thumb", "is-focused": "_4c40282bc4b85f01__is-focused", "input": "_9f9d2d76949b6e97__input", "tooltip": "e5e0e57fa03659d3__tooltip", "is-visible": "_3d76f75f23a3b86b__is-visible", "is-bottom": "a2c1548960f4840f__is-bottom", "input-number": "ce0552937139bab7__input-number", "action-right": "_17a230bac5de238b__action-right", "color-inherit": "_21ecef0ddd6b30cc__color-inherit", "background-inherit": "_09208cae4be0d86a__background-inherit", "color-initial": "_0912537971ff850a__color-initial", "background-initial": "_2725e6ff88306d8d__background-initial", "color-unset": "_5ced90e174221615__color-unset", "background-unset": "d26df97a68a49058__background-unset", "color-revert": "_6ffdbff3c2cce7b1__color-revert", "background-revert": "_39937b8ec4d503f0__background-revert", "color-revert-layer": "_7f42d7000a4d1141__color-revert-layer", "background-revert-layer": "_869689f01bfa7a3a__background-revert-layer" }; // packages/components/src/range-control/rail.tsx function RangeRail(props) { const { disabled = false, marks = false, min = 0, max = 100, step = 1, value = 0, railColor, className, style, ...restProps } = props; const railColorKeyword = getCSSWideKeyword(railColor); return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(PolymorphicElement, { as: "span", ...restProps, disabled, className: clsx(style_module_default.rail, className, { [style_module_default["is-disabled"]]: disabled }, !disabled && railColorKeyword && style_module_default[`background-${railColorKeyword}`]), style: { "--_wp-components-range-control-rail-color": railColor, ...style } }), marks && /* @__PURE__ */ jsx(Marks, { disabled, marks, min, max, step, value })] }); } function Marks(props) { const { disabled = false, marks = false, min = 0, max = 100, step: stepProp = 1, value = 0 } = props; const marksData = useMarks({ marks, min, max, step: stepProp === "any" ? 1 : stepProp, value }); return /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: clsx("components-range-control__marks", style_module_default.marks), children: marksData.map((mark) => /* @__PURE__ */ createElement(RangeMark, { ...mark, key: mark.key, "aria-hidden": "true", disabled })) }); } function useMarks({ marks, min = 0, max = 100, step = 1, value = 0 }) { if (!marks) return []; const range = max - min; if (!Array.isArray(marks)) { marks = []; const count = 1 + Math.round(range / step); while (count > marks.push({ value: step * marks.length + min })); } const placedMarks = []; marks.forEach((mark, index) => { if (mark.value < min || mark.value > max) return; const key = `mark-${index}`; const isFilled = mark.value <= value; const offset = `${(mark.value - min) / range * 100}%`; const offsetStyle = { [isRTL() ? "right" : "left"]: offset }; placedMarks.push({ ...mark, isFilled, key, style: offsetStyle }); }); return placedMarks; } export { RangeRail as default }; //# sourceMappingURL=rail.mjs.map