@wordpress/block-editor
Version:
214 lines (188 loc) • 5.94 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = FontAppearanceControl;
var _element = require("@wordpress/element");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
/**
* WordPress dependencies
*/
const FONT_STYLES = [{
name: (0, _i18n._x)('Regular', 'font style'),
value: 'normal'
}, {
name: (0, _i18n._x)('Italic', 'font style'),
value: 'italic'
}];
const FONT_WEIGHTS = [{
name: (0, _i18n._x)('Thin', 'font weight'),
value: '100'
}, {
name: (0, _i18n._x)('Extra Light', 'font weight'),
value: '200'
}, {
name: (0, _i18n._x)('Light', 'font weight'),
value: '300'
}, {
name: (0, _i18n._x)('Regular', 'font weight'),
value: '400'
}, {
name: (0, _i18n._x)('Medium', 'font weight'),
value: '500'
}, {
name: (0, _i18n._x)('Semi Bold', 'font weight'),
value: '600'
}, {
name: (0, _i18n._x)('Bold', 'font weight'),
value: '700'
}, {
name: (0, _i18n._x)('Extra Bold', 'font weight'),
value: '800'
}, {
name: (0, _i18n._x)('Black', 'font weight'),
value: '900'
}];
/**
* 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 unified font style and weight options.
*
* @param {Object} props Component props.
*
* @return {WPElement} Font appearance control.
*/
function FontAppearanceControl(props) {
const {
onChange,
hasFontStyles = true,
hasFontWeights = true,
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
}
}; // Combines both font style and weight options into a single dropdown.
const combineOptions = () => {
const combinedOptions = [defaultOption];
FONT_STYLES.forEach(({
name: styleName,
value: styleValue
}) => {
FONT_WEIGHTS.forEach(({
name: weightName,
value: weightValue
}) => {
const optionName = styleValue === 'normal' ? weightName : (0, _i18n.sprintf)(
/* translators: 1: Font weight name. 2: Font style name. */
(0, _i18n.__)('%1$s %2$s'), weightName, styleName);
combinedOptions.push({
key: `${styleValue}-${weightValue}`,
name: optionName,
style: {
fontStyle: styleValue,
fontWeight: weightValue
}
});
});
});
return combinedOptions;
}; // Generates select options for font styles only.
const styleOptions = () => {
const combinedOptions = [defaultOption];
FONT_STYLES.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];
FONT_WEIGHTS.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)(() => {
if (hasFontStyles && hasFontWeights) {
return combineOptions();
}
return hasFontStyles ? styleOptions() : weightOptions();
}, [props.options]); // 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);
};
return hasStylesOrWeights && (0, _element.createElement)(_components.CustomSelectControl, (0, _extends2.default)({}, otherProps, {
className: "components-font-appearance-control",
label: label,
describedBy: getDescribedBy(),
options: selectOptions,
value: currentSelection,
onChange: ({
selectedItem
}) => onChange(selectedItem.style),
__nextUnconstrainedWidth: true
}));
}
//# sourceMappingURL=index.js.map