Skip to content

Commit 6f752fc

Browse files
committed
Enhance documentation for useRenderAppenderWithLimit hook by adding detailed parameter descriptions and usage examples for default, button type, and custom appenders.
1 parent 54fc023 commit 6f752fc

File tree

1 file changed

+77
-1
lines changed
  • hooks/use-render-appender-with-limit

1 file changed

+77
-1
lines changed

hooks/use-render-appender-with-limit/readme.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ There are times when we need more granular control over how many blocks can be a
44

55
The hook will return either false or the block appender, depending on the maximum limit you pass in. Behind the scenes, it handles the heavy lifting by fetching and counting the inner blocks for your block, then conditionally returning the appender only when allowed.
66

7+
## Parameters
8+
9+
- **`limit`** (`number`): The maximum number of inner blocks allowed. When the current number of inner blocks reaches this limit, the appender will not be rendered.
10+
- **`appender`** (`React.ComponentType`, optional): The React component to render as the block appender when under the limit. Defaults to `InnerBlocks.DefaultBlockAppender` if not provided.
11+
712
## Usage
813

14+
**Default**
915
```js
1016
import { useBlockProps, useInnerBlockProps } from '@wordpress/block-editor';
1117
import { useRenderAppenderWithLimit } from '@10up/block-components';
@@ -23,9 +29,79 @@ function BlockEdit() {
2329
);
2430

2531
return (
26-
<div {...useBlockProps}>
32+
<div {...blockProps}>
2733
<div {...innerBlocksProps}>
2834
</div>
2935
);
3036
}
3137
```
38+
39+
**Button Type Appender**
40+
```js
41+
import { useBlockProps, useInnerBlocksProps, ButtonBlockAppender } from '@wordpress/block-editor';
42+
import { useRenderAppenderWithLimit } from '@10up/block-components';
43+
44+
function BlockEdit(props) {
45+
const { clientId } = props;
46+
const renderAppender = useRenderAppenderWithLimit(4, () => <ButtonBlockAppender rootClientId={clientId} />);
47+
48+
const blockProps = useBlockProps();
49+
const innerBlocksProps = useInnerBlocksProps(
50+
{},
51+
{
52+
...
53+
renderAppender,
54+
},
55+
);
56+
57+
return (
58+
<div {...blockProps}>
59+
<div {...innerBlocksProps}>
60+
</div>
61+
);
62+
}
63+
```
64+
> [!NOTE]
65+
> For appenders that require props (like `ButtonBlockAppender` needing `rootClientId`), pass a function component that returns the appender with the required props.
66+
67+
**Custom Appender**
68+
```js
69+
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
70+
import { Button } from '@wordpress/components';
71+
import { useRenderAppenderWithLimit } from '@10up/block-components';
72+
import { __ } from '@wordpress/i18n';
73+
74+
// Custom appender component
75+
function CustomAppender({ children, ...props }) {
76+
return (
77+
<div className="custom-appender" {...props}>
78+
<Button variant="secondary" className="custom-add-button">
79+
{__('+ Add New Block', 'text-domain')}
80+
</Button>
81+
{children}
82+
</div>
83+
);
84+
}
85+
86+
function BlockEdit() {
87+
const renderAppender = useRenderAppenderWithLimit(4, CustomAppender);
88+
89+
const blockProps = useBlockProps();
90+
const innerBlocksProps = useInnerBlocksProps(
91+
{},
92+
{
93+
...
94+
renderAppender,
95+
},
96+
);
97+
98+
return (
99+
<div {...blockProps}>
100+
<div {...innerBlocksProps}>
101+
</div>
102+
);
103+
}
104+
```
105+
106+
> [!NOTE]
107+
> For a fully functional custom appender that can actually add blocks, you would need to implement the block insertion logic using WordPress's `useBlockEditor` hook or similar APIs. The example above shows the structure, but in practice, you'd typically use one of WordPress's built-in appenders like `ButtonBlockAppender` or `DefaultBlockAppender` for functionality.

0 commit comments

Comments
 (0)