UNPKG

@wordpress/components

Version:
249 lines (248 loc) • 15.1 kB
import clsx from "clsx"; import { Children, cloneElement, useMemo } from "@wordpress/element"; import { hasConnectNamespace, useContextSystem } from "../context/index.mjs"; import { useTruncate } from "../truncate/index.mjs"; import { getOptimalTextShade } from "../utils/colors.mjs"; import { createHighlighterText } from "./utils.mjs"; import { getFontSize } from "../utils/font-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/text/style.module.scss if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("e27a9e3ac6", "._9adcfccb60b2247a__text{--wp-components-text-color:initial;--wp-components-text-display:initial;--wp-components-text-font-size:initial;--wp-components-text-font-weight:initial;--wp-components-text-line-height:initial;--wp-components-text-letter-spacing:initial;--wp-components-text-text-align:initial;color:var(--wp-components-text-color,var(--wp-components-color-foreground,var(--wpds-color-foreground-content-neutral,#1e1e1e)));font-size:var(--wp-components-text-font-size);font-weight:var(--wp-components-text-font-weight);line-height:var(--wp-components-text-line-height,1.4);margin:0}.be2801831b79afba__control-line-height{--wp-components-text-line-height:44px}._2766c02d4e2af624__control-line-height-large{--wp-components-text-line-height:51.2px}._9e9a12b52be6eaa6__control-line-height-small{--wp-components-text-line-height:36.8px}._9679ea4f8130b41b__control-line-height-xSmall{--wp-components-text-line-height:29.6px}:where(._9adcfccb60b2247a__text){text-wrap:pretty}.c60c92f64a762322__has-display{display:var(--wp-components-text-display)}.b175e6c806c12b91__has-letter-spacing{letter-spacing:var(--wp-components-text-letter-spacing)}._61d26f9e5a24b029__has-text-align{text-align:var(--wp-components-text-text-align)}._62c63396e878c636__color-inherit{color:inherit}.fa1a366a0316de8a__color-initial{color:initial}.bedd7276c2009829__color-unset{color:unset}._3d00572545c63e6a__color-revert{color:revert}._4dacbd11c988fe99__color-revert-layer{color:revert-layer}._5e040fbfaf4f4d38__display-inherit{display:inherit}._7220342a95fdd85c__display-initial{display:initial}.e20489bdd99704f2__display-unset{display:unset}.b5f39b2366b6dffe__display-revert{display:revert}.cf965ed661ac7fb2__display-revert-layer{display:revert-layer}._0f1f78c473941cbe__font-size-inherit{font-size:inherit}.a93c3905fbfc11bc__font-size-initial{font-size:medium}._1e50723f80ef5d43__font-size-unset{font-size:unset}.a95b24f6b61444bd__font-size-revert{font-size:revert}._2079c077bf1fbdcf__font-size-revert-layer{font-size:revert-layer}.d0d698763b755384__font-weight-inherit{font-weight:inherit}._13163ec25c4af630__font-weight-initial{font-weight:400}._8f72ce3ee1e025ad__font-weight-unset{font-weight:unset}._37b153dc1dea90f6__font-weight-revert{font-weight:revert}.f404e4e0bf4855e5__font-weight-revert-layer{font-weight:revert-layer}._9b78387743829f99__line-height-inherit{line-height:inherit}._8a98bdbfcd1d3306__line-height-initial{line-height:normal}._32bcd8dbe349bf0f__line-height-unset{line-height:unset}.b4012a1a310357f4__line-height-revert{line-height:revert}.ff2ef4eddcb6e028__line-height-revert-layer{line-height:revert-layer}._235241409a6f8103__letter-spacing-inherit{letter-spacing:inherit}.d860cf0adfc31190__letter-spacing-initial{letter-spacing:normal}.abadaec0a0ceadbf__letter-spacing-unset{letter-spacing:unset}.d6fb6f3766caddb2__letter-spacing-revert{letter-spacing:revert}._00768243984a3b13__letter-spacing-revert-layer{letter-spacing:revert-layer}._82d28f4984b6a9f7__text-align-inherit{text-align:inherit}._799255031fe610e4__text-align-initial{text-align:initial}.b1ee967d38e16fec__text-align-unset{text-align:unset}._6fc3935d62ac2f4e__text-align-revert{text-align:revert}._523bd3263fe71193__text-align-revert-layer{text-align:revert-layer}._7c2beb4bab7d50b1__readability-dark{color:#1e1e1e}._69065619bb900bef__readability-light{color:#fff}._72e2728c12780836__destructive{color:#d94f4f}._5319770adc3fb346__highlighter-text mark{background:#f0b849;border-radius:2px;box-shadow:inset 0 0 0 1px rgba(0,0,0,.05),inset 0 -1px 0 rgba(0,0,0,.1)}._739dffda40610a31__block{display:block}.dcc48a33948e2e77__muted{color:var(--wp-components-color-gray-700,var(--wpds-color-foreground-content-neutral-weak,#6e6e6e))}._813d9e8ef49f4085__upper-case{text-transform:uppercase}"); var style_module_default = { "text": "_9adcfccb60b2247a__text", "control-line-height": "be2801831b79afba__control-line-height", "control-line-height-large": "_2766c02d4e2af624__control-line-height-large", "control-line-height-small": "_9e9a12b52be6eaa6__control-line-height-small", "control-line-height-xSmall": "_9679ea4f8130b41b__control-line-height-xSmall", "has-display": "c60c92f64a762322__has-display", "has-letter-spacing": "b175e6c806c12b91__has-letter-spacing", "has-text-align": "_61d26f9e5a24b029__has-text-align", "color-inherit": "_62c63396e878c636__color-inherit", "color-initial": "fa1a366a0316de8a__color-initial", "color-unset": "bedd7276c2009829__color-unset", "color-revert": "_3d00572545c63e6a__color-revert", "color-revert-layer": "_4dacbd11c988fe99__color-revert-layer", "display-inherit": "_5e040fbfaf4f4d38__display-inherit", "display-initial": "_7220342a95fdd85c__display-initial", "display-unset": "e20489bdd99704f2__display-unset", "display-revert": "b5f39b2366b6dffe__display-revert", "display-revert-layer": "cf965ed661ac7fb2__display-revert-layer", "font-size-inherit": "_0f1f78c473941cbe__font-size-inherit", "font-size-initial": "a93c3905fbfc11bc__font-size-initial", "font-size-unset": "_1e50723f80ef5d43__font-size-unset", "font-size-revert": "a95b24f6b61444bd__font-size-revert", "font-size-revert-layer": "_2079c077bf1fbdcf__font-size-revert-layer", "font-weight-inherit": "d0d698763b755384__font-weight-inherit", "font-weight-initial": "_13163ec25c4af630__font-weight-initial", "font-weight-unset": "_8f72ce3ee1e025ad__font-weight-unset", "font-weight-revert": "_37b153dc1dea90f6__font-weight-revert", "font-weight-revert-layer": "f404e4e0bf4855e5__font-weight-revert-layer", "line-height-inherit": "_9b78387743829f99__line-height-inherit", "line-height-initial": "_8a98bdbfcd1d3306__line-height-initial", "line-height-unset": "_32bcd8dbe349bf0f__line-height-unset", "line-height-revert": "b4012a1a310357f4__line-height-revert", "line-height-revert-layer": "ff2ef4eddcb6e028__line-height-revert-layer", "letter-spacing-inherit": "_235241409a6f8103__letter-spacing-inherit", "letter-spacing-initial": "d860cf0adfc31190__letter-spacing-initial", "letter-spacing-unset": "abadaec0a0ceadbf__letter-spacing-unset", "letter-spacing-revert": "d6fb6f3766caddb2__letter-spacing-revert", "letter-spacing-revert-layer": "_00768243984a3b13__letter-spacing-revert-layer", "text-align-inherit": "_82d28f4984b6a9f7__text-align-inherit", "text-align-initial": "_799255031fe610e4__text-align-initial", "text-align-unset": "b1ee967d38e16fec__text-align-unset", "text-align-revert": "_6fc3935d62ac2f4e__text-align-revert", "text-align-revert-layer": "_523bd3263fe71193__text-align-revert-layer", "readability-dark": "_7c2beb4bab7d50b1__readability-dark", "readability-light": "_69065619bb900bef__readability-light", "destructive": "_72e2728c12780836__destructive", "highlighter-text": "_5319770adc3fb346__highlighter-text", "block": "_739dffda40610a31__block", "muted": "dcc48a33948e2e77__muted", "upper-case": "_813d9e8ef49f4085__upper-case" }; // packages/components/src/text/hook.ts const CSS_WIDE_KEYWORDS = /* @__PURE__ */ new Set([ "inherit", "initial", "unset", "revert", "revert-layer" ]); /** * @param {import('../context').WordPressComponentProps<import('./types').Props, 'span'>} props */ function useText(props) { const { adjustLineHeightForInnerControls, align, children, className, color, ellipsizeMode, isDestructive = false, display, highlightEscape = false, highlightCaseSensitive = false, highlightWords, highlightSanitize, isBlock = false, letterSpacing, lineHeight: lineHeightProp, optimizeReadabilityFor, size, style, truncate = false, upperCase = false, variant, weight = "var(--wpds-typography-font-weight-default, 400)", ...otherProps } = useContextSystem(props, "Text"); let content = children; const isHighlighter = Array.isArray(highlightWords); const isCaption = size === "caption"; if (isHighlighter) { if (typeof children !== "string") throw new TypeError("`children` of `Text` must only be `string` types when `highlightWords` is defined"); content = createHighlighterText({ autoEscape: highlightEscape, children, caseSensitive: highlightCaseSensitive, searchWords: highlightWords, sanitize: highlightSanitize }); } const optimalTextShade = useMemo(() => optimizeReadabilityFor && getOptimalTextShade(optimizeReadabilityFor), [optimizeReadabilityFor]); const typography = { color, display, "font-size": getFontSize(size), "font-weight": weight, "line-height": lineHeightProp || void 0, "letter-spacing": typeof letterSpacing === "number" ? `${letterSpacing}px` : letterSpacing, "text-align": align }; const textStyle = {}; const keywordClasses = []; for (const [property, value] of Object.entries(typography)) { const keyword = typeof value === "string" ? value.trim().toLowerCase() : void 0; if (keyword && CSS_WIDE_KEYWORDS.has(keyword)) keywordClasses.push(style_module_default[`${property}-${keyword}`]); else textStyle[`--wp-components-text-${property}`] = value === "" ? void 0 : value; } const classes = clsx(style_module_default.text, !lineHeightProp && adjustLineHeightForInnerControls && [style_module_default["control-line-height"], style_module_default[`control-line-height-${adjustLineHeightForInnerControls}`]], { [style_module_default["has-display"]]: !!display, [style_module_default["has-letter-spacing"]]: letterSpacing !== void 0 && letterSpacing !== "", [style_module_default["has-text-align"]]: !!align }, keywordClasses, { [style_module_default["readability-dark"]]: optimalTextShade === "dark", [style_module_default["readability-light"]]: !!optimalTextShade && optimalTextShade !== "dark", [style_module_default.destructive]: isDestructive, [style_module_default["highlighter-text"]]: isHighlighter, [style_module_default.block]: isBlock, [style_module_default.muted]: isCaption || variant === "muted", [style_module_default["upper-case"]]: upperCase }, className); let finalEllipsizeMode; if (truncate === true) finalEllipsizeMode = "auto"; if (truncate === false) finalEllipsizeMode = "none"; const finalComponentProps = { ...otherProps, className: classes, style: { ...textStyle, ...style }, children, ellipsizeMode: ellipsizeMode || finalEllipsizeMode }; const truncateProps = useTruncate(finalComponentProps); /** * Enhance child `<Link />` components to inherit font size. */ if (!truncate && Array.isArray(children)) content = Children.map(children, (child) => { if (typeof child !== "object" || child === null || !("props" in child)) return child; if (hasConnectNamespace(child, ["Link"])) return cloneElement(child, { size: child.props.size || "inherit" }); return child; }); return { ...truncateProps, children: truncate ? truncateProps.children : content }; } export { useText as default }; //# sourceMappingURL=hook.mjs.map