UNPKG

@wordpress/global-styles-ui

Version:
293 lines (292 loc) • 11.1 kB
import { store } from "@wordpress/blocks"; import { __, _n, sprintf } from "@wordpress/i18n"; import { Button, SearchControl, __experimentalHStack, __experimentalItemGroup, __experimentalText } from "@wordpress/components"; import { Menu, Stack } from "@wordpress/ui"; import { useSelect } from "@wordpress/data"; import { memo, useContext, useDeferredValue, useEffect, useMemo, useRef, useState } from "@wordpress/element"; import { BlockIcon, privateApis } from "@wordpress/block-editor"; import { useDebounce } from "@wordpress/compose"; import { speak } from "@wordpress/a11y"; import { button, caption, formInput, formSelect, funnel, heading, headingLevel1, headingLevel2, headingLevel3, headingLevel4, headingLevel5, headingLevel6, link } from "@wordpress/icons"; import { useBlockVariations } from "./variations/variations-panel.mjs"; import { ScreenHeader } from "./screen-header.mjs"; import { NavigationButtonAsItem } from "./navigation-button.mjs"; import { ELEMENTS } from "./screen-element.mjs"; import { Subtitle } from "./subtitle.mjs"; import { useSetting } from "./hooks.mjs"; import { unlock } from "./lock-unlock.mjs"; import { GlobalStylesContext } from "./context.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/global-styles-ui/src/screen-block-list.tsx const { useHasDimensionsPanel, useHasTypographyPanel, useHasBorderPanel, useSettingsForBlockElement, useHasColorPanel, useHasBackgroundPanel, searchItems } = unlock(privateApis); const ELEMENT_GROUPS = [ { title: __("Elements"), elements: [ "link", "caption", "cite" ] }, { title: __("Headings"), elements: [ "heading", "h1", "h2", "h3", "h4", "h5", "h6" ] }, { title: __("Form controls"), elements: [ "textInput", "select", "button" ] } ]; const ELEMENT_ICONS = { link, caption, cite: caption, heading, h1: headingLevel1, h2: headingLevel2, h3: headingLevel3, h4: headingLevel4, h5: headingLevel5, h6: headingLevel6, textInput: formInput, select: formSelect, button }; /** * Whether a value, or anything nested inside it, holds a real user value. * * A user entry can survive as an empty husk: clearing a value writes * `undefined` in place rather than removing the key, so * `{ color: { text: undefined } }` means "not customized". An empty string * and zero are real values — an intentionally blank CSS string is still a * user entry. * * @param value The value to inspect. * @return Whether the value holds at least one leaf value. */ function hasAnyValue(value) { if (value === void 0 || value === null) return false; if (Array.isArray(value)) return value.length > 0; if (typeof value === "object") return Object.values(value).some(hasAnyValue); return true; } /** * Whether the user has customized a block. * * Per-block user data lives under two roots: `styles.blocks` for style values * and `settings.blocks` for things like a block-specific color palette. Either * one counts as a customization, and both are resettable by the user. * * Reads the user layer only. The merged config would match nearly every block, * because themes style most of them. * * @param user The user's global styles config. * @param blockName The block to check, e.g. `core/quote`. * @return Whether the user has any styles or settings for that block. */ function hasUserStylesForBlock(user, blockName) { return hasAnyValue(user?.styles?.blocks?.[blockName]) || hasAnyValue(user?.settings?.blocks?.[blockName]); } function useSortedBlockTypes() { const blockItems = useSelect((select) => select(store).getBlockTypes(), []); const groupByType = (blocks, block) => { const { core, noncore } = blocks; (block.name.startsWith("core/") ? core : noncore).push(block); return blocks; }; const { core: coreItems, noncore: nonCoreItems } = blockItems.reduce(groupByType, { core: [], noncore: [] }); return [...coreItems, ...nonCoreItems]; } function useBlockHasGlobalStyles(blockName) { const [rawSettings] = useSetting("", blockName); const settings = useSettingsForBlockElement(rawSettings, blockName); const hasTypographyPanel = useHasTypographyPanel(settings); const hasColorPanel = useHasColorPanel(settings); const hasBackgroundPanel = useHasBackgroundPanel(settings); const hasBorderPanel = useHasBorderPanel(settings); const hasDimensionsPanel = useHasDimensionsPanel(settings); const hasLayoutPanel = hasBorderPanel || hasDimensionsPanel; const hasVariationsPanel = !!useBlockVariations(blockName)?.length; return hasTypographyPanel || hasColorPanel || hasBackgroundPanel || hasLayoutPanel || hasVariationsPanel; } function ListMenuItem({ path, icon, title }) { return /* @__PURE__ */ jsx(NavigationButtonAsItem, { path, children: /* @__PURE__ */ jsxs(Stack, { direction: "row", align: "center", gap: "sm", children: [/* @__PURE__ */ jsx(BlockIcon, { className: "global-styles-ui-block-types-item__icon", icon }), /* @__PURE__ */ jsx("span", { children: title })] }) }); } function BlockMenuItem({ block }) { if (!useBlockHasGlobalStyles(block.name)) return null; return /* @__PURE__ */ jsx(ListMenuItem, { path: "/blocks/" + encodeURIComponent(block.name), icon: block.icon, title: block.title }); } function ElementMenuItem({ element }) { return /* @__PURE__ */ jsx(ListMenuItem, { path: "/blocks/elements/" + element, icon: ELEMENT_ICONS[element], title: ELEMENTS[element].title }); } function ListGroup({ title, children }) { return /* @__PURE__ */ jsxs("div", { className: "global-styles-ui-block-types-group", children: [/* @__PURE__ */ jsx("div", { className: "global-styles-ui-block-types-group__title", children: /* @__PURE__ */ jsx(Subtitle, { level: 3, children: title }) }), children] }); } function EmptyList({ filterValue, styleFilter }) { const label = "customized" === styleFilter && !filterValue ? __("You haven't customized any blocks or elements yet.") : __("No results found."); return /* @__PURE__ */ jsx(__experimentalText, { align: "center", as: "p", className: "global-styles-ui-block-types-item-list__no-results", children: label }); } function getFilteredElementGroups(filterValue, styleFilter, customizedElementNames) { const search = filterValue.trim().toLowerCase(); return ELEMENT_GROUPS.map((group) => ({ ...group, elements: group.elements.filter((element) => (!search || [ ELEMENTS[element].title, element, group.title ].join(" ").toLowerCase().includes(search)) && (styleFilter !== "customized" || customizedElementNames.has(element))) })).filter((group) => group.elements.length > 0); } function BlockAndElementList({ filterValue, styleFilter }) { const sortedBlockTypes = useSortedBlockTypes(); const debouncedSpeak = useDebounce(speak, 500); const { user } = useContext(GlobalStylesContext); const customizedBlockNames = useMemo(() => { const names = /* @__PURE__ */ new Set(); [...Object.keys(user?.styles?.blocks ?? {}), ...Object.keys(user?.settings?.blocks ?? {})].forEach((blockName) => { if (hasUserStylesForBlock(user, blockName)) names.add(blockName); }); return names; }, [user]); const filteredElementGroups = getFilteredElementGroups(filterValue, styleFilter, useMemo(() => { const names = /* @__PURE__ */ new Set(); Object.entries(user?.styles?.elements ?? {}).forEach(([name, styles]) => { if (hasAnyValue(styles)) names.add(name); }); return names; }, [user])); const searchedBlockTypes = searchItems(sortedBlockTypes, filterValue); const filteredBlockTypes = styleFilter === "customized" ? searchedBlockTypes.filter((blockType) => customizedBlockNames.has(blockType.name)) : searchedBlockTypes; const blockTypesListRef = useRef(null); const elementCount = filteredElementGroups.reduce((total, group) => total + group.elements.length, 0); const hasBlockResults = filteredBlockTypes.length > 0; useEffect(() => { if (!filterValue && styleFilter === "all") return; const blockCount = hasBlockResults ? blockTypesListRef.current?.childElementCount || 0 : 0; const count = elementCount + blockCount; const resultsFoundMessage = sprintf(_n("%d result found.", "%d results found.", count), count); debouncedSpeak(resultsFoundMessage, "polite"); }, [ filterValue, styleFilter, hasBlockResults, elementCount, debouncedSpeak ]); if (!elementCount && !hasBlockResults) return /* @__PURE__ */ jsx(EmptyList, { filterValue, styleFilter }); return /* @__PURE__ */ jsxs(Fragment, { children: [filteredElementGroups.map((group) => /* @__PURE__ */ jsx(ListGroup, { title: group.title, children: /* @__PURE__ */ jsx(__experimentalItemGroup, { className: "global-styles-ui-block-types-item-list", size: "small", children: group.elements.map((element) => /* @__PURE__ */ jsx(ElementMenuItem, { element }, element)) }) }, group.title)), hasBlockResults && /* @__PURE__ */ jsx(ListGroup, { title: __("Blocks"), children: /* @__PURE__ */ jsx(__experimentalItemGroup, { ref: blockTypesListRef, className: "global-styles-ui-block-types-item-list", size: "small", children: filteredBlockTypes.map((block) => /* @__PURE__ */ jsx(BlockMenuItem, { block }, "menu-itemblock-" + block.name)) }) })] }); } const MemoizedBlockAndElementList = memo(BlockAndElementList); function ScreenBlockList() { const [filterValue, setFilterValue] = useState(""); const [styleFilter, setStyleFilter] = useState("all"); const deferredFilterValue = useDeferredValue(filterValue); return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(ScreenHeader, { title: __("Blocks & Elements"), description: __("Customize how a block or element looks everywhere it's used.") }), /* @__PURE__ */ jsxs(__experimentalHStack, { className: "global-styles-ui-block-types-filter", alignment: "center", spacing: 2, children: [/* @__PURE__ */ jsx(SearchControl, { className: "global-styles-ui-block-types-search", onChange: setFilterValue, value: filterValue, label: __("Search"), placeholder: __("Search"), size: "compact" }), /* @__PURE__ */ jsxs(Menu.Root, { children: [/* @__PURE__ */ jsx(Menu.Trigger, { render: /* @__PURE__ */ jsx(Button, { size: "compact", icon: funnel, label: __("Filter blocks"), isPressed: styleFilter !== "all" }) }), /* @__PURE__ */ jsx(Menu.Popup, { children: /* @__PURE__ */ jsxs(Menu.RadioGroup, { value: styleFilter, onValueChange: (value) => setStyleFilter(value), children: [/* @__PURE__ */ jsx(Menu.RadioItem, { value: "all", closeOnClick: true, children: /* @__PURE__ */ jsx(Menu.ItemLabel, { children: __("All blocks") }) }), /* @__PURE__ */ jsx(Menu.RadioItem, { value: "customized", closeOnClick: true, children: /* @__PURE__ */ jsx(Menu.ItemLabel, { children: __("Customized") }) })] }) })] })] }), /* @__PURE__ */ jsx(MemoizedBlockAndElementList, { filterValue: deferredFilterValue, styleFilter }) ] }); } export { BlockMenuItem, ScreenBlockList as default, hasUserStylesForBlock, useBlockHasGlobalStyles }; //# sourceMappingURL=screen-block-list.mjs.map