diff --git a/src/components/AutoFill/AutoFill.mdx b/src/components/AutoFill/AutoFill.mdx index c607b23..536fd74 100644 --- a/src/components/AutoFill/AutoFill.mdx +++ b/src/components/AutoFill/AutoFill.mdx @@ -48,7 +48,7 @@ Fills components vertically. function DemoComponent() { return (
- +
Child
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/AutoFill/AutoFill.tsx b/src/components/AutoFill/AutoFill.tsx index 2d4e798..8fc3c44 100644 --- a/src/components/AutoFill/AutoFill.tsx +++ b/src/components/AutoFill/AutoFill.tsx @@ -18,7 +18,7 @@ type AutoFillChildrenProps = { ref: RefCallback; }; -type AutoFillProps = { +export type AutoFillProps = { children: | ReactElement | ReadonlyArray>; diff --git a/src/components/InfiniteAutoFill/InfiniteAutoFill.mdx b/src/components/InfiniteAutoFill/InfiniteAutoFill.mdx new file mode 100644 index 0000000..237a0c6 --- /dev/null +++ b/src/components/InfiniteAutoFill/InfiniteAutoFill.mdx @@ -0,0 +1,65 @@ +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 marquees +or infinite carousels. + +## 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..d19043c --- /dev/null +++ b/src/components/InfiniteAutoFill/InfiniteAutoFill.tsx @@ -0,0 +1,16 @@ +import { type ComponentProps, type ReactElement } from 'react'; +import { AutoFill } from '../../index.js'; + +type InfiniteAutoFillProps = ComponentProps; + +/** + * Repeats children to fill the parent element in given axis. + */ +export function InfiniteAutoFill({ children, axis }: InfiniteAutoFillProps): ReactElement { + return ( + <> + {children} + {children} + + ); +}