UNPKG

@wordpress/global-styles-ui

Version:
188 lines (187 loc) • 6.12 kB
// packages/global-styles-ui/src/font-sizes/font-size.tsx import { __, sprintf } from "@wordpress/i18n"; import { __experimentalSpacer as Spacer, __experimentalView as View, useNavigator, FlexItem, ToggleControl } from "@wordpress/components"; import { useState, useEffect } from "@wordpress/element"; import { Stack } from "@wordpress/ui"; import FontSizePreview from "./font-size-preview.mjs"; import { SizeControl } from "../size-control/index.mjs"; import { usePresets } from "../presets/use-presets.mjs"; import { useSetting } from "../hooks.mjs"; import PresetEditHeader from "../presets/preset-edit-header.mjs"; import ConfirmDeleteDialog from "../presets/dialogs/confirm-delete-dialog.mjs"; import RenameDialog from "../presets/dialogs/rename-dialog.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; function FontSize() { const { params, goBack } = useNavigator(); const origin = params.origin; const slug = params.slug; const { presets, setPresets } = usePresets( "typography.fontSizes", origin ); const [globalFluid] = useSetting("typography.fluid"); const fontSize = presets.find((s) => s.slug === slug); const [isDeleteOpen, setIsDeleteOpen] = useState(false); const [isRenameOpen, setIsRenameOpen] = useState(false); useEffect(() => { if (!!slug && !fontSize) { goBack(); } }, [slug, fontSize, goBack]); if (!origin || !slug || !fontSize) { return null; } const isFluid = fontSize.fluid !== void 0 ? !!fontSize.fluid : !!globalFluid; const isCustomFluid = typeof fontSize.fluid === "object"; const set = (key, value) => setPresets( presets.map( (p) => p.slug === slug ? { ...fontSize, [key]: value } : p ) ); const handleCustomFluidValues = (value) => { if (value) { set("fluid", { min: fontSize.size, max: fontSize.size }); } else { set("fluid", true); } }; const handleMinChange = (value) => { const fluid = typeof fontSize.fluid === "object" ? fontSize.fluid : {}; set("fluid", { ...fluid, min: value }); }; const handleMaxChange = (value) => { const fluid = typeof fontSize.fluid === "object" ? fontSize.fluid : {}; set("fluid", { ...fluid, max: value }); }; const menuItems = origin === "custom" ? [ { label: __("Rename"), onClick: () => setIsRenameOpen(true) }, { label: __("Delete"), onClick: () => setIsDeleteOpen(true) } ] : []; return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsxs(Stack, { direction: "column", gap: "md", children: [ /* @__PURE__ */ jsx( PresetEditHeader, { title: fontSize.name, description: sprintf( /* translators: %s: font size preset name. */ __("Manage the font size %s."), fontSize.name ), menuLabel: __("Font size options"), menuItems } ), /* @__PURE__ */ jsx(View, { children: /* @__PURE__ */ jsx( Spacer, { paddingX: 4, marginBottom: 0, paddingBottom: 6, children: /* @__PURE__ */ jsxs(Stack, { direction: "column", gap: "md", children: [ /* @__PURE__ */ jsx(FlexItem, { children: /* @__PURE__ */ jsx(FontSizePreview, { fontSize }) }), /* @__PURE__ */ jsx( SizeControl, { label: __("Size"), value: !isCustomFluid && fontSize.size ? String(fontSize.size) : "", onChange: (value) => set("size", value), disabled: isCustomFluid } ), /* @__PURE__ */ jsx( ToggleControl, { label: __("Fluid typography"), help: __( "Scale the font size dynamically to fit the screen or viewport." ), checked: isFluid, onChange: (value) => set("fluid", value) } ), isFluid && /* @__PURE__ */ jsx( ToggleControl, { label: __("Custom fluid values"), help: __( "Set custom min and max values for the fluid font size." ), checked: isCustomFluid, onChange: handleCustomFluidValues } ), isCustomFluid && /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx( SizeControl, { label: __("Minimum"), value: typeof fontSize.fluid === "object" ? fontSize.fluid?.min : void 0, onChange: handleMinChange } ), /* @__PURE__ */ jsx( SizeControl, { label: __("Maximum"), value: typeof fontSize.fluid === "object" ? fontSize.fluid?.max : void 0, onChange: handleMaxChange } ) ] }) ] }) } ) }) ] }), isDeleteOpen && /* @__PURE__ */ jsx( ConfirmDeleteDialog, { message: sprintf( /* translators: %s: Name of the font size preset. */ __( 'Are you sure you want to delete "%s" font size preset?' ), fontSize.name ), isOpen: isDeleteOpen, toggleOpen: () => setIsDeleteOpen(false), onConfirm: () => { setPresets( presets.filter((p) => p.slug !== slug) ); goBack(); } } ), isRenameOpen && /* @__PURE__ */ jsx( RenameDialog, { initialName: fontSize.name, placeholder: __("Font size preset name"), toggleOpen: () => setIsRenameOpen(false), onRename: (name) => setPresets( presets.map( (p) => p.slug === slug ? { ...p, name } : p ) ) } ) ] }); } var font_size_default = FontSize; export { font_size_default as default }; //# sourceMappingURL=font-size.mjs.map