-
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 3 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 |
---|---|---|
|
@@ -47,6 +47,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 +348,60 @@ 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 ); | ||
|
||
const [ section, onChangeSection ] = useSection(); | ||
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. 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: useEffect( () => {
dispatch( blockEditorStore ).updateSettings( siteEditorSettings );
}, [ siteEditorSettings ] ); 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Ok so the error is caused by this PR rendering the Wrapping the settings update in a 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 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 not sure either 🤔 I assumed it's because 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 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.
It goes a bit beyond theme.json because for classic themes it also picks up anything defined in the 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. |
||
|
||
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 +433,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.
I think these replacements aren't quite right, as they don't take into account any subroutes or params of
/styles
, e.g.,/styles?section=%2Fvariations
To test, navigation to styles > browse styles, then activate the style book. Try to toggle it off or navigation back to the main menu.
Kapture.2024-12-17.at.12.18.00.mp4
Would adding and removing the preview query arg be enough?
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.
Oooh maybe, let me try!
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.
Yes it worked 😄 thanks!