@wordpress/block-editor
Version:
178 lines (167 loc) • 5.74 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = FontAppearanceControl;
var _components = require("@wordpress/components");
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _getFontStylesAndWeights = require("../../utils/get-font-styles-and-weights");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
/**
* Adjusts font appearance field label in case either font styles or weights
* are disabled.
*
* @param {boolean} hasFontStyles Whether font styles are enabled and present.
* @param {boolean} hasFontWeights Whether font weights are enabled and present.
* @return {string} A label representing what font appearance is being edited.
*/const getFontAppearanceLabel = (hasFontStyles, hasFontWeights) => {
if (!hasFontStyles) {
return (0, _i18n.__)('Font weight');
}
if (!hasFontWeights) {
return (0, _i18n.__)('Font style');
}
return (0, _i18n.__)('Appearance');
};
/**
* Control to display font style and weight options of the active font.
*
* @param {Object} props Component props.
*
* @return {Element} Font appearance control.
*/
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: (0, _i18n.__)('Default'),
style: {
fontStyle: undefined,
fontWeight: undefined
}
};
const {
fontStyles,
fontWeights,
combinedStyleAndWeightOptions
} = (0, _getFontStylesAndWeights.getFontStylesAndWeights)(fontFamilyFaces);
// Generates select options for combined font styles and weights.
const combineOptions = () => {
const combinedOptions = [defaultOption];
if (combinedStyleAndWeightOptions) {
combinedOptions.push(...combinedStyleAndWeightOptions);
}
return combinedOptions;
};
// Generates select options for font styles only.
const styleOptions = () => {
const combinedOptions = [defaultOption];
fontStyles.forEach(({
name,
value
}) => {
combinedOptions.push({
key: value,
name,
style: {
fontStyle: value,
fontWeight: undefined
}
});
});
return combinedOptions;
};
// Generates select options for font weights only.
const weightOptions = () => {
const combinedOptions = [defaultOption];
fontWeights.forEach(({
name,
value
}) => {
combinedOptions.push({
key: value,
name,
style: {
fontStyle: undefined,
fontWeight: value
}
});
});
return combinedOptions;
};
// Map font styles and weights to select options.
const selectOptions = (0, _element.useMemo)(() => {
// Display combined available font style and weight options.
if (hasFontStyles && hasFontWeights) {
return combineOptions();
}
// Display only font style options or font weight options.
return hasFontStyles ? styleOptions() : weightOptions();
}, [props.options, fontStyles, fontWeights, combinedStyleAndWeightOptions]);
// Find current selection by comparing font style & weight against options,
// and fall back to the Default option if there is no matching option.
const currentSelection = selectOptions.find(option => option.style.fontStyle === fontStyle && option.style.fontWeight === fontWeight) || selectOptions[0];
// Adjusts screen reader description based on styles or weights.
const getDescribedBy = () => {
if (!currentSelection) {
return (0, _i18n.__)('No selected font appearance');
}
if (!hasFontStyles) {
return (0, _i18n.sprintf)(
// translators: %s: Currently selected font weight.
(0, _i18n.__)('Currently selected font weight: %s'), currentSelection.name);
}
if (!hasFontWeights) {
return (0, _i18n.sprintf)(
// translators: %s: Currently selected font style.
(0, _i18n.__)('Currently selected font style: %s'), currentSelection.name);
}
return (0, _i18n.sprintf)(
// translators: %s: Currently selected font appearance.
(0, _i18n.__)('Currently selected font appearance: %s'), currentSelection.name);
};
if (!__next40pxDefaultSize && (otherProps.size === undefined || otherProps.size === 'default')) {
(0, _deprecated.default)(`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__*/(0, _jsxRuntime.jsx)(_components.CustomSelectControl, {
...otherProps,
className: "components-font-appearance-control",
__next40pxDefaultSize: __next40pxDefaultSize,
__shouldNotWarnDeprecated36pxSize: true,
label: label,
describedBy: getDescribedBy(),
options: selectOptions,
value: currentSelection,
onChange: ({
selectedItem
}) => onChange(selectedItem.style)
});
}
//# sourceMappingURL=index.js.map