UNPKG

@wordpress/global-styles-ui

Version:
175 lines (174 loc) • 6.69 kB
import { __, sprintf } from "@wordpress/i18n"; import { useMemo, useState } from "@wordpress/element"; import { PanelBody, useNavigator } from "@wordpress/components"; import { useSelect } from "@wordpress/data"; import { store } from "@wordpress/blocks"; import { privateApis } from "@wordpress/block-editor"; import { ScreenHeader } from "./screen-header.mjs"; import ElementPreview from "./element-preview.mjs"; import ElementBlockOverrides from "./element-block-overrides.mjs"; import { useCanEditCSS, useSetting, useStyle } from "./hooks.mjs"; import { unlock } from "./lock-unlock.mjs"; import { getValidPseudoStates } from "./utils.mjs"; import { BACKGROUND_BLOCK_DEFAULT_VALUES, normalizeBorderStyle } from "./screen-block.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/global-styles-ui/src/screen-element.tsx const { useSettingsForBlockElement, useHasBackgroundPanel, useHasBorderPanel, useHasDimensionsPanel, useHasTypographyPanel, BackgroundPanel: StylesBackgroundPanel, BorderPanel: StylesBorderPanel, DimensionsPanel: StylesDimensionsPanel, TypographyPanel: StylesTypographyPanel, AdvancedPanel: StylesAdvancedPanel, getBlockTypesForElement } = unlock(privateApis); const ELEMENTS = { text: { description: __("Manage the fonts used on the site."), title: __("Text") }, link: { description: __("Manage the appearance of links."), title: __("Link") }, caption: { description: __("Manage the appearance of captions."), title: __("Caption") }, cite: { description: __("Manage the appearance of citations."), title: __("Citation") }, heading: { description: __("Manage the appearance of all headings."), title: __("All headings") }, h1: { description: __("Manage the appearance of level 1 headings."), title: __("Heading 1") }, h2: { description: __("Manage the appearance of level 2 headings."), title: __("Heading 2") }, h3: { description: __("Manage the appearance of level 3 headings."), title: __("Heading 3") }, h4: { description: __("Manage the appearance of level 4 headings."), title: __("Heading 4") }, h5: { description: __("Manage the appearance of level 5 headings."), title: __("Heading 5") }, h6: { description: __("Manage the appearance of level 6 headings."), title: __("Heading 6") }, textInput: { description: __("Manage the appearance of inputs."), title: __("Form input") }, select: { description: __("Manage the appearance of selects."), title: __("Form select") }, button: { description: __("Manage the appearance of buttons."), title: __("Form button") } }; /** * Renders every style panel for an element. The element's settings are * resolved once here and shared by all the panels, as are the styles that the * screen resolves for them. * * @param props * @param props.element The element being styled. * @param props.label Its display name, used in the custom CSS help text. * @param props.style The element's user styles in the selected state. * @param props.inheritedStyle The element's merged styles in the selected state. * @param props.setStyle Saves the element's user styles in the selected state. */ function ElementStylePanels({ element, label, style, inheritedStyle, setStyle }) { const isRoot = element === "text"; const [rawSettings] = useSetting(""); const settings = useSettingsForBlockElement(rawSettings, void 0, element); const hasTypographyPanel = useHasTypographyPanel(settings); const hasBackgroundPanel = useHasBackgroundPanel(settings) && !isRoot; const hasDimensionsPanel = useHasDimensionsPanel(settings) && !isRoot; const hasBorderPanel = useHasBorderPanel(settings) && !isRoot; const canEditCSS = useCanEditCSS(); const onChangeBorders = (newStyle) => setStyle(normalizeBorderStyle(newStyle)); return /* @__PURE__ */ jsxs(Fragment, { children: [ hasTypographyPanel && /* @__PURE__ */ jsx(StylesTypographyPanel, { inheritedValue: inheritedStyle, value: style, onChange: setStyle, settings }), hasBackgroundPanel && /* @__PURE__ */ jsx(StylesBackgroundPanel, { inheritedValue: inheritedStyle, value: style, onChange: setStyle, settings, defaultValues: BACKGROUND_BLOCK_DEFAULT_VALUES }), hasDimensionsPanel && /* @__PURE__ */ jsx(StylesDimensionsPanel, { inheritedValue: inheritedStyle, value: style, onChange: setStyle, settings }), hasBorderPanel && /* @__PURE__ */ jsx(StylesBorderPanel, { inheritedValue: inheritedStyle, value: style, onChange: onChangeBorders, settings }), canEditCSS && !isRoot && /* @__PURE__ */ jsx(PanelBody, { title: __("Advanced"), initialOpen: false, children: /* @__PURE__ */ jsx(StylesAdvancedPanel, { value: style, onChange: setStyle, inheritedValue: inheritedStyle, help: sprintf(__("Add your own CSS to customize the appearance of %s. You do not need to include a CSS selector, just add the property and value."), label) }) }) ] }); } function ScreenElement({ element: elementProp, showStateControls = true }) { const { params } = useNavigator(); const element = elementProp ?? params.element; const [selectedPseudoState, setSelectedPseudoState] = useState("default"); const pseudoStates = useMemo(() => showStateControls ? getValidPseudoStates(element) : [], [element, showStateControls]); const allBlockTypes = useSelect((select) => select(store).getBlockTypes(), []); const elementBlockTypes = useMemo(() => getBlockTypesForElement(element).map((name) => allBlockTypes.find((blockType) => blockType.name === name)).filter((blockType) => !!blockType), [element, allBlockTypes]); const state = selectedPseudoState === "default" ? void 0 : selectedPseudoState; const prefix = element === "text" ? "" : `elements.${element}`; const [style] = useStyle(prefix, "", "user", false, state); const [inheritedStyle, setStyle] = useStyle(prefix, "", "merged", false, state); if (!Object.hasOwn(ELEMENTS, element)) return null; return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(ScreenHeader, { title: ELEMENTS[element].title, description: ELEMENTS[element].description, pseudoStates, selectedPseudoState, onChangePseudoState: setSelectedPseudoState }), /* @__PURE__ */ jsx(ElementPreview, { element, state, stateStyles: state ? inheritedStyle : void 0 }), /* @__PURE__ */ jsx(ElementStylePanels, { element, label: ELEMENTS[element].title, style, inheritedStyle, setStyle }), /* @__PURE__ */ jsx(ElementBlockOverrides, { element, blockTypes: elementBlockTypes }) ] }); } export { ELEMENTS, ScreenElement as default }; //# sourceMappingURL=screen-element.mjs.map