-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Try splitting style book into sections. #68071
Changes from all commits
81f7a75
719aaf8
6f7541e
81b2e37
5c101c2
ad39bce
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 |
---|---|---|
|
@@ -49,8 +49,11 @@ import { | |
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'; | ||
import { GlobalStylesRenderer } from '../global-styles-renderer'; | ||
import { | ||
STYLE_BOOK_COLOR_GROUPS, | ||
STYLE_BOOK_PREVIEW_CATEGORIES, | ||
} from '../style-book/constants'; | ||
|
||
const { | ||
ExperimentalBlockEditorProvider, | ||
|
@@ -89,35 +92,24 @@ const scrollToSection = ( anchorId, iframe ) => { | |
}; | ||
|
||
/** | ||
* Parses a Block Editor navigation path to extract the block name and | ||
* build a style book navigation path. The object can be extended to include a category, | ||
* representing a style book tab/section. | ||
* Parses a Block Editor navigation path to build a style book navigation path. | ||
* The object can be extended to include a category, representing a style book tab/section. | ||
* | ||
* @param {string} path An internal Block Editor navigation path. | ||
* @return {null|{block: string}} An object containing the example to navigate to. | ||
*/ | ||
const getStyleBookNavigationFromPath = ( path ) => { | ||
if ( path && typeof path === 'string' ) { | ||
if ( path === '/' ) { | ||
if ( | ||
path === '/' || | ||
path.startsWith( '/typography' ) || | ||
path.startsWith( '/colors' ) || | ||
path.startsWith( '/blocks' ) | ||
) { | ||
return { | ||
top: true, | ||
}; | ||
} | ||
|
||
if ( path.startsWith( '/typography' ) ) { | ||
return { | ||
block: 'typography', | ||
}; | ||
} | ||
let block = path.includes( '/blocks/' ) | ||
? decodeURIComponent( path.split( '/blocks/' )[ 1 ] ) | ||
: null; | ||
// Default to theme-colors if the path ends with /colors. | ||
block = path.endsWith( '/colors' ) ? 'theme-colors' : block; | ||
|
||
return { | ||
block, | ||
}; | ||
} | ||
return null; | ||
}; | ||
|
@@ -311,29 +303,43 @@ function StyleBook( { | |
) ) } | ||
</Tabs.TabList> | ||
</div> | ||
{ tabs.map( ( tab ) => ( | ||
<Tabs.TabPanel | ||
key={ tab.slug } | ||
tabId={ tab.slug } | ||
focusable={ false } | ||
className="edit-site-style-book__tabpanel" | ||
> | ||
<StyleBookBody | ||
category={ tab.slug } | ||
examples={ examples } | ||
isSelected={ isSelected } | ||
onSelect={ onSelect } | ||
settings={ settings } | ||
sizes={ sizes } | ||
title={ tab.title } | ||
goTo={ goTo } | ||
/> | ||
</Tabs.TabPanel> | ||
) ) } | ||
{ tabs.map( ( tab ) => { | ||
const categoryDefinition = tab.slug | ||
? getTopLevelStyleBookCategories().find( | ||
( _category ) => | ||
_category.slug === tab.slug | ||
) | ||
: null; | ||
const filteredExamples = categoryDefinition | ||
? getExamplesByCategory( | ||
categoryDefinition, | ||
examples | ||
) | ||
: { examples }; | ||
return ( | ||
<Tabs.TabPanel | ||
key={ tab.slug } | ||
tabId={ tab.slug } | ||
focusable={ false } | ||
className="edit-site-style-book__tabpanel" | ||
> | ||
<StyleBookBody | ||
category={ tab.slug } | ||
examples={ filteredExamples } | ||
isSelected={ isSelected } | ||
onSelect={ onSelect } | ||
settings={ settings } | ||
sizes={ sizes } | ||
title={ tab.title } | ||
goTo={ goTo } | ||
/> | ||
</Tabs.TabPanel> | ||
); | ||
} ) } | ||
</Tabs> | ||
) : ( | ||
<StyleBookBody | ||
examples={ examplesForSinglePageUse } | ||
examples={ { examples: examplesForSinglePageUse } } | ||
isSelected={ isSelected } | ||
onClick={ onClick } | ||
onSelect={ onSelect } | ||
|
@@ -405,6 +411,44 @@ export const StyleBookPreview = ( { userConfig = {}, isStatic = false } ) => { | |
const examples = getExamples( colors ); | ||
const examplesForSinglePageUse = getExamplesForSinglePageUse( examples ); | ||
|
||
let previewCategory = null; | ||
if ( section.includes( '/colors' ) ) { | ||
previewCategory = 'colors'; | ||
} else if ( section.includes( '/typography' ) ) { | ||
previewCategory = 'text'; | ||
} else if ( section.includes( '/blocks' ) ) { | ||
previewCategory = 'blocks'; | ||
const blockName = | ||
decodeURIComponent( section ).split( '/blocks/' )[ 1 ]; | ||
if ( | ||
blockName && | ||
examples.find( ( example ) => example.name === blockName ) | ||
) { | ||
previewCategory = blockName; | ||
} | ||
} else if ( ! isStatic ) { | ||
previewCategory = 'overview'; | ||
} | ||
const categoryDefinition = STYLE_BOOK_PREVIEW_CATEGORIES.find( | ||
( category ) => category.slug === previewCategory | ||
); | ||
|
||
// If there's no category definition there may be a single block. | ||
const filteredExamples = categoryDefinition | ||
? getExamplesByCategory( categoryDefinition, examples ) | ||
: { | ||
examples: [ | ||
examples.find( | ||
( example ) => example.name === previewCategory | ||
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.
Some other blocks are listed on #64707. Also there are links to PRs where we created missing block examples. 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. Oh I just fixed that! Blocks without examples should now just show the all blocks page, I think that's the best we can do until we get all the examples added. |
||
), | ||
], | ||
}; | ||
|
||
// If there's no preview category, show all examples. | ||
const displayedExamples = previewCategory | ||
? filteredExamples | ||
: { examples: examplesForSinglePageUse }; | ||
|
||
const { base: baseConfig } = useContext( GlobalStylesContext ); | ||
const goTo = getStyleBookNavigationFromPath( section ); | ||
|
||
|
@@ -435,7 +479,7 @@ export const StyleBookPreview = ( { userConfig = {}, isStatic = false } ) => { | |
<BlockEditorProvider settings={ settings }> | ||
<GlobalStylesRenderer disableRootPadding /> | ||
<StyleBookBody | ||
examples={ examplesForSinglePageUse } | ||
examples={ displayedExamples } | ||
settings={ settings } | ||
goTo={ goTo } | ||
sizes={ sizes } | ||
|
@@ -448,7 +492,6 @@ export const StyleBookPreview = ( { userConfig = {}, isStatic = false } ) => { | |
}; | ||
|
||
export const StyleBookBody = ( { | ||
category, | ||
examples, | ||
isSelected, | ||
onClick, | ||
|
@@ -494,13 +537,6 @@ export const StyleBookBody = ( { | |
if ( hasIframeLoaded && iframeRef?.current ) { | ||
if ( goTo?.top ) { | ||
scrollToSection( 'top', iframeRef?.current ); | ||
return; | ||
} | ||
if ( goTo?.block ) { | ||
scrollToSection( | ||
`example-${ goTo?.block }`, | ||
iframeRef?.current | ||
); | ||
} | ||
} | ||
}, [ iframeRef?.current, goTo, scrollToSection, hasIframeLoaded ] ); | ||
|
@@ -527,8 +563,7 @@ export const StyleBookBody = ( { | |
className={ clsx( 'edit-site-style-book__examples', { | ||
'is-wide': sizes.width > 600, | ||
} ) } | ||
examples={ examples } | ||
category={ category } | ||
filteredExamples={ examples } | ||
label={ | ||
title | ||
? sprintf( | ||
|
@@ -540,24 +575,14 @@ export const StyleBookBody = ( { | |
} | ||
isSelected={ isSelected } | ||
onSelect={ onSelect } | ||
key={ category } | ||
key={ title } | ||
/> | ||
</Iframe> | ||
); | ||
}; | ||
|
||
const Examples = memo( | ||
( { className, examples, category, label, isSelected, onSelect } ) => { | ||
const categoryDefinition = category | ||
? getTopLevelStyleBookCategories().find( | ||
( _category ) => _category.slug === category | ||
) | ||
: null; | ||
|
||
const filteredExamples = categoryDefinition | ||
? getExamplesByCategory( categoryDefinition, examples ) | ||
: { examples }; | ||
|
||
( { className, filteredExamples, label, isSelected, onSelect } ) => { | ||
return ( | ||
<Composite | ||
orientation="vertical" | ||
|
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.
Should
/colors
also be here? Without, there's no scroll to topKapture.2024-12-23.at.08.59.48.mp4
I'll tentatively add 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.
Done in ad39bce