@wordpress/components
Version:
UI components for WordPress.
1 lines • 4.7 kB
Source Map (JSON)
{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/color-palette/utils.ts"],"sourcesContent":["import { colord, extend } from 'colord';\nimport namesPlugin from 'colord/plugins/names';\nimport a11yPlugin from 'colord/plugins/a11y';\nimport { __ } from '@wordpress/i18n';\nimport type { ColorObject, ColorPaletteProps, PaletteObject } from './types';\n\nextend( [ namesPlugin, a11yPlugin ] );\n\n/**\n * Checks if a color value is a simple CSS color.\n *\n * @param value The color value to check.\n * @return A boolean indicating whether the color value is a simple CSS color.\n */\nconst isSimpleCSSColor = ( value: string ): boolean => {\n\tconst valueIsCssVariable = /var\\(/.test( value ?? '' );\n\tconst valueIsColorMix = /color-mix\\(/.test( value ?? '' );\n\treturn ! valueIsCssVariable && ! valueIsColorMix;\n};\n\nexport const extractColorNameFromCurrentValue = (\n\tcurrentValue?: ColorPaletteProps[ 'value' ],\n\tcolors: ColorPaletteProps[ 'colors' ] = [],\n\tshowMultiplePalettes: boolean = false,\n\tselectedSlug?: ColorPaletteProps[ 'selectedSlug' ]\n) => {\n\tif ( ! currentValue ) {\n\t\treturn '';\n\t}\n\tconst currentValueIsSimpleColor = currentValue\n\t\t? isSimpleCSSColor( currentValue )\n\t\t: false;\n\tconst normalizedCurrentValue = currentValueIsSimpleColor\n\t\t? colord( currentValue ).toHex()\n\t\t: currentValue;\n\n\t// Normalize format of `colors` to simplify the following loop\n\ttype normalizedPaletteObject = { colors: ColorObject[] };\n\tconst colorPalettes: normalizedPaletteObject[] = showMultiplePalettes\n\t\t? ( colors as PaletteObject[] )\n\t\t: [ { colors: colors as ColorObject[] } ];\n\tfor ( const { colors: paletteColors } of colorPalettes ) {\n\t\tfor ( const {\n\t\t\tname: colorName,\n\t\t\tcolor: colorValue,\n\t\t\tslug,\n\t\t} of paletteColors ) {\n\t\t\tif ( selectedSlug ) {\n\t\t\t\tif ( slug === selectedSlug ) {\n\t\t\t\t\treturn colorName;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst normalizedColorValue = currentValueIsSimpleColor\n\t\t\t\t? colord( colorValue ).toHex()\n\t\t\t\t: colorValue;\n\n\t\t\tif ( normalizedCurrentValue === normalizedColorValue ) {\n\t\t\t\treturn colorName;\n\t\t\t}\n\t\t}\n\t}\n\n\t// translators: shown when the user has picked a custom color (i.e not in the palette of colors).\n\treturn __( 'Custom' );\n};\n\n// The PaletteObject type has a `colors` property (an array of ColorObject),\n// while the ColorObject type has a `color` property (the CSS color value).\nexport const isMultiplePaletteObject = (\n\tobj: PaletteObject | ColorObject\n): obj is PaletteObject =>\n\tArray.isArray( ( obj as PaletteObject ).colors ) && ! ( 'color' in obj );\n\nexport const isMultiplePaletteArray = (\n\tarr: ( PaletteObject | ColorObject )[]\n): arr is PaletteObject[] => {\n\treturn (\n\t\tarr.length > 0 &&\n\t\tarr.every( ( colorObj ) => isMultiplePaletteObject( colorObj ) )\n\t);\n};\n\n/**\n * Transform a CSS variable used as background color into the color value itself.\n *\n * @param value The color value that may be a CSS variable.\n * @param element The element for which to get the computed style.\n * @return The background color value computed from a element.\n */\nexport const normalizeColorValue = (\n\tvalue: string | undefined,\n\telement: HTMLElement | null\n) => {\n\tif ( ! value || ! element || isSimpleCSSColor( value ) ) {\n\t\treturn value;\n\t}\n\n\tconst { ownerDocument } = element;\n\tconst { defaultView } = ownerDocument;\n\tconst computedBackgroundColor =\n\t\tdefaultView?.getComputedStyle( element ).backgroundColor;\n\n\treturn computedBackgroundColor\n\t\t? colord( computedBackgroundColor ).toHex()\n\t\t: value;\n};\n"],"mappings":";;;;;AAMA,OAAQ,CAAE,aAAa,UAAW,CAAE;;;;;;;AAQpC,MAAM,oBAAqB,UAA4B;CACtD,MAAM,qBAAqB,QAAQ,KAAM,SAAS,EAAG;CACrD,MAAM,kBAAkB,cAAc,KAAM,SAAS,EAAG;CACxD,OAAO,CAAE,sBAAsB,CAAE;AAClC;AAEA,MAAa,oCACZ,cACA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