@wordpress/components
Version:
UI components for WordPress.
146 lines (145 loc) • 6.71 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let fast_deep_equal_es6_index_js = require("fast-deep-equal/es6/index.js");
fast_deep_equal_es6_index_js = __toESM(fast_deep_equal_es6_index_js);
let _wordpress_element = require("@wordpress/element");
let _wordpress_i18n = require("@wordpress/i18n");
let __color_list_picker_index_tsx = require("./color-list-picker/index.cjs");
__color_list_picker_index_tsx = __toESM(__color_list_picker_index_tsx);
let ___circular_option_picker_index_tsx = require("../circular-option-picker/index.cjs");
___circular_option_picker_index_tsx = __toESM(___circular_option_picker_index_tsx);
let ___v_stack_index_ts = require("../v-stack/index.cjs");
let __custom_duotone_bar_tsx = require("./custom-duotone-bar.cjs");
__custom_duotone_bar_tsx = __toESM(__custom_duotone_bar_tsx);
let __utils_ts = require("./utils.cjs");
let ___spacer_index_ts = require("../spacer/index.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/components/src/duotone-picker/duotone-picker.tsx
/**
* ```jsx
* import { DuotonePicker, DuotoneSwatch } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const DUOTONE_PALETTE = [
* { colors: [ '#8c00b7', '#fcff41' ], name: 'Purple and yellow', slug: 'purple-yellow' },
* { colors: [ '#000097', '#ff4747' ], name: 'Blue and red', slug: 'blue-red' },
* ];
*
* const COLOR_PALETTE = [
* { color: '#ff4747', name: 'Red', slug: 'red' },
* { color: '#fcff41', name: 'Yellow', slug: 'yellow' },
* { color: '#000097', name: 'Blue', slug: 'blue' },
* { color: '#8c00b7', name: 'Purple', slug: 'purple' },
* ];
*
* const Example = () => {
* const [ duotone, setDuotone ] = useState( [ '#000000', '#ffffff' ] );
* return (
* <>
* <DuotonePicker
* duotonePalette={ DUOTONE_PALETTE }
* colorPalette={ COLOR_PALETTE }
* value={ duotone }
* onChange={ setDuotone }
* />
* <DuotoneSwatch values={ duotone } />
* </>
* );
* };
* ```
*/
function DuotonePicker({ asButtons, presentation, loop, clearable = true, unsetable = true, colorPalette, duotonePalette, disableCustomColors, disableCustomDuotone, value, selectedSlug, onChange, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledby, ...otherProps }) {
(0, ___circular_option_picker_index_tsx.warnIfCircularOptionPickerAsButtonsIsSet)("DuotonePicker", asButtons);
const { metaProps, labelProps, resolvedPresentation } = (0, ___circular_option_picker_index_tsx.getComputeCircularOptionPickerCommonProps)(asButtons, loop, ariaLabel, ariaLabelledby, presentation);
const [defaultDark, defaultLight] = (0, _wordpress_element.useMemo)(() => (0, __utils_ts.getDefaultColors)(colorPalette), [colorPalette]);
const isUnset = value === "unset";
const isUnsetSelected = resolvedPresentation !== "command-buttons" && isUnset;
const unsetOptionLabel = (0, _wordpress_i18n.__)("Unset");
const unsetOption = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default.Option, {
value: "unset",
isSelected: isUnsetSelected,
tooltipText: unsetOptionLabel,
"aria-label": unsetOptionLabel,
className: "components-duotone-picker__color-indicator",
onClick: () => {
onChange(isUnsetSelected ? void 0 : "unset");
}
}, "unset");
const duotoneOptions = duotonePalette.map(({ colors, slug, name }, index) => {
const style = {
background: (0, __utils_ts.getGradientFromCSSColors)(colors, "135deg"),
color: "transparent"
};
const tooltipText = name ?? (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Duotone code: %s"), slug);
const label = name ? (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Duotone: %s"), name) : tooltipText;
const isSelected = resolvedPresentation !== "command-buttons" && (selectedSlug ? slug === selectedSlug : (0, fast_deep_equal_es6_index_js.default)(colors, value));
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default.Option, {
value: colors,
isSelected,
"aria-label": label,
tooltipText,
style,
onClick: isSelected ? () => onChange(void 0) : () => onChange(colors, index, slug)
}, slug);
});
const options = unsetable ? [unsetOption, ...duotoneOptions] : duotoneOptions;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default, {
...otherProps,
...metaProps,
...labelProps,
options,
actions: !!clearable && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default.ButtonAction, {
onClick: () => onChange(void 0),
accessibleWhenDisabled: true,
disabled: !value,
children: (0, _wordpress_i18n.__)("Clear")
}),
children: !disableCustomDuotone && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___spacer_index_ts.Spacer, {
paddingTop: options.length === 0 ? 0 : 4,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___v_stack_index_ts.VStack, {
spacing: 3,
children: [!disableCustomColors && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__custom_duotone_bar_tsx.default, {
value: isUnset ? void 0 : value,
onChange
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__color_list_picker_index_tsx.default, {
labels: [(0, _wordpress_i18n.__)("Shadows"), (0, _wordpress_i18n.__)("Highlights")],
colors: colorPalette,
value: isUnset ? void 0 : value,
disableCustomColors,
enableAlpha: true,
onChange: (newColors) => {
if (!newColors[0]) newColors[0] = defaultDark;
if (!newColors[1]) newColors[1] = defaultLight;
onChange(newColors.length >= 2 ? newColors : void 0);
}
})]
})
})
});
}
exports.default = DuotonePicker;
//# sourceMappingURL=duotone-picker.cjs.map