-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
||
## 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
<div style={{ padding: 15 }}> | ||
<div style={{ width: 50, outline: '1px solid red' }}>Child</div> | ||
</div> | ||
</InfiniteAutoFill> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
### Demo | ||
|
||
<Canvas of={stories.Vertical} /> |
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 />, | ||
}, | ||
}; |
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); | ||
}); | ||
}); | ||
}); |
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 = { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||||||
</> | ||||||
); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.