@hauktui/registry 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/accordion/accordion.tsx +146 -0
- package/components/accordion/index.ts +2 -0
- package/components/alert/alert.tsx +69 -0
- package/components/alert/index.ts +2 -0
- package/components/alert-dialog/alert-dialog.tsx +185 -0
- package/components/alert-dialog/index.ts +2 -0
- package/components/avatar/avatar.tsx +57 -0
- package/components/avatar/index.ts +2 -0
- package/components/avatar-group/avatar-group.tsx +144 -0
- package/components/avatar-group/index.ts +2 -0
- package/components/badge/badge.tsx +52 -0
- package/components/badge/index.ts +2 -0
- package/components/banner/banner.tsx +407 -0
- package/components/banner/index.ts +2 -0
- package/components/breadcrumb/breadcrumb.tsx +58 -0
- package/components/breadcrumb/index.ts +2 -0
- package/components/button/button.tsx +114 -0
- package/components/button/index.ts +2 -0
- package/components/calendar/calendar.tsx +250 -0
- package/components/calendar/index.ts +2 -0
- package/components/card/card.tsx +88 -0
- package/components/card/index.ts +2 -0
- package/components/carousel/carousel.tsx +185 -0
- package/components/carousel/index.ts +2 -0
- package/components/chart/chart.tsx +189 -0
- package/components/chart/index.ts +2 -0
- package/components/checkbox/checkbox.tsx +98 -0
- package/components/checkbox/index.ts +2 -0
- package/components/code-block/code-block.tsx +214 -0
- package/components/code-block/index.ts +2 -0
- package/components/collapsible/collapsible.tsx +123 -0
- package/components/collapsible/index.ts +2 -0
- package/components/color-picker/color-picker.tsx +211 -0
- package/components/color-picker/index.ts +2 -0
- package/components/combobox/combobox.tsx +275 -0
- package/components/combobox/index.ts +2 -0
- package/components/command/command.tsx +304 -0
- package/components/command/index.ts +2 -0
- package/components/confirm-dialog/confirm-dialog.tsx +140 -0
- package/components/confirm-dialog/index.ts +2 -0
- package/components/context-menu/context-menu.tsx +188 -0
- package/components/context-menu/index.ts +2 -0
- package/components/countdown/countdown.tsx +165 -0
- package/components/countdown/index.ts +2 -0
- package/components/data-table/data-table.tsx +256 -0
- package/components/data-table/index.ts +2 -0
- package/components/date-picker/date-picker.tsx +280 -0
- package/components/date-picker/index.ts +2 -0
- package/components/dialog/dialog.tsx +84 -0
- package/components/dialog/index.ts +2 -0
- package/components/drawer/drawer.tsx +141 -0
- package/components/drawer/index.ts +2 -0
- package/components/dropdown-menu/dropdown-menu.tsx +188 -0
- package/components/dropdown-menu/index.ts +2 -0
- package/components/empty/empty.tsx +107 -0
- package/components/empty/index.ts +2 -0
- package/components/field/field.tsx +83 -0
- package/components/field/index.ts +2 -0
- package/components/form/form.tsx +202 -0
- package/components/form/index.ts +8 -0
- package/components/hover-card/hover-card.tsx +72 -0
- package/components/hover-card/index.ts +2 -0
- package/components/input-otp/index.ts +2 -0
- package/components/input-otp/input-otp.tsx +176 -0
- package/components/kbd/index.ts +2 -0
- package/components/kbd/kbd.tsx +30 -0
- package/components/label/index.ts +2 -0
- package/components/label/label.tsx +56 -0
- package/components/list/index.ts +2 -0
- package/components/list/list.tsx +247 -0
- package/components/menubar/index.ts +2 -0
- package/components/menubar/menubar.tsx +220 -0
- package/components/navigation-menu/index.ts +6 -0
- package/components/navigation-menu/navigation-menu.tsx +216 -0
- package/components/pagination/index.ts +2 -0
- package/components/pagination/pagination.tsx +158 -0
- package/components/password-input/index.ts +2 -0
- package/components/password-input/password-input.tsx +198 -0
- package/components/popover/index.ts +2 -0
- package/components/popover/popover.tsx +102 -0
- package/components/progress/index.ts +2 -0
- package/components/progress/progress.tsx +73 -0
- package/components/radio-group/index.ts +2 -0
- package/components/radio-group/radio-group.tsx +167 -0
- package/components/resizable/index.ts +2 -0
- package/components/resizable/resizable.tsx +141 -0
- package/components/scroll-area/index.ts +2 -0
- package/components/scroll-area/scroll-area.tsx +133 -0
- package/components/select/index.ts +2 -0
- package/components/select/select.tsx +185 -0
- package/components/separator/index.ts +2 -0
- package/components/separator/separator.tsx +63 -0
- package/components/sheet/index.ts +2 -0
- package/components/sheet/sheet.tsx +137 -0
- package/components/sidebar/index.ts +2 -0
- package/components/sidebar/sidebar.tsx +225 -0
- package/components/skeleton/index.ts +2 -0
- package/components/skeleton/skeleton.tsx +64 -0
- package/components/slider/index.ts +2 -0
- package/components/slider/slider.tsx +128 -0
- package/components/spinner/index.ts +2 -0
- package/components/spinner/spinner.tsx +57 -0
- package/components/stat/index.ts +2 -0
- package/components/stat/stat.tsx +138 -0
- package/components/stepper/index.ts +2 -0
- package/components/stepper/stepper.tsx +219 -0
- package/components/switch/index.ts +2 -0
- package/components/switch/switch.tsx +102 -0
- package/components/table/index.ts +2 -0
- package/components/table/table.tsx +242 -0
- package/components/tabs/index.ts +2 -0
- package/components/tabs/tabs.tsx +240 -0
- package/components/tag-input/index.ts +2 -0
- package/components/tag-input/tag-input.tsx +180 -0
- package/components/terminal/index.ts +2 -0
- package/components/terminal/terminal.tsx +162 -0
- package/components/text-input/index.ts +2 -0
- package/components/text-input/text-input.tsx +179 -0
- package/components/textarea/index.ts +2 -0
- package/components/textarea/textarea.tsx +206 -0
- package/components/timeline/index.ts +2 -0
- package/components/timeline/timeline.tsx +167 -0
- package/components/toast/index.ts +2 -0
- package/components/toast/toast.tsx +93 -0
- package/components/toggle/index.ts +2 -0
- package/components/toggle/toggle.tsx +114 -0
- package/components/toggle-group/index.ts +2 -0
- package/components/toggle-group/toggle-group.tsx +176 -0
- package/components/tooltip/index.ts +2 -0
- package/components/tooltip/tooltip.tsx +65 -0
- package/components/tree-view/index.ts +2 -0
- package/components/tree-view/tree-view.tsx +245 -0
- package/components/typography/index.ts +12 -0
- package/components/typography/typography.tsx +154 -0
- package/dist/index.d.ts +102 -0
- package/dist/index.js +938 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
- package/registry.json +923 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { Box, Text, useInput } from "ink";
|
|
3
|
+
import type { Tokens } from "@hauktui/tokens";
|
|
4
|
+
import { useTokens, useFocusable } from "@hauktui/primitives-ink";
|
|
5
|
+
import { stableId } from "@hauktui/core";
|
|
6
|
+
|
|
7
|
+
export interface CalendarProps {
|
|
8
|
+
/** Selected date */
|
|
9
|
+
value?: Date;
|
|
10
|
+
/** Callback when date is selected */
|
|
11
|
+
onChange?: (date: Date) => void;
|
|
12
|
+
/** Minimum selectable date */
|
|
13
|
+
minDate?: Date;
|
|
14
|
+
/** Maximum selectable date */
|
|
15
|
+
maxDate?: Date;
|
|
16
|
+
/** Custom tokens override */
|
|
17
|
+
tokens?: Tokens;
|
|
18
|
+
/** Focus ID for focus management */
|
|
19
|
+
focusId?: string;
|
|
20
|
+
/** Show week numbers */
|
|
21
|
+
showWeekNumbers?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const DAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
|
|
25
|
+
const MONTHS = [
|
|
26
|
+
"January",
|
|
27
|
+
"February",
|
|
28
|
+
"March",
|
|
29
|
+
"April",
|
|
30
|
+
"May",
|
|
31
|
+
"June",
|
|
32
|
+
"July",
|
|
33
|
+
"August",
|
|
34
|
+
"September",
|
|
35
|
+
"October",
|
|
36
|
+
"November",
|
|
37
|
+
"December",
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
function getDaysInMonth(year: number, month: number): number {
|
|
41
|
+
return new Date(year, month + 1, 0).getDate();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getFirstDayOfMonth(year: number, month: number): number {
|
|
45
|
+
return new Date(year, month, 1).getDay();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function Calendar({
|
|
49
|
+
value,
|
|
50
|
+
onChange,
|
|
51
|
+
minDate,
|
|
52
|
+
maxDate,
|
|
53
|
+
tokens: propTokens,
|
|
54
|
+
focusId,
|
|
55
|
+
showWeekNumbers = false,
|
|
56
|
+
}: CalendarProps): React.ReactElement {
|
|
57
|
+
const contextTokens = useTokens();
|
|
58
|
+
const tokens = propTokens ?? contextTokens;
|
|
59
|
+
const id = focusId ?? stableId("calendar");
|
|
60
|
+
const { isFocused } = useFocusable(id);
|
|
61
|
+
|
|
62
|
+
const today = new Date();
|
|
63
|
+
const [viewDate, setViewDate] = useState(value || today);
|
|
64
|
+
const [selectedDay, setSelectedDay] = useState(
|
|
65
|
+
value?.getDate() || today.getDate()
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const year = viewDate.getFullYear();
|
|
69
|
+
const month = viewDate.getMonth();
|
|
70
|
+
const daysInMonth = getDaysInMonth(year, month);
|
|
71
|
+
const firstDay = getFirstDayOfMonth(year, month);
|
|
72
|
+
|
|
73
|
+
useInput(
|
|
74
|
+
(input, key) => {
|
|
75
|
+
if (!isFocused) return;
|
|
76
|
+
|
|
77
|
+
if (key.leftArrow || input === "h") {
|
|
78
|
+
if (selectedDay > 1) {
|
|
79
|
+
setSelectedDay(selectedDay - 1);
|
|
80
|
+
} else {
|
|
81
|
+
// Go to previous month
|
|
82
|
+
const prevMonth = new Date(year, month - 1, 1);
|
|
83
|
+
setViewDate(prevMonth);
|
|
84
|
+
setSelectedDay(
|
|
85
|
+
getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth())
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
} else if (key.rightArrow || input === "l") {
|
|
89
|
+
if (selectedDay < daysInMonth) {
|
|
90
|
+
setSelectedDay(selectedDay + 1);
|
|
91
|
+
} else {
|
|
92
|
+
// Go to next month
|
|
93
|
+
setViewDate(new Date(year, month + 1, 1));
|
|
94
|
+
setSelectedDay(1);
|
|
95
|
+
}
|
|
96
|
+
} else if (key.upArrow || input === "k") {
|
|
97
|
+
if (selectedDay > 7) {
|
|
98
|
+
setSelectedDay(selectedDay - 7);
|
|
99
|
+
} else {
|
|
100
|
+
const prevMonth = new Date(year, month - 1, 1);
|
|
101
|
+
const prevDays = getDaysInMonth(
|
|
102
|
+
prevMonth.getFullYear(),
|
|
103
|
+
prevMonth.getMonth()
|
|
104
|
+
);
|
|
105
|
+
setViewDate(prevMonth);
|
|
106
|
+
setSelectedDay(prevDays - (7 - selectedDay));
|
|
107
|
+
}
|
|
108
|
+
} else if (key.downArrow || input === "j") {
|
|
109
|
+
if (selectedDay + 7 <= daysInMonth) {
|
|
110
|
+
setSelectedDay(selectedDay + 7);
|
|
111
|
+
} else {
|
|
112
|
+
setViewDate(new Date(year, month + 1, 1));
|
|
113
|
+
setSelectedDay(selectedDay + 7 - daysInMonth);
|
|
114
|
+
}
|
|
115
|
+
} else if (key.return) {
|
|
116
|
+
const selected = new Date(year, month, selectedDay);
|
|
117
|
+
if (minDate && selected < minDate) return;
|
|
118
|
+
if (maxDate && selected > maxDate) return;
|
|
119
|
+
onChange?.(selected);
|
|
120
|
+
} else if (input === "[") {
|
|
121
|
+
// Previous month
|
|
122
|
+
setViewDate(new Date(year, month - 1, 1));
|
|
123
|
+
setSelectedDay(1);
|
|
124
|
+
} else if (input === "]") {
|
|
125
|
+
// Next month
|
|
126
|
+
setViewDate(new Date(year, month + 1, 1));
|
|
127
|
+
setSelectedDay(1);
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
{ isActive: isFocused }
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
// Build calendar grid
|
|
134
|
+
const weeks: React.ReactElement[] = [];
|
|
135
|
+
|
|
136
|
+
// Header
|
|
137
|
+
weeks.push(
|
|
138
|
+
React.createElement(
|
|
139
|
+
Box,
|
|
140
|
+
{ key: "header", gap: 1 },
|
|
141
|
+
showWeekNumbers
|
|
142
|
+
? React.createElement(Text, { color: tokens.colors.muted }, "Wk")
|
|
143
|
+
: null,
|
|
144
|
+
...DAYS.map((day) =>
|
|
145
|
+
React.createElement(
|
|
146
|
+
Text,
|
|
147
|
+
{ key: day, color: tokens.colors.muted, bold: true },
|
|
148
|
+
day
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
// Days
|
|
155
|
+
let week: React.ReactElement[] = [];
|
|
156
|
+
let weekNum = 1;
|
|
157
|
+
|
|
158
|
+
// Add empty cells for days before first of month
|
|
159
|
+
for (let i = 0; i < firstDay; i++) {
|
|
160
|
+
week.push(React.createElement(Text, { key: `empty-${i}` }, " "));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
for (let day = 1; day <= daysInMonth; day++) {
|
|
164
|
+
const isSelected = day === selectedDay;
|
|
165
|
+
const isToday =
|
|
166
|
+
day === today.getDate() &&
|
|
167
|
+
month === today.getMonth() &&
|
|
168
|
+
year === today.getFullYear();
|
|
169
|
+
const date = new Date(year, month, day);
|
|
170
|
+
const isDisabled =
|
|
171
|
+
(minDate && date < minDate) || (maxDate && date > maxDate);
|
|
172
|
+
|
|
173
|
+
week.push(
|
|
174
|
+
React.createElement(
|
|
175
|
+
Text,
|
|
176
|
+
{
|
|
177
|
+
key: `day-${day}`,
|
|
178
|
+
color: isDisabled
|
|
179
|
+
? tokens.colors.muted
|
|
180
|
+
: isSelected
|
|
181
|
+
? tokens.colors.accent
|
|
182
|
+
: isToday
|
|
183
|
+
? tokens.colors.warning
|
|
184
|
+
: tokens.colors.fg,
|
|
185
|
+
bold: isSelected || isToday,
|
|
186
|
+
inverse: isSelected,
|
|
187
|
+
dimColor: isDisabled,
|
|
188
|
+
},
|
|
189
|
+
day.toString().padStart(2, " ")
|
|
190
|
+
)
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
if ((firstDay + day) % 7 === 0 || day === daysInMonth) {
|
|
194
|
+
// Fill remaining cells in last week
|
|
195
|
+
while (week.length < 7 + (showWeekNumbers ? 1 : 0)) {
|
|
196
|
+
week.push(
|
|
197
|
+
React.createElement(Text, { key: `fill-${week.length}` }, " ")
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
weeks.push(
|
|
202
|
+
React.createElement(
|
|
203
|
+
Box,
|
|
204
|
+
{ key: `week-${weekNum}`, gap: 1 },
|
|
205
|
+
showWeekNumbers
|
|
206
|
+
? React.createElement(
|
|
207
|
+
Text,
|
|
208
|
+
{ color: tokens.colors.muted, dimColor: true },
|
|
209
|
+
weekNum.toString().padStart(2, " ")
|
|
210
|
+
)
|
|
211
|
+
: null,
|
|
212
|
+
...week
|
|
213
|
+
)
|
|
214
|
+
);
|
|
215
|
+
week = [];
|
|
216
|
+
weekNum++;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return React.createElement(
|
|
221
|
+
Box,
|
|
222
|
+
{
|
|
223
|
+
flexDirection: "column",
|
|
224
|
+
borderStyle: "round",
|
|
225
|
+
borderColor: isFocused ? tokens.colors.focus : tokens.colors.border,
|
|
226
|
+
paddingX: 1,
|
|
227
|
+
paddingY: 0,
|
|
228
|
+
},
|
|
229
|
+
// Month/Year header
|
|
230
|
+
React.createElement(
|
|
231
|
+
Box,
|
|
232
|
+
{ justifyContent: "space-between", marginBottom: 0 },
|
|
233
|
+
React.createElement(Text, { color: tokens.colors.muted }, "["),
|
|
234
|
+
React.createElement(
|
|
235
|
+
Text,
|
|
236
|
+
{ bold: true, color: tokens.colors.fg },
|
|
237
|
+
`${MONTHS[month]} ${year}`
|
|
238
|
+
),
|
|
239
|
+
React.createElement(Text, { color: tokens.colors.muted }, "]")
|
|
240
|
+
),
|
|
241
|
+
// Calendar grid
|
|
242
|
+
...weeks,
|
|
243
|
+
// Footer hint
|
|
244
|
+
React.createElement(
|
|
245
|
+
Text,
|
|
246
|
+
{ color: tokens.colors.muted, dimColor: true },
|
|
247
|
+
"←↑↓→ navigate [/] month Enter select"
|
|
248
|
+
)
|
|
249
|
+
);
|
|
250
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Box, Text } from "ink";
|
|
3
|
+
import type { Tokens } from "@hauktui/tokens";
|
|
4
|
+
import { useTokens } from "@hauktui/primitives-ink";
|
|
5
|
+
|
|
6
|
+
export interface CardProps {
|
|
7
|
+
/** Card title */
|
|
8
|
+
title?: string;
|
|
9
|
+
/** Card description */
|
|
10
|
+
description?: string;
|
|
11
|
+
/** Card content */
|
|
12
|
+
children?: React.ReactNode;
|
|
13
|
+
/** Card footer */
|
|
14
|
+
footer?: React.ReactNode;
|
|
15
|
+
/** Custom tokens override */
|
|
16
|
+
tokens?: Tokens;
|
|
17
|
+
/** Border style */
|
|
18
|
+
borderStyle?: "single" | "double" | "round" | "bold" | "classic";
|
|
19
|
+
/** Card width */
|
|
20
|
+
width?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function Card({
|
|
24
|
+
title,
|
|
25
|
+
description,
|
|
26
|
+
children,
|
|
27
|
+
footer,
|
|
28
|
+
tokens: propTokens,
|
|
29
|
+
borderStyle = "round",
|
|
30
|
+
width,
|
|
31
|
+
}: CardProps): React.ReactElement {
|
|
32
|
+
const contextTokens = useTokens();
|
|
33
|
+
const tokens = propTokens ?? contextTokens;
|
|
34
|
+
|
|
35
|
+
return React.createElement(
|
|
36
|
+
Box,
|
|
37
|
+
{
|
|
38
|
+
flexDirection: "column",
|
|
39
|
+
borderStyle,
|
|
40
|
+
borderColor: tokens.colors.border,
|
|
41
|
+
paddingX: 1,
|
|
42
|
+
paddingY: 0,
|
|
43
|
+
width,
|
|
44
|
+
},
|
|
45
|
+
// Header
|
|
46
|
+
title || description
|
|
47
|
+
? React.createElement(
|
|
48
|
+
Box,
|
|
49
|
+
{ flexDirection: "column", marginBottom: children ? 1 : 0 },
|
|
50
|
+
title
|
|
51
|
+
? React.createElement(
|
|
52
|
+
Text,
|
|
53
|
+
{ color: tokens.colors.fg, bold: true },
|
|
54
|
+
title
|
|
55
|
+
)
|
|
56
|
+
: null,
|
|
57
|
+
description
|
|
58
|
+
? React.createElement(
|
|
59
|
+
Text,
|
|
60
|
+
{ color: tokens.colors.muted, dimColor: true },
|
|
61
|
+
description
|
|
62
|
+
)
|
|
63
|
+
: null
|
|
64
|
+
)
|
|
65
|
+
: null,
|
|
66
|
+
// Content
|
|
67
|
+
children
|
|
68
|
+
? React.createElement(Box, { flexDirection: "column" }, children)
|
|
69
|
+
: null,
|
|
70
|
+
// Footer
|
|
71
|
+
footer
|
|
72
|
+
? React.createElement(
|
|
73
|
+
Box,
|
|
74
|
+
{
|
|
75
|
+
marginTop: 1,
|
|
76
|
+
paddingTop: 0,
|
|
77
|
+
borderStyle: "single",
|
|
78
|
+
borderTop: true,
|
|
79
|
+
borderBottom: false,
|
|
80
|
+
borderLeft: false,
|
|
81
|
+
borderRight: false,
|
|
82
|
+
borderColor: tokens.colors.border,
|
|
83
|
+
},
|
|
84
|
+
footer
|
|
85
|
+
)
|
|
86
|
+
: null
|
|
87
|
+
);
|
|
88
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import React, { useState, useEffect } from "react";
|
|
2
|
+
import { Box, Text, useInput } from "ink";
|
|
3
|
+
import type { Tokens } from "@hauktui/tokens";
|
|
4
|
+
import { useTokens, useFocusable } from "@hauktui/primitives-ink";
|
|
5
|
+
import { stableId, clamp } from "@hauktui/core";
|
|
6
|
+
|
|
7
|
+
export interface CarouselItem {
|
|
8
|
+
/** Unique identifier */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Content to display */
|
|
11
|
+
content: React.ReactNode;
|
|
12
|
+
/** Optional label */
|
|
13
|
+
label?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CarouselProps {
|
|
17
|
+
/** Carousel items */
|
|
18
|
+
items: CarouselItem[];
|
|
19
|
+
/** Current active index */
|
|
20
|
+
activeIndex?: number;
|
|
21
|
+
/** Callback when active item changes */
|
|
22
|
+
onChange?: (index: number) => void;
|
|
23
|
+
/** Auto-play interval in ms (0 to disable) */
|
|
24
|
+
autoPlay?: number;
|
|
25
|
+
/** Show navigation arrows */
|
|
26
|
+
showArrows?: boolean;
|
|
27
|
+
/** Show dots indicator */
|
|
28
|
+
showDots?: boolean;
|
|
29
|
+
/** Custom tokens override */
|
|
30
|
+
tokens?: Tokens;
|
|
31
|
+
/** Focus ID for focus management */
|
|
32
|
+
focusId?: string;
|
|
33
|
+
/** Orientation */
|
|
34
|
+
orientation?: "horizontal" | "vertical";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function Carousel({
|
|
38
|
+
items,
|
|
39
|
+
activeIndex: controlledIndex,
|
|
40
|
+
onChange,
|
|
41
|
+
autoPlay = 0,
|
|
42
|
+
showArrows = true,
|
|
43
|
+
showDots = true,
|
|
44
|
+
tokens: propTokens,
|
|
45
|
+
focusId,
|
|
46
|
+
orientation = "horizontal",
|
|
47
|
+
}: CarouselProps): React.ReactElement {
|
|
48
|
+
const contextTokens = useTokens();
|
|
49
|
+
const tokens = propTokens ?? contextTokens;
|
|
50
|
+
const id = focusId ?? stableId("carousel");
|
|
51
|
+
const { isFocused } = useFocusable(id);
|
|
52
|
+
|
|
53
|
+
const [internalIndex, setInternalIndex] = useState(0);
|
|
54
|
+
const activeIdx = controlledIndex ?? internalIndex;
|
|
55
|
+
|
|
56
|
+
const setIndex = (index: number) => {
|
|
57
|
+
const newIndex = clamp(index, 0, items.length - 1);
|
|
58
|
+
setInternalIndex(newIndex);
|
|
59
|
+
onChange?.(newIndex);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (autoPlay > 0 && !isFocused) {
|
|
64
|
+
const timer = setInterval(() => {
|
|
65
|
+
setIndex((activeIdx + 1) % items.length);
|
|
66
|
+
}, autoPlay);
|
|
67
|
+
return () => clearInterval(timer);
|
|
68
|
+
}
|
|
69
|
+
}, [autoPlay, activeIdx, items.length, isFocused]);
|
|
70
|
+
|
|
71
|
+
useInput(
|
|
72
|
+
(input, key) => {
|
|
73
|
+
if (!isFocused) return;
|
|
74
|
+
|
|
75
|
+
if (orientation === "horizontal") {
|
|
76
|
+
if (key.leftArrow || input === "h") {
|
|
77
|
+
setIndex(activeIdx > 0 ? activeIdx - 1 : items.length - 1);
|
|
78
|
+
} else if (key.rightArrow || input === "l") {
|
|
79
|
+
setIndex(activeIdx < items.length - 1 ? activeIdx + 1 : 0);
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
if (key.upArrow || input === "k") {
|
|
83
|
+
setIndex(activeIdx > 0 ? activeIdx - 1 : items.length - 1);
|
|
84
|
+
} else if (key.downArrow || input === "j") {
|
|
85
|
+
setIndex(activeIdx < items.length - 1 ? activeIdx + 1 : 0);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Number keys for direct access
|
|
90
|
+
const num = parseInt(input, 10);
|
|
91
|
+
if (num >= 1 && num <= items.length) {
|
|
92
|
+
setIndex(num - 1);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
{ isActive: isFocused }
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const currentItem = items[activeIdx];
|
|
99
|
+
|
|
100
|
+
const leftArrow = showArrows
|
|
101
|
+
? React.createElement(
|
|
102
|
+
Text,
|
|
103
|
+
{
|
|
104
|
+
color: isFocused ? tokens.colors.accent : tokens.colors.muted,
|
|
105
|
+
bold: isFocused,
|
|
106
|
+
},
|
|
107
|
+
orientation === "horizontal" ? "◀" : "▲"
|
|
108
|
+
)
|
|
109
|
+
: null;
|
|
110
|
+
|
|
111
|
+
const rightArrow = showArrows
|
|
112
|
+
? React.createElement(
|
|
113
|
+
Text,
|
|
114
|
+
{
|
|
115
|
+
color: isFocused ? tokens.colors.accent : tokens.colors.muted,
|
|
116
|
+
bold: isFocused,
|
|
117
|
+
},
|
|
118
|
+
orientation === "horizontal" ? "▶" : "▼"
|
|
119
|
+
)
|
|
120
|
+
: null;
|
|
121
|
+
|
|
122
|
+
const dots = showDots
|
|
123
|
+
? React.createElement(
|
|
124
|
+
Box,
|
|
125
|
+
{ gap: 1, justifyContent: "center" },
|
|
126
|
+
...items.map((item, index) =>
|
|
127
|
+
React.createElement(
|
|
128
|
+
Text,
|
|
129
|
+
{
|
|
130
|
+
key: item.id,
|
|
131
|
+
color:
|
|
132
|
+
index === activeIdx
|
|
133
|
+
? tokens.colors.accent
|
|
134
|
+
: tokens.colors.muted,
|
|
135
|
+
},
|
|
136
|
+
index === activeIdx ? "●" : "○"
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
: null;
|
|
141
|
+
|
|
142
|
+
const content = React.createElement(
|
|
143
|
+
Box,
|
|
144
|
+
{
|
|
145
|
+
flexDirection: "column",
|
|
146
|
+
borderStyle: "round",
|
|
147
|
+
borderColor: isFocused ? tokens.colors.focus : tokens.colors.border,
|
|
148
|
+
paddingX: 2,
|
|
149
|
+
paddingY: 1,
|
|
150
|
+
flexGrow: 1,
|
|
151
|
+
},
|
|
152
|
+
currentItem?.label
|
|
153
|
+
? React.createElement(
|
|
154
|
+
Text,
|
|
155
|
+
{ bold: true, color: tokens.colors.fg },
|
|
156
|
+
currentItem.label
|
|
157
|
+
)
|
|
158
|
+
: null,
|
|
159
|
+
currentItem?.content
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
if (orientation === "horizontal") {
|
|
163
|
+
return React.createElement(
|
|
164
|
+
Box,
|
|
165
|
+
{ flexDirection: "column", gap: 0 },
|
|
166
|
+
React.createElement(
|
|
167
|
+
Box,
|
|
168
|
+
{ alignItems: "center", gap: 1 },
|
|
169
|
+
leftArrow,
|
|
170
|
+
content,
|
|
171
|
+
rightArrow
|
|
172
|
+
),
|
|
173
|
+
dots
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return React.createElement(
|
|
178
|
+
Box,
|
|
179
|
+
{ flexDirection: "column", alignItems: "center", gap: 0 },
|
|
180
|
+
leftArrow,
|
|
181
|
+
content,
|
|
182
|
+
rightArrow,
|
|
183
|
+
dots
|
|
184
|
+
);
|
|
185
|
+
}
|