@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
140 lines (139 loc) • 5.01 kB
JavaScript
import { __, sprintf } from "@wordpress/i18n";
import { FlexItem, ToggleControl, __experimentalSpacer, __experimentalView, useNavigator } from "@wordpress/components";
import { useEffect, useState } 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";
// packages/global-styles-ui/src/font-sizes/font-size.tsx
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 update = (changes) => setPresets(presets.map((p) => p.slug === slug ? {
...fontSize,
...changes
} : p));
const set = (key, value) => update({ [key]: value });
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 : {};
update({
fluid: {
...fluid,
max: value
},
...value ? { size: 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(__("Manage the font size %s."), fontSize.name),
menuLabel: __("Font size options"),
menuItems
}), /* @__PURE__ */ jsx(__experimentalView, { children: /* @__PURE__ */ jsx(__experimentalSpacer, {
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(__("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))
})
] });
}
export { FontSize as default };
//# sourceMappingURL=font-size.mjs.map