// Reference: https://github.com/WordPress/gutenberg/blob/94ff2ba55379d9ad7f6bed743b20b85ff26cf56d/packages/block-editor/src/components/block-patterns-list/index.js#L1
/* eslint-disable @woocommerce/dependency-group */
/* eslint-disable @typescript-eslint/ban-ts-comment */
/**
* External dependencies
*/
import {
VisuallyHidden,
__unstableComposite as Composite,
__unstableUseCompositeState as useCompositeState,
__unstableCompositeItem as CompositeItem,
Tooltip,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { store as blockEditorStore } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import BlockPreview from './block-preview';
const WithToolTip = ( { showTooltip, title, children } ) => {
if ( showTooltip ) {
return { children };
}
return <>{ children }>;
};
function BlockPattern( { pattern, onClick, onHover, composite, showTooltip } ) {
const { blocks, viewportWidth } = pattern;
const instanceId = useInstanceId( BlockPattern );
const descriptionId = `block-editor-block-patterns-list__item-description-${ instanceId }`;
const originalSettings = useSelect(
( select ) => select( blockEditorStore ).getSettings(),
[]
);
const settings = useMemo(
() => ( { ...originalSettings, __unstableIsPreviewMode: true } ),
[ originalSettings ]
);
return (
{
onClick( pattern, blocks );
onHover?.( null );
} }
onMouseEnter={ () => {
onHover?.( pattern );
} }
onMouseLeave={ () => onHover?.( null ) }
aria-label={ pattern.title }
aria-describedby={
pattern.description ? descriptionId : undefined
}
>
{ ! showTooltip && (
{ pattern.title }
) }
{ !! pattern.description && (
{ pattern.description }
) }
);
}
function BlockPatternPlaceholder() {
return (
);
}
function BlockPatternList( {
isDraggable,
blockPatterns,
shownPatterns,
onHover,
onClickPattern,
orientation,
label = __( 'Block Patterns', 'woocommerce' ),
showTitlesAsTooltip,
} ) {
const composite = useCompositeState( { orientation } );
return (
{ blockPatterns.map( ( pattern ) => {
const isShown = shownPatterns.includes( pattern );
return isShown ? (
) : (
);
} ) }
);
}
export default BlockPatternList;