UNPKG

@wordpress/global-styles-ui

Version:
210 lines (209 loc) • 8.16 kB
import { Navigator, useNavigator } from "@wordpress/components"; import { getBlockTypes, store } from "@wordpress/blocks"; import { useSelect } from "@wordpress/data"; import { BlockEditorProvider } from "@wordpress/block-editor"; import { Fragment, useEffect, useMemo } from "@wordpress/element"; import { usePrevious } from "@wordpress/compose"; import { generateGlobalStyles, mergeGlobalStyles } from "@wordpress/global-styles-engine"; import { GlobalStylesProvider } from "./provider.mjs"; import ScreenRoot from "./screen-root.mjs"; import ScreenBlockList from "./screen-block-list.mjs"; import ScreenBlock from "./screen-block.mjs"; import ScreenTypography from "./screen-typography.mjs"; import ScreenElement from "./screen-element.mjs"; import ScreenColors from "./screen-colors.mjs"; import ScreenColorPalette from "./screen-color-palette.mjs"; import ScreenBackground from "./screen-background.mjs"; import { ScreenShadows, ScreenShadowsEdit } from "./screen-shadows.mjs"; import ScreenTextShadows from "./screen-text-shadows.mjs"; import ScreenTextShadowsEdit from "./screen-text-shadows-edit.mjs"; import ScreenLayout from "./screen-layout.mjs"; import ScreenStyleVariations from "./screen-style-variations.mjs"; import ScreenCSS from "./screen-css.mjs"; import ScreenRevisions from "./screen-revisions/index.mjs"; import FontSizes from "./font-sizes/font-sizes.mjs"; import FontSize from "./font-sizes/font-size.mjs"; import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime"; // packages/global-styles-ui/src/global-styles-ui.tsx function BlockStylesNavigationScreens({ parentMenu, blockStyles, blockName, selectedViewport, showResponsiveStateControls, showBlockStateControls }) { return /* @__PURE__ */ jsx(Fragment$1, { children: blockStyles.map((style, index) => /* @__PURE__ */ jsx(Navigator.Screen, { path: parentMenu + "/variations/" + style.name, children: /* @__PURE__ */ jsx(ScreenBlock, { name: blockName, variation: style.name, selectedViewport, showResponsiveStateControls, showBlockStateControls }) }, index)) }); } function ContextScreens({ name, parentMenu = "", selectedViewport, showResponsiveStateControls, showBlockStateControls }) { const blockStyleVariations = useSelect((select) => { if (!name) return []; const { getBlockStyles } = select(store); return getBlockStyles(name); }, [name]); if (!blockStyleVariations?.length) return null; return /* @__PURE__ */ jsx(BlockStylesNavigationScreens, { parentMenu, blockStyles: blockStyleVariations, blockName: name || "", selectedViewport, showResponsiveStateControls, showBlockStateControls }); } function GlobalStylesUI({ value, baseValue, onChange, path, onPathChange, fontLibraryEnabled = false, serverCSS, serverSettings, selectedViewport, showResponsiveStateControls = true, showBlockStateControls = true }) { const blocks = getBlockTypes(); const mergedValue = useMemo(() => { return mergeGlobalStyles(baseValue, value); }, [baseValue, value]); const [globalStylesCSS, globalSettings] = generateGlobalStyles(mergedValue, [], { styleOptions: { variationStyles: true } }); const styles = useMemo(() => [...serverCSS ?? [], ...globalStylesCSS ?? []], [serverCSS, globalStylesCSS]); const settings = useMemo(() => { return { ...serverSettings, __experimentalFeatures: globalSettings, styles }; }, [ globalSettings, serverSettings, styles ]); return /* @__PURE__ */ jsx(GlobalStylesProvider, { value, baseValue, onChange, fontLibraryEnabled, children: /* @__PURE__ */ jsx(BlockEditorProvider, { settings, children: /* @__PURE__ */ jsxs(Navigator, { className: "global-styles-ui-sidebar__navigator-provider", initialPath: path || "/", children: [ (path || onPathChange) && /* @__PURE__ */ jsx(PathSynchronizer, { path, onPathChange }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/", children: /* @__PURE__ */ jsx(ScreenRoot, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/colors", children: /* @__PURE__ */ jsx(ScreenColors, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/typography", children: /* @__PURE__ */ jsx(ScreenTypography, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/typography/text", children: /* @__PURE__ */ jsx(ScreenElement, { element: "text" }) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/typography/font-sizes", children: /* @__PURE__ */ jsx(FontSizes, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/typography/font-sizes/:origin/:slug", children: /* @__PURE__ */ jsx(FontSize, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/layout", children: /* @__PURE__ */ jsx(ScreenLayout, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/colors/palette", children: /* @__PURE__ */ jsx(ScreenColorPalette, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/variations", children: /* @__PURE__ */ jsx(ScreenStyleVariations, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/css", children: /* @__PURE__ */ jsx(ScreenCSS, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/revisions/:revisionId?", children: /* @__PURE__ */ jsx(ScreenRevisions, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/shadows", children: /* @__PURE__ */ jsx(ScreenShadows, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/shadows/edit/:category/:slug", children: /* @__PURE__ */ jsx(ScreenShadowsEdit, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/typography/text-shadows", children: /* @__PURE__ */ jsx(ScreenTextShadows, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/typography/text-shadows/edit/:category/:slug", children: /* @__PURE__ */ jsx(ScreenTextShadowsEdit, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/background", children: /* @__PURE__ */ jsx(ScreenBackground, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/blocks", children: /* @__PURE__ */ jsx(ScreenBlockList, {}) }), /* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/blocks/elements/:element", children: /* @__PURE__ */ jsx(ScreenElement, { showStateControls: showBlockStateControls }) }), blocks.map((block) => /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(GlobalStylesNavigationScreen, { path: "/blocks/" + encodeURIComponent(block.name), children: /* @__PURE__ */ jsx(ScreenBlock, { name: block.name, selectedViewport, showResponsiveStateControls, showBlockStateControls }) }), /* @__PURE__ */ jsx(ContextScreens, { name: block.name, parentMenu: "/blocks/" + encodeURIComponent(block.name), selectedViewport, showResponsiveStateControls, showBlockStateControls })] }, block.name)) ] }) }) }); } function GlobalStylesNavigationScreen({ path, children }) { return /* @__PURE__ */ jsx(Navigator.Screen, { className: "global-styles-ui-sidebar__navigator-screen", path, children }); } function PathSynchronizer({ path, onPathChange }) { const navigator = useNavigator(); const { path: childPath } = navigator.location; const previousParentPath = usePrevious(path); const previousChildPath = usePrevious(childPath); useEffect(() => { if (path && path !== childPath) { if (path !== previousParentPath) navigator.goTo(path); else if (childPath !== previousChildPath && onPathChange) onPathChange(childPath ?? "/"); } }, [ onPathChange, path, previousChildPath, previousParentPath, childPath, navigator ]); return null; } export { GlobalStylesUI }; //# sourceMappingURL=global-styles-ui.mjs.map