@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
1 lines • 1.83 kB
Source Map (JSON)
{"version":3,"file":"palette-colors.cjs","names":[],"sources":["../src/palette-colors.ts"],"sourcesContent":["import type { Color } from '@wordpress/global-styles-engine';\n\n/**\n * Moves the background and text colors to the front of the palette so that\n * style variation previews show the main color scheme (e.g. light or dark)\n * first. Colors that are not in the palette are ignored, and the order of\n * the remaining colors is preserved.\n *\n * @param paletteColors The palette colors to reorder.\n * @param backgroundColor The background color to place first.\n * @param textColor The text color to place second.\n *\n * @return A new array with the background and text colors at the front.\n */\nexport function prioritizePaletteColors(\n\tpaletteColors: Color[],\n\tbackgroundColor: string,\n\ttextColor: string\n): Color[] {\n\tconst prioritizedColors = [ ...paletteColors ];\n\n\tconst moveColorToIndex = ( color: string, targetIndex: number ) => {\n\t\tconst currentIndex = prioritizedColors.findIndex(\n\t\t\t( paletteColor ) => paletteColor.color === color\n\t\t);\n\t\tif ( currentIndex === -1 ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst [ paletteColor ] = prioritizedColors.splice( currentIndex, 1 );\n\t\tprioritizedColors.splice( targetIndex, 0, paletteColor );\n\t};\n\n\tmoveColorToIndex( backgroundColor, 0 );\n\tif ( textColor !== backgroundColor ) {\n\t\tmoveColorToIndex( textColor, 1 );\n\t}\n\n\treturn prioritizedColors;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAcA,SAAgB,wBACf,eACA,iBACA,WACU;CACV,MAAM,oBAAoB,CAAE,GAAG,aAAc;CAE7C,MAAM,oBAAqB,OAAe,gBAAyB;EAClE,MAAM,eAAe,kBAAkB,WACpC,iBAAkB,aAAa,UAAU,KAC5C;EACA,IAAK,iBAAiB,IACrB;EAGD,MAAM,CAAE,gBAAiB,kBAAkB,OAAQ,cAAc,CAAE;EACnE,kBAAkB,OAAQ,aAAa,GAAG,YAAa;CACxD;CAEA,iBAAkB,iBAAiB,CAAE;CACrC,IAAK,cAAc,iBAClB,iBAAkB,WAAW,CAAE;CAGhC,OAAO;AACR"}