Repository navigation
GradientPicker: select by slug so two presets sharing a gradient keep their identity #80554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
51a44c3
eeda7a5
ed60d9e
3f21e2c
db2f85e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,11 +141,29 @@ Only used when `asButtons` is not true. | |
|
|
||
| ### `onChange` | ||
|
|
||
| - Type: `(currentGradient: string | undefined) => void` | ||
| - Type: `(currentGradient: string | undefined, index?: number | undefined, slug?: string | undefined) => void` | ||
| - Required: Yes | ||
|
|
||
| The function called when a new gradient has been defined. It is passed to | ||
| the `currentGradient` as an argument. | ||
| The function called when a new gradient has been defined. It is passed | ||
| the `currentGradient` as an argument. When a predefined gradient is | ||
| selected, the second argument is its index (or, for multiple-origin | ||
| gradients, the origin index) and the third argument is its slug. | ||
|
|
||
| ### `selectedSlug` | ||
|
|
||
| - Type: `string` | ||
| - Required: No | ||
|
|
||
| The slug of the currently selected predefined gradient. | ||
|
|
||
| When set to a non-empty string, selection is determined by slug rather | ||
| than by gradient value, which correctly handles palettes where two | ||
| entries share the same gradient. Entries whose slug does not match will | ||
| not appear selected in this mode, even if their gradient value matches | ||
| `value`. | ||
|
|
||
| An empty string is treated the same as `undefined`: selection falls | ||
| back to matching by gradient value. | ||
|
Comment on lines
+152
to
+166
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we need Two presets can intentionally (or not) share that string while having different preset slugs. That means a selection can't be represented by value alone. |
||
|
|
||
| ### `value` | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,9 +99,9 @@ const Template = ( { | |
| <GradientPicker | ||
| { ...props } | ||
| value={ gradient } | ||
| onChange={ ( ...changeArgs ) => { | ||
| setGradient( ...changeArgs ); | ||
| onChange?.( ...changeArgs ); | ||
|
Comment on lines
-102
to
-104
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smoke tested story Kapture.2026-07-23.at.11.44.41.mp4I think I'll need to update the code examples too. |
||
| onChange={ ( currentGradient, index, slug ) => { | ||
| setGradient( currentGradient ); | ||
| onChange?.( currentGradient, index, slug ); | ||
| } } | ||
| /> | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing
slugas the 3rd argument is required so consumers can persist the selected preset identity directly, instead of reverse-matching by gradient string and accidentally choosing the first duplicate.This mirrors the existing
ColorPaletteAPI shape, so it keeps color and gradient picker behavior consistent.See: https://github.com/WordPress/gutenberg/blob/fix/duplicate-gradient-preset-slug/packages/components/src/color-palette/types.ts#L30