@wordpress/components
Version:
UI components for WordPress.
162 lines (161 loc) • 11.7 kB
JavaScript
import clsx from "clsx";
import { useContextSystem } from "../../context/index.mjs";
import { getPaddingBySize } from "../get-padding-by-size.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/card/style.module.scss
if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("fc4ee5131a", "._258c1ce2d4e16874__card{box-shadow:0 0 0 var(--wpds-border-width-xs,1px) var(--wpds-color-stroke-surface-neutral,#dbdbdb);outline:none}._42c9e5f64a3fb269__card-borderless{box-shadow:none}.b64e8d4efff00058__is-rounded{border-radius:max(0px,var(--wpds-border-radius-lg,8px) - var(--wpds-border-width-xs,1px))}.d797cd7f1fd46312__content{height:100%}._115fc6b628947900__header{border-bottom:var(--wpds-border-width-xs,1px) solid var(--wpds-color-stroke-surface-neutral,#dbdbdb);border-color:var(--wpds-color-stroke-surface-neutral,#dbdbdb);box-sizing:border-box}._115fc6b628947900__header:last-child{border-bottom:none}._7269faf6c95b0762__footer{border-top:var(--wpds-border-width-xs,1px) solid var(--wpds-color-stroke-surface-neutral,#dbdbdb);border-color:var(--wpds-color-stroke-surface-neutral,#dbdbdb);box-sizing:border-box}._7269faf6c95b0762__footer:first-of-type{border-top:none}.e01b9e3e951a7d13__body{box-sizing:border-box;height:auto;max-height:100%}._9a163c498b5de7d8__is-scrollable{height:100%}._81765b2289fa1461__media{box-sizing:border-box;overflow:hidden}._81765b2289fa1461__media>iframe,._81765b2289fa1461__media>img{display:block;height:auto;max-width:100%;width:100%}._115fc6b628947900__header:first-of-type,._7269faf6c95b0762__footer:first-of-type,._81765b2289fa1461__media:first-of-type,.e01b9e3e951a7d13__body:first-of-type{border-start-end-radius:max(0px,var(--wpds-border-radius-lg,8px) - var(--wpds-border-width-xs,1px));border-start-start-radius:max(0px,var(--wpds-border-radius-lg,8px) - var(--wpds-border-width-xs,1px))}._115fc6b628947900__header:last-of-type,._7269faf6c95b0762__footer:last-of-type,._81765b2289fa1461__media:last-of-type,.e01b9e3e951a7d13__body:last-of-type{border-end-end-radius:max(0px,var(--wpds-border-radius-lg,8px) - var(--wpds-border-width-xs,1px));border-end-start-radius:max(0px,var(--wpds-border-radius-lg,8px) - var(--wpds-border-width-xs,1px))}._67309c75691cb06e__divider._67309c75691cb06e__divider{--_wp-components-divider-border-width:var(--wpds-border-width-xs,1px);border-color:var(--wpds-color-stroke-surface-neutral,#dbdbdb);box-sizing:border-box;display:block;width:100%}._0737e0d02ce50a60__section-borderless{border:none}._0c62836e2455867c__is-shady{background-color:var(--wp-components-color-gray-100,var(--wpds-color-background-surface-neutral,#fcfcfc))}.f81bbb6338f6c1cc__padding-none{padding:0}._9effffc1b0e250eb__padding-x-small{padding:var(--wpds-dimension-padding-sm,8px)}._397f1b1b1183f5eb__padding-small{padding:var(--wpds-dimension-padding-lg,16px)}._6c9d36c046b7cbb4__padding-medium{padding:var(--wpds-dimension-padding-lg,16px) var(--wpds-dimension-padding-2xl,24px)}._1b850e5f88c05181__padding-large{padding:var(--wpds-dimension-padding-2xl,24px) var(--wpds-dimension-padding-3xl,32px)}.dac29efbc1e0a342__padding-block-start-none{padding-block-start:0}._7911a5ed70dd0454__padding-block-start-x-small{padding-block-start:var(--wpds-dimension-padding-sm,8px)}._105b22e497345ed9__padding-block-start-small{padding-block-start:var(--wpds-dimension-padding-lg,16px)}._4bea5996d0e8808d__padding-block-start-medium{padding-block-start:var(--wpds-dimension-padding-2xl,24px)}.a9bed7cfe29148ea__padding-block-start-large{padding-block-start:var(--wpds-dimension-padding-3xl,32px)}.faa4d8781487cdb9__padding-block-end-none{padding-block-end:0}._59d7036134a4077d__padding-block-end-x-small{padding-block-end:var(--wpds-dimension-padding-sm,8px)}.ba0f51a7d38005b3__padding-block-end-small{padding-block-end:var(--wpds-dimension-padding-lg,16px)}._64c618b6d8247db6__padding-block-end-medium{padding-block-end:var(--wpds-dimension-padding-2xl,24px)}.a1204142117f795d__padding-block-end-large{padding-block-end:var(--wpds-dimension-padding-3xl,32px)}.b5c8dd48cd3e52da__padding-inline-start-none{padding-inline-start:0}._32e9274059a97f16__padding-inline-start-x-small{padding-inline-start:var(--wpds-dimension-padding-sm,8px)}.c1a2d6a93b5d9e4f__padding-inline-start-small{padding-inline-start:var(--wpds-dimension-padding-lg,16px)}._7fbe71e16a09d777__padding-inline-start-medium{padding-inline-start:var(--wpds-dimension-padding-2xl,24px)}._7302b4da797b4293__padding-inline-start-large{padding-inline-start:var(--wpds-dimension-padding-3xl,32px)}._645cf5620059df75__padding-inline-end-none{padding-inline-end:0}._65f785a54d92961e__padding-inline-end-x-small{padding-inline-end:var(--wpds-dimension-padding-sm,8px)}._2c809d7e946fd61a__padding-inline-end-small{padding-inline-end:var(--wpds-dimension-padding-lg,16px)}.b83dda04e1b53ccb__padding-inline-end-medium{padding-inline-end:var(--wpds-dimension-padding-2xl,24px)}.da9b6af297e2b763__padding-inline-end-large{padding-inline-end:var(--wpds-dimension-padding-3xl,32px)}");
var style_module_default = {
"card": "_258c1ce2d4e16874__card",
"card-borderless": "_42c9e5f64a3fb269__card-borderless",
"is-rounded": "b64e8d4efff00058__is-rounded",
"content": "d797cd7f1fd46312__content",
"header": "_115fc6b628947900__header",
"footer": "_7269faf6c95b0762__footer",
"body": "e01b9e3e951a7d13__body",
"is-scrollable": "_9a163c498b5de7d8__is-scrollable",
"media": "_81765b2289fa1461__media",
"divider": "_67309c75691cb06e__divider",
"section-borderless": "_0737e0d02ce50a60__section-borderless",
"is-shady": "_0c62836e2455867c__is-shady",
"padding-none": "f81bbb6338f6c1cc__padding-none",
"padding-x-small": "_9effffc1b0e250eb__padding-x-small",
"padding-small": "_397f1b1b1183f5eb__padding-small",
"padding-medium": "_6c9d36c046b7cbb4__padding-medium",
"padding-large": "_1b850e5f88c05181__padding-large",
"padding-block-start-none": "dac29efbc1e0a342__padding-block-start-none",
"padding-block-start-x-small": "_7911a5ed70dd0454__padding-block-start-x-small",
"padding-block-start-small": "_105b22e497345ed9__padding-block-start-small",
"padding-block-start-medium": "_4bea5996d0e8808d__padding-block-start-medium",
"padding-block-start-large": "a9bed7cfe29148ea__padding-block-start-large",
"padding-block-end-none": "faa4d8781487cdb9__padding-block-end-none",
"padding-block-end-x-small": "_59d7036134a4077d__padding-block-end-x-small",
"padding-block-end-small": "ba0f51a7d38005b3__padding-block-end-small",
"padding-block-end-medium": "_64c618b6d8247db6__padding-block-end-medium",
"padding-block-end-large": "a1204142117f795d__padding-block-end-large",
"padding-inline-start-none": "b5c8dd48cd3e52da__padding-inline-start-none",
"padding-inline-start-x-small": "_32e9274059a97f16__padding-inline-start-x-small",
"padding-inline-start-small": "c1a2d6a93b5d9e4f__padding-inline-start-small",
"padding-inline-start-medium": "_7fbe71e16a09d777__padding-inline-start-medium",
"padding-inline-start-large": "_7302b4da797b4293__padding-inline-start-large",
"padding-inline-end-none": "_645cf5620059df75__padding-inline-end-none",
"padding-inline-end-x-small": "_65f785a54d92961e__padding-inline-end-x-small",
"padding-inline-end-small": "_2c809d7e946fd61a__padding-inline-end-small",
"padding-inline-end-medium": "b83dda04e1b53ccb__padding-inline-end-medium",
"padding-inline-end-large": "da9b6af297e2b763__padding-inline-end-large"
};
// packages/components/src/card/card-footer/hook.ts
function useCardFooter(props) {
const { className, justify, isBorderless = false, isShady = false, size = "medium", ...otherProps } = useContextSystem(props, "CardFooter");
const classes = clsx(style_module_default.footer, getPaddingBySize(size), {
[style_module_default["section-borderless"]]: isBorderless,
[style_module_default["is-shady"]]: isShady
}, "components-card__footer", className);
return {
...otherProps,
className: classes,
justify
};
}
export { useCardFooter };
//# sourceMappingURL=hook.mjs.map