diff --git a/packages/block-editor/src/components/recursion-provider/stories/index.story.js b/packages/block-editor/src/components/recursion-provider/stories/index.story.js new file mode 100644 index 00000000000000..d0a0a2af40b6b0 --- /dev/null +++ b/packages/block-editor/src/components/recursion-provider/stories/index.story.js @@ -0,0 +1,138 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { Warning } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import { RecursionProvider, useHasRecursion } from '../'; + +// Example recursive block component for the stories +const RecursiveBlock = ( { uniqueId, blockName, children, depth = 0 } ) => { + const hasRecursion = useHasRecursion( uniqueId, blockName ); + + if ( hasRecursion ) { + return ( + + { __( 'Block cannot be rendered inside itself.' ) } + + ); + } + + return ( + +
+
+ { __( 'Block Info:' ) } +
+ { __( 'UniqueId:' ) } { uniqueId } +
+ { blockName && ( +
+ { __( 'BlockName:' ) } { blockName } +
+ ) } +
+
+ { children || __( 'Default Content' ) } +
+ { depth === 0 && ( + + { __( 'Nested Content' ) } + + ) } +
+
+ ); +}; + +const meta = { + title: 'BlockEditor/RecursionProvider', + component: RecursionProvider, + parameters: { + docs: { + canvas: { sourceState: 'shown' }, + description: { + component: __( + 'A provider component that helps prevent infinite recursion in blocks by tracking rendered instances.' + ), + }, + }, + }, + argTypes: { + uniqueId: { + control: { type: 'text' }, + description: __( 'Unique identifier for the block instance' ), + table: { + type: { summary: 'any' }, + }, + }, + blockName: { + control: { type: 'text' }, + description: __( 'Optional block name' ), + table: { + type: { summary: 'string' }, + defaultValue: { summary: '' }, + }, + }, + children: { + control: { type: 'text' }, + description: __( 'Child content to be rendered' ), + table: { + type: { summary: 'Element' }, + }, + }, + }, + args: { + uniqueId: 'example-block-1', + blockName: '', + children: __( 'Sample Content' ), + }, +}; + +export default meta; + +export const Default = { + render: ( args ) => , +}; + +export const WithCustomBlockName = { + render: ( args ) => , + args: { + uniqueId: 'example-block-2', + blockName: 'core/custom-block', + children: __( 'Custom Block Content' ), + }, +}; + +export const MultipleInstances = { + render: ( args ) => ( +
+

{ __( 'Multiple Independent Instances' ) }

+
+ + +
+
+ ), +}; + +export const RecursionWarning = { + render: ( args ) => ( + +
+

{ __( 'Parent Block' ) }

+ +
+
+ ), +};