Skip to content

Commit

Permalink
Add reset button to color control (WordPress#67116)
Browse files Browse the repository at this point in the history
* Add reset button to color control
* Apply suggestions from code review.
* Keep focus on the toggle button when hitting reset.
* Apply suggestions from code review.
* Add `exact` to specify which element to resolve.
* Apply suggestions from code review.
* Remove inclusion of `sprintf`

Co-authored-by: juanfra <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: mirka <[email protected]>
Co-authored-by: ciampo <[email protected]>
Co-authored-by: dhruvang21 <[email protected]>
Co-authored-by: jasmussen <[email protected]>
Co-authored-by: jameskoster <[email protected]>
  • Loading branch information
9 people authored and yogeshbhutkar committed Dec 18, 2024
1 parent 771d4e8 commit d9daa21
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
21 changes: 21 additions & 0 deletions packages/block-editor/src/components/colors-gradients/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ $swatch-gap: 12px;
.block-editor-tools-panel-color-gradient-settings__item {
padding: 0;
max-width: 100%;
position: relative;

// Border styles.
border-left: 1px solid $gray-300;
Expand Down Expand Up @@ -120,3 +121,23 @@ $swatch-gap: 12px;
flex-shrink: 0;
}
}

.block-editor-panel-color-gradient-settings__reset {
position: absolute;
right: 0;
top: $grid-unit;
margin: auto $grid-unit auto;
opacity: 0;
transition: opacity 0.1s ease-in-out;
@include reduce-motion("transition");

&.block-editor-panel-color-gradient-settings__reset {
border-radius: $radius-small;
}

.block-editor-panel-color-gradient-settings__dropdown:hover + &,
&:focus,
&:hover {
opacity: 1;
}
}
33 changes: 26 additions & 7 deletions packages/block-editor/src/components/global-styles/color-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Button,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { useCallback } from '@wordpress/element';
import { useCallback, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -30,6 +30,7 @@ import { useColorsPerOrigin, useGradientsPerOrigin } from './hooks';
import { getValueFromVariable, useToolsPanelDropdownMenuProps } from './utils';
import { setImmutably } from '../../utils/object';
import { unlock } from '../../lock-unlock';
import { reset as resetIcon } from '@wordpress/icons';

export function useHasColorPanel( settings ) {
const hasTextPanel = useHasTextPanel( settings );
Expand Down Expand Up @@ -208,6 +209,7 @@ function ColorPanelDropdown( {
} ) {
const currentTab = tabs.find( ( tab ) => tab.userValue !== undefined );
const { key: firstTabKey, ...firstTab } = tabs[ 0 ] ?? {};
const colorGradientDropdownButtonRef = useRef( undefined );
return (
<ToolsPanelItem
className="block-editor-tools-panel-color-gradient-settings__item"
Expand All @@ -228,15 +230,32 @@ function ColorPanelDropdown( {
{ 'is-open': isOpen }
),
'aria-expanded': isOpen,
ref: colorGradientDropdownButtonRef,
};

return (
<Button __next40pxDefaultSize { ...toggleProps }>
<LabeledColorIndicators
indicators={ indicators }
label={ label }
/>
</Button>
<>
<Button { ...toggleProps } __next40pxDefaultSize>
<LabeledColorIndicators
indicators={ indicators }
label={ label }
/>
</Button>
{ hasValue() && (
<Button
__next40pxDefaultSize
label={ __( 'Reset' ) }
className="block-editor-panel-color-gradient-settings__reset"
size="small"
icon={ resetIcon }
onClick={ () => {
resetValue();
// Return focus to parent button
colorGradientDropdownButtonRef.current?.focus();
} }
/>
) }
</>
);
} }
renderContent={ () => (
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/specs/editor/blocks/heading.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ test.describe( 'Heading', () => {
.getByRole( 'region', {
name: 'Editor settings',
} )
.getByRole( 'button', { name: 'Text' } );
.getByRole( 'button', { name: 'Text', exact: true } );

await textColor.click();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ test.describe( 'Style Revisions', () => {
await editor.canvas.locator( 'body' ).click();
await userGlobalStylesRevisions.openStylesPanel();
await page.getByRole( 'button', { name: 'Colors' } ).click();
await page.getByRole( 'button', { name: 'Background' } ).click();
await page
.getByRole( 'button', { name: 'Background', exact: true } )
.click();
await page
.getByRole( 'option', { name: 'Color: Luminous vivid amber' } )
.click( { force: true } );
Expand Down

0 comments on commit d9daa21

Please sign in to comment.