Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function ColorGradientControlInner( {
colorValue,
colorSlug,
gradientValue,
gradientSlug,
clearable,
showTitle = true,
enableAlpha,
Expand Down Expand Up @@ -102,13 +103,15 @@ function ColorGradientControlInner( {
[ TAB_IDS.gradient ]: (
<GradientPicker
value={ gradientValue }
selectedSlug={ gradientSlug }
onChange={
canChooseAColor
? ( newGradient ) => {
onGradientChange( newGradient );
? ( newGradient, _index, newSlug ) => {
onGradientChange( newGradient, newSlug );
onColorChange();
}
: onGradientChange
: ( newGradient, _index, newSlug ) =>
onGradientChange( newGradient, newSlug )
}
{ ...{ gradients, disableCustomGradients } }
__experimentalIsRenderedInSidebar={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,11 @@ export default function BackgroundImagePanel( {
// Legacy `color.gradient` setters.
const legacyColorGradient = decodeValue( inheritedValue?.color?.gradient );
const userLegacyColorGradient = decodeValue( value?.color?.gradient );
const setLegacyColorGradient = ( newGradient ) => {
const setLegacyColorGradient = ( newGradient, newSlug ) => {
const newValue = setImmutably(
value,
[ 'color', 'gradient' ],
encodeGradientValue( newGradient )
encodeGradientValue( newGradient, newSlug )
);
newValue.color.background = undefined;
onChange( newValue );
Expand All @@ -323,11 +323,11 @@ export default function BackgroundImagePanel( {
// Set gradient value, encoding preset matches as slug references.
// Also clear color.gradient to migrate from the legacy location,
// matching the block inspector behavior in hooks/background.js.
const setGradient = ( newGradient ) => {
const setGradient = ( newGradient, newSlug ) => {
let newValue = setImmutably(
value,
[ 'background', 'gradient' ],
encodeGradientValue( newGradient )
encodeGradientValue( newGradient, newSlug )
);
newValue = setImmutably( newValue, [ 'color', 'gradient' ], undefined );
onChange( newValue );
Expand Down Expand Up @@ -450,6 +450,16 @@ export default function BackgroundImagePanel( {
key: 'gradient',
label: __( 'Gradient' ),
inheritedValue: inheritedGradient,
inheritedSlug: extractPresetSlug(
inheritedValue?.background?.gradient ??
inheritedValue?.color?.gradient,
'gradient'
),
userSlug: extractPresetSlug(
value?.background?.gradient ??
value?.color?.gradient,
'gradient'
),
setValue: setGradient,
userValue: currentGradient,
isGradient: true,
Expand Down Expand Up @@ -487,6 +497,14 @@ export default function BackgroundImagePanel( {
key: 'gradient',
label: __( 'Gradient' ),
inheritedValue: legacyColorGradient,
inheritedSlug: extractPresetSlug(
inheritedValue?.color?.gradient,
'gradient'
),
userSlug: extractPresetSlug(
value?.color?.gradient,
'gradient'
),
setValue: setLegacyColorGradient,
userValue: userLegacyColorGradient,
isGradient: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ function ColorGradientTab( {
colorValue={ isGradient ? undefined : displayed }
colorSlug={ isGradient ? undefined : displayedSlug }
gradientValue={ isGradient ? displayed : undefined }
gradientSlug={ isGradient ? displayedSlug : undefined }
onColorChange={ isGradient ? undefined : onChange }
onGradientChange={ isGradient ? onChange : undefined }
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ export default function ColorPanel( {
newValue.elements[ name ].color.gradient = undefined;
onChange( newValue );
};
const setElementGradient = ( newGradient ) => {
const setElementGradient = ( newGradient, newSlug ) => {
const newValue = setImmutably(
value,
[ 'elements', name, 'color', 'gradient' ],
encodeGradientValue( newGradient )
encodeGradientValue( newGradient, newSlug )
);
newValue.elements[ name ].color.background = undefined;
onChange( newValue );
Expand Down Expand Up @@ -516,6 +516,14 @@ export default function ColorPanel( {
key: 'gradient',
label: __( 'Gradient' ),
inheritedValue: elementGradient,
inheritedSlug: extractPresetSlug(
inheritedValue?.elements?.[ name ]?.color?.gradient,
'gradient'
),
userSlug: extractPresetSlug(
value?.elements?.[ name ]?.color?.gradient,
'gradient'
),
setValue: setElementGradient,
userValue: elementGradientUserColor,
isGradient: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,14 @@ export function useColorGradientSettings( settings ) {
);
const decodeValue = ( rawValue ) =>
getValueFromVariable( { settings }, '', rawValue );
const encodeGradientValue = ( gradientValue ) => {
// When a slug is provided it is used directly: two presets can share the
// same gradient string, and matching by string alone would collapse them
// onto whichever entry appears first. Without a slug, fall back to
// matching the gradient string against the presets.
const encodeGradientValue = ( gradientValue, slug ) => {
if ( slug ) {
return 'var:preset|gradient|' + slug;
}
const allGradients = gradients.flatMap(
( { gradients: originGradients } ) => originGradients
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,89 @@ const baseSettings = {
},
};

describe( 'BackgroundPanel — duplicate gradient preset slug identity', () => {
const SHARED_GRADIENT =
'linear-gradient(135deg, rgb(74, 0, 224) 0%, rgb(142, 45, 226) 100%)';
const duplicateGradientSettings = {
background: {
gradient: true,
},
color: {
gradients: {
theme: [
{
name: 'Dark background',
slug: 'dup-background',
gradient: SHARED_GRADIENT,
},
{
name: 'Dark text',
slug: 'dup-text',
gradient: SHARED_GRADIENT,
},
],
},
},
};

async function openGradientDropdown( user ) {
await user.click( screen.getByRole( 'button', { name: /Gradient/ } ) );
return screen.findAllByRole( 'option' );
}

it( 'commits the inherited preset slug when accepting the preselected inherited gradient', async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(
<BackgroundPanel
value={ {} }
inheritedValue={ {
background: { gradient: 'var:preset|gradient|dup-text' },
} }
settings={ duplicateGradientSettings }
onChange={ onChange }
panelId="test-panel"
/>
);

const swatches = await openGradientDropdown( user );
// swatch[1] ('Dark text') is the preselected inherited option;
// activating it is the "accept inherited value" gesture. The commit
// must carry the inherited slug, not re-encode the shared gradient
// string to whichever duplicate appears first.
await user.click( swatches[ 1 ] );

const result = onChange.mock.calls[ 0 ][ 0 ];
expect( result?.background?.gradient ).toBe(
'var:preset|gradient|dup-text'
);
} );

it( 'marks only the local preset as selected when another preset shares its gradient', async () => {
const user = userEvent.setup();

render(
<BackgroundPanel
value={ {
background: { gradient: 'var:preset|gradient|dup-text' },
} }
inheritedValue={ {} }
settings={ duplicateGradientSettings }
onChange={ jest.fn() }
panelId="test-panel"
/>
);

// swatch[0] = 'Dark background', swatch[1] = 'Dark text'. Selection
// must follow the stored slug; matching by gradient string would
// mark both.
const swatches = await openGradientDropdown( user );
expect( swatches[ 1 ] ).toHaveAttribute( 'aria-selected', 'true' );
expect( swatches[ 0 ] ).toHaveAttribute( 'aria-selected', 'false' );
} );
} );

describe( 'BackgroundPanel — inherited Global Styles label treatment', () => {
describe( 'Background gradient slot', () => {
it( 'applies the local-override className when a local gradient is set', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- `GradientPicker`: Add `selectedSlug` prop for slug-based selection and pass the selected preset's slug to `onChange`, so two presets sharing a gradient keep their identity ([#80554](https://github.com/WordPress/gutenberg/pull/80554)).

### Bug Fixes

- `Autocomplete`: Expose the suggestions list to assistive technology with `aria-controls` and `aria-haspopup`, both required alongside `aria-autocomplete="list"` ([#80403](https://github.com/WordPress/gutenberg/pull/80403)).
Expand Down
24 changes: 21 additions & 3 deletions packages/components/src/gradient-picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing slug as 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 ColorPalette API 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

- 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

@ramonjd ramonjd Jul 23, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we need selectedSlug coz the value is only the CSS gradient string.

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`

Expand Down
71 changes: 43 additions & 28 deletions packages/components/src/gradient-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,48 @@ function SingleOrigin( {
gradients,
onChange,
value,
selectedSlug,
...additionalProps
}: PickerProps< GradientObject > ) {
const gradientOptions = useMemo( () => {
return gradients.map( ( { gradient, name, slug }, index ) => (
<CircularOptionPicker.Option
key={ slug }
value={ gradient }
isSelected={ value === gradient }
tooltipText={
name ||
// translators: %s: gradient code e.g: "linear-gradient(90deg, rgba(98,16,153,1) 0%, rgba(172,110,22,1) 100%);".
sprintf( __( 'Gradient code: %s' ), gradient )
}
style={ { color: 'rgba( 0,0,0,0 )', background: gradient } }
onClick={
value === gradient
? clearGradient
: () => onChange( gradient, index )
}
aria-label={
name
? // translators: %s: The name of the gradient e.g: "Angular red to blue".
sprintf( __( 'Gradient: %s' ), name )
: // translators: %s: gradient code e.g: "linear-gradient(90deg, rgba(98,16,153,1) 0%, rgba(172,110,22,1) 100%);".
sprintf( __( 'Gradient code: %s' ), gradient )
}
/>
) );
}, [ gradients, value, onChange, clearGradient ] );
return gradients.map( ( { gradient, name, slug }, index ) => {
// When a non-empty selectedSlug is provided, selection is decided
// strictly by slug, which keeps two entries with the same gradient
// value apart. Otherwise selection falls back to matching the
// gradient value.
const isSelected = selectedSlug
? slug === selectedSlug
: value === gradient;
return (
<CircularOptionPicker.Option
key={ slug }
value={ gradient }
isSelected={ isSelected }
tooltipText={
name ||
// translators: %s: gradient code e.g: "linear-gradient(90deg, rgba(98,16,153,1) 0%, rgba(172,110,22,1) 100%);".
sprintf( __( 'Gradient code: %s' ), gradient )
}
style={ {
color: 'rgba( 0,0,0,0 )',
background: gradient,
} }
onClick={
isSelected
? clearGradient
: () => onChange( gradient, index, slug )
}
aria-label={
name
? // translators: %s: The name of the gradient e.g: "Angular red to blue".
sprintf( __( 'Gradient: %s' ), name )
: // translators: %s: gradient code e.g: "linear-gradient(90deg, rgba(98,16,153,1) 0%, rgba(172,110,22,1) 100%);".
sprintf( __( 'Gradient code: %s' ), gradient )
}
/>
);
} );
}, [ gradients, value, onChange, clearGradient, selectedSlug ] );
return (
<CircularOptionPicker.OptionGroup
className={ className }
Expand All @@ -85,6 +98,7 @@ function MultipleOrigin( {
gradients,
onChange,
value,
selectedSlug,
headingLevel,
}: PickerProps< OriginObject > ) {
const instanceId = useInstanceId( MultipleOrigin );
Expand All @@ -101,10 +115,11 @@ function MultipleOrigin( {
<SingleOrigin
clearGradient={ clearGradient }
gradients={ gradientSet }
onChange={ ( gradient ) =>
onChange( gradient, index )
onChange={ ( gradient, _index, slug ) =>
onChange( gradient, index, slug )
}
value={ value }
selectedSlug={ selectedSlug }
aria-labelledby={ id }
/>
</VStack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ const Template = ( {
<GradientPicker
{ ...props }
value={ gradient }
onChange={ ( ...changeArgs ) => {
setGradient( ...changeArgs );
onChange?.( ...changeArgs );
Comment on lines -102 to -104

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smoke tested story

Kapture.2026-07-23.at.11.44.41.mp4

I think I'll need to update the code examples too.

onChange={ ( currentGradient, index, slug ) => {
setGradient( currentGradient );
onChange?.( currentGradient, index, slug );
} }
/>
);
Expand Down
Loading
Loading