UNPKG

@wordpress/global-styles-ui

Version:
253 lines (252 loc) • 10.8 kB
import { getBlockType } from "@wordpress/blocks"; import { privateApis } from "@wordpress/block-editor"; import { useContext, useMemo, useState } from "@wordpress/element"; import { PanelBody, __experimentalHasSplitBorders, __experimentalVStack } from "@wordpress/components"; import { __, sprintf } from "@wordpress/i18n"; import { setSetting, setStyle } from "@wordpress/global-styles-engine"; import { ScreenHeader } from "./screen-header.mjs"; import BlockPreviewPanel from "./block-preview-panel.mjs"; import { Subtitle } from "./subtitle.mjs"; import { VariationsPanel, useBlockVariations } from "./variations/variations-panel.mjs"; import { useCanEditCSS, useSetting, useStyle, useStyleWithResolvedBackground } from "./hooks.mjs"; import { GlobalStylesContext } from "./context.mjs"; import { unlock } from "./lock-unlock.mjs"; import { getValidPseudoStates, getValidViewportStates } from "./utils.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/global-styles-ui/src/screen-block.tsx const BACKGROUND_BLOCK_DEFAULT_VALUES = { backgroundSize: "cover", backgroundPosition: "50% 50%" }; function applyFallbackStyle(border) { if (!border) return border; const hasColorOrWidth = border.color || border.width; if (!border.style && hasColorOrWidth) return { ...border, style: "solid" }; if (border.style && !hasColorOrWidth) return; return border; } function applyAllFallbackStyles(border) { if (!border) return border; if (__experimentalHasSplitBorders(border)) return { top: applyFallbackStyle(border.top), right: applyFallbackStyle(border.right), bottom: applyFallbackStyle(border.bottom), left: applyFallbackStyle(border.left) }; return applyFallbackStyle(border); } /** * Prepares a style object from the border panel for storing in Global Styles. * * @param newStyle The style object as the border panel produced it. * @return The style object with its border ready to store. */ function normalizeBorderStyle(newStyle) { if (!newStyle?.border) return newStyle; const { radius, ...newBorder } = newStyle.border; const border = applyAllFallbackStyles(newBorder); const updatedBorder = !__experimentalHasSplitBorders(border) ? { top: border, right: border, bottom: border, left: border } : { color: null, style: null, width: null, ...border }; return { ...newStyle, border: { ...updatedBorder, radius } }; } const { useHasDimensionsPanel, useHasTypographyPanel, useHasBorderPanel, useSettingsForBlockElement, useHasColorPanel, useHasFiltersPanel, useHasImageSettingsPanel, useHasBackgroundPanel, BackgroundPanel: StylesBackgroundPanel, BorderPanel: StylesBorderPanel, ColorPanel: StylesColorPanel, TypographyPanel: StylesTypographyPanel, DimensionsPanel: StylesDimensionsPanel, FiltersPanel: StylesFiltersPanel, ImageSettingsPanel, AdvancedPanel: StylesAdvancedPanel } = unlock(privateApis); function ScreenBlock({ name, variation, selectedViewport: controlledSelectedViewport, showResponsiveStateControls = true, showBlockStateControls = true }) { const { user: userConfig, merged: mergedConfig, onChange: onChangeGlobalStyles } = useContext(GlobalStylesContext); let prefixParts = []; if (variation) prefixParts = ["variations", variation].concat(prefixParts); const prefix = prefixParts.join("."); const [localSelectedViewport, setSelectedViewport] = useState("default"); const [selectedPseudoState, setSelectedPseudoState] = useState("default"); const selectedViewport = controlledSelectedViewport ?? localSelectedViewport; const viewportSettings = mergedConfig.settings?.viewport; const validViewportStates = useMemo(() => getValidViewportStates(viewportSettings), [viewportSettings]); const effectiveSelectedViewport = selectedViewport === "default" || validViewportStates.some((state) => state.value === selectedViewport) ? selectedViewport : "default"; const validPseudoStates = useMemo(() => getValidPseudoStates(name), [name]); const stateParam = [effectiveSelectedViewport, selectedPseudoState].filter((value) => value !== "default").join("."); const hasSelectedState = stateParam.length > 0; const [style, setStyle$1] = useStyle(prefix, name, "user", false, hasSelectedState ? stateParam : void 0); const [inheritedStyle] = useStyle(prefix, name, "merged", false, hasSelectedState ? stateParam : void 0); const [baseStyle] = useStyle(prefix, name, "merged", false); const baseValue = hasSelectedState ? baseStyle : void 0; const inheritedStyleWithResolvedBackground = useStyleWithResolvedBackground(inheritedStyle); const [userSettings] = useSetting("", name, "user"); const [rawSettings, setSettings] = useSetting("", name); const settingsForBlockElement = useSettingsForBlockElement(rawSettings, name); const blockType = getBlockType(name); let disableBlockGap = false; if (settingsForBlockElement?.spacing?.blockGap && blockType?.supports?.spacing?.blockGap && (blockType?.supports?.spacing?.__experimentalSkipSerialization === true || blockType?.supports?.spacing?.__experimentalSkipSerialization?.some?.((spacingType) => spacingType === "blockGap"))) disableBlockGap = true; let disableAspectRatio = false; if (settingsForBlockElement?.dimensions?.aspectRatio && name === "core/group") disableAspectRatio = true; const settings = useMemo(() => { const updatedSettings = structuredClone(settingsForBlockElement); if (disableBlockGap) updatedSettings.spacing.blockGap = false; if (disableAspectRatio) updatedSettings.dimensions.aspectRatio = false; return updatedSettings; }, [ settingsForBlockElement, disableBlockGap, disableAspectRatio ]); const blockVariations = useBlockVariations(name); const hasBackgroundPanel = useHasBackgroundPanel(settings); const hasTypographyPanel = useHasTypographyPanel(settings); const hasColorPanel = useHasColorPanel(settings); const hasBorderPanel = useHasBorderPanel(settings); const hasDimensionsPanel = useHasDimensionsPanel(settings); const shouldShowFiltersPanel = useHasFiltersPanel(settings) && effectiveSelectedViewport === "default"; const hasImageSettingsPanel = useHasImageSettingsPanel(name, userSettings, settings); const hasVariationsPanel = !!blockVariations?.length && !variation && !hasSelectedState; const canEditCSS = useCanEditCSS(); const currentBlockStyle = variation ? blockVariations.find((s) => s.name === variation) : null; const inheritedStyleWithLayout = useMemo(() => { return { ...inheritedStyle, layout: settings.layout }; }, [inheritedStyle, settings.layout]); const styleWithLayout = useMemo(() => { return { ...style, layout: userSettings.layout }; }, [style, userSettings.layout]); const onChangeDimensions = (newStyle) => { const updatedStyle = { ...newStyle }; delete updatedStyle.layout; setStyle$1(updatedStyle); if (newStyle.layout !== userSettings.layout) setSettings({ ...userSettings, layout: newStyle.layout }); }; const onChangeLightbox = (newSetting) => { if (newSetting === void 0) setSettings({ ...rawSettings, lightbox: void 0 }); else setSettings({ ...rawSettings, lightbox: { ...rawSettings.lightbox, ...newSetting } }); }; const onChangeTypography = (newStyle) => { const { settings: newSettings, ...styleWithoutSettings } = newStyle; if (newSettings?.typography) { const stylePathForState = [prefix, stateParam].filter(Boolean).join("."); let updatedConfig = setStyle(userConfig, stylePathForState, styleWithoutSettings, name); updatedConfig = setSetting(updatedConfig, "typography", { ...userSettings.typography, ...newSettings.typography }, name); onChangeGlobalStyles(updatedConfig); } else setStyle$1(styleWithoutSettings); }; const onChangeBorders = (newStyle) => setStyle$1(normalizeBorderStyle(newStyle)); return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(ScreenHeader, { title: variation ? currentBlockStyle?.label : blockType?.title, viewportStates: validViewportStates, pseudoStates: showBlockStateControls ? validPseudoStates : [], selectedViewport: effectiveSelectedViewport, selectedPseudoState, onChangeViewport: showResponsiveStateControls ? setSelectedViewport : void 0, onChangePseudoState: setSelectedPseudoState, showResponsiveStateControls }), /* @__PURE__ */ jsx(BlockPreviewPanel, { name, variation, selectedViewport: effectiveSelectedViewport, selectedState: hasSelectedState ? stateParam : "default", stateStyles: hasSelectedState ? inheritedStyle : void 0, viewportSettings }), hasVariationsPanel && /* @__PURE__ */ jsx("div", { className: "global-styles-ui-screen-variations", children: /* @__PURE__ */ jsxs(__experimentalVStack, { spacing: 3, children: [/* @__PURE__ */ jsx(Subtitle, { children: __("Style Variations") }), /* @__PURE__ */ jsx(VariationsPanel, { name })] }) }), hasTypographyPanel && /* @__PURE__ */ jsx(StylesTypographyPanel, { inheritedValue: inheritedStyle, value: style, baseValue, onChange: onChangeTypography, settings, isGlobalStyles: !hasSelectedState }), hasBackgroundPanel && /* @__PURE__ */ jsx(StylesBackgroundPanel, { inheritedValue: inheritedStyleWithResolvedBackground, value: style, baseValue, onChange: setStyle$1, settings, defaultValues: BACKGROUND_BLOCK_DEFAULT_VALUES }), shouldShowFiltersPanel && /* @__PURE__ */ jsx(StylesFiltersPanel, { inheritedValue: inheritedStyleWithLayout, value: styleWithLayout, onChange: setStyle$1, settings, includeLayoutControls: true }), hasDimensionsPanel && /* @__PURE__ */ jsx(StylesDimensionsPanel, { inheritedValue: inheritedStyleWithLayout, value: styleWithLayout, onChange: onChangeDimensions, settings, includeLayoutControls: true }), hasBorderPanel && /* @__PURE__ */ jsx(StylesBorderPanel, { inheritedValue: inheritedStyle, value: style, onChange: onChangeBorders, settings }), hasColorPanel && /* @__PURE__ */ jsx(StylesColorPanel, { inheritedValue: inheritedStyle, value: style, onChange: setStyle$1, settings }), hasImageSettingsPanel && !hasSelectedState && /* @__PURE__ */ jsx(ImageSettingsPanel, { onChange: onChangeLightbox, value: userSettings, inheritedValue: settings }), canEditCSS && /* @__PURE__ */ jsx(PanelBody, { title: __("Advanced"), initialOpen: false, children: /* @__PURE__ */ jsx(StylesAdvancedPanel, { value: style, onChange: setStyle$1, inheritedValue: inheritedStyle, help: sprintf(__("Add your own CSS to customize the appearance of the %s block. You do not need to include a CSS selector, just add the property and value."), blockType?.title) }) }) ] }); } export { BACKGROUND_BLOCK_DEFAULT_VALUES, ScreenBlock as default, normalizeBorderStyle }; //# sourceMappingURL=screen-block.mjs.map