Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index.story.js for recursion provider component #68593

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 (
<Warning>
{ __( 'Block cannot be rendered inside itself.' ) }
</Warning>
);
}

return (
<RecursionProvider uniqueId={ uniqueId } blockName={ blockName }>
<div style={ { padding: 16, border: '1px solid #ccc', margin: 8 } }>
<div>
<strong>{ __( 'Block Info:' ) }</strong>
<div>
{ __( 'UniqueId:' ) } { uniqueId }
</div>
{ blockName && (
<div>
{ __( 'BlockName:' ) } { blockName }
</div>
) }
</div>
<div style={ { marginTop: 8 } }>
{ children || __( 'Default Content' ) }
</div>
{ depth === 0 && (
<RecursiveBlock
uniqueId={ uniqueId }
blockName={ blockName }
depth={ depth + 1 }
>
{ __( 'Nested Content' ) }
</RecursiveBlock>
) }
</div>
</RecursionProvider>
);
};

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 ) => <RecursiveBlock { ...args } />,
};

export const WithCustomBlockName = {
render: ( args ) => <RecursiveBlock { ...args } />,
args: {
uniqueId: 'example-block-2',
blockName: 'core/custom-block',
children: __( 'Custom Block Content' ),
},
};

export const MultipleInstances = {
render: ( args ) => (
<div>
<h3>{ __( 'Multiple Independent Instances' ) }</h3>
<div style={ { display: 'flex', gap: '16px' } }>
<RecursiveBlock { ...args } uniqueId="block-a" />
<RecursiveBlock { ...args } uniqueId="block-b" />
</div>
</div>
),
};

export const RecursionWarning = {
render: ( args ) => (
<RecursionProvider
uniqueId={ args.uniqueId }
blockName={ args.blockName }
>
<div style={ { padding: 16, border: '1px solid #ccc', margin: 8 } }>
<p>{ __( 'Parent Block' ) }</p>
<RecursiveBlock { ...args } />
</div>
</RecursionProvider>
),
};
Loading