@wordpress/components
Version:
UI components for WordPress.
276 lines (246 loc) • 9.03 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ColorPalette = void 0;
exports.CustomColorPickerDropdown = CustomColorPickerDropdown;
exports.default = void 0;
var _element = require("@wordpress/element");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _colord = require("colord");
var _names = _interopRequireDefault(require("colord/plugins/names"));
var _a11y = _interopRequireDefault(require("colord/plugins/a11y"));
var _i18n = require("@wordpress/i18n");
var _dropdown = _interopRequireDefault(require("../dropdown"));
var _colorPicker = require("../color-picker");
var _circularOptionPicker = _interopRequireDefault(require("../circular-option-picker"));
var _vStack = require("../v-stack");
var _flex = require("../flex");
var _truncate = require("../truncate");
var _styles = require("./styles");
var _dropdownContentWrapper = _interopRequireDefault(require("../dropdown/dropdown-content-wrapper"));
var _utils = require("./utils");
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
(0, _colord.extend)([_names.default, _a11y.default]);
function SinglePalette(_ref) {
let {
className,
clearColor,
colors,
onChange,
value,
actions
} = _ref;
const colorOptions = (0, _element.useMemo)(() => {
return colors.map((_ref2, index) => {
let {
color,
name
} = _ref2;
const colordColor = (0, _colord.colord)(color);
const isSelected = value === color;
return (0, _element.createElement)(_circularOptionPicker.default.Option, {
key: `${color}-${index}`,
isSelected: isSelected,
selectedIconProps: isSelected ? {
fill: colordColor.contrast() > colordColor.contrast('#000') ? '#fff' : '#000'
} : {},
tooltipText: name || // translators: %s: color hex code e.g: "#f00".
(0, _i18n.sprintf)((0, _i18n.__)('Color code: %s'), color),
style: {
backgroundColor: color,
color
},
onClick: isSelected ? clearColor : () => onChange(color, index),
"aria-label": name ? // translators: %s: The name of the color e.g: "vivid red".
(0, _i18n.sprintf)((0, _i18n.__)('Color: %s'), name) : // translators: %s: color hex code e.g: "#f00".
(0, _i18n.sprintf)((0, _i18n.__)('Color code: %s'), color)
});
});
}, [colors, value, onChange, clearColor]);
return (0, _element.createElement)(_circularOptionPicker.default, {
className: className,
options: colorOptions,
actions: actions
});
}
function MultiplePalettes(_ref3) {
let {
className,
clearColor,
colors,
onChange,
value,
actions,
headingLevel
} = _ref3;
if (colors.length === 0) {
return null;
}
return (0, _element.createElement)(_vStack.VStack, {
spacing: 3,
className: className
}, colors.map((_ref4, index) => {
let {
name,
colors: colorPalette
} = _ref4;
return (0, _element.createElement)(_vStack.VStack, {
spacing: 2,
key: index
}, (0, _element.createElement)(_styles.ColorHeading, {
level: headingLevel
}, name), (0, _element.createElement)(SinglePalette, {
clearColor: clearColor,
colors: colorPalette,
onChange: newColor => onChange(newColor, index),
value: value,
actions: colors.length === index + 1 ? actions : null
}));
}));
}
function CustomColorPickerDropdown(_ref5) {
let {
isRenderedInSidebar,
popoverProps: receivedPopoverProps,
...props
} = _ref5;
const popoverProps = (0, _element.useMemo)(() => ({
shift: true,
...(isRenderedInSidebar ? {
// When in the sidebar: open to the left (stacking),
// leaving the same gap as the parent popover.
placement: 'left-start',
offset: 34
} : {
// Default behavior: open below the anchor
placement: 'bottom',
offset: 8
}),
...receivedPopoverProps
}), [isRenderedInSidebar, receivedPopoverProps]);
return (0, _element.createElement)(_dropdown.default, (0, _extends2.default)({
contentClassName: "components-color-palette__custom-color-dropdown-content",
popoverProps: popoverProps
}, props));
}
function UnforwardedColorPalette(props, forwardedRef) {
const {
clearable = true,
colors = [],
disableCustomColors = false,
enableAlpha = false,
onChange,
value,
__experimentalIsRenderedInSidebar = false,
headingLevel = 2,
...otherProps
} = props;
const [normalizedColorValue, setNormalizedColorValue] = (0, _element.useState)(value);
const clearColor = (0, _element.useCallback)(() => onChange(undefined), [onChange]);
const customColorPaletteCallbackRef = (0, _element.useCallback)(node => {
setNormalizedColorValue((0, _utils.normalizeColorValue)(value, node));
}, [value]);
const hasMultipleColorOrigins = (0, _utils.isMultiplePaletteArray)(colors);
const buttonLabelName = (0, _element.useMemo)(() => (0, _utils.extractColorNameFromCurrentValue)(value, colors, hasMultipleColorOrigins), [value, colors, hasMultipleColorOrigins]);
const renderCustomColorPicker = () => (0, _element.createElement)(_dropdownContentWrapper.default, {
paddingSize: "none"
}, (0, _element.createElement)(_colorPicker.ColorPicker, {
color: normalizedColorValue,
onChange: color => onChange(color),
enableAlpha: enableAlpha
}));
const colordColor = (0, _colord.colord)(normalizedColorValue !== null && normalizedColorValue !== void 0 ? normalizedColorValue : '');
const valueWithoutLeadingHash = value !== null && value !== void 0 && value.startsWith('#') ? value.substring(1) : value !== null && value !== void 0 ? value : '';
const customColorAccessibleLabel = !!valueWithoutLeadingHash ? (0, _i18n.sprintf)( // translators: %1$s: The name of the color e.g: "vivid red". %2$s: The color's hex code e.g: "#f00".
(0, _i18n.__)('Custom color picker. The currently selected color is called "%1$s" and has a value of "%2$s".'), buttonLabelName, valueWithoutLeadingHash) : (0, _i18n.__)('Custom color picker.');
const paletteCommonProps = {
clearable,
clearColor,
onChange,
value,
actions: !!clearable && (0, _element.createElement)(_circularOptionPicker.default.ButtonAction, {
onClick: clearColor
}, (0, _i18n.__)('Clear')),
headingLevel
};
return (0, _element.createElement)(_vStack.VStack, (0, _extends2.default)({
spacing: 3,
ref: forwardedRef
}, otherProps), !disableCustomColors && (0, _element.createElement)(CustomColorPickerDropdown, {
isRenderedInSidebar: __experimentalIsRenderedInSidebar,
renderContent: renderCustomColorPicker,
renderToggle: _ref6 => {
let {
isOpen,
onToggle
} = _ref6;
return (0, _element.createElement)(_flex.Flex, {
as: 'button',
ref: customColorPaletteCallbackRef,
justify: "space-between",
align: "flex-start",
className: "components-color-palette__custom-color",
"aria-expanded": isOpen,
"aria-haspopup": "true",
onClick: onToggle,
"aria-label": customColorAccessibleLabel,
style: (0, _utils.showTransparentBackground)(value) ? {
color: '#000'
} : {
background: value,
color: colordColor.contrast() > colordColor.contrast('#000') ? '#fff' : '#000'
}
}, (0, _element.createElement)(_flex.FlexItem, {
isBlock: true,
as: _truncate.Truncate,
className: "components-color-palette__custom-color-name"
}, buttonLabelName), (0, _element.createElement)(_flex.FlexItem, {
as: "span",
className: "components-color-palette__custom-color-value"
}, valueWithoutLeadingHash));
}
}), hasMultipleColorOrigins ? (0, _element.createElement)(MultiplePalettes, (0, _extends2.default)({}, paletteCommonProps, {
colors: colors
})) : (0, _element.createElement)(SinglePalette, (0, _extends2.default)({}, paletteCommonProps, {
colors: colors
})));
}
/**
* Allows the user to pick a color from a list of pre-defined color entries.
*
* ```jsx
* import { ColorPalette } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const MyColorPalette = () => {
* const [ color, setColor ] = useState ( '#f00' )
* const colors = [
* { name: 'red', color: '#f00' },
* { name: 'white', color: '#fff' },
* { name: 'blue', color: '#00f' },
* ];
* return (
* <ColorPalette
* colors={ colors }
* value={ color }
* onChange={ ( color ) => setColor( color ) }
* />
* );
* } );
* ```
*/
const ColorPalette = (0, _element.forwardRef)(UnforwardedColorPalette);
exports.ColorPalette = ColorPalette;
var _default = ColorPalette;
exports.default = _default;
//# sourceMappingURL=index.js.map