@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
301 lines (300 loc) • 13.3 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_blocks = require("@wordpress/blocks");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_components = require("@wordpress/components");
let _wordpress_ui = require("@wordpress/ui");
let _wordpress_data = require("@wordpress/data");
let _wordpress_element = require("@wordpress/element");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_a11y = require("@wordpress/a11y");
let _wordpress_icons = require("@wordpress/icons");
let __variations_variations_panel_tsx = require("./variations/variations-panel.cjs");
let __screen_header_tsx = require("./screen-header.cjs");
let __navigation_button_tsx = require("./navigation-button.cjs");
let __screen_element_tsx = require("./screen-element.cjs");
let __subtitle_tsx = require("./subtitle.cjs");
let __hooks_ts = require("./hooks.cjs");
let __lock_unlock_ts = require("./lock-unlock.cjs");
let __context_ts = require("./context.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/global-styles-ui/src/screen-block-list.tsx
const { useHasDimensionsPanel, useHasTypographyPanel, useHasBorderPanel, useSettingsForBlockElement, useHasColorPanel, useHasBackgroundPanel, searchItems } = (0, __lock_unlock_ts.unlock)(_wordpress_block_editor.privateApis);
const ELEMENT_GROUPS = [
{
title: (0, _wordpress_i18n.__)("Elements"),
elements: [
"link",
"caption",
"cite"
]
},
{
title: (0, _wordpress_i18n.__)("Headings"),
elements: [
"heading",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6"
]
},
{
title: (0, _wordpress_i18n.__)("Form controls"),
elements: [
"textInput",
"select",
"button"
]
}
];
const ELEMENT_ICONS = {
link: _wordpress_icons.link,
caption: _wordpress_icons.caption,
cite: _wordpress_icons.caption,
heading: _wordpress_icons.heading,
h1: _wordpress_icons.headingLevel1,
h2: _wordpress_icons.headingLevel2,
h3: _wordpress_icons.headingLevel3,
h4: _wordpress_icons.headingLevel4,
h5: _wordpress_icons.headingLevel5,
h6: _wordpress_icons.headingLevel6,
textInput: _wordpress_icons.formInput,
select: _wordpress_icons.formSelect,
button: _wordpress_icons.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 = (0, _wordpress_data.useSelect)((select) => select(_wordpress_blocks.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] = (0, __hooks_ts.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 = !!(0, __variations_variations_panel_tsx.useBlockVariations)(blockName)?.length;
return hasTypographyPanel || hasColorPanel || hasBackgroundPanel || hasLayoutPanel || hasVariationsPanel;
}
function ListMenuItem({ path, icon, title }) {
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__navigation_button_tsx.NavigationButtonAsItem, {
path,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_ui.Stack, {
direction: "row",
align: "center",
gap: "sm",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockIcon, {
className: "global-styles-ui-block-types-item__icon",
icon
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: title })]
})
});
}
function BlockMenuItem({ block }) {
if (!useBlockHasGlobalStyles(block.name)) return null;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ListMenuItem, {
path: "/blocks/" + encodeURIComponent(block.name),
icon: block.icon,
title: block.title
});
}
function ElementMenuItem({ element }) {
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ListMenuItem, {
path: "/blocks/elements/" + element,
icon: ELEMENT_ICONS[element],
title: __screen_element_tsx.ELEMENTS[element].title
});
}
function ListGroup({ title, children }) {
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
className: "global-styles-ui-block-types-group",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "global-styles-ui-block-types-group__title",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__subtitle_tsx.Subtitle, {
level: 3,
children: title
})
}), children]
});
}
function EmptyList({ filterValue, styleFilter }) {
const label = "customized" === styleFilter && !filterValue ? (0, _wordpress_i18n.__)("You haven't customized any blocks or elements yet.") : (0, _wordpress_i18n.__)("No results found.");
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__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 || [
__screen_element_tsx.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 = (0, _wordpress_compose.useDebounce)(_wordpress_a11y.speak, 500);
const { user } = (0, _wordpress_element.useContext)(__context_ts.GlobalStylesContext);
const customizedBlockNames = (0, _wordpress_element.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, (0, _wordpress_element.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 = (0, _wordpress_element.useRef)(null);
const elementCount = filteredElementGroups.reduce((total, group) => total + group.elements.length, 0);
const hasBlockResults = filteredBlockTypes.length > 0;
(0, _wordpress_element.useEffect)(() => {
if (!filterValue && styleFilter === "all") return;
const blockCount = hasBlockResults ? blockTypesListRef.current?.childElementCount || 0 : 0;
const count = elementCount + blockCount;
const resultsFoundMessage = (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n._n)("%d result found.", "%d results found.", count), count);
debouncedSpeak(resultsFoundMessage, "polite");
}, [
filterValue,
styleFilter,
hasBlockResults,
elementCount,
debouncedSpeak
]);
if (!elementCount && !hasBlockResults) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EmptyList, {
filterValue,
styleFilter
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [filteredElementGroups.map((group) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ListGroup, {
title: group.title,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalItemGroup, {
className: "global-styles-ui-block-types-item-list",
size: "small",
children: group.elements.map((element) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ElementMenuItem, { element }, element))
})
}, group.title)), hasBlockResults && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ListGroup, {
title: (0, _wordpress_i18n.__)("Blocks"),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalItemGroup, {
ref: blockTypesListRef,
className: "global-styles-ui-block-types-item-list",
size: "small",
children: filteredBlockTypes.map((block) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(BlockMenuItem, { block }, "menu-itemblock-" + block.name))
})
})] });
}
const MemoizedBlockAndElementList = (0, _wordpress_element.memo)(BlockAndElementList);
function ScreenBlockList() {
const [filterValue, setFilterValue] = (0, _wordpress_element.useState)("");
const [styleFilter, setStyleFilter] = (0, _wordpress_element.useState)("all");
const deferredFilterValue = (0, _wordpress_element.useDeferredValue)(filterValue);
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(__screen_header_tsx.ScreenHeader, {
title: (0, _wordpress_i18n.__)("Blocks & Elements"),
description: (0, _wordpress_i18n.__)("Customize how a block or element looks everywhere it's used.")
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalHStack, {
className: "global-styles-ui-block-types-filter",
alignment: "center",
spacing: 2,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.SearchControl, {
className: "global-styles-ui-block-types-search",
onChange: setFilterValue,
value: filterValue,
label: (0, _wordpress_i18n.__)("Search"),
placeholder: (0, _wordpress_i18n.__)("Search"),
size: "compact"
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_ui.Menu.Root, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Menu.Trigger, { render: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, {
size: "compact",
icon: _wordpress_icons.funnel,
label: (0, _wordpress_i18n.__)("Filter blocks"),
isPressed: styleFilter !== "all"
}) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Menu.Popup, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_ui.Menu.RadioGroup, {
value: styleFilter,
onValueChange: (value) => setStyleFilter(value),
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Menu.RadioItem, {
value: "all",
closeOnClick: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Menu.ItemLabel, { children: (0, _wordpress_i18n.__)("All blocks") })
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Menu.RadioItem, {
value: "customized",
closeOnClick: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Menu.ItemLabel, { children: (0, _wordpress_i18n.__)("Customized") })
})]
}) })] })]
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(MemoizedBlockAndElementList, {
filterValue: deferredFilterValue,
styleFilter
})
] });
}
exports.BlockMenuItem = BlockMenuItem;
exports.default = ScreenBlockList;
exports.hasUserStylesForBlock = hasUserStylesForBlock;
exports.useBlockHasGlobalStyles = useBlockHasGlobalStyles;
//# sourceMappingURL=screen-block-list.cjs.map