UNPKG

@wordpress/components

Version:
294 lines (293 loc) • 12 kB
"use strict"; 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 _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_element = require("@wordpress/element"); let ___circular_option_picker_index_tsx = require("../circular-option-picker/index.cjs"); ___circular_option_picker_index_tsx = __toESM(___circular_option_picker_index_tsx); let ___custom_gradient_picker_index_tsx = require("../custom-gradient-picker/index.cjs"); ___custom_gradient_picker_index_tsx = __toESM(___custom_gradient_picker_index_tsx); let ___v_stack_index_ts = require("../v-stack/index.cjs"); let ___heading_index_ts = require("../heading/index.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/color-palette/style.module.scss if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("ba988b4dc4", ".d557a658e7010f86__color-heading{text-transform:uppercase}.d557a658e7010f86__color-heading.d557a658e7010f86__color-heading.d557a658e7010f86__color-heading{font-size:11px;line-height:24px;margin-bottom:0}"); var style_module_default = { "color-heading": "d557a658e7010f86__color-heading" }; // packages/components/src/gradient-picker/index.tsx const isMultipleOriginObject = (obj) => Array.isArray(obj.gradients) && !("gradient" in obj); const isMultipleOriginArray = (arr) => { return arr.length > 0 && arr.every((gradientObj) => isMultipleOriginObject(gradientObj)); }; function SingleOrigin({ className, clearGradient, gradients, onChange, value, selectedSlug, presentation, ...additionalProps }) { const gradientOptions = (0, _wordpress_element.useMemo)(() => { return gradients.map(({ gradient, name, slug }, index) => { const isSelected = presentation !== "command-buttons" && (selectedSlug ? slug === selectedSlug : value === gradient); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default.Option, { value: gradient, isSelected, tooltipText: name || (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Gradient code: %s"), gradient), style: { color: "rgba( 0,0,0,0 )", background: gradient }, onClick: isSelected ? clearGradient : () => onChange(gradient, index, slug), "aria-label": name ? (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Gradient: %s"), name) : (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Gradient code: %s"), gradient) }, slug); }); }, [ gradients, value, onChange, clearGradient, selectedSlug, presentation ]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default.OptionGroup, { className, options: gradientOptions, ...additionalProps }); } function MultipleOrigin({ className, clearGradient, gradients, onChange, value, selectedSlug, headingLevel, presentation }) { const instanceId = (0, _wordpress_compose.useInstanceId)(MultipleOrigin); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___v_stack_index_ts.VStack, { spacing: 3, className, children: gradients.map(({ name, gradients: gradientSet }, index) => { const id = `color-palette-${instanceId}-${index}`; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___v_stack_index_ts.VStack, { spacing: 2, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(___heading_index_ts.Heading, { className: style_module_default["color-heading"], level: headingLevel, id, children: name }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SingleOrigin, { clearGradient, gradients: gradientSet, onChange: (gradient, _index, slug) => onChange(gradient, index, slug), value, selectedSlug, presentation, "aria-labelledby": id })] }, index); }) }); } function Component(props) { const { asButtons, presentation, loop, actions, headingLevel, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledby, ...additionalProps } = props; (0, ___circular_option_picker_index_tsx.warnIfCircularOptionPickerAsButtonsIsSet)("GradientPicker", asButtons); const { metaProps, labelProps, resolvedPresentation } = (0, ___circular_option_picker_index_tsx.getComputeCircularOptionPickerCommonProps)(asButtons, loop, ariaLabel, ariaLabelledby, presentation); const options = isMultipleOriginArray(props.gradients) ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MultipleOrigin, { headingLevel, ...additionalProps, presentation: resolvedPresentation }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SingleOrigin, { ...additionalProps, presentation: resolvedPresentation }); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default, { ...metaProps, ...labelProps, actions, options }); } /** * GradientPicker is a React component that renders a color gradient picker to * define a multi step gradient. There's either a _linear_ or a _radial_ type * available. * * ```jsx * import { useState } from 'react'; * import { GradientPicker } from '@wordpress/components'; * * const MyGradientPicker = () => { * const [ gradient, setGradient ] = useState( null ); * * return ( * <GradientPicker * value={ gradient } * onChange={ ( currentGradient ) => setGradient( currentGradient ) } * gradients={ [ * { * name: 'JShine', * gradient: * 'linear-gradient(135deg,#12c2e9 0%,#c471ed 50%,#f64f59 100%)', * slug: 'jshine', * }, * { * name: 'Moonlit Asteroid', * gradient: * 'linear-gradient(135deg,#0F2027 0%, #203A43 0%, #2c5364 100%)', * slug: 'moonlit-asteroid', * }, * { * name: 'Rastafarie', * gradient: * 'linear-gradient(135deg,#1E9600 0%, #FFF200 0%, #FF0000 100%)', * slug: 'rastafari', * }, * ] } * /> * ); * }; *``` * */ function GradientPicker({ className, gradients = [], onChange, value, clearable = true, enableAlpha = true, disableCustomGradients = false, __experimentalIsRenderedInSidebar, headingLevel = 2, ...additionalProps }) { const clearGradient = (0, _wordpress_element.useCallback)(() => onChange(void 0), [onChange]); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___v_stack_index_ts.VStack, { spacing: gradients.length ? 4 : 0, children: [!disableCustomGradients && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___custom_gradient_picker_index_tsx.default, { __experimentalIsRenderedInSidebar, enableAlpha, value, onChange }), (gradients.length > 0 || clearable) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, { ...additionalProps, className, clearGradient, gradients, onChange, value, actions: clearable && !disableCustomGradients && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default.ButtonAction, { onClick: clearGradient, accessibleWhenDisabled: true, disabled: !value, children: (0, _wordpress_i18n.__)("Clear") }), headingLevel })] }); } exports.GradientPicker = GradientPicker; exports.default = GradientPicker; //# sourceMappingURL=index.cjs.map