Skip to content

Commit 04577eb

Browse files
committed
feat(addon-controls): add maxPresetColors option to ColorControl
The ColorControl preset limit was previously hardcoded to 27 colors. This prevented users with large design systems (more than 27 colors) from displaying all their preset colors. Add a new optional maxPresetColors prop to ColorConfig/ColorControl that allows users to configure or remove this limit: - Defaults to 27 for backward compatibility - Setting to 0 or Infinity removes the limit entirely - Any other positive number limits presets to that count Add two new stories demonstrating the feature: - WithMaxPresetColors (limit to 5) - WithUnlimitedPresetColors (maxPresetColors: 0, 40 colors) Fixes #20298
1 parent 2932d61 commit 04577eb

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

code/addons/docs/src/blocks/controls/Color.stories.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,36 @@ export const WithPresetColors: Story = {
6565
},
6666
};
6767

68+
export const WithMaxPresetColors: Story = {
69+
name: 'With maxPresetColors (limit to 5)',
70+
args: {
71+
value: '#00ffff',
72+
maxPresetColors: 5,
73+
presetColors: [
74+
{ color: '#ff4785', title: 'Coral' },
75+
{ color: '#1EA7FD', title: 'Ocean' },
76+
{ color: 'rgb(252, 82, 31)', title: 'Orange' },
77+
{ color: 'rgba(255, 174, 0, 0.5)', title: 'Gold' },
78+
{ color: 'hsl(101, 52%, 49%)', title: 'Green' },
79+
{ color: 'hsla(179,65%,53%,0.5)', title: 'Seafoam' },
80+
{ color: '#6F2CAC', title: 'Purple' },
81+
{ color: '#2A0481', title: 'Ultraviolet' },
82+
],
83+
},
84+
};
85+
86+
export const WithUnlimitedPresetColors: Story = {
87+
name: 'With unlimited presets (maxPresetColors: 0)',
88+
args: {
89+
value: '#00ffff',
90+
maxPresetColors: 0,
91+
presetColors: Array.from({ length: 40 }, (_, i) => {
92+
const hue = Math.round((i / 40) * 360);
93+
return { color: `hsl(${hue}, 70%, 50%)`, title: `Color ${i + 1}` };
94+
}),
95+
},
96+
};
97+
6898
export const StartOpen: Story = {
6999
args: {
70100
startOpen: true,

code/addons/docs/src/blocks/controls/Color.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ const id = (value: string) => value.replace(/\s*/, '').toLowerCase();
307307
const usePresets = (
308308
presetColors: PresetColor[],
309309
currentColor: ParsedColor | undefined,
310-
colorSpace: ColorSpace
310+
colorSpace: ColorSpace,
311+
maxPresetColors = 27
311312
) => {
312313
const [selectedColors, setSelectedColors] = useState(currentColor?.valid ? [currentColor] : []);
313314

@@ -330,8 +331,12 @@ const usePresets = (
330331
}
331332
return parseValue(preset.color);
332333
});
333-
return initialPresets.concat(selectedColors).filter(Boolean).slice(-27);
334-
}, [presetColors, selectedColors]);
334+
const combined = initialPresets.concat(selectedColors).filter(Boolean);
335+
if (!maxPresetColors || maxPresetColors === Infinity) {
336+
return combined;
337+
}
338+
return combined.slice(-maxPresetColors);
339+
}, [presetColors, selectedColors, maxPresetColors]);
335340

336341
const addPreset: (color: ParsedColor) => void = useCallback(
337342
(color) => {
@@ -365,6 +370,7 @@ export const ColorControl: FC<ColorControlProps> = ({
365370
onFocus,
366371
onBlur,
367372
presetColors,
373+
maxPresetColors,
368374
startOpen = false,
369375
argType,
370376
}) => {
@@ -373,7 +379,7 @@ export const ColorControl: FC<ColorControlProps> = ({
373379
initialValue,
374380
debouncedOnChange
375381
);
376-
const { presets, addPreset } = usePresets(presetColors ?? [], color, colorSpace);
382+
const { presets, addPreset } = usePresets(presetColors ?? [], color, colorSpace, maxPresetColors);
377383
const Picker = ColorPicker[colorSpace];
378384

379385
const readOnly = !!argType?.table?.readonly;

code/addons/docs/src/blocks/controls/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ export type ColorValue = string;
1717
export type PresetColor = ColorValue | { color: ColorValue; title?: string };
1818
export interface ColorConfig {
1919
presetColors?: PresetColor[];
20+
/**
21+
* Maximum number of preset colors shown in the color picker. When the number of presets exceeds
22+
* this value, the oldest presets are removed first (most-recently-used order). Set to `Infinity`
23+
* or `0` to disable the limit and show all presets.
24+
*
25+
* @default 27
26+
*/
27+
maxPresetColors?: number;
2028
/**
2129
* Whether the color picker should be open by default when rendered.
2230
*

0 commit comments

Comments
 (0)