@aleph-alpha/ui-library 6.3.3 → 6.3.4

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.
Files changed (50) hide show
  1. package/dist/system/components/UiFileUploadBox/types.d.ts +8 -5
  2. package/dist/system/components/UiStepper/context.d.ts +8 -0
  3. package/dist/system/components/core/range-calendar/RangeCalendar.vue.d.ts +24 -24
  4. package/dist/system/icons/story-sidebar-icons.json +7 -0
  5. package/dist/system/lib.js +14776 -14598
  6. package/dist/system/patterns/UiLayout/layout-shell.d.ts +217 -5
  7. package/dist/system/patterns/UiTopBar/accessory-dropdown.d.ts +227 -0
  8. package/dist/system/story-sidebar-icons.json +7 -0
  9. package/package.json +1 -1
  10. package/src/components/UiAccordion/UiAccordion.stories.ts +1079 -0
  11. package/src/components/UiCalendar/UiCalendar.stories.ts +734 -0
  12. package/src/components/UiCalendar/__tests__/UiCalendar.test.ts +8 -5
  13. package/src/components/UiDropdownMenu/UiDropdownMenuTrigger.vue +3 -1
  14. package/src/components/UiFileUploadBox/UiFileUploadBox.stories.ts +389 -3
  15. package/src/components/UiFileUploadBox/UiFileUploadBox.vue +55 -75
  16. package/src/components/UiFileUploadBox/__tests__/UiFileUploadBox.test.ts +44 -0
  17. package/src/components/UiFileUploadBox/types.ts +8 -5
  18. package/src/components/UiNavigationMenu/UiNavigationMenu.stories.ts +723 -0
  19. package/src/components/UiRangeCalendar/UiRangeCalendar.stories.ts +819 -7
  20. package/src/components/UiRangeCalendar/__tests__/UiRangeCalendar.test.ts +8 -3
  21. package/src/components/UiResizable/UiResizable.stories.ts +1215 -5
  22. package/src/components/UiSnackbar/UiSnackbar.stories.ts +1017 -4
  23. package/src/components/UiSnackbar/UiSnackbar.vue +3 -2
  24. package/src/components/UiSnackbar/UiSnackbarContent.vue +1 -1
  25. package/src/components/UiSnackbar/__tests__/UiSnackbar.test.ts +3 -1
  26. package/src/components/UiStepper/UiStepper.stories.ts +566 -2
  27. package/src/components/UiStepper/UiStepper.vue +14 -1
  28. package/src/components/UiStepper/UiStepperItem.vue +22 -4
  29. package/src/components/UiStepper/__tests__/UiStepper.test.ts +66 -0
  30. package/src/components/UiStepper/context.ts +10 -0
  31. package/src/components/core/calendar/Calendar.vue +234 -2
  32. package/src/components/core/calendar/CalendarCellTrigger.vue +1 -1
  33. package/src/components/core/navigation-menu/NavigationMenuList.vue +1 -1
  34. package/src/components/core/range-calendar/RangeCalendar.vue +254 -6
  35. package/src/components/core/range-calendar/RangeCalendarCellTrigger.vue +1 -1
  36. package/src/patterns/UiDatePicker/UiDatePicker.stories.ts +846 -17
  37. package/src/patterns/UiDatePicker/UiDatePicker.vue +9 -0
  38. package/src/patterns/UiDatePicker/__tests__/UiDatePicker.test.ts +13 -8
  39. package/src/patterns/UiFileUpload/UiFileItem.stories.ts +23 -17
  40. package/src/patterns/UiFileUpload/UiFileUpload.stories.ts +138 -113
  41. package/src/patterns/UiFileUpload/UiFileUpload.vue +1 -1
  42. package/src/patterns/UiFileUpload/__tests__/UiFileUpload.test.ts +2 -2
  43. package/src/patterns/UiLayout/UiLayout.stories.ts +25 -0
  44. package/src/patterns/UiLayout/UiLayoutContent.stories.ts +359 -0
  45. package/src/patterns/UiLayout/UiLayoutContent.vue +45 -23
  46. package/src/patterns/UiLayout/UiLayoutMobile.stories.ts +13 -0
  47. package/src/patterns/UiLayout/layout-shell.ts +159 -13
  48. package/src/patterns/UiTopBar/UiTopBar.stories.ts +245 -36
  49. package/src/patterns/UiTopBar/UiTopBar.vue +4 -2
  50. package/src/patterns/UiTopBar/accessory-dropdown.ts +97 -0
@@ -1,24 +1,42 @@
1
1
  <script setup lang="ts">
