Skip to content

Commit 623b9bf

Browse files
committed
feat: enhance keyboard navigation for vertical carousel orientation
1 parent b863417 commit 623b9bf

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/components/ui/carousel.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,23 @@ function Carousel({
7979

8080
const handleKeyDown = React.useCallback(
8181
(event: React.KeyboardEvent<HTMLDivElement>) => {
82-
if (event.key === "ArrowLeft") {
82+
if (orientation === "vertical") {
83+
if (event.key === "ArrowUp") {
84+
event.preventDefault()
85+
scrollPrev()
86+
} else if (event.key === "ArrowDown") {
87+
event.preventDefault()
88+
scrollNext()
89+
}
90+
} else if (event.key === "ArrowLeft") {
8391
event.preventDefault()
8492
scrollPrev()
8593
} else if (event.key === "ArrowRight") {
8694
event.preventDefault()
8795
scrollNext()
8896
}
8997
},
90-
[scrollPrev, scrollNext]
98+
[orientation, scrollPrev, scrollNext]
9199
)
92100

93101
React.useEffect(() => {
@@ -102,7 +110,8 @@ function Carousel({
102110
api.on("select", onSelect)
103111

104112
return () => {
105-
api?.off("select", onSelect)
113+
api.off("reInit", onSelect)
114+
api.off("select", onSelect)
106115
}
107116
}, [api, onSelect])
108117

@@ -162,7 +171,7 @@ function CarouselDots({ className, ...props }: React.ComponentProps<"div">) {
162171
<Button
163172
key={scrollSnap}
164173
type="button"
165-
aria-label={`Vai alla slide ${index + 1}`}
174+
aria-label={`Go to slide ${index + 1}`}
166175
aria-current={selectedIndex === index}
167176
onClick={() => api?.scrollTo(index)}
168177
className={cn(

0 commit comments

Comments
 (0)