UNPKG

@wordpress/global-styles-ui

Version:
95 lines (94 loc) • 4.18 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 clsx = require("clsx"); clsx = __toESM(clsx); let _wordpress_components = require("@wordpress/components"); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_element = require("@wordpress/element"); let _wordpress_ui = require("@wordpress/ui"); let __hooks_ts = require("./hooks.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/global-styles-ui/src/preview-wrapper.tsx const normalizedWidth = 248; const normalizedHeight = 152; const THROTTLE_OPTIONS = { leading: true, trailing: true }; function PreviewWrapper({ children, label, isFocused, withHoverView }) { const [backgroundColor = "white"] = (0, __hooks_ts.useStyle)("color.background"); const [gradientValue] = (0, __hooks_ts.useStyle)("color.gradient"); const disableMotion = (0, _wordpress_compose.useReducedMotion)(); const [isHovered, setIsHovered] = (0, _wordpress_element.useState)(false); const [containerResizeListener, { width }] = (0, _wordpress_compose.useResizeObserver)(); const [throttledWidth, setThrottledWidthState] = (0, _wordpress_element.useState)(width); const [ratioState, setRatioState] = (0, _wordpress_element.useState)(); const setThrottledWidth = (0, _wordpress_compose.useThrottle)(setThrottledWidthState, 250, THROTTLE_OPTIONS); (0, _wordpress_element.useLayoutEffect)(() => { if (width) setThrottledWidth(width); }, [width, setThrottledWidth]); (0, _wordpress_element.useLayoutEffect)(() => { const newRatio = throttledWidth ? throttledWidth / normalizedWidth : 1; const ratioDiff = newRatio - (ratioState || 0); if (Math.abs(ratioDiff) > .1 || !ratioState) setRatioState(newRatio); }, [throttledWidth, ratioState]); const fallbackRatio = width ? width / normalizedWidth : 1; const ratio = ratioState ? ratioState : fallbackRatio; const isReady = !!width; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { style: { position: "relative" }, children: containerResizeListener }), !isReady && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Skeleton, { className: "global-styles-ui-preview__wrapper", style: { aspectRatio: normalizedWidth / normalizedHeight } }), isReady && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: (0, clsx.default)("global-styles-ui-preview__wrapper", { "is-hoverable": withHoverView }), style: { height: normalizedHeight * ratio }, onMouseEnter: () => setIsHovered(true), onMouseLeave: () => setIsHovered(false), tabIndex: -1, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__unstableMotion.div, { style: { height: normalizedHeight * ratio, width: "100%", background: gradientValue ?? backgroundColor }, initial: "start", animate: (isHovered || isFocused) && !disableMotion && label ? "hover" : "start", children: [].concat(children).map((child, key) => child({ ratio, key })) }) }) ] }); } exports.default = PreviewWrapper; //# sourceMappingURL=preview-wrapper.cjs.map