Skip to content

Commit 52aa59a

Browse files
nakagam3nakagam3claude
authored
fix(Calendar): focus first day when no selected or today cell on initial focus (#2676)
The third fallback branch of handleCalendarInitialFocus queried for `[data-reka-calendar-day]`, an attribute that no component renders (day cells expose `data-reka-calendar-cell-trigger` and friends instead). As a result, when neither a selected day nor today is present in the visible view (e.g. an unselected calendar whose displayed month does not contain today), initial focus landed nowhere. Switch the fallback to `[data-value]:not([data-outside-view]):not([data-disabled])`. `data-value` is shared by every calendar/picker cell trigger, so the fix covers Calendar, RangeCalendar, DatePicker, DateRangePicker and the Month/Year (Range) pickers alike, and it matches the selector reka-ui already uses internally for cell navigation. Co-authored-by: nakagam3 <botch.10npo@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 40232d3 commit 52aa59a

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

packages/core/src/Calendar/Calendar.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { render } from '@testing-library/vue'
66
import { describe, expect, it } from 'vitest'
77
import { axe } from 'vitest-axe'
88
import { useTestKbd } from '@/shared'
9+
import { handleCalendarInitialFocus } from '@/shared/date'
910
import Calendar from './story/_Calendar.vue'
1011
import CalendarMultiple from './story/_CalendarMultiple.vue'
1112

@@ -899,6 +900,19 @@ describe('calendar - edge cases', () => {
899900
await user.keyboard(kbd.ARROW_LEFT)
900901
expect(getByTestId('date-0-1-31')).toHaveFocus()
901902
})
903+
904+
it('handles initial focus when no date is selected and today is out of view', async () => {
905+
const { calendar, getByTestId } = setup({
906+
calendarProps: { defaultPlaceholder: edgeCaseCalendarDate },
907+
})
908+
909+
expect(calendar.querySelector('[data-selected]')).toBeNull()
910+
expect(calendar.querySelector('[data-today]')).toBeNull()
911+
912+
handleCalendarInitialFocus(calendar)
913+
914+
expect(getByTestId('date-1-1')).toHaveFocus()
915+
})
902916
})
903917

904918
describe('calendar - tabindex states', () => {

packages/core/src/shared/date/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function handleCalendarInitialFocus(calendar: HTMLElement) {
6262
if (today)
6363
return today.focus()
6464

65-
const firstDay = calendar.querySelector<HTMLElement>('[data-reka-calendar-day]')
65+
const firstDay = calendar.querySelector<HTMLElement>('[data-value]:not([data-outside-view]):not([data-disabled])')
6666
if (firstDay)
6767
return firstDay.focus()
6868
}

0 commit comments

Comments
 (0)