-
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
Give style book its own route so it can be linked to directly. #67811
Changes from all commits
ca4b740
fc32d39
72ba66d
67c9d71
005a47e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ import { | |
useContext, | ||
useRef, | ||
useLayoutEffect, | ||
useEffect, | ||
} from '@wordpress/element'; | ||
import { ENTER, SPACE } from '@wordpress/keycodes'; | ||
|
||
|
@@ -47,6 +48,8 @@ import { | |
} from './categories'; | ||
import { getExamples } from './examples'; | ||
import { store as siteEditorStore } from '../../store'; | ||
import { useSection } from '../sidebar-global-styles-wrapper'; | ||
import { STYLE_BOOK_COLOR_GROUPS } from '../style-book/constants'; | ||
|
||
const { | ||
ExperimentalBlockEditorProvider, | ||
|
@@ -346,33 +349,63 @@ function StyleBook( { | |
/** | ||
* Style Book Preview component renders the stylebook without the Editor dependency. | ||
* | ||
* @param {Object} props Component props. | ||
* @param {string} props.path Path to the selected block. | ||
* @param {Object} props.userConfig User configuration. | ||
* @param {Function} props.isSelected Function to check if a block is selected. | ||
* @param {Function} props.onSelect Function to select a block. | ||
* @param {Object} props Component props. | ||
* @param {Object} props.userConfig User configuration. | ||
* @param {boolean} props.isStatic Whether the stylebook is static or clickable. | ||
* @return {Object} Style Book Preview component. | ||
*/ | ||
export const StyleBookPreview = ( { | ||
path = '', | ||
userConfig = {}, | ||
isSelected, | ||
onSelect, | ||
} ) => { | ||
export const StyleBookPreview = ( { userConfig = {}, isStatic = false } ) => { | ||
const siteEditorSettings = useSelect( | ||
( select ) => select( siteEditorStore ).getSettings(), | ||
[] | ||
); | ||
|
||
// Update block editor settings because useMultipleOriginColorsAndGradients fetch colours from there. | ||
dispatch( blockEditorStore ).updateSettings( siteEditorSettings ); | ||
useEffect( () => { | ||
dispatch( blockEditorStore ).updateSettings( siteEditorSettings ); | ||
}, [ siteEditorSettings ] ); | ||
|
||
const [ section, onChangeSection ] = useSection(); | ||
|
||
const isSelected = ( blockName ) => { | ||
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. This is all beside the point, but I was looking into abstracting the stylebook component further so it can be dropped into the edit and view contexts without knowing too much about its own props. I mean, this works too 😄 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. I'm wondering if long term we'll still need the style book in the edit context at all. Reading through #66719, it seems the main reason global styles should stay in that right hand side sidebar in the editor is so folks can tweak styles while building their templates. While revisions should probably be accessible from wherever global styles can be edited, the style book is a more high-level tool so I'd question the usefulness of keeping it in that right hand sidebar. 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.
👍🏻 I'd be for this if only to simplify the all the edit canvas view juggling etc we do. |
||
// Match '/blocks/core%2Fbutton' and | ||
// '/blocks/core%2Fbutton/typography', but not | ||
// '/blocks/core%2Fbuttons'. | ||
return ( | ||
section === `/blocks/${ encodeURIComponent( blockName ) }` || | ||
section.startsWith( | ||
`/blocks/${ encodeURIComponent( blockName ) }/` | ||
) | ||
); | ||
}; | ||
|
||
const onSelect = ( blockName ) => { | ||
if ( | ||
STYLE_BOOK_COLOR_GROUPS.find( | ||
( group ) => group.slug === blockName | ||
) | ||
) { | ||
// Go to color palettes Global Styles. | ||
onChangeSection( '/colors/palette' ); | ||
return; | ||
} | ||
if ( blockName === 'typography' ) { | ||
// Go to typography Global Styles. | ||
onChangeSection( '/typography' ); | ||
return; | ||
} | ||
|
||
// Now go to the selected block. | ||
onChangeSection( `/blocks/${ encodeURIComponent( blockName ) }` ); | ||
}; | ||
|
||
const [ resizeObserver, sizes ] = useResizeObserver(); | ||
const colors = useMultiOriginPalettes(); | ||
const examples = getExamples( colors ); | ||
const examplesForSinglePageUse = getExamplesForSinglePageUse( examples ); | ||
|
||
const { base: baseConfig } = useContext( GlobalStylesContext ); | ||
const goTo = getStyleBookNavigationFromPath( path ); | ||
const goTo = getStyleBookNavigationFromPath( section ); | ||
|
||
const mergedConfig = useMemo( () => { | ||
if ( ! isObjectEmpty( userConfig ) && ! isObjectEmpty( baseConfig ) ) { | ||
|
@@ -404,8 +437,8 @@ export const StyleBookPreview = ( { | |
settings={ settings } | ||
goTo={ goTo } | ||
sizes={ sizes } | ||
isSelected={ isSelected } | ||
onSelect={ onSelect } | ||
isSelected={ ! isStatic ? isSelected : null } | ||
onSelect={ ! isStatic ? onSelect : null } | ||
/> | ||
</BlockEditorProvider> | ||
</div> | ||
|
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.
Something about this hook placement triggers the following warning:
For example, when navigating to an individual block's global style controls, and activating the style book.
It means we might have to wrap the dispatch in a useEffect so the settings are updated after render:
But I'm unsure of the side-effects - will it preserve the original intention to get the updated colors?
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.
Hmm that was merged in the classic stylebook PR though I'm not seeing the same error on trunk. I'll investigate.
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.
Ok so the error is caused by this PR rendering the
StyleBookPreview
component simultaneously with the global styles sidebar, and it seems to be complaining about the fact that we're updating the block editor settings insideStyleBookPreview
while at the same time using those settings to render theBlockPreview
inside the global styles sidebar. That's why the error only occurs when a specific block is selected in global styles and we click over to the stylebook view.Wrapping the settings update in a
useEffect
does seem to fix the issue, so I'll add that in for now.What I don't understand though is why we need to do this settings update in here at all. With the stylebook that renders inside the editor, the block editor settings are already up to date, but I can't work out whereabouts they're being updated.
Another thing to consider is: should we be depending on the block editor settings here, or the site editor ones? Perhaps instead of using
useMultipleOriginColorsAndGradients
we should have an equivalent function that grabs the color palettes from the site editor settings?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 not sure either 🤔 I assumed it's because
useMultiOriginPalettes
needed the updated values here, but I commented the update line out and can't discern any difference when I update the palette in global styles and apply it to a block (the style book updates still).Wasn't the update added as part of the class style book? #66851
I am missing something, that much is clear.
I'd say site settings is right, because the site editor is initialized (and therefore the site editor store) with preloaded theme styles settings.
Conceptually, that sounds legit to me, but I haven't gone deeper than inspecting
useMultipleOriginColorsAndGradients
anduseSettings
. The context is always going to be global styles/theme json, right?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.
It goes a bit beyond theme.json because for classic themes it also picks up anything defined in the
editor-color-palette
andeditor-gradient-presets
theme supports. But that's when those palettes are in the block editor settings already.I think it might be that the block editor settings only get updated when an actual block editor renders. In which case, if we're in the site preview, it makes sense that block editor settings aren't up to date, but site editor settings (which are essentially the same shape) already are.