// Reference: https://github.com/WordPress/gutenberg/blob/release/16.4/packages/block-editor/src/components/block-preview/auto.js /* eslint-disable @woocommerce/dependency-group */ /* eslint-disable @typescript-eslint/ban-ts-comment */ /** * External dependencies */ import { useResizeObserver, pure, useRefEffect } from '@wordpress/compose'; import { useMemo } from '@wordpress/element'; import { Disabled } from '@wordpress/components'; import { __unstableEditorStyles as EditorStyles, __unstableIframe as Iframe, BlockList, // @ts-ignore No types for this exist yet. } from '@wordpress/block-editor'; const MAX_HEIGHT = 2000; // @ts-ignore No types for this exist yet. const { Provider: DisabledProvider } = Disabled.Context; // This is used to avoid rendering the block list if the sizes change. let MemoizedBlockList: typeof BlockList | undefined; export type ScaledBlockPreviewProps = { viewportWidth?: number; containerWidth: number; minHeight?: number; settings: { styles: string[]; [ key: string ]: unknown; }; additionalStyles: string; onClickNavigationItem: ( event: MouseEvent ) => void; }; function ScaledBlockPreview( { viewportWidth, containerWidth, minHeight, settings, additionalStyles, onClickNavigationItem, }: ScaledBlockPreviewProps ) { if ( ! viewportWidth ) { viewportWidth = containerWidth; } const [ contentResizeListener, { height: contentHeight } ] = useResizeObserver(); // Avoid scrollbars for pattern previews. const editorStyles = useMemo( () => { return [ { css: 'body{height:auto;overflow:hidden;border:none;padding:0;}', __unstableType: 'presets', }, ...settings.styles, ]; }, [ settings.styles ] ); // Initialize on render instead of module top level, to avoid circular dependency issues. MemoizedBlockList = MemoizedBlockList || pure( BlockList ); const scale = containerWidth / viewportWidth; return ( ); } export const AutoHeightBlockPreview = ( props: Omit< ScaledBlockPreviewProps, 'containerWidth' > ) => { const [ containerResizeListener, { width: containerWidth } ] = useResizeObserver(); return ( <>
{ containerResizeListener }
{ !! containerWidth && ( ) }
); };