diff --git a/src/components/AutoFill/AutoFill.stories.tsx b/src/components/AutoFill/AutoFill.stories.tsx index 636d925..8777010 100644 --- a/src/components/AutoFill/AutoFill.stories.tsx +++ b/src/components/AutoFill/AutoFill.stories.tsx @@ -1,4 +1,3 @@ -/* eslint-disable react/jsx-no-literals */ import { type Meta, type StoryObj } from '@storybook/react'; import { AutoFill } from './AutoFill.js'; diff --git a/src/components/InfiniteAutoFill/InfiniteAutoFill.mdx b/src/components/InfiniteAutoFill/InfiniteAutoFill.mdx new file mode 100644 index 0000000..e8aee6d --- /dev/null +++ b/src/components/InfiniteAutoFill/InfiniteAutoFill.mdx @@ -0,0 +1,64 @@ +import { Canvas, Meta } from '@storybook/blocks'; +import * as stories from './InfiniteAutoFill.stories'; + + + +# InfiniteAutoFill + +Uses the AutoFill to repeat children to fill the parent element in given axis. In addition to that +it adds the children one more time to the end of the list. This can be useful for example marquee's. + +## Reference + +```ts +interface InfiniteAutoFillProps { + children: ReadonlyArray; + axis?: 'x' | 'y'; // Default: 'x' +} + +function InfiniteAutoFill(props: InfiniteAutoFillProps): ReactElement; +``` + +## InfiniteAutoFill for X axis + +Fills components horizontally + one extra. + +```tsx +function DemoComponent() { + return ( +
+ +
+
Child
+
+
+
+ ); +} +``` + +### Demo + + + +## InfiniteAutoFill for Y axis + +Fills components vertically + one extra. + +```tsx +function DemoComponent() { + return ( +
+ +
+
Child
+
+
+
+ ); +} +``` + +### Demo + + diff --git a/src/components/InfiniteAutoFill/InfiniteAutoFill.stories.tsx b/src/components/InfiniteAutoFill/InfiniteAutoFill.stories.tsx new file mode 100644 index 0000000..d18884a --- /dev/null +++ b/src/components/InfiniteAutoFill/InfiniteAutoFill.stories.tsx @@ -0,0 +1,96 @@ +import { type Meta, type StoryObj } from '@storybook/react'; +import { InfiniteAutoFill } from './InfiniteAutoFill.js'; + +const meta = { + title: 'Components / InfiniteAutoFill', + component: InfiniteAutoFill, +} satisfies Meta; + +type Story = StoryObj; + +export default meta; + +export const Horizontal: Story = { + render() { + return ( +
+ +
+
+
+ +
+
+
+ +
+ ); + }, + args: { + children:
, + }, +}; + +export const Vertical: Story = { + render() { + return ( +
+ +
+
+
+ +
+ ); + }, + args: { + children:
, + }, +}; diff --git a/src/components/InfiniteAutoFill/InfiniteAutoFill.test.tsx b/src/components/InfiniteAutoFill/InfiniteAutoFill.test.tsx new file mode 100644 index 0000000..68cf18b --- /dev/null +++ b/src/components/InfiniteAutoFill/InfiniteAutoFill.test.tsx @@ -0,0 +1,66 @@ +import { render, waitFor } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; +import { InfiniteAutoFill } from './InfiniteAutoFill.js'; + +describe('InfiniteAutoFill', () => { + it('should not crash', async () => { + const result = render( + +
+ , + ); + + expect(result.baseElement.firstElementChild).toBeDefined(); + }); + + it('should fill parent element', async () => { + const result = render( +
+ +
+ +
, + {}, + ); + + waitFor(async () => { + const children = await result.findAllByTestId('child'); + + expect(children.length).toBe(6); + }); + }); + + it('should overflow parent element', async () => { + const result = render( +
+ +
+ +
, + {}, + ); + + waitFor(async () => { + const children = await result.findAllByTestId('child'); + + expect(children.length).toBe(7); + }); + }); + + it('should fill parent element', async () => { + const result = render( +
+ +
+ +
, + {}, + ); + + waitFor(async () => { + const children = await result.findAllByTestId('child'); + + expect(children.length).toBe(6); + }); + }); +}); diff --git a/src/components/InfiniteAutoFill/InfiniteAutoFill.tsx b/src/components/InfiniteAutoFill/InfiniteAutoFill.tsx new file mode 100644 index 0000000..6d7c5a6 --- /dev/null +++ b/src/components/InfiniteAutoFill/InfiniteAutoFill.tsx @@ -0,0 +1,25 @@ +import { type ReactElement, type RefCallback } from 'react'; +import { AutoFill } from '../../index.js'; + +type InfiniteAutoFillChildrenProps = { + ref: RefCallback; +}; + +type InfiniteAutoFillProps = { + children: + | ReactElement + | ReadonlyArray>; + axis?: 'x' | 'y'; +}; + +/** + * Repeats children to fill the parent element in given axis. + */ +export function InfiniteAutoFill({ children, axis = 'x' }: InfiniteAutoFillProps): ReactElement { + return ( + <> + {children} + {children} + + ); +}