-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathcarousel.tsx
52 lines (47 loc) · 1.5 KB
/
carousel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as carousel from "@zag-js/carousel"
import { normalizeProps, useMachine } from "@zag-js/react"
import { useId } from "react"
import { HiChevronLeft, HiChevronRight } from "react-icons/hi"
const items = [
"https://tinyurl.com/5b6ka8jd",
"https://tinyurl.com/7rmccdn5",
"https://tinyurl.com/59jxz9uu",
"https://tinyurl.com/6jurv23t",
"https://tinyurl.com/yp4rfum7",
]
export function Carousel(props: Omit<carousel.Props, "id" | "slideCount">) {
const service = useMachine(carousel.machine, {
id: useId(),
slideCount: items.length,
...props,
})
const api = carousel.connect(service, normalizeProps)
return (
<>
<main className="carousel">
<div {...api.getRootProps()}>
<div {...api.getItemGroupProps()}>
{items.map((image, index) => (
<div {...api.getItemProps({ index })} key={index}>
<img src={image} alt={`Slide Image ${index}`} />
</div>
))}
</div>
<div {...api.getControlProps()}>
<button {...api.getPrevTriggerProps()}>
<HiChevronLeft />
</button>
<div {...api.getIndicatorGroupProps()}>
{api.pageSnapPoints.map((_, index) => (
<button key={index} {...api.getIndicatorProps({ index })} />
))}
</div>
<button {...api.getNextTriggerProps()}>
<HiChevronRight />
</button>
</div>
</div>
</main>
</>
)
}