-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Toolbar: Add unstyled variant #55139
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
Changes from 1 commit
b0e8e1f
4b2cafc
b2c3662
b8abb3f
084f41f
b53ea1d
a5e772a
a542f36
4de2267
fc0c029
74d2c30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -19,21 +19,6 @@ import type { ToolbarProps } from './types'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { WordPressComponentProps } from '../../context'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { ContextSystemProvider } from '../../context'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const CONTEXT_SYSTEM_VALUE = ( variant: string | undefined ) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( variant !== undefined ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DropdownMenu: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| variant: 'toolbar', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dropdown: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| variant: 'toolbar', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function UnforwardedToolbar( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -43,6 +28,21 @@ function UnforwardedToolbar( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }: WordPressComponentProps< ToolbarProps, 'div', false >, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ref: ForwardedRef< any > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const CONTEXT_SYSTEM_VALUE = useMemo( () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( variant !== undefined ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
brookewp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DropdownMenu: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| variant: 'toolbar', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dropdown: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| variant: 'toolbar', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [ 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
useMemowe 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.
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_VALUEtocontextSystemValuesince 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_VALUEbefore 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.