UNPKG

@wordpress/components

Version:
1 lines • 8.78 kB
{"version":3,"file":"duotone-picker.mjs","names":[],"sources":["../../src/duotone-picker/duotone-picker.tsx"],"sourcesContent":["import fastDeepEqual from 'fast-deep-equal/es6/index.js';\nimport { useMemo } from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\nimport ColorListPicker from './color-list-picker';\nimport CircularOptionPicker, {\n\tgetComputeCircularOptionPickerCommonProps,\n\twarnIfCircularOptionPickerAsButtonsIsSet,\n} from '../circular-option-picker';\nimport { VStack } from '../v-stack';\nimport CustomDuotoneBar from './custom-duotone-bar';\nimport { getDefaultColors, getGradientFromCSSColors } from './utils';\nimport { Spacer } from '../spacer';\nimport type { DuotonePickerProps } from './types';\n\n/**\n * ```jsx\n * import { DuotonePicker, DuotoneSwatch } from '@wordpress/components';\n * import { useState } from '@wordpress/element';\n *\n * const DUOTONE_PALETTE = [\n * \t{ colors: [ '#8c00b7', '#fcff41' ], name: 'Purple and yellow', slug: 'purple-yellow' },\n * \t{ colors: [ '#000097', '#ff4747' ], name: 'Blue and red', slug: 'blue-red' },\n * ];\n *\n * const COLOR_PALETTE = [\n * \t{ color: '#ff4747', name: 'Red', slug: 'red' },\n * \t{ color: '#fcff41', name: 'Yellow', slug: 'yellow' },\n * \t{ color: '#000097', name: 'Blue', slug: 'blue' },\n * \t{ color: '#8c00b7', name: 'Purple', slug: 'purple' },\n * ];\n *\n * const Example = () => {\n * \tconst [ duotone, setDuotone ] = useState( [ '#000000', '#ffffff' ] );\n * \treturn (\n * \t\t<>\n * \t\t\t<DuotonePicker\n * \t\t\t\tduotonePalette={ DUOTONE_PALETTE }\n * \t\t\t\tcolorPalette={ COLOR_PALETTE }\n * \t\t\t\tvalue={ duotone }\n * \t\t\t\tonChange={ setDuotone }\n * \t\t\t/>\n * \t\t\t<DuotoneSwatch values={ duotone } />\n * \t\t</>\n * \t);\n * };\n * ```\n */\nfunction DuotonePicker( {\n\tasButtons,\n\tpresentation,\n\tloop,\n\tclearable = true,\n\tunsetable = true,\n\tcolorPalette,\n\tduotonePalette,\n\tdisableCustomColors,\n\tdisableCustomDuotone,\n\tvalue,\n\tselectedSlug,\n\tonChange,\n\t'aria-label': ariaLabel,\n\t'aria-labelledby': ariaLabelledby,\n\t...otherProps\n}: DuotonePickerProps ) {\n\twarnIfCircularOptionPickerAsButtonsIsSet( 'DuotonePicker', asButtons );\n\tconst { metaProps, labelProps, resolvedPresentation } =\n\t\tgetComputeCircularOptionPickerCommonProps(\n\t\t\tasButtons,\n\t\t\tloop,\n\t\t\tariaLabel,\n\t\t\tariaLabelledby,\n\t\t\tpresentation\n\t\t);\n\tconst [ defaultDark, defaultLight ] = useMemo(\n\t\t() => getDefaultColors( colorPalette ),\n\t\t[ colorPalette ]\n\t);\n\n\tconst isUnset = value === 'unset';\n\tconst isUnsetSelected =\n\t\tresolvedPresentation !== 'command-buttons' && isUnset;\n\tconst unsetOptionLabel = __( 'Unset' );\n\n\tconst unsetOption = (\n\t\t<CircularOptionPicker.Option\n\t\t\tkey=\"unset\"\n\t\t\tvalue=\"unset\"\n\t\t\tisSelected={ isUnsetSelected }\n\t\t\ttooltipText={ unsetOptionLabel }\n\t\t\taria-label={ unsetOptionLabel }\n\t\t\tclassName=\"components-duotone-picker__color-indicator\"\n\t\t\tonClick={ () => {\n\t\t\t\tonChange( isUnsetSelected ? undefined : 'unset' );\n\t\t\t} }\n\t\t/>\n\t);\n\n\tconst duotoneOptions = duotonePalette.map(\n\t\t( { colors, slug, name }, index ) => {\n\t\t\tconst style = {\n\t\t\t\tbackground: getGradientFromCSSColors( colors, '135deg' ),\n\t\t\t\tcolor: 'transparent',\n\t\t\t};\n\t\t\tconst tooltipText =\n\t\t\t\tname ??\n\t\t\t\tsprintf(\n\t\t\t\t\t// translators: %s: duotone code e.g: \"dark-grayscale\" or \"7f7f7f-ffffff\".\n\t\t\t\t\t__( 'Duotone code: %s' ),\n\t\t\t\t\tslug\n\t\t\t\t);\n\t\t\tconst label = name\n\t\t\t\t? sprintf(\n\t\t\t\t\t\t// translators: %s: The name of the option e.g: \"Dark grayscale\".\n\t\t\t\t\t\t__( 'Duotone: %s' ),\n\t\t\t\t\t\tname\n\t\t\t\t\t)\n\t\t\t\t: tooltipText;\n\t\t\t// When a non-empty selectedSlug is provided, selection is decided\n\t\t\t// strictly by slug, which keeps two presets holding the same\n\t\t\t// colors apart. Otherwise selection falls back to matching the\n\t\t\t// colors themselves.\n\t\t\tconst isSelected =\n\t\t\t\tresolvedPresentation !== 'command-buttons' &&\n\t\t\t\t( selectedSlug\n\t\t\t\t\t? slug === selectedSlug\n\t\t\t\t\t: fastDeepEqual( colors, value ) );\n\n\t\t\treturn (\n\t\t\t\t<CircularOptionPicker.Option\n\t\t\t\t\tkey={ slug }\n\t\t\t\t\tvalue={ colors }\n\t\t\t\t\tisSelected={ isSelected }\n\t\t\t\t\taria-label={ label }\n\t\t\t\t\ttooltipText={ tooltipText }\n\t\t\t\t\tstyle={ style }\n\t\t\t\t\tonClick={\n\t\t\t\t\t\t// Deselecting reports no preset, matching\n\t\t\t\t\t\t// `ColorPalette` and `GradientPicker`. Passing the slug\n\t\t\t\t\t\t// back would leave a controlled consumer marking the\n\t\t\t\t\t\t// swatch as selected after its value had been cleared.\n\t\t\t\t\t\tisSelected\n\t\t\t\t\t\t\t? () => onChange( undefined )\n\t\t\t\t\t\t\t: () => onChange( colors, index, slug )\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t);\n\t\t}\n\t);\n\n\tconst options = unsetable\n\t\t? [ unsetOption, ...duotoneOptions ]\n\t\t: duotoneOptions;\n\n\treturn (\n\t\t<CircularOptionPicker\n\t\t\t{ ...otherProps }\n\t\t\t{ ...metaProps }\n\t\t\t{ ...labelProps }\n\t\t\toptions={ options }\n\t\t\tactions={\n\t\t\t\t!! clearable && (\n\t\t\t\t\t<CircularOptionPicker.ButtonAction\n\t\t\t\t\t\tonClick={ () => onChange( undefined ) }\n\t\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\t\tdisabled={ ! value }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ __( 'Clear' ) }\n\t\t\t\t\t</CircularOptionPicker.ButtonAction>\n\t\t\t\t)\n\t\t\t}\n\t\t>\n\t\t\t{ /* Both controls are hidden when custom duotones are disabled,\n\t\t\t so the wrapper would contribute only its padding. */ }\n\t\t\t{ ! disableCustomDuotone && (\n\t\t\t\t<Spacer paddingTop={ options.length === 0 ? 0 : 4 }>\n\t\t\t\t\t<VStack spacing={ 3 }>\n\t\t\t\t\t\t{ ! disableCustomColors && (\n\t\t\t\t\t\t\t<CustomDuotoneBar\n\t\t\t\t\t\t\t\tvalue={ isUnset ? undefined : value }\n\t\t\t\t\t\t\t\tonChange={ onChange }\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t) }\n\t\t\t\t\t\t<ColorListPicker\n\t\t\t\t\t\t\tlabels={ [ __( 'Shadows' ), __( 'Highlights' ) ] }\n\t\t\t\t\t\t\tcolors={ colorPalette }\n\t\t\t\t\t\t\tvalue={ isUnset ? undefined : value }\n\t\t\t\t\t\t\tdisableCustomColors={ disableCustomColors }\n\t\t\t\t\t\t\tenableAlpha\n\t\t\t\t\t\t\tonChange={ ( newColors ) => {\n\t\t\t\t\t\t\t\tif ( ! newColors[ 0 ] ) {\n\t\t\t\t\t\t\t\t\tnewColors[ 0 ] = defaultDark;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif ( ! newColors[ 1 ] ) {\n\t\t\t\t\t\t\t\t\tnewColors[ 1 ] = defaultLight;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst newValue =\n\t\t\t\t\t\t\t\t\tnewColors.length >= 2\n\t\t\t\t\t\t\t\t\t\t? newColors\n\t\t\t\t\t\t\t\t\t\t: undefined;\n\t\t\t\t\t\t\t\t// @ts-expect-error TODO: The color arrays for a DuotonePicker should be a tuple of two colors,\n\t\t\t\t\t\t\t\t// but it's currently typed as a string[].\n\t\t\t\t\t\t\t\t// See also https://github.com/WordPress/gutenberg/pull/49060#discussion_r1136951035\n\t\t\t\t\t\t\t\tonChange( newValue );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</VStack>\n\t\t\t\t</Spacer>\n\t\t\t) }\n\t\t</CircularOptionPicker>\n\t);\n}\n\nexport default DuotonePicker;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA,SAAS,cAAe,EACvB,WACA,cACA,MACA,YAAY,MACZ,YAAY,MACZ,cACA,gBACA,qBACA,sBACA,OACA,cACA,UACA,cAAc,WACd,mBAAmB,gBACnB,GAAG,cACoB;CACvB,yCAA0C,iBAAiB,SAAU;CACrE,MAAM,EAAE,WAAW,YAAY,yBAC9B,0CACC,WACA,MACA,WACA,gBACA,YACD;CACD,MAAM,CAAE,aAAa,gBAAiB,cAC/B,iBAAkB,YAAa,GACrC,CAAE,YAAa,CAChB;CAEA,MAAM,UAAU,UAAU;CAC1B,MAAM,kBACL,yBAAyB,qBAAqB;CAC/C,MAAM,mBAAmB,GAAI,OAAQ;CAErC,MAAM,cACL,oBAAC,qBAAqB,QAAtB;EAEC,OAAM;EACN,YAAa;EACb,aAAc;EACd,cAAa;EACb,WAAU;EACV,eAAgB;GACf,SAAU,kBAAkB,KAAA,IAAY,OAAQ;EACjD;CACA,GATI,OASJ;CAGF,MAAM,iBAAiB,eAAe,KACnC,EAAE,QAAQ,MAAM,QAAQ,UAAW;EACpC,MAAM,QAAQ;GACb,YAAY,yBAA0B,QAAQ,QAAS;GACvD,OAAO;EACR;EACA,MAAM,cACL,QACA,QAEC,GAAI,kBAAmB,GACvB,IACD;EACD,MAAM,QAAQ,OACX,QAEA,GAAI,aAAc,GAClB,IACD,IACC;EAKH,MAAM,aACL,yBAAyB,sBACvB,eACC,SAAS,eACT,cAAe,QAAQ,KAAM;EAEjC,OACC,oBAAC,qBAAqB,QAAtB;GAEC,OAAQ;GACK;GACb,cAAa;G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