Skip to content

Commit bee2271

Browse files
kvnloodavidmckayv
andauthored
fix(ui): preserve carousel keyboard and accessibility semantics (#728)
* fix(ui): preserve carousel keyboard semantics * fix(home): name the explore carousel * test(ui): cover carousel arrow ownership * Format with biome --------- Co-authored-by: David McKay <david@copilotkit.ai>
1 parent 9722f70 commit bee2271

3 files changed

Lines changed: 80 additions & 11 deletions

File tree

‎app/src/components/ui/carousel.tsx‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ function useCarousel() {
4343
return context;
4444
}
4545

46+
export function carouselKeyboardAction(
47+
orientation: "horizontal" | "vertical",
48+
key: string,
49+
target: EventTarget | null,
50+
): "prev" | "next" | null {
51+
if (
52+
target instanceof HTMLElement &&
53+
(target.matches("input, textarea, select") || target.isContentEditable)
54+
)
55+
return null;
56+
if (orientation === "horizontal") {
57+
if (key === "ArrowLeft") return "prev";
58+
if (key === "ArrowRight") return "next";
59+
} else {
60+
if (key === "ArrowUp") return "prev";
61+
if (key === "ArrowDown") return "next";
62+
}
63+
return null;
64+
}
65+
4666
function Carousel({
4767
orientation = "horizontal",
4868
opts,
@@ -78,15 +98,17 @@ function Carousel({
7898

7999
const handleKeyDown = React.useCallback(
80100
(event: React.KeyboardEvent<HTMLDivElement>) => {
81-
if (event.key === "ArrowLeft") {
82-
event.preventDefault();
83-
scrollPrev();
84-
} else if (event.key === "ArrowRight") {
85-
event.preventDefault();
86-
scrollNext();
87-
}
101+
const action = carouselKeyboardAction(
102+
orientation,
103+
event.key,
104+
event.target,
105+
);
106+
if (!action) return;
107+
event.preventDefault();
108+
if (action === "prev") scrollPrev();
109+
else scrollNext();
88110
},
89-
[scrollPrev, scrollNext],
111+
[orientation, scrollPrev, scrollNext],
90112
);
91113

92114
React.useEffect(() => {
@@ -101,7 +123,8 @@ function Carousel({
101123
api.on("select", onSelect);
102124

103125
return () => {
104-
api?.off("select", onSelect);
126+
api.off("reInit", onSelect);
127+
api.off("select", onSelect);
105128
};
106129
}, [api, onSelect]);
107130

‎app/src/routes/_authed/_app/index.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,14 @@ function RouteComponent() {
154154
// Wins over `failed`: a failed background refetch does not clear TanStack Query's
155155
// cached `data`, so a stale carousel here beats an error card claiming there is
156156
// nothing to explore, which would be false while this list is still populated.
157-
<Carousel opts={{ align: "start" }}>
157+
<Carousel
158+
opts={{ align: "start" }}
159+
aria-labelledby="explore-agents-heading"
160+
>
158161
<div className="flex flex-row items-center justify-between gap-4">
159-
<h2 className="font-bold text-lg">Explore agents</h2>
162+
<h2 id="explore-agents-heading" className="font-bold text-lg">
163+
Explore agents
164+
</h2>
160165
{/*
161166
* `static` undoes the primitive's own absolute placement, which parks these either
162167
* side of the row and off the edge of a prose-width column. They belong on the
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { afterAll, beforeAll, expect, test } from "bun:test";
2+
import { GlobalRegistrator } from "@happy-dom/global-registrator";
3+
import { carouselKeyboardAction } from "../src/components/ui/carousel";
4+
5+
beforeAll(() => GlobalRegistrator.register());
6+
afterAll(() => GlobalRegistrator.unregister());
7+
8+
test("horizontal carousels use left and right arrows", () => {
9+
const target = document.createElement("div");
10+
expect(carouselKeyboardAction("horizontal", "ArrowLeft", target)).toBe(
11+
"prev",
12+
);
13+
expect(carouselKeyboardAction("horizontal", "ArrowRight", target)).toBe(
14+
"next",
15+
);
16+
expect(carouselKeyboardAction("horizontal", "ArrowUp", target)).toBeNull();
17+
});
18+
19+
test("vertical carousels use up and down arrows", () => {
20+
const target = document.createElement("div");
21+
expect(carouselKeyboardAction("vertical", "ArrowUp", target)).toBe("prev");
22+
expect(carouselKeyboardAction("vertical", "ArrowDown", target)).toBe("next");
23+
expect(carouselKeyboardAction("vertical", "ArrowLeft", target)).toBeNull();
24+
});
25+
26+
test("editable descendants keep their native arrow keys", () => {
27+
for (const tag of ["input", "textarea", "select"] as const) {
28+
const target = document.createElement(tag);
29+
expect(
30+
carouselKeyboardAction("horizontal", "ArrowLeft", target),
31+
).toBeNull();
32+
expect(
33+
carouselKeyboardAction("horizontal", "ArrowRight", target),
34+
).toBeNull();
35+
}
36+
const editable = document.createElement("div");
37+
editable.contentEditable = "true";
38+
expect(
39+
carouselKeyboardAction("horizontal", "ArrowLeft", editable),
40+
).toBeNull();
41+
});

0 commit comments

Comments
 (0)