Skip to content

Commit

Permalink
Toolbar: Add unstyled variant (#55139)
Browse files Browse the repository at this point in the history
* Toolbar: Add unstyled variant

* Add variants to block editor and remove workarounds

* Update stories

* Add test for new variant

* Update changelog

* Memoize toolbar context

* Clean up and improve CSS

* Add to README and update phrasing

* Implement feedback to simplify and optimize

* Rename and simplify story by reusing default args
  • Loading branch information
brookewp authored Oct 12, 2023
1 parent c39b352 commit d84da61
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
className={ classes }
/* translators: accessibility text for the block toolbar */
aria-label={ __( 'Block tools' ) }
variant={ isFixed ? 'unstyled' : undefined }
{ ...props }
>
{ ! isCollapsed && <BlockToolbar hideDragHandle={ isFixed } /> }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
overflow-y: hidden;
}

border: none;
border-bottom: $border-width solid $gray-200;
border-radius: 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function ToolSelector( props, ref ) {
label={ __( 'Tools' ) }
/>
) }
popoverProps={ { placement: 'bottom-start', variant: undefined } }
popoverProps={ { placement: 'bottom-start' } }
renderContent={ () => (
<>
<NavigableMenu role="menu" aria-label={ __( 'Tools' ) }>
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

- `ConfirmDialog`: Migrate to TypeScript. ([#54954](https://github.com/WordPress/gutenberg/pull/54954)).

### New Features

- `Toolbar`: add new `variant` prop for 'unstyled' option ([#55139](https://github.com/WordPress/gutenberg/pull/55139)).

## 25.9.0 (2023-10-05)

### Enhancements
Expand Down
15 changes: 15 additions & 0 deletions packages/components/src/toolbar/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const meta: Meta< typeof Toolbar > = {
},
argTypes: {
children: { control: { type: null } },
variant: {
options: [ undefined, 'unstyled' ],
control: { type: 'radio' },
},
},
parameters: {
controls: { expanded: true },
Expand Down Expand Up @@ -181,3 +185,14 @@ WithoutGroup.args = {
</>
),
};

/**
* Set the variant to `unstyled` to remove default border styles.
* Otherwise, leave it as `undefined` for default styles.
*/

export const Unstyled = Template.bind( {} );
Unstyled.args = {
...Default.args,
variant: 'unstyled',
};
8 changes: 8 additions & 0 deletions packages/components/src/toolbar/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ describe( 'Toolbar', () => {
screen.getByLabelText( 'control2', { selector: 'button' } )
).toBeInTheDocument();
} );

it( 'should apply the unstyled variant correctly via the `variant` prop', () => {
render( <Toolbar label="blocks" variant="unstyled" /> );

expect( screen.getByRole( 'toolbar' ) ).toHaveClass(
'is-unstyled'
);
} );
} );
} );
9 changes: 9 additions & 0 deletions packages/components/src/toolbar/toolbar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ An accessible label for the toolbar.

- Required: Yes

#### `variant`: `'unstyled' | undefined`

Specifies the toolbar's style.

Leave undefined for the default style. Or `'unstyled'` which removes the border from the toolbar, but keeps the default popover style.

- Required: No
- Default: `undefined`

## Related components

- Toolbar may contain [ToolbarGroup](/packages/components/src/toolbar-group/README.md), [ToolbarButton](/packages/components/src/toolbar-button/README.md) and [ToolbarItem](/packages/components/src/toolbar-Item/README.md) as children.
33 changes: 21 additions & 12 deletions packages/components/src/toolbar/toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ForwardedRef } from 'react';
/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';
import { forwardRef, useMemo } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';

/**
Expand All @@ -19,23 +19,30 @@ import type { ToolbarProps } from './types';
import type { WordPressComponentProps } from '../../context';
import { ContextSystemProvider } from '../../context';

const CONTEXT_SYSTEM_VALUE = {
DropdownMenu: {
variant: 'toolbar',
},
Dropdown: {
variant: 'toolbar',
},
};

function UnforwardedToolbar(
{
className,
label,
variant,
...props
}: WordPressComponentProps< ToolbarProps, 'div', false >,
ref: ForwardedRef< any >
) {
const isVariantDefined = variant !== undefined;
const contextSystemValue = useMemo( () => {
if ( isVariantDefined ) {
return {};
}
return {
DropdownMenu: {
variant: 'toolbar',
},
Dropdown: {
variant: 'toolbar',
},
};
}, [ isVariantDefined ] );

if ( ! label ) {
deprecated( 'Using Toolbar without label prop', {
since: '5.6',
Expand All @@ -55,10 +62,12 @@ function UnforwardedToolbar(
// `ToolbarGroup` already uses components-toolbar for compatibility reasons.
const finalClassName = classnames(
'components-accessible-toolbar',
className
className,
variant && `is-${ variant }`
);

return (
<ContextSystemProvider value={ CONTEXT_SYSTEM_VALUE }>
<ContextSystemProvider value={ contextSystemValue }>
<ToolbarContainer
className={ finalClassName }
label={ label }
Expand Down
9 changes: 9 additions & 0 deletions packages/components/src/toolbar/toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
& > .components-toolbar-group:last-child {
border-right: none;
}

&.is-unstyled {
border: none;

& > .components-toolbar-group {
border-right: none;
}

}
}

.components-accessible-toolbar,
Expand Down
10 changes: 10 additions & 0 deletions packages/components/src/toolbar/toolbar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ export type ToolbarProps = {
* An accessible label for the toolbar.
*/
label: string;
/**
* Specifies the toolbar's style.
*
* Leave undefined for the default style. Or `'unstyled'` which
* removes the border from the toolbar, but keeps the default
* popover style.
*
* @default undefined
*/
variant?: 'unstyled' | undefined;
};
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function HeaderToolbar( { setListViewToggleElement } ) {
className="edit-post-header-toolbar"
aria-label={ toolbarAriaLabel }
shouldUseKeyboardFocusShortcut={ ! blockToolbarCanBeFocused }
variant="unstyled"
>
<div className="edit-post-header-toolbar__left">
<ToolbarItem
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.edit-post-header-toolbar {
display: inline-flex;
align-items: center;
border: none;

// Hide all action buttons except the inserter on mobile.
.edit-post-header-toolbar__left > .components-button {
Expand Down

1 comment on commit d84da61

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in d84da61.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6489392463
📝 Reported issues:

Please sign in to comment.