@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
28 lines (27 loc) • 1.23 kB
JavaScript
// packages/global-styles-ui/src/palette-colors.ts
/**
* Moves the background and text colors to the front of the palette so that
* style variation previews show the main color scheme (e.g. light or dark)
* first. Colors that are not in the palette are ignored, and the order of
* the remaining colors is preserved.
*
* @param paletteColors The palette colors to reorder.
* @param backgroundColor The background color to place first.
* @param textColor The text color to place second.
*
* @return A new array with the background and text colors at the front.
*/
function prioritizePaletteColors(paletteColors, backgroundColor, textColor) {
const prioritizedColors = [...paletteColors];
const moveColorToIndex = (color, targetIndex) => {
const currentIndex = prioritizedColors.findIndex((paletteColor) => paletteColor.color === color);
if (currentIndex === -1) return;
const [paletteColor] = prioritizedColors.splice(currentIndex, 1);
prioritizedColors.splice(targetIndex, 0, paletteColor);
};
moveColorToIndex(backgroundColor, 0);
if (textColor !== backgroundColor) moveColorToIndex(textColor, 1);
return prioritizedColors;
}
export { prioritizePaletteColors };
//# sourceMappingURL=palette-colors.mjs.map