-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/carousel] Add useAnimationOffsetEffect hook to fix issue wi…
…th incorrect slides offset (#2041)
- Loading branch information
Showing
7 changed files
with
294 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { Carousel } from './Carousel'; | ||
export { useAnimationOffsetEffect } from './use-animation-offset-effect'; | ||
export type { CarouselProps, CarouselStylesNames } from './Carousel'; | ||
export type { Embla } from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useEffect } from 'react'; | ||
import { Embla } from './types'; | ||
|
||
export function useAnimationOffsetEffect(embla: Embla, transitionDuration: number) { | ||
useEffect(() => { | ||
if (embla) { | ||
window.setTimeout(() => { | ||
embla.reInit(); | ||
}, transitionDuration); | ||
} | ||
}, [embla, transitionDuration]); | ||
} |
111 changes: 111 additions & 0 deletions
111
src/mantine-demos/src/demos/carousel/Carousel.demo.animationOffset.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React, { useState } from 'react'; | ||
import { Button, Modal, Group } from '@mantine/core'; | ||
import { Carousel, useAnimationOffsetEffect } from '@mantine/carousel'; | ||
|
||
const code = ` | ||
import { useState } from 'react'; | ||
import { Button, Modal, Group } from '@mantine/core'; | ||
import { Carousel, useAnimationOffsetEffect } from '@mantine/carousel'; | ||
function Demo() { | ||
const TRANSITION_DURATION = 200; | ||
const [opened, setOpened] = useState(false); | ||
const [embla, setEmbla] = useState(null); | ||
useAnimationOffsetEffect(embla, TRANSITION_DURATION); | ||
return ( | ||
<> | ||
<Group position="center"> | ||
<Button onClick={() => setOpened(true)}>Open modal with carousel</Button> | ||
</Group> | ||
<Modal | ||
opened={opened} | ||
size="300px" | ||
padding={0} | ||
transitionDuration={TRANSITION_DURATION} | ||
withCloseButton={false} | ||
onClose={() => setOpened(false)} | ||
> | ||
<Carousel loop getEmblaApi={setEmbla}> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat/cute" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat/angry" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
</Carousel> | ||
</Modal> | ||
</> | ||
); | ||
} | ||
`; | ||
|
||
function Demo() { | ||
const TRANSITION_DURATION = 200; | ||
const [opened, setOpened] = useState(false); | ||
const [embla, setEmbla] = useState(null); | ||
|
||
useAnimationOffsetEffect(embla, TRANSITION_DURATION); | ||
|
||
return ( | ||
<> | ||
<Group position="center"> | ||
<Button onClick={() => setOpened(true)}>Open modal with carousel</Button> | ||
</Group> | ||
<Modal | ||
opened={opened} | ||
size="300px" | ||
padding={0} | ||
transitionDuration={TRANSITION_DURATION} | ||
withCloseButton={false} | ||
onClose={() => setOpened(false)} | ||
> | ||
<Carousel loop getEmblaApi={setEmbla}> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat/cute" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat/angry" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
</Carousel> | ||
</Modal> | ||
</> | ||
); | ||
} | ||
|
||
export const animationOffset: MantineDemo = { | ||
type: 'demo', | ||
component: Demo, | ||
code, | ||
}; |
105 changes: 105 additions & 0 deletions
105
src/mantine-demos/src/demos/carousel/Carousel.demo.brokenAnimation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React, { useState } from 'react'; | ||
import { Button, Modal, Group } from '@mantine/core'; | ||
import { Carousel } from '@mantine/carousel'; | ||
|
||
const code = ` | ||
import { useState } from 'react'; | ||
import { Button, Modal, Group } from '@mantine/core'; | ||
import { Carousel } from '@mantine/carousel'; | ||
function Demo() { | ||
const TRANSITION_DURATION = 200; | ||
const [opened, setOpened] = useState(false); | ||
return ( | ||
<> | ||
<Group position="center"> | ||
<Button onClick={() => setOpened(true)}>Open carousel with broken offset</Button> | ||
</Group> | ||
<Modal | ||
opened={opened} | ||
size="300px" | ||
padding={0} | ||
transitionDuration={TRANSITION_DURATION} | ||
withCloseButton={false} | ||
onClose={() => setOpened(false)} | ||
> | ||
<Carousel loop> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat/cute" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat/angry" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
</Carousel> | ||
</Modal> | ||
</> | ||
); | ||
} | ||
`; | ||
|
||
function Demo() { | ||
const TRANSITION_DURATION = 200; | ||
const [opened, setOpened] = useState(false); | ||
|
||
return ( | ||
<> | ||
<Group position="center"> | ||
<Button onClick={() => setOpened(true)}>Open carousel with broken offset</Button> | ||
</Group> | ||
<Modal | ||
opened={opened} | ||
size="300px" | ||
padding={0} | ||
transitionDuration={TRANSITION_DURATION} | ||
withCloseButton={false} | ||
onClose={() => setOpened(false)} | ||
> | ||
<Carousel loop> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat/cute" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
<Carousel.Slide> | ||
<img | ||
src="https://cataas.com/cat/angry" | ||
alt="" | ||
style={{ width: 300, height: 200, objectFit: 'cover' }} | ||
/> | ||
</Carousel.Slide> | ||
</Carousel> | ||
</Modal> | ||
</> | ||
); | ||
} | ||
|
||
export const brokenAnimation: MantineDemo = { | ||
type: 'demo', | ||
component: Demo, | ||
code, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters