diff --git a/src/components/AutoFill/AutoFill.mdx b/src/components/AutoFill/AutoFill.mdx index c607b23..2135f65 100644 --- a/src/components/AutoFill/AutoFill.mdx +++ b/src/components/AutoFill/AutoFill.mdx @@ -12,6 +12,7 @@ Repeats children to fill the parent element in given axis. ```ts interface AutoFillProps { children: ReadonlyArray; + additionalCloneCount?: number; // Default: 0 axis?: 'x' | 'y'; // Default: 'x' } diff --git a/src/components/AutoFill/AutoFill.stories.tsx b/src/components/AutoFill/AutoFill.stories.tsx index 636d925..9e7746c 100644 --- a/src/components/AutoFill/AutoFill.stories.tsx +++ b/src/components/AutoFill/AutoFill.stories.tsx @@ -60,6 +60,55 @@ export const Horizontal: Story = { }, }; +export const HorizontalWithAdditionalCloneCount: Story = { + render() { + return ( +
+ +
+
+
+ +
+
+
+ +
+ ); + }, + args: { + children:
, + }, +}; + export const Vertical: Story = { render() { return ( diff --git a/src/components/AutoFill/AutoFill.tsx b/src/components/AutoFill/AutoFill.tsx index 2d4e798..23b7541 100644 --- a/src/components/AutoFill/AutoFill.tsx +++ b/src/components/AutoFill/AutoFill.tsx @@ -22,13 +22,18 @@ type AutoFillProps = { children: | ReactElement | ReadonlyArray>; + additionalCloneCount?: number; axis?: 'x' | 'y'; }; /** * Repeats children to fill the parent element in given axis. */ -export function AutoFill({ children, axis = 'x' }: AutoFillProps): ReactElement { +export function AutoFill({ + children, + additionalCloneCount = 0, + axis = 'x', +}: AutoFillProps): ReactElement { const childrenRef = useRef | null>([]); const [repeatCount, setRepeatCount] = useState(0); @@ -43,11 +48,15 @@ export function AutoFill({ children, axis = 'x' }: AutoFillProps): ReactElement const { clientWidth: parentClientWidth, clientHeight: parentClientHeight } = parentElement; if (axis === 'x') { - setRepeatCount(Math.ceil(parentClientWidth / (offsetLeft + clientWidth))); + setRepeatCount( + Math.ceil(parentClientWidth / (offsetLeft + clientWidth)) + additionalCloneCount, + ); } else { - setRepeatCount(Math.ceil(parentClientHeight / (offsetTop + clientHeight))); + setRepeatCount( + Math.ceil(parentClientHeight / (offsetTop + clientHeight)) + additionalCloneCount, + ); } - }, [axis]); + }, [additionalCloneCount, axis]); useEffect(updateRepeatCount, [updateRepeatCount, children]); useEventListener(globalThis.window, 'resize', useRafCallback(updateRepeatCount));