Skip to content

Commit

Permalink
hide inline preview when style book is open
Browse files Browse the repository at this point in the history
  • Loading branch information
madhusudhand committed Dec 21, 2022
1 parent 05bef82 commit e59c130
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
7 changes: 7 additions & 0 deletions packages/components/src/tab-panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function TabPanel( {
tabs,
selectOnMove = true,
initialTabName,
selectedTabName,
orientation = 'horizontal',
activeClass = 'is-active',
onSelect,
Expand Down Expand Up @@ -110,6 +111,12 @@ export function TabPanel( {
}
}, [ tabs, selectedTab?.name, initialTabName, handleTabSelection ] );

useEffect( () => {
if ( selectedTabName ) {
setSelected( selectedTabName );
}
}, [ selectedTabName ] );

return (
<div className={ className }>
<NavigableMenu
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/tab-panel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export type TabPanelProps = {
* If this prop is not set, the first tab will be selected by default.
*/
initialTabName?: string;
/**
* The name of the tab to be selected.
*/
selectedTabName?: string;
/**
* The function called when a tab has been selected.
* It is passed the `tabName` as an argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ import { getBlockType, getBlockFromExample } from '@wordpress/blocks';
import { useResizeObserver } from '@wordpress/compose';
import { __experimentalSpacer as Spacer } from '@wordpress/components';

/**
* Internal dependencies
*/
import { useHasStyleBook } from '../style-book';

const BlockPreviewPanel = ( { name } ) => {
const [
containerResizeListener,
{ width: containerWidth, height: containerHeight },
] = useResizeObserver();
const hasStyleBook = useHasStyleBook();
const blockExample = getBlockType( name )?.example;
const blocks = blockExample && getBlockFromExample( name, blockExample );
const viewportWidth = blockExample?.viewportWidth || containerWidth;
const minHeight = containerHeight;

return ! blockExample ? null : (
return ! blockExample || hasStyleBook ? null : (
<Spacer marginX={ 4 } marginBottom={ 4 }>
<div className="edit-site-global-styles__block-preview-panel">
{ containerResizeListener }
Expand Down
20 changes: 11 additions & 9 deletions packages/edit-site/src/components/global-styles/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
__experimentalUseNavigator as useNavigator,
} from '@wordpress/components';
import { getBlockTypes } from '@wordpress/blocks';
import { useEffect, useState } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -127,17 +128,18 @@ function ContextScreens( { name } ) {
function GlobalStylesStyleBook( { onClose } ) {
const navigator = useNavigator();
const { path } = navigator.location;
const [ selectedBlockPath, setSelectedBlockPath ] = useState( '' );

useEffect( () => {
const pathPrefix = /^\/blocks\//g;
setSelectedBlockPath(
decodeURIComponent( path ).replace( pathPrefix, '' )
);
}, [ path ] );

return (
<StyleBook
isSelected={ ( blockName ) =>
// Match '/blocks/core%2Fbutton' and
// '/blocks/core%2Fbutton/typography', but not
// '/blocks/core%2Fbuttons'.
path === `/blocks/${ encodeURIComponent( blockName ) }` ||
path.startsWith(
`/blocks/${ encodeURIComponent( blockName ) }/`
)
}
selectedBlockPath={ selectedBlockPath }
onSelect={ ( blockName ) => {
// Clear navigator history by going back to the root.
const depth = path.match( /\//g ).length;
Expand Down
27 changes: 26 additions & 1 deletion packages/edit-site/src/components/style-book/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function getExamples() {
return [ headingsExample, ...otherExamples ];
}

function StyleBook( { isSelected, onSelect, onClose } ) {
function StyleBook( { selectedBlockPath, onSelect, onClose } ) {
const [ resizeObserver, sizes ] = useResizeObserver();
const [ textColor ] = useStyle( 'color.text' );
const [ backgroundColor ] = useStyle( 'color.background' );
Expand All @@ -99,6 +99,30 @@ function StyleBook( { isSelected, onSelect, onClose } ) {
} ) ),
[ examples ]
);

// Match 'core/button' and
// 'core/button/typography', but not
// 'core/buttons'.
const isSelected = ( blockName ) =>
selectedBlockPath === blockName ||
selectedBlockPath.startsWith( `${ blockName }/` );

const selectedTab = useMemo(
() =>
(
getCategories()
.filter( ( category ) =>
examples.some(
( example ) =>
example.category === category.slug &&
isSelected( example.name )
)
)
.shift() || {}
).slug,
[ examples, isSelected ]
);

return (
<StyleBookFill>
<section
Expand All @@ -121,6 +145,7 @@ function StyleBook( { isSelected, onSelect, onClose } ) {
<TabPanel
className="edit-site-style-book__tab-panel"
tabs={ tabs }
selectedTabName={ selectedTab }
>
{ ( tab ) => (
<Examples
Expand Down

1 comment on commit e59c130

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3749412645
📝 Reported issues:

Please sign in to comment.