-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
Size Change: +58 B (0%) Total Size: 1.65 MB
ℹ️ View Unchanged
|
* Otherwise, leave it as `undefined` for default styles. | ||
*/ | ||
|
||
export const WithoutStyles = Template.bind( {} ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!
Flaky tests detected in 74d2c30. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6487678810
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd let the API design review to others who already have more context, but in the meantime, I've left some minor drive-by improvement suggestions.
packages/edit-post/src/components/header/header-toolbar/index.js
Outdated
Show resolved
Hide resolved
const CONTEXT_SYSTEM_VALUE = useMemo( () => { | ||
if ( variant !== undefined ) { | ||
return {}; | ||
} | ||
|
||
return { | ||
DropdownMenu: { | ||
variant: 'toolbar', | ||
}, | ||
Dropdown: { | ||
variant: 'toolbar', | ||
}, | ||
}; | ||
}, [ variant ] ); |
There was a problem hiding this comment.
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
:
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 ] ); |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
...props | ||
}: WordPressComponentProps< ToolbarProps, 'div', false >, | ||
ref: ForwardedRef< any > | ||
) { | ||
const CONTEXT_SYSTEM_VALUE = useMemo( () => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Thanks for keeping on this! From just discerning the before and after, this looks right, good job! I'll defer to others for a code review, but let me know if you need me to dive in and test more deeply. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing well for me too. Left a couple of suggestions!
* Otherwise, leave it as `undefined` for default styles. | ||
*/ | ||
|
||
export const WithoutStyles = Template.bind( {} ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice PR @brookewp Looks good to me once the existing feedback from others is addressed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my perspective, this is good to go 👍
Feel free to 🚀 once you address the rest of the feedback (@ciampo brought up a suggestion the Storybook stories)
# Conflicts: # packages/components/CHANGELOG.md
What?
Closes #54868
This covers a missing requirement where the
Toolbar
component shouldn't always use thetoolbar
variant ofPopover
forDropdownMenu
when in the block editor. @jasmussen clarified the requirements:Why?
This was discovered as a bug in #54840 and a fix was added to remove the variant.
As a longer-term solution, @ciampo suggested we add a variant to
Toolbar
so we can remove existing overrides (which also include CSS overrides) and avoid adding more in the future.How?
As suggested, an 'unstyled'
variant
has been added toToolbar
. If this is set, theDropdownMenu
'sPopover
will be set to the default (undefined
) value.This PR also removes the overrides added, the one mentioned above, and CSS overrides. See these changes in the testing steps below.
Testing Instructions
The main thing to look for is that the
Toolbar
components appear as they did before, with no regressions. It should only appear differently in the blocks where an 'unstyled'variant
has been set.BlockContextualToolbar
The only change here should be on smaller screens when the toolbar becomes full width and to a lighter border. Its dropdown should now match:
Otherwise, the block toolbar and its dropdowns should appear as it did before:
HeaderToolbar
The override added in #54840 is removed from
ToolSelector
but is resolved by adding the variant toHeaderToolbar
, which allows us to remove the CSS override added before. This should continue to appear the same as it does in trunk currently.✍️ Dev note
A new prop,
variant
, has been added to the Toolbar component to add more flexibility in styling. This allows consumers to adopt an'unstyled'
option which removes default border styles, so fewer manual overrides will be needed. If the prop isundefined
, the Toolbar will retain its default border styles.