UNPKG

@wordpress/components

Version:
116 lines (115 loc) • 4.67 kB
import fastDeepEqual from "fast-deep-equal/es6/index.js"; import { useMemo } from "@wordpress/element"; import { __, sprintf } from "@wordpress/i18n"; import ColorListPicker from "./color-list-picker/index.mjs"; import CircularOptionPicker, { getComputeCircularOptionPickerCommonProps, warnIfCircularOptionPickerAsButtonsIsSet } from "../circular-option-picker/index.mjs"; import { VStack } from "../v-stack/index.mjs"; import CustomDuotoneBar from "./custom-duotone-bar.mjs"; import { getDefaultColors, getGradientFromCSSColors } from "./utils.mjs"; import { Spacer } from "../spacer/index.mjs"; import { jsx, jsxs } from "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 }) { warnIfCircularOptionPickerAsButtonsIsSet("DuotonePicker", asButtons); const { metaProps, labelProps, resolvedPresentation } = getComputeCircularOptionPickerCommonProps(asButtons, loop, ariaLabel, ariaLabelledby, presentation); const [defaultDark, defaultLight] = useMemo(() => getDefaultColors(colorPalette), [colorPalette]); const isUnset = value === "unset"; const isUnsetSelected = resolvedPresentation !== "command-buttons" && isUnset; const unsetOptionLabel = __("Unset"); const unsetOption = /* @__PURE__ */ jsx(CircularOptionPicker.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: getGradientFromCSSColors(colors, "135deg"), color: "transparent" }; const tooltipText = name ?? sprintf(__("Duotone code: %s"), slug); const label = name ? sprintf(__("Duotone: %s"), name) : tooltipText; const isSelected = resolvedPresentation !== "command-buttons" && (selectedSlug ? slug === selectedSlug : fastDeepEqual(colors, value)); return /* @__PURE__ */ jsx(CircularOptionPicker.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__ */ jsx(CircularOptionPicker, { ...otherProps, ...metaProps, ...labelProps, options, actions: !!clearable && /* @__PURE__ */ jsx(CircularOptionPicker.ButtonAction, { onClick: () => onChange(void 0), accessibleWhenDisabled: true, disabled: !value, children: __("Clear") }), children: !disableCustomDuotone && /* @__PURE__ */ jsx(Spacer, { paddingTop: options.length === 0 ? 0 : 4, children: /* @__PURE__ */ jsxs(VStack, { spacing: 3, children: [!disableCustomColors && /* @__PURE__ */ jsx(CustomDuotoneBar, { value: isUnset ? void 0 : value, onChange }), /* @__PURE__ */ jsx(ColorListPicker, { labels: [__("Shadows"), __("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); } })] }) }) }); } export { DuotonePicker as default }; //# sourceMappingURL=duotone-picker.mjs.map