Skip to content
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

Toolbar: Add unstyled variant #55139

Merged
merged 11 commits into from
Oct 12, 2023
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 @@ -6,6 +6,10 @@

- `Modal`: fix closing when contained iframe is focused ([#51602](https://github.com/WordPress/gutenberg/pull/51602)).

### 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
43 changes: 43 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,42 @@ WithoutGroup.args = {
</>
),
};

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

export const WithoutStyles = Template.bind( {} );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mentioned consistency with Popover...

So if we decide to keep the name 'unstyled', I'll update the story name to match.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll update the story name to match.

Yeah, "Unstyled" sounds like a better name for this story.

Also, I think we could get away with re-using the default story's arguments — ie.

WithoutStyles.args = {
	...Default.args,
	variant: 'unstyled',
};

And even more in general, I wonder if we could avoid adding a new story altogether, since folks could just use the variant controls on existing stories?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could leave the story out. I added it because it's a visual change, so I thought seeing an explicit demo could be helpful. Also, Popover has the same story so I can add another point to my consistency list 😄

But I'm happy to remove it if you don't think it adds enough value.

Copy link
Contributor

@ciampo ciampo Oct 11, 2023

Choose a reason for hiding this comment

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

Let's keep it for now, but maybe rewrite the args to reuse Default.args like suggested above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good!

WithoutStyles.args = {
label: 'Options',
id: 'options-toolbar-without-styles',
variant: 'unstyled',
children: (
<>
<ToolbarGroup>
<ToolbarButton icon={ paragraph } text="Paragraph" />
</ToolbarGroup>
<ToolbarGroup>
<ToolbarButton>Text</ToolbarButton>
<ToolbarButton icon={ formatBold } label="Bold" isPressed />
<ToolbarButton icon={ formatItalic } label="Italic" />
<ToolbarButton icon={ link } label="Link" />
</ToolbarGroup>
<ToolbarGroup
icon={ chevronDown }
title="Align"
isCollapsed
controls={ [
{
icon: alignLeft,
title: 'Align left',
isActive: true,
},
{ icon: alignCenter, title: 'Align center' },
{ icon: alignRight, title: 'Align right' },
] }
/>
</>
),
};
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 render a toolbar without styles if variant has been defined', () => {
brookewp marked this conversation as resolved.
Show resolved Hide resolved
render( <Toolbar label="blocks" variant="unstyled" /> );

expect( screen.getByRole( 'toolbar' ) ).toHaveClass(
'is-unstyled'
);
} );
} );
} );
31 changes: 20 additions & 11 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,
brookewp marked this conversation as resolved.
Show resolved Hide resolved
...props
}: WordPressComponentProps< ToolbarProps, 'div', false >,
ref: ForwardedRef< any >
) {
const CONTEXT_SYSTEM_VALUE = useMemo( () => {
Copy link
Member

Choose a reason for hiding this comment

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

I'd encourage changing CONTEXT_SYSTEM_VALUE to contextSystemValue since this is no longer a module constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm also unfamiliar with the term 'module constants' so thanks for suggesting! My assumption on this change is now there is more than one possible return value vs. before. Is that correct, or is it based on something else?

Copy link
Contributor

Choose a reason for hiding this comment

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

This simply a naming convention: constant values variables are usually UPPERCASE_LIKE_THIS. That was the case for CONTEXT_SYSTEM_VALUE before this PR.

But since it's been moved inside the react component's function, it's no longer a constant. Marin is suggesting that we change the casing (if that's even a word) to a more traditional camelCase

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for elaborating, @ciampo, that's exactly what I meant.

if ( variant !== undefined ) {
return {};
brookewp marked this conversation as resolved.
Show resolved Hide resolved
}

return {
DropdownMenu: {
variant: 'toolbar',
},
Dropdown: {
variant: 'toolbar',
},
};
}, [ variant ] );
Copy link
Member

Choose a reason for hiding this comment

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

CONTEXT_SYSTEM_VALUE will be recalculated on every value, regardless what it is. At the same time, we only care about its declaration if it changes from undefined to any other value, or the other way around.

So, to micro-optimize, I'd recommend declaring that as a value above the useMemo and using that instead of variant:

Suggested change
const CONTEXT_SYSTEM_VALUE = useMemo( () => {
if ( variant !== undefined ) {
return {};
}
return {
DropdownMenu: {
variant: 'toolbar',
},
Dropdown: {
variant: 'toolbar',
},
};
}, [ variant ] );
const isVariantDefined = variant !== undefined;
const CONTEXT_SYSTEM_VALUE = useMemo( () => {
if ( isVariantDefined ) {
return {};
}
return {
DropdownMenu: {
variant: 'toolbar',
},
Dropdown: {
variant: 'toolbar',
},
};
}, [ isVariantDefined ] );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for sharing! I'm unfamiliar with useMemo and implemented it due to a console.warn. So, I'm unsure if I fully understand the difference. 🤔 If you have time to share more or if you know of any examples I could look into to understand further, I'd appreciate it!

Copy link
Contributor

@ciampo ciampo Oct 11, 2023

Choose a reason for hiding this comment

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

The code inside useMemo gets executed only when any of its dependencies change — it is therefore useful when we want to only re-compute something when absolutely necessary, and otherwise use the result from past computations. This is particularly useful for a bunch of reasons:

  • it's great when we need to compute something particularly complex / slow to calculate (not this case)
  • since React components re-render any time one of their props change, by memoizing a value we can try to reduce that number of rerenders. In fact, without useMemo we would be technically creating a new, different object every time

The react docs are a good start if you're interested in learning more

Copy link
Member

Choose a reason for hiding this comment

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

What @ciampo said 👍

In terms of how often it recomputes, useMemo() is very similar to useEffect or useCallback - it will be recomputed only if one of the dependencies change, so we always strive to keep the dependencies as minimal and as static as possible.

Let's consider an example. If variant is a dependency, then the useMemo() will be recompute every time variant changes, for example, if variant was A and gets changed to B, or if it was B and was changed to undefined. However, if we declare const isVariantDefined = variant !== undefined; and then use isVariantDefined as a dependency, useMemo will recompute only if isVariantDefined changes from undefined to anything else, or from anything else to undefined. That means, if variant changes from A to B, isVariantDefined won't change, therefore useMemo() won't recompute.

That being said, it's a micro-optimization, and likely won't have a measurable impact, unless we change the variant prop often for some reason.


if ( ! label ) {
deprecated( 'Using Toolbar without label prop', {
since: '5.6',
Expand All @@ -55,8 +62,10 @@ function UnforwardedToolbar(
// `ToolbarGroup` already uses components-toolbar for compatibility reasons.
const finalClassName = classnames(
'components-accessible-toolbar',
className
className,
variant === undefined ? undefined : `is-${ variant }`
brookewp marked this conversation as resolved.
Show resolved Hide resolved
);

return (
<ContextSystemProvider value={ CONTEXT_SYSTEM_VALUE }>
<ToolbarContainer
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;
brookewp marked this conversation as resolved.
Show resolved Hide resolved
};
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' }
brookewp marked this conversation as resolved.
Show resolved Hide resolved
>
<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
Loading