Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- `ListView`: Only move focus into the list when the new `focusOnMount` prop is set, so that a list mounted as a side effect of a selection no longer pulls focus out of the editor canvas ([#81659](https://github.com/WordPress/gutenberg/pull/81659)).
- `RichText`: Handle Enter using the current record, so pressing it right after moving the caret splits at the caret's new position instead of the previous one ([#81696](https://github.com/WordPress/gutenberg/pull/81696)).
- `useInnerBlocksProps`: Resolve the manual grid placement check that disables the standard drop zone against a layout passed through the options, when provided, instead of always using the block edit context layout ([#81120](https://github.com/WordPress/gutenberg/pull/81120)).
- `BorderPanel`: Match a chosen drop shadow against the presets from every origin rather than the first origin that defines any, so theme and default presets keep being stored as `var:preset|shadow|<slug>` once a custom preset exists ([#81346](https://github.com/WordPress/gutenberg/pull/81346)).

### Internal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,18 @@ export default function BorderPanel( {
inheritedShadow !== undefined &&
inheritedShadow !== '';
const shadow = isShadowPlaceholder ? inheritedShadow : localShadow;
const shadowPresets = settings?.shadow?.presets ?? {};
const mergedShadowPresets =
shadowPresets.custom ??
shadowPresets.theme ??
shadowPresets.default ??
[];
// The presets the popover offers, from every origin. A preset the user
// picked has to be stored as a reference to its CSS variable rather than as
// the value it resolves to today, otherwise it stops tracking later edits
// to the preset. Reading the same list the popover renders is what keeps
// the two in step.
const shadowPresets = useShadowPresets( settings );
const setShadow = ( newValue ) => {
const slug = mergedShadowPresets?.find(
// The list runs default, theme, custom, so searching backwards picks
// the most specific origin when more than one defines the same shadow
// value — a newly added custom preset starts out identical to the
// `natural` default one, and it is the custom slug the user means.
const slug = shadowPresets.findLast(
Comment thread
ramonjd marked this conversation as resolved.
Resolved
( { shadow: shadowName } ) => shadowName === newValue
)?.slug;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ const EMPTY_ARRAY = [];

export function ShadowPopoverContainer( { shadow, onShadowChange, settings } ) {
const shadows = useShadowPresets( settings );
const presets = useMemo( () => {
if ( ! shadows.length ) {
return shadows;
}
// The entry that clears the shadow is a display-only affordance with no
// counterpart in `theme.json`. It is added here rather than in
// `useShadowPresets` so that callers mapping a value back to a preset
// never mistake it for one and persist a reference to a CSS variable
// that is never output.
return [
{ name: __( 'Unset' ), slug: 'unset', shadow: 'none' },
...shadows,
];
}, [ shadows ] );

return (
<div className="block-editor-global-styles__shadow-popover-container">
<VStack spacing={ 4 }>
<Heading level={ 5 }>{ __( 'Drop shadow' ) }</Heading>
<ShadowPresets
presets={ shadows }
presets={ presets }
activeShadow={ shadow }
onSelect={ onShadowChange }
/>
Expand Down Expand Up @@ -209,6 +223,18 @@ function renderShadowToggle( shadow, onShadowChange, resetConfig ) {
};
}

/**
* Returns the available shadow presets, from every origin, in the order they
* are presented to the user.
*
* This is the single source of truth for which presets exist: it backs both the
* popover's preset list and the mapping of a chosen shadow back to the preset
* it came from, so what is shown and what is stored cannot drift apart.
*
* @param {Object} settings Theme.json settings for the current context.
*
* @return {Array} The shadow presets.
*/
export function useShadowPresets( settings ) {
return useMemo( () => {
if ( ! settings?.shadow ) {
Expand All @@ -221,21 +247,22 @@ export function useShadowPresets( settings ) {
theme: themeShadows,
custom: customShadows,
} = settings?.shadow?.presets ?? {};
const unsetShadow = {
name: __( 'Unset' ),
slug: 'unset',
shadow: 'none',
};

const shadowPresets = [
const allPresets = [
...( ( defaultPresetsEnabled && defaultShadows ) || EMPTY_ARRAY ),
...( themeShadows || EMPTY_ARRAY ),
...( customShadows || EMPTY_ARRAY ),
];
if ( shadowPresets.length ) {
shadowPresets.unshift( unsetShadow );
}

return shadowPresets;
// More than one origin can define the same slug, but only the most
// specific definition is output as a CSS variable. Keeping the ones it
// overrides would offer swatches that apply a different shadow than
// the one they show.
return allPresets.filter(
( preset, index ) =>
allPresets.findLastIndex(
( { slug } ) => slug === preset.slug
) === index
);
}, [ settings ] );
}
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,233 @@ describe( 'BorderPanel — inherited Global Styles label treatment', () => {
} );
} );
} );

describe( 'BorderPanel — shadow preset persistence', () => {
const selectShadowPreset = async ( user, name ) => {
await user.click(
screen.getByRole( 'button', { name: 'Drop shadow' } )
);
await user.click( screen.getByRole( 'option', { name } ) );
};

const shadowSettings = ( presets, defaultPresets = true ) => ( {
shadow: { defaultPresets, presets },
} );

const defaultPreset = {
name: 'Natural',
slug: 'natural',
shadow: '6px 6px 9px rgba(0, 0, 0, 0.2)',
};
const themePreset = {
name: 'Outlined',
slug: 'outlined',
shadow: '6px 6px 0px -3px rgba(255, 255, 255, 1), 6px 6px rgba(0, 0, 0, 1)',
};
const customPreset = {
name: 'My shadow',
slug: 'my-shadow',
shadow: '0 0 10px rgba(255, 0, 0, 1)',
};

it( 'persists a theme preset as a preset reference when custom presets also exist', async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(
<BorderPanel
value={ {} }
settings={ shadowSettings( {
default: [ defaultPreset ],
theme: [ themePreset ],
custom: [ customPreset ],
} ) }
onChange={ onChange }
panelId="test-panel"
/>
);

await selectShadowPreset( user, 'Outlined' );

expect( onChange ).toHaveBeenCalledWith( {
shadow: 'var:preset|shadow|outlined',
} );
} );

it( 'persists a default preset as a preset reference when custom presets also exist', async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(
<BorderPanel
value={ {} }
settings={ shadowSettings( {
default: [ defaultPreset ],
theme: [ themePreset ],
custom: [ customPreset ],
} ) }
onChange={ onChange }
panelId="test-panel"
/>
);

await selectShadowPreset( user, 'Natural' );

expect( onChange ).toHaveBeenCalledWith( {
shadow: 'var:preset|shadow|natural',
} );
} );

it( 'persists a custom preset as a preset reference', async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(
<BorderPanel
value={ {} }
settings={ shadowSettings( {
default: [ defaultPreset ],
theme: [ themePreset ],
custom: [ customPreset ],
} ) }
onChange={ onChange }
panelId="test-panel"
/>
);

await selectShadowPreset( user, 'My shadow' );

expect( onChange ).toHaveBeenCalledWith( {
shadow: 'var:preset|shadow|my-shadow',
} );
} );

it( 'persists the unset entry as a literal value, not a preset reference', async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(
<BorderPanel
value={ {} }
settings={ shadowSettings( {
theme: [ themePreset ],
custom: [ customPreset ],
} ) }
onChange={ onChange }
panelId="test-panel"
/>
);

await selectShadowPreset( user, 'Unset' );

expect( onChange ).toHaveBeenCalledWith( { shadow: 'none' } );
} );

it( 'does not reference default presets the theme has opted out of', async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(
<BorderPanel
value={ {} }
settings={ shadowSettings(
{
// Same shadow value as the theme preset below, so the
// lookup has to skip it rather than match it first.
default: [
{ ...defaultPreset, shadow: themePreset.shadow },
],
theme: [ themePreset ],
},
false
) }
onChange={ onChange }
panelId="test-panel"
/>
);

await selectShadowPreset( user, 'Outlined' );

// The opted-out preset is not offered, so it is not something the
// user can have meant.
expect(
screen.queryByRole( 'option', { name: 'Natural' } )
).not.toBeInTheDocument();
expect( onChange ).toHaveBeenCalledWith( {
shadow: 'var:preset|shadow|outlined',
} );
} );

it( 'offers only the most specific preset when a slug is defined twice', async () => {
const user = userEvent.setup();
const onChange = jest.fn();

// Custom preset slugs are generated as `shadow-<n>` from the custom
// presets alone, so they can collide with a theme preset's slug.
const themeShadowOne = {
name: 'Theme shadow',
slug: 'shadow-1',
shadow: '0 0 10px rgba(0, 0, 255, 1)',
};
const customShadowOne = {
name: 'Shadow 1',
slug: 'shadow-1',
shadow: '0 0 10px rgba(255, 0, 0, 1)',
};

render(
<BorderPanel
value={ {} }
settings={ shadowSettings( {
theme: [ themeShadowOne ],
custom: [ customShadowOne ],
} ) }
onChange={ onChange }
panelId="test-panel"
/>
);

await user.click(
screen.getByRole( 'button', { name: 'Drop shadow' } )
);

// `--wp--preset--shadow--shadow-1` holds the custom preset's value, so
// offering the theme one would show a shadow it cannot apply.
expect(
screen.queryByRole( 'option', { name: 'Theme shadow' } )
).not.toBeInTheDocument();

await user.click( screen.getByRole( 'option', { name: 'Shadow 1' } ) );

expect( onChange ).toHaveBeenCalledWith( {
shadow: 'var:preset|shadow|shadow-1',
} );
} );

it( 'references the most specific origin when two presets share a value', async () => {
const user = userEvent.setup();
const onChange = jest.fn();

// A custom preset starts out with the same value as the `natural`
// default one, so this is the state right after adding one.
render(
<BorderPanel
value={ {} }
settings={ shadowSettings( {
default: [ defaultPreset ],
custom: [
{ ...customPreset, shadow: defaultPreset.shadow },
],
} ) }
onChange={ onChange }
panelId="test-panel"
/>
);

await selectShadowPreset( user, 'My shadow' );

expect( onChange ).toHaveBeenCalledWith( {
shadow: 'var:preset|shadow|my-shadow',
} );
} );
} );
Loading