@wordpress/components
Version:
UI components for WordPress.
226 lines (225 loc) • 14 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 ___context_index_ts = require("../context/index.cjs");
let __utils_ts = require("./utils.cjs");
let ___utils_use_responsive_value_ts = require("../utils/use-responsive-value.cjs");
// 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/grid/style.module.scss
if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("b1a8e206b7", ".ad3e2e48d7abbecd__grid{--wp-components-grid-align:initial;--wp-components-grid-justify:initial;--wp-components-grid-gap:initial;--wp-components-grid-template-columns:initial;--wp-components-grid-template-rows:initial;--wp-components-grid-row-gap:initial;--wp-components-grid-column-gap:initial;display:grid}.ad3e2e48d7abbecd__grid:not(:where(._519ed3f6107b2f0e__row-gap-inherit,.cd62b95f8b7a55ca__row-gap-initial,._226744df7703790b__row-gap-unset,._372e9fe7fa60a162__row-gap-revert,.a788bb6771f557d4__row-gap-revert-layer)){row-gap:var(--wp-components-grid-row-gap,calc(4px*var(--wp-components-grid-gap)))}.ad3e2e48d7abbecd__grid:not(:where(.c811e15146756906__column-gap-inherit,._17d3c2ea7daf9367__column-gap-initial,._595e6195481b9016__column-gap-unset,.a3ecb3e47731c19e__column-gap-revert,._7c87cfbf3c22f476__column-gap-revert-layer)){column-gap:var(--wp-components-grid-column-gap,calc(4px*var(--wp-components-grid-gap)))}._25a972dc3223dff3__has-align:not(:where(._3cda38ca04ad4eb9__align-inherit,.f1d7342c52a8cb04__align-initial,._0d1a6c7ee990f351__align-unset,._57c7b85e7abf618b__align-revert,._82cb0c4950962787__align-revert-layer)){align-items:var(--wp-components-grid-align)}._801d1214723237b8__has-justify:not(:where(.b83c5dbf3264515b__justify-inherit,._5b7394b39d69670e__justify-initial,._3e7f0418752602e5__justify-unset,._5f937df46d7b2259__justify-revert,.bf2a4718a1ddba5c__justify-revert-layer)){justify-content:var(--wp-components-grid-justify)}._3c6ac9820776f1cb__has-template-columns:not(:where(._35faa4d32eedef32__template-columns-inherit,._8150cadb3f438833__template-columns-initial,.bcd5f2b096dec164__template-columns-unset,._48d047bc6fb687b8__template-columns-revert,._93f250669aa1a5a7__template-columns-revert-layer)){grid-template-columns:var(--wp-components-grid-template-columns)}.c2703b6b15e69b82__has-template-rows:not(:where(._3e9d5ffc53e4afc0__template-rows-inherit,._64bb5fbdf6d1038a__template-rows-initial,.f8f5df7043222325__template-rows-unset,._408aaf58100e60d5__template-rows-revert,.e0730d6abf0f3d2a__template-rows-revert-layer)){grid-template-rows:var(--wp-components-grid-template-rows)}._43af1cbfad3e73dd__is-inline{display:inline-grid;vertical-align:middle}._3cda38ca04ad4eb9__align-inherit{align-items:inherit}.f1d7342c52a8cb04__align-initial{align-items:normal}._0d1a6c7ee990f351__align-unset{align-items:unset}._57c7b85e7abf618b__align-revert{align-items:revert}._82cb0c4950962787__align-revert-layer{align-items:revert-layer}.b83c5dbf3264515b__justify-inherit{justify-content:inherit}._5b7394b39d69670e__justify-initial{justify-content:normal}._3e7f0418752602e5__justify-unset{justify-content:unset}._5f937df46d7b2259__justify-revert{justify-content:revert}.bf2a4718a1ddba5c__justify-revert-layer{justify-content:revert-layer}._35faa4d32eedef32__template-columns-inherit{grid-template-columns:inherit}._8150cadb3f438833__template-columns-initial{grid-template-columns:none}.bcd5f2b096dec164__template-columns-unset{grid-template-columns:unset}._48d047bc6fb687b8__template-columns-revert{grid-template-columns:revert}._93f250669aa1a5a7__template-columns-revert-layer{grid-template-columns:revert-layer}._3e9d5ffc53e4afc0__template-rows-inherit{grid-template-rows:inherit}._64bb5fbdf6d1038a__template-rows-initial{grid-template-rows:none}.f8f5df7043222325__template-rows-unset{grid-template-rows:unset}._408aaf58100e60d5__template-rows-revert{grid-template-rows:revert}.e0730d6abf0f3d2a__template-rows-revert-layer{grid-template-rows:revert-layer}._519ed3f6107b2f0e__row-gap-inherit{row-gap:inherit}.cd62b95f8b7a55ca__row-gap-initial{row-gap:normal}._226744df7703790b__row-gap-unset{row-gap:unset}._372e9fe7fa60a162__row-gap-revert{row-gap:revert}.a788bb6771f557d4__row-gap-revert-layer{row-gap:revert-layer}.c811e15146756906__column-gap-inherit{column-gap:inherit}._17d3c2ea7daf9367__column-gap-initial{column-gap:normal}._595e6195481b9016__column-gap-unset{column-gap:unset}.a3ecb3e47731c19e__column-gap-revert{column-gap:revert}._7c87cfbf3c22f476__column-gap-revert-layer{column-gap:revert-layer}");
var style_module_default = {
"grid": "ad3e2e48d7abbecd__grid",
"row-gap-inherit": "_519ed3f6107b2f0e__row-gap-inherit",
"row-gap-initial": "cd62b95f8b7a55ca__row-gap-initial",
"row-gap-unset": "_226744df7703790b__row-gap-unset",
"row-gap-revert": "_372e9fe7fa60a162__row-gap-revert",
"row-gap-revert-layer": "a788bb6771f557d4__row-gap-revert-layer",
"column-gap-inherit": "c811e15146756906__column-gap-inherit",
"column-gap-initial": "_17d3c2ea7daf9367__column-gap-initial",
"column-gap-unset": "_595e6195481b9016__column-gap-unset",
"column-gap-revert": "a3ecb3e47731c19e__column-gap-revert",
"column-gap-revert-layer": "_7c87cfbf3c22f476__column-gap-revert-layer",
"has-align": "_25a972dc3223dff3__has-align",
"align-inherit": "_3cda38ca04ad4eb9__align-inherit",
"align-initial": "f1d7342c52a8cb04__align-initial",
"align-unset": "_0d1a6c7ee990f351__align-unset",
"align-revert": "_57c7b85e7abf618b__align-revert",
"align-revert-layer": "_82cb0c4950962787__align-revert-layer",
"has-justify": "_801d1214723237b8__has-justify",
"justify-inherit": "b83c5dbf3264515b__justify-inherit",
"justify-initial": "_5b7394b39d69670e__justify-initial",
"justify-unset": "_3e7f0418752602e5__justify-unset",
"justify-revert": "_5f937df46d7b2259__justify-revert",
"justify-revert-layer": "bf2a4718a1ddba5c__justify-revert-layer",
"has-template-columns": "_3c6ac9820776f1cb__has-template-columns",
"template-columns-inherit": "_35faa4d32eedef32__template-columns-inherit",
"template-columns-initial": "_8150cadb3f438833__template-columns-initial",
"template-columns-unset": "bcd5f2b096dec164__template-columns-unset",
"template-columns-revert": "_48d047bc6fb687b8__template-columns-revert",
"template-columns-revert-layer": "_93f250669aa1a5a7__template-columns-revert-layer",
"has-template-rows": "c2703b6b15e69b82__has-template-rows",
"template-rows-inherit": "_3e9d5ffc53e4afc0__template-rows-inherit",
"template-rows-initial": "_64bb5fbdf6d1038a__template-rows-initial",
"template-rows-unset": "f8f5df7043222325__template-rows-unset",
"template-rows-revert": "_408aaf58100e60d5__template-rows-revert",
"template-rows-revert-layer": "e0730d6abf0f3d2a__template-rows-revert-layer",
"is-inline": "_43af1cbfad3e73dd__is-inline"
};
// packages/components/src/grid/hook.ts
const CSS_WIDE_KEYWORDS = /* @__PURE__ */ new Set([
"inherit",
"initial",
"unset",
"revert",
"revert-layer"
]);
function toCSSValue(value) {
return typeof value === "number" ? `${value}px` : value?.trim() || void 0;
}
function useGrid(props) {
const { align, alignment, className, columnGap, columns = 2, gap = 3, isInline = false, justify, rowGap, rows, style, templateColumns, templateRows, ...otherProps } = (0, ___context_index_ts.useContextSystem)(props, "Grid");
const column = (0, ___utils_use_responsive_value_ts.useResponsiveValue)(Array.isArray(columns) ? columns : [columns]);
const row = (0, ___utils_use_responsive_value_ts.useResponsiveValue)(Array.isArray(rows) ? rows : [rows]);
const gridTemplateColumns = templateColumns || !!column && `repeat( ${column}, 1fr )`;
const gridTemplateRows = templateRows || !!row && `repeat( ${row}, 1fr )`;
const alignmentProps = (0, __utils_ts.getAlignmentProps)(alignment);
const values = {
align: alignmentProps.alignItems ?? align,
justify: alignmentProps.justifyContent ?? justify,
"template-columns": gridTemplateColumns || void 0,
"template-rows": gridTemplateRows || void 0,
"row-gap": rowGap,
"column-gap": columnGap
};
return {
...otherProps,
className: (0, clsx.default)(style_module_default.grid, {
[style_module_default["is-inline"]]: isInline,
[style_module_default["has-align"]]: !!values.align?.trim(),
[style_module_default["has-justify"]]: !!values.justify?.trim(),
[style_module_default["has-template-columns"]]: !!toCSSValue(values["template-columns"]),
[style_module_default["has-template-rows"]]: !!toCSSValue(values["template-rows"])
}, Object.entries(values).map(([property, value]) => {
const keyword = typeof value === "string" ? value.trim().toLowerCase() : "";
return CSS_WIDE_KEYWORDS.has(keyword) && style_module_default[`${property}-${keyword}`];
}), className),
style: {
"--wp-components-grid-align": values.align,
"--wp-components-grid-justify": values.justify,
"--wp-components-grid-gap": gap,
"--wp-components-grid-template-columns": toCSSValue(values["template-columns"]),
"--wp-components-grid-template-rows": toCSSValue(values["template-rows"]),
"--wp-components-grid-row-gap": toCSSValue(rowGap),
"--wp-components-grid-column-gap": toCSSValue(columnGap),
...style
}
};
}
exports.default = useGrid;
//# sourceMappingURL=hook.cjs.map