2
+ import { computed, inject } from 'vue';
2
3
  import { StepperItem as ShadcnStepperItem } from '@/components/core/stepper';
4
+ import { cn } from '@/lib/utils';
5
+ import { UI_STEPPER_CURRENT_STEP } from './context';
3
6
  import type { UiStepperItemProps } from './types';
4
7
 
5
8
  defineOptions({
6
9
  name: 'UiStepperItem',
10
+ inheritAttrs: false,
7
11
  });
8
12
 
9
13
  const props = defineProps<UiStepperItemProps>();
14
+
15
+ const currentStep = inject(UI_STEPPER_CURRENT_STEP, undefined);
16
+
17
+ // Mirrors reka-ui's item state (an explicitly `completed` item is never
18
+ // "active"). The fallthrough attribute overrides reka-ui's
19
+ // `aria-current="true"` with the specific "step" token on the active item.
20
+ const ariaCurrent = computed(() =>
21
+ !props.completed && currentStep?.value === props.step ? 'step' : undefined,
22
+ );
10
23
  </script>
11
24
 
12
25
  <template>
13
26
  <ShadcnStepperItem
14
27
  #default="slotProps"
28
+ v-bind="$attrs"
15
29
  :step="props.step"
16
30
  :disabled="props.disabled"
17
31
  :completed="props.completed"
18
- :class="[
19
- 'relative w-full flex-col justify-center',
20
- 'data-[orientation=vertical]:flex-row data-[orientation=vertical]:items-start data-[orientation=vertical]:gap-6',
21
- ]"
32
+ :aria-current="ariaCurrent"
33
+ :class="
34
+ cn(
35
+ 'relative w-full flex-col justify-center',
36
+ 'data-[orientation=vertical]:flex-row data-[orientation=vertical]:items-start data-[orientation=vertical]:gap-6',
37
+ $attrs.class ?? '',
38
+ )
39
+ "
22
40
  >
23
41
  <slot v-bind="slotProps" />
24
42
  </ShadcnStepperItem>
@@ -129,6 +129,72 @@ describe('UiStepper', () => {
129
129
  });
130
130
  });
131
131
 
