@wordpress/block-editor
Version:
132 lines (131 loc) • 4.21 kB
JavaScript
// packages/block-editor/src/components/font-appearance-control/index.js
import { CustomSelectControl } from "@wordpress/components";
import deprecated from "@wordpress/deprecated";
import { useMemo } from "@wordpress/element";
import { __, sprintf } from "@wordpress/i18n";
import { getFontStylesAndWeights } from "../../utils/get-font-styles-and-weights";
import { jsx } from "react/jsx-runtime";
var getFontAppearanceLabel = (hasFontStyles, hasFontWeights) => {
if (!hasFontStyles) {
return __("Font weight");
}
if (!hasFontWeights) {
return __("Font style");
}
return __("Appearance");
};
function FontAppearanceControl(props) {
const {
/** Start opting into the larger default height that will become the default size in a future version. */
__next40pxDefaultSize = false,
onChange,
hasFontStyles = true,
hasFontWeights = true,
fontFamilyFaces,
value: { fontStyle, fontWeight },
...otherProps
} = props;
const hasStylesOrWeights = hasFontStyles || hasFontWeights;
const label = getFontAppearanceLabel(hasFontStyles, hasFontWeights);
const defaultOption = {
key: "default",
name: __("Default"),
style: { fontStyle: void 0, fontWeight: void 0 }
};
const { fontStyles, fontWeights, combinedStyleAndWeightOptions } = getFontStylesAndWeights(fontFamilyFaces);
const combineOptions = () => {
const combinedOptions = [defaultOption];
if (combinedStyleAndWeightOptions) {
combinedOptions.push(...combinedStyleAndWeightOptions);
}
return combinedOptions;
};
const styleOptions = () => {
const combinedOptions = [defaultOption];
fontStyles.forEach(({ name, value }) => {
combinedOptions.push({
key: value,
name,
style: { fontStyle: value, fontWeight: void 0 }
});
});
return combinedOptions;
};
const weightOptions = () => {
const combinedOptions = [defaultOption];
fontWeights.forEach(({ name, value }) => {
combinedOptions.push({
key: value,
name,
style: { fontStyle: void 0, fontWeight: value }
});
});
return combinedOptions;
};
const selectOptions = useMemo(() => {
if (hasFontStyles && hasFontWeights) {
return combineOptions();
}
return hasFontStyles ? styleOptions() : weightOptions();
}, [
props.options,
fontStyles,
fontWeights,
combinedStyleAndWeightOptions
]);
const currentSelection = selectOptions.find(
(option) => option.style.fontStyle === fontStyle && option.style.fontWeight === fontWeight
) || selectOptions[0];
const getDescribedBy = () => {
if (!currentSelection) {
return __("No selected font appearance");
}
if (!hasFontStyles) {
return sprintf(
// translators: %s: Currently selected font weight.
__("Currently selected font weight: %s"),
currentSelection.name
);
}
if (!hasFontWeights) {
return sprintf(
// translators: %s: Currently selected font style.
__("Currently selected font style: %s"),
currentSelection.name
);
}
return sprintf(
// translators: %s: Currently selected font appearance.
__("Currently selected font appearance: %s"),
currentSelection.name
);
};
if (!__next40pxDefaultSize && (otherProps.size === void 0 || otherProps.size === "default")) {
deprecated(
`36px default size for wp.blockEditor.__experimentalFontAppearanceControl`,
{
since: "6.8",
version: "7.1",
hint: "Set the `__next40pxDefaultSize` prop to true to start opting into the new default size, which will become the default in a future version."
}
);
}
return hasStylesOrWeights && /* @__PURE__ */ jsx(
CustomSelectControl,
{
...otherProps,
className: "components-font-appearance-control",
__next40pxDefaultSize,
__shouldNotWarnDeprecated36pxSize: true,
label,
describedBy: getDescribedBy(),
options: selectOptions,
value: currentSelection,
onChange: ({ selectedItem }) => onChange(selectedItem.style)
}
);
}
export {
FontAppearanceControl as default
};
//# sourceMappingURL=index.js.map