@wordpress/components
Version:
UI components for WordPress.
173 lines (172 loc) • 10.6 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let clsx = require("clsx");
clsx = __toESM(clsx);
let _wordpress_element = require("@wordpress/element");
let __utils_ts = require("./utils.cjs");
let react_jsx_runtime = require("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/unit-control/style.module.scss
if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("abc278533a", ".c259d71ec42c1f39__value-input.c259d71ec42c1f39__value-input.c259d71ec42c1f39__value-input input{display:block;width:100%}.c259d71ec42c1f39__value-input.c259d71ec42c1f39__value-input.c259d71ec42c1f39__value-input .components-input-control__backdrop{transition:box-shadow .1s linear}._432c86bd74f61719__unit-label._432c86bd74f61719__unit-label._432c86bd74f61719__unit-label{color:#1e1e1e;pointer-events:none}._432c86bd74f61719__unit-label._432c86bd74f61719__unit-label._432c86bd74f61719__unit-label:where(:not(.dd21e6d701b2835d__is-small)){field-sizing:content;box-sizing:border-box;font-size:13px;height:24px;line-height:1;margin-inline-end:8px;max-width:48px;min-width:24px;overflow:hidden;padding:4px;text-align-last:center;text-overflow:ellipsis;white-space:nowrap}._432c86bd74f61719__unit-label._432c86bd74f61719__unit-label._432c86bd74f61719__unit-label:where(.dd21e6d701b2835d__is-small){box-sizing:border-box;font-size:8px;letter-spacing:-.5px;line-height:1;padding:2px 1px;text-align-last:center;text-transform:uppercase;width:20px}._432c86bd74f61719__unit-label._432c86bd74f61719__unit-label._432c86bd74f61719__unit-label:where(.dd21e6d701b2835d__is-small):not(:disabled){color:#2f2f2f}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select{appearance:none;background:transparent;border:none;border-radius:1px;display:block;font-family:inherit;margin:0;min-height:auto;outline:none}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select:not(:disabled){cursor:var(--wpds-cursor-control,pointer)}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select:not(:disabled):hover{background-color:#f0f0f0}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select:focus{box-shadow:none;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))}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select:where(.dd21e6d701b2835d__is-small){box-sizing:border-box;font-size:8px;letter-spacing:-.5px;line-height:1;padding:2px 1px;text-align-last:center;text-transform:uppercase;width:20px}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select:where(.dd21e6d701b2835d__is-small):not(:disabled){color:#2f2f2f}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select:where(.dd21e6d701b2835d__is-small){border-end-start-radius:0;border-start-start-radius:0;height:100%}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select:where(.dd21e6d701b2835d__is-small):focus{outline-offset:calc(var(--wpds-border-width-focus, var(--wp-admin-border-width-focus, 2px))*-1);z-index:1}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select:where(:not(.dd21e6d701b2835d__is-small)){field-sizing:content;align-items:center;border-radius:var(--wpds-border-radius-sm,2px);box-sizing:border-box;display:flex;font-size:13px;height:24px;justify-content:center;line-height:1;margin-inline-end:8px;max-width:48px;min-width:24px;overflow:hidden;padding:4px;text-align-last:center;text-overflow:ellipsis;white-space:nowrap}._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select._60ab6e3748cd9e88__unit-select:where(:not(.dd21e6d701b2835d__is-small)):not(:disabled){color:var(--wp-components-color-accent,var(--wp-admin-theme-color,#3858e9))}");
var style_module_default = {
"value-input": "c259d71ec42c1f39__value-input",
"unit-label": "_432c86bd74f61719__unit-label",
"is-small": "dd21e6d701b2835d__is-small",
"unit-select": "_60ab6e3748cd9e88__unit-select"
};
// packages/components/src/unit-control/unit-select-control.tsx
function UnitSelectControl({ className, isUnitSelectTabbable: isTabbable = true, onChange, size = "default", unit = "px", units = __utils_ts.CSS_UNITS, ...props }, ref) {
if (!(0, __utils_ts.hasUnits)(units) || units?.length === 1) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: (0, clsx.default)("components-unit-control__unit-label", style_module_default["unit-label"], { [style_module_default["is-small"]]: size === "small" }),
children: unit
});
const handleOnChange = (event) => {
const { value: unitValue } = event.target;
const data = units.find((option) => option.value === unitValue);
onChange?.(unitValue, {
event,
data
});
};
const classes = (0, clsx.default)("components-unit-control__select", style_module_default["unit-select"], className, { [style_module_default["is-small"]]: size === "small" });
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("select", {
ref,
className: classes,
onChange: handleOnChange,
tabIndex: isTabbable ? void 0 : -1,
value: unit,
...props,
children: units.map((option) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("option", {
value: option.value,
children: option.label
}, option.value))
});
}
var unit_select_control_default = (0, _wordpress_element.forwardRef)(UnitSelectControl);
exports.default = unit_select_control_default;
//# sourceMappingURL=unit-select-control.cjs.map