132
+ test('exposes the active item via aria-current="step"', async () => {
133
+ const user = userEvent.setup();
134
+
135
+ const { getAllByRole } = render({
136
+ components,
137
+ template: stepperTemplate,
138
+ setup() {
139
+ return {
140
+ stepperProps: { defaultValue: 2, linear: false },
141
+ stepperEvents: {},
142
+ };
143
+ },
144
+ });
145
+
146
+ const triggers = getAllByRole('button');
147
+ const itemOf = (trigger: HTMLElement) => trigger.closest('div[data-state]');
148
+
149
+ // The specific "step" token, not reka-ui's generic `aria-current="true"`.
150
+ expect(itemOf(triggers[1])).toHaveAttribute('aria-current', 'step');
151
+ expect(itemOf(triggers[0])).not.toHaveAttribute('aria-current');
152
+ expect(itemOf(triggers[2])).not.toHaveAttribute('aria-current');
153
+
154
+ // Also tracks uncontrolled (defaultValue-driven) step changes.
155
+ await user.click(triggers[2]);
156
+
157
+ await waitFor(() => {
158
+ expect(itemOf(triggers[2])).toHaveAttribute('aria-current', 'step');
159
+ expect(itemOf(triggers[1])).not.toHaveAttribute('aria-current');
160
+ });
161
+ });
162
+
163
+ test('enforced aria-current wins over a consumer-provided attribute', () => {
164
+ const { getAllByRole } = render({
165
+ components,
166
+ template: `
167
+ <UiStepper :default-value="1" :linear="false">
168
+ <UiStepperItem :step="1" aria-current="false" class="custom-item">
169
+ <UiStepperTrigger>
170
+ <UiStepperIndicator>1</UiStepperIndicator>
171
+ <UiStepperTitle>Step 1</UiStepperTitle>
172
+ </UiStepperTrigger>
173
+ <UiStepperSeparator />
174
+ </UiStepperItem>
175
+ <UiStepperItem :step="2" aria-current="step">
176
+ <UiStepperTrigger>
177
+ <UiStepperIndicator>2</UiStepperIndicator>
178
+ <UiStepperTitle>Step 2</UiStepperTitle>
179
+ </UiStepperTrigger>
180
+ </UiStepperItem>
181
+ </UiStepper>
182
+ `,
183
+ });
184
+
185
+ const triggers = getAllByRole('button');
186
+ const itemOf = (trigger: HTMLElement) => trigger.closest('div[data-state]');
187
+
188
+ // The active item keeps the enforced "step" token even though the
189
+ // consumer passed aria-current="false", and the inactive item drops the
190
+ // consumer's aria-current="step".
191
+ expect(itemOf(triggers[0])).toHaveAttribute('aria-current', 'step');
192
+ expect(itemOf(triggers[1])).not.toHaveAttribute('aria-current');
193
+
194
+ // Fallthrough classes still merge with the wrapper's base classes.
195
+ expect(itemOf(triggers[0])).toHaveClass('custom-item', 'relative');
196
+ });
197
+
132
198
  test('does not activate a disabled step when clicked', async () => {
133
199
  const user = userEvent.setup();
134
200
  const onUpdateModelValue = vi.fn();
@@ -0,0 +1,10 @@
1
+ import type { ComputedRef, InjectionKey } from 'vue';
2
+
3
+ /**
4
+ * Current active step provided by `UiStepper` so `UiStepperItem` can expose
5
+ * `aria-current="step"` on the active item. reka-ui only sets
6
+ * `aria-current="true"`, which screen readers announce as a generic "current
7
+ * item" instead of the specific "current step".
8
+ */
9
+ export const UI_STEPPER_CURRENT_STEP: InjectionKey<ComputedRef<number>> =
10
+ Symbol('UiStepperCurrentStep');
@@ -2,7 +2,12 @@
2
2
  import type { CalendarRootEmits, CalendarRootProps, DateValue } from 'reka-ui';
3
3
  import type { HTMLAttributes, Ref } from 'vue';
4
4
  import type { LayoutTypes } from '.';
5
- import { getLocalTimeZone, today } from '@internationalized/date';
5
+ import {
6
+ getLocalTimeZone,
7
+ parseDate,
8
+ toCalendar,
9
+ today,
10
+ } from '@internationalized/date';
6
11
  import { createReusableTemplate, reactiveOmit, useVModel } from '@vueuse/core';
7
12
  import { CalendarRoot, useDateFormatter, useForwardPropsEmits } from 'reka-ui';
8
13
  import { createYear, createYearRange, toDate } from 'reka-ui/date';
@@ -95,7 +100,233 @@ function removeUnsupportedReadonlyAttributes() {
95
100
  }
96
101
 
97
102
  onMounted(removeUnsupportedReadonlyAttributes);
98
- watch(() => props.readonly, removeUnsupportedReadonlyAttributes);
103
+ watch([() => props.readonly, placeholder], removeUnsupportedReadonlyAttributes);
104
+
105
+ const GRID_NAV_KEYS = ['Home', 'End', 'PageUp', 'PageDown'];
106
+
107
+ /**
108
+ * Converts a cell's Gregorian ISO `data-value` into the placeholder's
109
+ * calendar system. `data-value` strings are always Gregorian (that is what
110
+ * `DateValue.toString()` emits), but placeholder fields and month arithmetic
111
+ * must be interpreted in the placeholder's own calendar (e.g. Hebrew).
112
+ * @param value ISO date string carried by the cell's `data-value` attribute.
113
+ */
114
+ function parseCellValue(value: string): DateValue {
115
+ return toCalendar(parseDate(value), placeholder.value.calendar);
116
+ }
117
+
118
+ /**
119
+ * Moves the placeholder to the given day while preserving the placeholder's
120
+ * date type, calendar system, and era. reka-ui's roving tabindex follows the
121
+ * placeholder, so this keeps the single tab stop in sync with the day we
122
+ * focus.
123
+ * @param date Target day.
124
+ */
125
+ function setPlaceholderDay(date: DateValue) {
126
+ const target = toCalendar(date, placeholder.value.calendar);
127
+ placeholder.value = placeholder.value.set({
128
+ era: target.era,
129
+ year: target.year,
130
+ month: target.month,
131
+ day: target.day,
132
+ });
133
+ }
134
+
135
+ /**
136
+ * Focuses a day cell trigger and syncs the placeholder to it.
137
+ * @param cell The day cell trigger element to focus.
138
+ */
139
+ function focusDay(cell: HTMLElement | null | undefined) {
140
+ const value = cell?.getAttribute('data-value');
141
+ if (!cell || !value) return;
142
+ setPlaceholderDay(parseCellValue(value));
143
+ cell.focus();
144
+ }
145
+
146
+ /**
147
+ * Finds the in-view (not outside-view) day cell trigger for a date value in
148
+ * the currently rendered view.
149
+ * @param value ISO date string carried by the cell's `data-value` attribute.
150
+ * @returns The matching trigger element, or null when not rendered.
151
+ */
152
+ function queryInViewDay(value: string): HTMLElement | null {
153
+ const root = calendarRootRef.value?.$el as HTMLElement | undefined;
154
+ return (
155
+ root?.querySelector<HTMLElement>(
156
+ `[data-value='${value}']:not([data-outside-view])`,
157
+ ) ?? null
158
+ );
159
+ }
160
+
161
+ /**
162
+ * Whether a day cannot take keyboard focus — disabled by the `isDateDisabled`
163
+ * matcher or outside the min/max bounds.
164
+ * @param date Candidate day.
165
+ */
166
+ function isDayDisabled(date: DateValue): boolean {
167
+ return Boolean(
168
+ props.isDateDisabled?.(date) ||
169
+ (props.minValue && date.compare(props.minValue) < 0) ||
170
+ (props.maxValue && date.compare(props.maxValue) > 0),
171
+ );
172
+ }
173
+
174
+ /**
175
+ * Resolves an enabled landing day within the candidate's month, stepping past
176
+ * disabled dates in the given direction.
177
+ * @param candidate First-choice landing day.
178
+ * @param direction 1 to step forwards, -1 to step backwards.
179
+ * @returns The first enabled day, or null when the whole stretch is disabled
180
+ * (e.g. a fully disabled target month).
181
+ */
182
+ function resolveEnabledLandingDay(
183
+ candidate: DateValue,
184
+ direction: number,
185
+ ): DateValue | null {
186
+ const targetMonth = candidate.month;
187
+ let day = candidate;
188
+ while (day.month === targetMonth && isDayDisabled(day)) {
189
+ day = day.add({ days: direction });
190
+ }
191
+ return day.month === targetMonth ? day : null;
192
+ }
193
+
194
+ /**
195
+ * Home/End move to the first/last enabled day of the focused row.
196
+ * Outside-view days stay included: with the default
197
+ * `disableDaysOutsideCurrentView: false` they are visible and selectable, so
198
+ * row navigation must reach them too. When that prop is true, reka-ui marks
199
+ * them `data-disabled`, and the filter excludes them consistently.
200
+ * @param trigger The focused day cell trigger.
201
+ * @param key The pressed key, 'Home' or 'End'.
202
+ */
203
+ function handleHomeEndKey(trigger: HTMLElement, key: string) {
204
+ const week = Array.from(
205
+ trigger
206
+ .closest('tr')
207
+ ?.querySelectorAll<HTMLElement>(
208
+ '[data-reka-calendar-cell-trigger]:not([data-disabled])',
209
+ ) ?? [],
210
+ );
211
+ const target = key === 'Home' ? week[0] : week[week.length - 1];
212
+ const targetValue = target?.getAttribute('data-value');
213
+ if (!target || !targetValue) return;
214
+
215
+ if (!target.hasAttribute('data-outside-view')) {
216
+ focusDay(target);
217
+ return;
218
+ }
219
+
220
+ // The row's first/last day belongs to the adjacent month. Mirror reka-ui's
221
+ // arrow navigation, which never focuses outside-view cells: flip the view
222
+ // to that month, then focus the same date as an in-view cell.
223
+ setPlaceholderDay(parseCellValue(targetValue));
224
+ nextTick(() => focusDay(queryInViewDay(targetValue)));
225
+ }
226
+
227
+ /**
228
+ * Clamps a landing day into the min/max bounds so paging stays possible when
229
+ * only part of the target month is allowed (e.g. PageUp from June 10 with
230
+ * minValue May 15 lands on May 15 instead of being rejected).
231
+ * @param target Unclamped landing day (same day in the target month/year).
232
+ * @param direction 1 when paging forwards, -1 backwards.
233
+ * @returns The clamped landing day plus the direction to search for an
234
+ * enabled day (away from the clamped bound, into the month), or null when the
235
+ * whole target month lies outside the bounds.
236
+ */
237
+ function clampLandingDay(
238
+ target: DateValue,
239
+ direction: number,
240
+ ): { day: DateValue; searchDirection: number } | null {
241
+ let day = target;
242
+ let searchDirection = direction;
243
+ if (props.minValue && day.compare(props.minValue) < 0) {
244
+ day = toCalendar(props.minValue, placeholder.value.calendar);
245
+ searchDirection = 1;
246
+ } else if (props.maxValue && day.compare(props.maxValue) > 0) {
247
+ day = toCalendar(props.maxValue, placeholder.value.calendar);
248
+ searchDirection = -1;
249
+ }
250
+ const sameMonth =
251
+ day.era === target.era &&
252
+ day.year === target.year &&
253
+ day.month === target.month;
254
+ return sameMonth ? { day, searchDirection } : null;
255
+ }
256
+
257
+ /**
258
+ * PageUp/PageDown move to the same day in the previous/next month;
259
+ * Shift+PageUp/Shift+PageDown move by a year per the WAI-ARIA calendar
260
+ * pattern. The month/year arithmetic runs in the placeholder's calendar
261
+ * system, landing days are clamped into the min/max bounds, and the enabled
262
+ * landing day is resolved *before* flipping the view, so bailing out (e.g. a
263
+ * fully disabled or out-of-bounds target month) keeps the current view and
264
+ * focus intact instead of stranding keyboard focus.
265
+ * @param trigger The focused day cell trigger.
266
+ * @param e The keydown event.
267
+ */
268
+ function handlePageKey(trigger: HTMLElement, e: KeyboardEvent) {
269
+ const currentValue = trigger.getAttribute('data-value');
270
+ if (!currentValue) return;
271
+ const direction = e.key === 'PageDown' ? 1 : -1;
272
+ const target = parseCellValue(currentValue).add(
273
+ e.shiftKey ? { years: direction } : { months: direction },
274
+ );
275
+ const clamped = clampLandingDay(target, direction);
276
+ if (!clamped) return;
277
+ const candidate = resolveEnabledLandingDay(
278
+ clamped.day,
279
+ clamped.searchDirection,
280
+ );
281
+ if (!candidate) return;
282
+
283
+ setPlaceholderDay(candidate);
284
+ nextTick(() => focusLandingDay(candidate, currentValue));
285
+ }
286
+
287
+ /**
288
+ * Focuses the resolved landing day after the view flipped. Defensive: if the
289
+ * landing cell is unexpectedly missing or disabled in the new view, restores
290
+ * the previous view and focus instead of leaving keyboard focus lost.
291
+ * @param candidate The resolved landing day.
292
+ * @param originValue `data-value` of the day the navigation started from.
293
+ */
294
+ function focusLandingDay(candidate: DateValue, originValue: string) {
295
+ const cell = queryInViewDay(candidate.toString());
296
+ if (cell && !cell.hasAttribute('data-disabled')) {
297
+ focusDay(cell);
298
+ return;
299
+ }
300
+ setPlaceholderDay(parseCellValue(originValue));
301
+ nextTick(() => focusDay(queryInViewDay(originValue)));
302
+ }
303
+
304
+ /**
305
+ * reka-ui's CalendarCellTrigger only implements arrow-key navigation plus
306
+ * Enter/Space selection. The WAI-ARIA date-grid pattern also expects Home/End
307
+ * (first/last day of the visible week), PageUp/PageDown (same day in the
308
+ * previous/next month), and Shift+PageUp/Shift+PageDown (same day in the
309
+ * previous/next year), so we add those here on the calendar root.
310
+ * @param e The keydown event bubbling up from a day cell trigger.
311
+ */
312
+ function handleGridKeydown(e: KeyboardEvent) {
313
+ if (!GRID_NAV_KEYS.includes(e.key)) return;
314
+ const trigger = e.target as HTMLElement | null;
315
+ if (
316
+ !trigger?.hasAttribute('data-reka-calendar-cell-trigger') ||
317
+ trigger.hasAttribute('data-disabled')
318
+ ) {
319
+ return;
320
+ }
321
+ e.preventDefault();
322
+ e.stopPropagation();
323
+
324
+ if (e.key === 'Home' || e.key === 'End') {
325
+ handleHomeEndKey(trigger, e.key);
326
+ } else {
327
+ handlePageKey(trigger, e);
328
+ }
329
+ }
99
330
  </script>
100
331
 
101
332
  <template>
@@ -168,6 +399,7 @@ watch(() => props.readonly, removeUnsupportedReadonlyAttributes);
168
399
  v-model:placeholder="placeholder"
169
400
  data-slot="calendar"
170
401
  :class="cn('p-3', props.class)"
402
+ @keydown="handleGridKeydown"
171
403
  >
172
404
  <CalendarHeader class="pt-0">
173
405
  <nav
@@ -9,7 +9,7 @@ import { buttonVariants } from '@/components/core/button';
9
9
  const props = withDefaults(
10
10
  defineProps<CalendarCellTriggerProps & { class?: HTMLAttributes['class'] }>(),
11
11
  {
12
- as: 'button',
12
+ as: 'div',
13
13
  },
14
14
  );
15
15
 
@@ -16,7 +16,7 @@ const forwardedProps = useForwardProps(props);
16
16
  v-bind="{ ...forwardedProps, ...$attrs }"
17
17
  :class="
18
18
  cn(
19
- 'group flex flex-1 list-none items-center justify-center gap-1',
19
+ 'group flex flex-1 list-none flex-wrap items-center justify-center gap-1',
20
20
  $attrs.class ?? '',
21
21
  )
22
22
  "
@@ -1,7 +1,17 @@
1
1
  <script lang="ts" setup>
2
- import type { RangeCalendarRootEmits, RangeCalendarRootProps } from 'reka-ui';
3
- import type { HTMLAttributes } from 'vue';
4
- import { reactiveOmit } from '@vueuse/core';
2
+ import type {
3
+ DateValue,
4
+ RangeCalendarRootEmits,
5
+ RangeCalendarRootProps,
6
+ } from 'reka-ui';
7
+ import type { HTMLAttributes, Ref } from 'vue';
8
+ import {
9
+ getLocalTimeZone,
10
+ parseDate,
11
+ toCalendar,
12
+ today,
13
+ } from '@internationalized/date';
14
+ import { reactiveOmit, useVModel } from '@vueuse/core';
5
15
  import { RangeCalendarRoot, useForwardPropsEmits } from 'reka-ui';
6
16
  import { nextTick, onMounted, ref, watch } from 'vue';
7
17
  import { cn } from '@/lib/utils';
@@ -25,7 +35,14 @@ const props = defineProps<
25
35
 
26
36
  const emits = defineEmits<RangeCalendarRootEmits>();
27
37
 
28
- const delegatedProps = reactiveOmit(props, 'class');
38
+ const delegatedProps = reactiveOmit(props, 'class', 'placeholder');
39
+
40
+ // Managed locally (instead of forwarded) so the grid keydown handler below
41
+ // can move the roving tab stop, which follows the placeholder in reka-ui.
42
+ const placeholder = useVModel(props, 'placeholder', emits, {
43
+ passive: true,
44
+ defaultValue: props.defaultPlaceholder ?? today(getLocalTimeZone()),
45
+ }) as Ref<DateValue>;
29
46
 
30
47
  const forwarded = useForwardPropsEmits(delegatedProps, emits);
31
48
  const rangeCalendarRootRef = ref<InstanceType<typeof RangeCalendarRoot> | null>(
@@ -44,16 +61,247 @@ function removeUnsupportedReadonlyAttributes() {
44
61
  }
45
62
 
46
63
  onMounted(removeUnsupportedReadonlyAttributes);
47
- watch(() => props.readonly, removeUnsupportedReadonlyAttributes);
64
+ // Month navigation re-renders the grid table, which re-adds the unsupported
65
+ // attribute, so rerun the cleanup whenever the visible month (placeholder)
66
+ // changes — not only when the readonly prop flips.
67
+ watch([() => props.readonly, placeholder], removeUnsupportedReadonlyAttributes);
68
+
69
+ const GRID_NAV_KEYS = ['Home', 'End', 'PageUp', 'PageDown'];
70
+
71
+ /**
72
+ * Converts a cell's Gregorian ISO `data-value` into the placeholder's
73
+ * calendar system. `data-value` strings are always Gregorian (that is what
74
+ * `DateValue.toString()` emits), but placeholder fields and month arithmetic
75
+ * must be interpreted in the placeholder's own calendar (e.g. Hebrew).
76
+ * @param value ISO date string carried by the cell's `data-value` attribute.
77
+ */
78
+ function parseCellValue(value: string): DateValue {
79
+ return toCalendar(parseDate(value), placeholder.value.calendar);
80
+ }
81
+
82
+ /**
83
+ * Moves the placeholder to the given day while preserving the placeholder's
84
+ * date type, calendar system, and era. reka-ui's roving tabindex follows the
85
+ * placeholder, so this keeps the single tab stop in sync with the day we
86
+ * focus.
87
+ * @param date Target day.
88
+ */
89
+ function setPlaceholderDay(date: DateValue) {
90
+ const target = toCalendar(date, placeholder.value.calendar);
91
+ placeholder.value = placeholder.value.set({
92
+ era: target.era,
93
+ year: target.year,
94
+ month: target.month,
95
+ day: target.day,
96
+ });
97
+ }
98
+
99
+ /**
100
+ * Focuses a day cell trigger and syncs the placeholder to it.
101
+ * @param cell The day cell trigger element to focus.
102
+ */
103
+ function focusDay(cell: HTMLElement | null | undefined) {
104
+ const value = cell?.getAttribute('data-value');
105
+ if (!cell || !value) return;
106
+ setPlaceholderDay(parseCellValue(value));
107
+ cell.focus();
108
+ }
109
+
110
+ /**
111
+ * Finds the in-view (not outside-view) day cell trigger for a date value in
112
+ * the currently rendered view.
113
+ * @param value ISO date string carried by the cell's `data-value` attribute.
114
+ * @returns The matching trigger element, or null when not rendered.
115
+ */
116
+ function queryInViewDay(value: string): HTMLElement | null {
117
+ const root = rangeCalendarRootRef.value?.$el as HTMLElement | undefined;
118
+ return (
119
+ root?.querySelector<HTMLElement>(
120
+ `[data-value='${value}']:not([data-outside-view])`,
121
+ ) ?? null
122
+ );
123
+ }
124
+
125
+ /**
126
+ * Whether a day cannot take keyboard focus — disabled by the `isDateDisabled`
127
+ * matcher or outside the min/max bounds.
128
+ * @param date Candidate day.
129
+ */
130
+ function isDayDisabled(date: DateValue): boolean {
131
+ return Boolean(
132
+ props.isDateDisabled?.(date) ||
133
+ (props.minValue && date.compare(props.minValue) < 0) ||
134
+ (props.maxValue && date.compare(props.maxValue) > 0),
135
+ );
136
+ }
137
+
138
+ /**
139
+ * Resolves an enabled landing day within the candidate's month, stepping past
140
+ * disabled dates in the given direction.
141
+ * @param candidate First-choice landing day.
142
+ * @param direction 1 to step forwards, -1 to step backwards.
143
+ * @returns The first enabled day, or null when the whole stretch is disabled
144
+ * (e.g. a fully disabled target month).
145
+ */
146
+ function resolveEnabledLandingDay(
147
+ candidate: DateValue,
148
+ direction: number,
149
+ ): DateValue | null {
150
+ const targetMonth = candidate.month;
151
+ let day = candidate;
152
+ while (day.month === targetMonth && isDayDisabled(day)) {
153
+ day = day.add({ days: direction });
154
+ }
155
+ return day.month === targetMonth ? day : null;
156
+ }
157
+
158
+ /**
159
+ * Clamps a landing day into the min/max bounds so paging stays possible when
160
+ * only part of the target month is allowed (e.g. PageUp from June 10 with
161
+ * minValue May 15 lands on May 15 instead of being rejected).
162
+ * @param target Unclamped landing day (same day in the target month/year).
163
+ * @param direction 1 when paging forwards, -1 backwards.
164
+ * @returns The clamped landing day plus the direction to search for an
165
+ * enabled day (away from the clamped bound, into the month), or null when the
166
+ * whole target month lies outside the bounds.
167
+ */
168
+ function clampLandingDay(
169
+ target: DateValue,
170
+ direction: number,
171
+ ): { day: DateValue; searchDirection: number } | null {
172
+ let day = target;
173
+ let searchDirection = direction;
174
+ if (props.minValue && day.compare(props.minValue) < 0) {
175
+ day = toCalendar(props.minValue, placeholder.value.calendar);
176
+ searchDirection = 1;
177
+ } else if (props.maxValue && day.compare(props.maxValue) > 0) {
178
+ day = toCalendar(props.maxValue, placeholder.value.calendar);
179
+ searchDirection = -1;
180
+ }
181
+ const sameMonth =
182
+ day.era === target.era &&
183
+ day.year === target.year &&
184
+ day.month === target.month;
185
+ return sameMonth ? { day, searchDirection } : null;
186
+ }
187
+
188
+ /**
189
+ * Home/End move to the first/last enabled day of the focused row.
190
+ * Outside-view days stay included: with the default
191
+ * `disableDaysOutsideCurrentView: false` they are visible and selectable, so
192
+ * row navigation must reach them too. When that prop is true, reka-ui marks
193
+ * them `data-disabled`, and the filter excludes them consistently.
194
+ * @param trigger The focused day cell trigger.
195
+ * @param key The pressed key, 'Home' or 'End'.
196
+ */
197
+ function handleHomeEndKey(trigger: HTMLElement, key: string) {
198
+ const week = Array.from(
199
+ trigger
200
+ .closest('tr')
201
+ ?.querySelectorAll<HTMLElement>(
202
+ '[data-reka-calendar-cell-trigger]:not([data-disabled])',
203
+ ) ?? [],
204
+ );
205
+ const target = key === 'Home' ? week[0] : week[week.length - 1];
206
+ const targetValue = target?.getAttribute('data-value');
207
+ if (!target || !targetValue) return;
208
+
209
+ if (!target.hasAttribute('data-outside-view')) {
210
+ focusDay(target);
211
+ return;
212
+ }
213
+
214
+ // The row's first/last day belongs to the adjacent month. Mirror reka-ui's
215
+ // arrow navigation, which never focuses outside-view cells: flip the view
216
+ // to that month, then focus the same date as an in-view cell.
217
+ setPlaceholderDay(parseCellValue(targetValue));
218
+ nextTick(() => focusDay(queryInViewDay(targetValue)));
219
+ }
220
+
221
+ /**
222
+ * PageUp/PageDown move to the same day in the previous/next month;
223
+ * Shift+PageUp/Shift+PageDown move by a year per the WAI-ARIA calendar
224
+ * pattern. The month/year arithmetic runs in the placeholder's calendar
225
+ * system, landing days are clamped into the min/max bounds, and the enabled
226
+ * landing day is resolved *before* flipping the view, so bailing out (e.g. a
227
+ * fully disabled or out-of-bounds target month) keeps the current view and
228
+ * focus intact instead of stranding keyboard focus.
229
+ * @param trigger The focused day cell trigger.
230
+ * @param e The keydown event.
231
+ */
232
+ function handlePageKey(trigger: HTMLElement, e: KeyboardEvent) {
233
+ const currentValue = trigger.getAttribute('data-value');
234
+ if (!currentValue) return;
235
+ const direction = e.key === 'PageDown' ? 1 : -1;
236
+ const target = parseCellValue(currentValue).add(
237
+ e.shiftKey ? { years: direction } : { months: direction },
238
+ );
239
+ const clamped = clampLandingDay(target, direction);
240
+ if (!clamped) return;
241
+ const candidate = resolveEnabledLandingDay(
242
+ clamped.day,
243
+ clamped.searchDirection,
244
+ );
245
+ if (!candidate) return;
246
+
247
+ setPlaceholderDay(candidate);
248
+ nextTick(() => focusLandingDay(candidate, currentValue));
249
+ }
250
+
251
+ /**
252
+ * Focuses the resolved landing day after the view flipped. Defensive: if the
253
+ * landing cell is unexpectedly missing or disabled in the new view, restores
254
+ * the previous view and focus instead of leaving keyboard focus lost.
255
+ * @param candidate The resolved landing day.
256
+ * @param originValue `data-value` of the day the navigation started from.
257
+ */
258
+ function focusLandingDay(candidate: DateValue, originValue: string) {
259
+ const cell = queryInViewDay(candidate.toString());
260
+ if (cell && !cell.hasAttribute('data-disabled')) {
261
+ focusDay(cell);
262
+ return;
263
+ }
264
+ setPlaceholderDay(parseCellValue(originValue));
265
+ nextTick(() => focusDay(queryInViewDay(originValue)));
266
+ }
267
+
268
+ /**
269
+ * reka-ui's RangeCalendarCellTrigger only implements arrow-key navigation plus
270
+ * Enter/Space selection. The WAI-ARIA date-grid pattern also expects Home/End
271
+ * (first/last day of the visible week), PageUp/PageDown (same day in the
272
+ * previous/next month), and Shift+PageUp/Shift+PageDown (same day in the
273
+ * previous/next year), so we add those here on the calendar root.
274
+ * @param e The keydown event bubbling up from a day cell trigger.
275
+ */
276
+ function handleGridKeydown(e: KeyboardEvent) {
277
+ if (!GRID_NAV_KEYS.includes(e.key)) return;
278
+ const trigger = e.target as HTMLElement | null;
279
+ if (
280
+ !trigger?.hasAttribute('data-reka-calendar-cell-trigger') ||
281
+ trigger.hasAttribute('data-disabled')
282
+ ) {
283
+ return;
284
+ }
285
+ e.preventDefault();
286
+ e.stopPropagation();
287
+
288
+ if (e.key === 'Home' || e.key === 'End') {
289
+ handleHomeEndKey(trigger, e.key);
290
+ } else {
291
+ handlePageKey(trigger, e);
292
+ }
293
+ }
48
294
  </script>
49
295
 
50
296
  <template>
51
297
  <RangeCalendarRoot
52
298
  ref="rangeCalendarRootRef"
53
299
  #default="{ grid, weekDays }"
300
+ v-bind="forwarded"
301
+ v-model:placeholder="placeholder"
54
302
  data-slot="range-calendar"
55
303
  :class="cn('p-3', props.class)"
56
- v-bind="forwarded"
304
+ @keydown="handleGridKeydown"
57
305
  >
58
306
  <RangeCalendarHeader>
59
307
  <RangeCalendarHeading />