-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Try responsive global block styles with states #77513
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
9e7b2c6
b147b5b
81008e5
c14fbdf
f7cd7d3
fad1be4
2546fc9
c54bbe8
7c3dfd6
2011fd3
d0a4785
c185831
e5e4f4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,12 @@ export function useStyle< T = any >( | |
| state?: string | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If as per the other comment, a user is in the future able to select a combination of states, then this would need to be updated to support more than just a string. It's probably not too complicate though, and object like |
||
| ) { | ||
| const { user, base, merged, onChange } = useContext( GlobalStylesContext ); | ||
| const isPseudoSelectorState = state?.startsWith( ':' ); | ||
| const pseudoSelectorState = isPseudoSelectorState ? state : undefined; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming here is a bit confusing. Might we in the future have states that are neither responsive nor pseudo-selectors? If not it might not be a problem |
||
| const stylePath = | ||
| state && ! isPseudoSelectorState | ||
| ? [ path, state ].filter( Boolean ).join( '.' ) | ||
| : path; | ||
|
|
||
| let sourceValue = merged; | ||
| if ( readFrom === 'base' ) { | ||
|
|
@@ -61,23 +67,33 @@ export function useStyle< T = any >( | |
| sourceValue = user; | ||
| } | ||
|
|
||
| const styleValue = useMemo( () => { | ||
| const styleValue = useMemo< T | undefined >( () => { | ||
| const rawValue = getStyle< T >( | ||
| sourceValue, | ||
| path, | ||
| stylePath, | ||
| blockName, | ||
| shouldDecodeEncode | ||
| ); | ||
| if ( state ) { | ||
| return ( rawValue as any )?.[ state ] ?? {}; | ||
| if ( pseudoSelectorState ) { | ||
| return ( | ||
| ( rawValue as Record< string, T | undefined > )?.[ | ||
| pseudoSelectorState | ||
| ] ?? ( {} as T ) | ||
| ); | ||
| } | ||
| return rawValue; | ||
| }, [ sourceValue, path, blockName, shouldDecodeEncode, state ] ); | ||
| }, [ | ||
| sourceValue, | ||
| stylePath, | ||
| blockName, | ||
| shouldDecodeEncode, | ||
| pseudoSelectorState, | ||
| ] ); | ||
|
|
||
| const setStyleValue = useCallback( | ||
| ( newValue: T | undefined ) => { | ||
| let valueToSet: any = newValue; | ||
| if ( state ) { | ||
| if ( pseudoSelectorState ) { | ||
| const fullCurrentValue = getStyle( | ||
| user, | ||
| path, | ||
|
|
@@ -86,18 +102,18 @@ export function useStyle< T = any >( | |
| ); | ||
| valueToSet = { | ||
| ...( fullCurrentValue as object ), | ||
| [ state ]: newValue, | ||
| [ pseudoSelectorState ]: newValue, | ||
| }; | ||
| } | ||
| const newGlobalStyles = setStyle< any >( | ||
| user, | ||
| path, | ||
| stylePath, | ||
| valueToSet, | ||
| blockName | ||
| ); | ||
| onChange( newGlobalStyles ); | ||
| }, | ||
| [ user, onChange, path, blockName, state ] | ||
| [ user, onChange, path, stylePath, blockName, pseudoSelectorState ] | ||
| ); | ||
|
|
||
| return [ styleValue, setStyleValue ] as const; | ||
|
|
||
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 the other part of the fix for the Button variation selector issue mentioned above.