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

Create infinite autofill component #305

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/components/AutoFill/AutoFill.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Fills components vertically.
function DemoComponent() {
return (
<div style={{ height: 1000, outline: '1px solid black' }}>
<AutoFill>
<AutoFill axis="y">
<div style={{ padding: 15 }}>
<div style={{ width: 50, outline: '1px solid red' }}>Child</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/components/AutoFill/AutoFill.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable react/jsx-no-literals */
import { type Meta, type StoryObj } from '@storybook/react';
import { AutoFill } from './AutoFill.js';

Expand Down
2 changes: 1 addition & 1 deletion src/components/AutoFill/AutoFill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type AutoFillChildrenProps = {
ref: RefCallback<unknown>;
};

type AutoFillProps = {
export type AutoFillProps = {
children:
| ReactElement<AutoFillChildrenProps>
| ReadonlyArray<ReactElement<AutoFillChildrenProps>>;
Expand Down
65 changes: 65 additions & 0 deletions src/components/InfiniteAutoFill/InfiniteAutoFill.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Canvas, Meta } from '@storybook/blocks';
import * as stories from './InfiniteAutoFill.stories';

<Meta title="Components / InfiniteAutoFill" />

# 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<ReactNode>;
axis?: 'x' | 'y'; // Default: 'x'
}

function InfiniteAutoFill(props: InfiniteAutoFillProps): ReactElement;
```

## InfiniteAutoFill for X axis

Fills components horizontally + one extra.

```tsx
function DemoComponent() {
return (
<div style={{ width: 1000, display: 'grid', outline: '1px solid black' }}>
<InfiniteAutoFill>
<div style={{ padding: 15 }}>
<div style={{ width: 50, outline: '1px solid red' }}>Child</div>
</div>
</InfiniteAutoFill>
</div>
);
}
```

### Demo

<Canvas of={stories.Horizontal} />

## InfiniteAutoFill for Y axis

Fills components vertically + one extra.

```tsx
function DemoComponent() {
return (
<div style={{ height: 1000, outline: '1px solid black' }}>
<InfiniteAutoFill axis="y">
<div style={{ padding: 15 }}>
<div style={{ width: 50, outline: '1px solid red' }}>Child</div>
</div>
</InfiniteAutoFill>
</div>
);
}
```

### Demo

<Canvas of={stories.Vertical} />
96 changes: 96 additions & 0 deletions src/components/InfiniteAutoFill/InfiniteAutoFill.stories.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof meta>;

export default meta;

export const Horizontal: Story = {
render() {
return (
<div
style={{
width: 520,
display: 'flex',
outline: '1px solid blue',
}}
>
<InfiniteAutoFill axis="x">
<div
style={{
padding: 15,
flex: '0 1 auto',
}}
>
<div
style={{
width: 50,
height: 50,
outline: '1px solid red',
}}
/>
</div>

<div
style={{
padding: 15,
flex: '0 1 auto',
}}
>
<div
style={{
width: 75,
height: 50,
outline: '1px solid yellow',
}}
/>
</div>
</InfiniteAutoFill>
</div>
);
},
args: {
children: <div />,
},
};

export const Vertical: Story = {
render() {
return (
<div
style={{
height: 520,
width: 80,
display: 'flex',
flexDirection: 'column',
outline: '1px solid blue',
}}
>
<InfiniteAutoFill axis="y">
<div
style={{
padding: 15,
flex: '0 1 auto',
}}
>
<div
style={{
width: 50,
height: 50,
outline: '1px solid red',
}}
/>
</div>
</InfiniteAutoFill>
</div>
);
},
args: {
children: <div />,
},
};
66 changes: 66 additions & 0 deletions src/components/InfiniteAutoFill/InfiniteAutoFill.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
<InfiniteAutoFill>
<div />
</InfiniteAutoFill>,
);

expect(result.baseElement.firstElementChild).toBeDefined();
});

it('should fill parent element', async () => {
const result = render(
<div style={{ width: 500 }}>
<InfiniteAutoFill>
<div data-testid="child" style={{ width: 100 }} />
</InfiniteAutoFill>
</div>,
{},
);

waitFor(async () => {
const children = await result.findAllByTestId('child');

expect(children.length).toBe(6);
});
});

it('should overflow parent element', async () => {
const result = render(
<div style={{ width: 500 }}>
<InfiniteAutoFill>
<div data-testid="child" style={{ width: 90 }} />
</InfiniteAutoFill>
</div>,
{},
);

waitFor(async () => {
const children = await result.findAllByTestId('child');

expect(children.length).toBe(7);
});
});

it('should fill parent element', async () => {
const result = render(
<div style={{ height: 500 }}>
<InfiniteAutoFill axis="y">
<div data-testid="child" style={{ height: 100 }} />
</InfiniteAutoFill>
</div>,
{},
);

waitFor(async () => {
const children = await result.findAllByTestId('child');

expect(children.length).toBe(6);
});
});
});
16 changes: 16 additions & 0 deletions src/components/InfiniteAutoFill/InfiniteAutoFill.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { type ComponentProps, type ReactElement } from 'react';
import { AutoFill } from '../../index.js';

type InfiniteAutoFillProps = ComponentProps<typeof AutoFill>;

/**
* Repeats children to fill the parent element in given axis.
*/
export function InfiniteAutoFill({ children, axis }: InfiniteAutoFillProps): ReactElement {
return (
<>
<AutoFill axis={axis}>{children}</AutoFill>
{children}
</>
);
}
Loading