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 1 commit
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
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
64 changes: 64 additions & 0 deletions src/components/InfiniteAutoFill/InfiniteAutoFill.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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 marquee's.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it adds the children one more time to the end of the list. This can be useful for example marquee's.
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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing y axis.

<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);
});
});
});
25 changes: 25 additions & 0 deletions src/components/InfiniteAutoFill/InfiniteAutoFill.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type ReactElement, type RefCallback } from 'react';
import { AutoFill } from '../../index.js';

type InfiniteAutoFillChildrenProps = {
ref: RefCallback<unknown>;
};

type InfiniteAutoFillProps = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use the AutoFillProps instead of copying them, so this will be in sync if the others are updated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type InfiniteAutoFillProps = {
type InfiniteAutoFillProps = ComponentProps<typeof AutoFill>;

children:
| ReactElement<InfiniteAutoFillChildrenProps>
| ReadonlyArray<ReactElement<InfiniteAutoFillChildrenProps>>;
axis?: 'x' | 'y';
};

/**
* Repeats children to fill the parent element in given axis.
*/
export function InfiniteAutoFill({ children, axis = 'x' }: InfiniteAutoFillProps): ReactElement {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to "copy" the default here? Making it undefined is enough IMO.

return (
<>
<AutoFill axis={axis}>{children}</AutoFill>
{children}
</>
);
}
Loading