Skip to content

Commit

Permalink
Fix paths to and from global styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
tellthemachines committed Dec 12, 2024
1 parent e308ab0 commit 02a3b12
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { addQueryArgs } from '@wordpress/url';
import GlobalStylesUI from '../global-styles/ui';
import Page from '../page';
import { unlock } from '../../lock-unlock';
// import StyleBook from '../style-book';
// import { STYLE_BOOK_COLOR_GROUPS } from '../style-book/constants';

const { useLocation, useHistory } = unlock( routerPrivateApis );

Expand Down Expand Up @@ -44,15 +42,15 @@ const GlobalStylesPageActions = ( {
);
};

export default function GlobalStylesUIWrapper() {
/**
* Hook to deal with navigation and location state.
*
* @return {Array} The current section and a function to update it.
*/
export const useSection = () => {
const { path, query } = useLocation();

const history = useHistory();
const [ isStyleBookOpened, setIsStyleBookOpened ] = useState(
path.includes( 'styles-stylebook' )
);
const isMobileViewport = useViewportMatch( 'medium', '<' );
const [ section, onChangeSection ] = useMemo( () => {
return useMemo( () => {
return [
query.section ?? '/',
( updatedSection ) => {
Expand All @@ -64,6 +62,16 @@ export default function GlobalStylesUIWrapper() {
},
];
}, [ path, query.section, history ] );
};

export default function GlobalStylesUIWrapper() {
const { path } = useLocation();

const [ isStyleBookOpened, setIsStyleBookOpened ] = useState(
path.includes( 'styles-stylebook' )
);
const isMobileViewport = useViewportMatch( 'medium', '<' );
const [ section, onChangeSection ] = useSection();

return (
<>
Expand All @@ -85,45 +93,6 @@ export default function GlobalStylesUIWrapper() {
onPathChange={ onChangeSection }
/>
</Page>
{ /* { canvas === 'view' && isStyleBookOpened && (
<StyleBook
enableResizing={ false }
showCloseButton={ false }
showTabs={ false }
isSelected={ ( blockName ) =>
// Match '/blocks/core%2Fbutton' and
// '/blocks/core%2Fbutton/typography', but not
// '/blocks/core%2Fbuttons'.
section ===
`/blocks/${ encodeURIComponent( blockName ) }` ||
section.startsWith(
`/blocks/${ encodeURIComponent( blockName ) }/`
)
}
path={ section }
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 ) }`
);
} }
/>
) } */ }
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const stylebookRoute = {
) }
/>
),
preview: <StyleBookPreview />,
mobile: <StyleBookPreview />,
preview: <StyleBookPreview isStatic />,
mobile: <StyleBookPreview isStatic />,
},
};
57 changes: 43 additions & 14 deletions packages/edit-site/src/components/style-book/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();

const isSelected = ( blockName ) => {
// 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 ) ) {
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 02a3b12

Please sign in to comment.