Skip to content

feat(DatePicker): New DatePicker component#3286

Open
aresnik11 wants to merge 81 commits intomainfrom
ajr-datepicker-localization
Open

feat(DatePicker): New DatePicker component#3286
aresnik11 wants to merge 81 commits intomainfrom
ajr-datepicker-localization

Conversation

@aresnik11
Copy link
Copy Markdown
Contributor

@aresnik11 aresnik11 commented Mar 17, 2026

Overview

Adds DatePicker to Gamut: a locale-aware, accessible date (or date range) picker with segmented inputs, a popover calendar, keyboard support, and optional composition via context.

Modes

  • Single dateselectedDate / setSelectedDate.
  • Date rangestartDate, endDate, setStartDate, setEndDate; optional startLabel / endLabel.
  • Calendar closes once a single date or date range is selected.

Default UI vs composition

  • Default — text input(s) + calendar in a popover under the input.
  • Custom — pass children for layout only; compose DatePickerInput and DatePickerCalendar (calendar requires DatePicker context).

Segmented inputs

  • Segmented entry (month / day / year) with locale-driven order and separators (Intl-based layout).
  • spinbutton pattern (role="spinbutton"), which matches Arrow Up/Down stepping and numeric constraints.
  • Live typing with blur normalization when input is invalid or partial.
  • Empty input clears the relevant selection (single or range bound).
  • Hidden input submits an ISO 8601 date-only value for forms (name / form supported).

Calendar & layout

  • Responsivetwo adjacent months from the xs breakpoint up; one month on smaller viewports.
  • Month navigation with nav adjusted for two-month view.
  • Week starts from Intl.Locale#getWeekInfo() (polyfill when needed), optional weekStartsOn on DatePickerCalendar.

Selection behavior

Disabled dates

  • disabledDates — unselectable days; integrated into range validation.

Footer

  • Today — select today and align visible month(s).
  • Clearrange mode; clears range; disabled when empty.

Keyboard & focus

  • Each input segment is a role="spinbutton" span (tabIndex={0} when enabled). Focus moves with Tab / Shift+Tab like normal focusable controls. **Arrow Left / Right ** moves focus within the segments. Arrow Up / Arrow Down steps the current segment up or down, clamped to min/max for that field. Month: 1–12. Day: 1–last day of month when month/year are known. Year: 1–9999; if empty, stepping uses sensible defaults (e.g. current year when stepping up from empty on year).
  • Alt + ArrowDown from input opens calendar and moves focus into the grid (or focuses grid if already open).
  • Open via click keeps focus on the input (pointer-friendly / WCAG-oriented).
  • Grid — arrows (day/week), Home / End (row), PageUp / PageDown (month; Shift for year), Enter / Space to select, Escape closes and returns focus to input.
  • Two-month — horizontal arrows can move between visible months appropriately.

Accessibility

  • Calendar role="dialog" with configurable aria-label.
  • Input shell uses role="group"; FormGroup associates the visible label with the first segment via htmlFor / id.
  • Visual focus: The shell uses :focus-within so the field still shows focus when any inner segment is focused.
  • Input segments use role="spinbutton", matching Arrow Up/Down stepping and numeric min/max.
  • Input segments include:
    • aria-valuemin / aria-valuemax — match spin bounds (day max depends on month/year when known).
    • aria-valuenow — when there is a numeric value; omitted when empty.
    • aria-valuetext — display string (digits or placeholders like MM / DD / YYYY).
    • aria-label — field name (month, day, year).
    • aria-invalid — validation/error state.
    • aria-disabled and tabIndex={-1} when disabled.
  • Grid tied to month heading and per-day accessible names.

Internationalization

  • locale usesIntl.LocalesArgument, defaults to runtime locale but ability to override via locale prop
  • translations for clear button, field labels, and dialog label. default values in English but ability to override via translations prop
  • weekStartsOn uses Intl.Locale#getWeekInfo() (polyfill when needed) but ability to override via weekStartsOn prop
  • Calendar month/year, weekday table headers, placeholder date format (MM/DD/YYYY), date cell aria labels, are automatically localized to the locale via Intl.DateTimeFormat
  • Last month/next month tip text and today button text are automatically localized to the locale via Intl.RelativeTimeFormat

Other

  • inputSize passes through to Input size in the default layout.

Things I know are missing/not completely working:

  • calendar is supposed to close after selecting a date(s)
  • tests
  • calendar quick actions

PR Checklist

Testing Instructions

Don't make me tap the sign.

  1. Go to DatePicker
  2. Play around with DatePicker and make sure all the functionality matches the description above
  3. Do that something in dark mode
  4. Check it with VO
  5. Finish and do a celebratory dance

PR Links and Envs

Repository PR Link
Monolith Monolith PR
Mono Mono PR

@aresnik11 aresnik11 marked this pull request as ready for review April 10, 2026 18:02
@aresnik11 aresnik11 requested a review from a team as a code owner April 10, 2026 18:02
Comment thread packages/gamut/src/DatePicker/DatePickerInput/Segment/elements.tsx
Copy link
Copy Markdown
Contributor

@dreamwasp dreamwasp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i also had an issue opening the Calendar with the keyboard, but everything else looks + sounds good on VO.

i'd like to see some form integration tests (Gamut DatePicker / calendar in a form and assert submitted (or controlled) field data). i will prob look over this again tomorrow once i've let it percolate a little bit more ☕

Comment thread packages/gamut/src/DatePicker/Calendar/utils/dateGrid.ts
Comment thread packages/gamut/src/DatePicker/Calendar/utils/elements.tsx
Comment thread packages/gamut/src/DatePicker/Calendar/utils/format.ts
// eslint-disable-next-line jsx-a11y/control-has-associated-label
<td
// fix this error
// eslint-disable-next-line react/no-array-index-key, jsx-a11y/control-has-associated-label
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious about the a11y error here

Comment thread packages/gamut/src/DatePicker/Calendar/CalendarBody.tsx
Comment thread packages/gamut/src/DatePicker/Calendar/CalendarFooter.tsx Outdated
Comment on lines +1 to +38
import { MiniChevronLeftIcon } from '@codecademy/gamut-icons';
import * as React from 'react';

import { IconButton } from '../../Button';
import { useResolvedLocale } from '../utils/locale';
import { CalendarNavProps } from './types';
import { getRelativeMonthLabels } from './utils/format';

export const CalendarNavLastMonth: React.FC<CalendarNavProps> = ({
displayDate,
onDisplayDateChange,
onLastMonthClick,
locale,
}) => {
const resolvedLocale = useResolvedLocale(locale);
const { lastMonth } = getRelativeMonthLabels(resolvedLocale);

const handleLastMonth = () => {
const lastMonth = new Date(
displayDate.getFullYear(),
displayDate.getMonth() - 1,
1
);
onDisplayDateChange?.(lastMonth);
onLastMonthClick?.();
};

return (
<IconButton
alignSelf="flex-start"
aria-label={lastMonth}
icon={MiniChevronLeftIcon}
size="small"
tip={lastMonth}
onClick={handleLastMonth}
/>
);
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like CalendarNavLastMonth + NextMonths could be one component + DRYED up

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah ive been fussing with this a bunch. do you think the keyboard nav/tab order makes sense? its something like left arrow left calendar body right arrow. or should it be left arrow right arrow calendar. i was updating to make the calendar headers more attached to the calendar body in the reading order

Comment thread packages/gamut/src/DatePicker/DatePickerInput/Segment/elements.tsx
Comment thread packages/gamut/src/DatePicker/DatePickerInput/elements.tsx
Comment thread packages/gamut/src/DatePicker/DatePicker.tsx Outdated
@codecademydev
Copy link
Copy Markdown
Collaborator

📬 Published Alpha Packages:

Package Version npm Diff
@codecademy/gamut 68.2.3-alpha.6d52f4.0 npm diff
@codecademy/gamut-icons 9.57.3-alpha.6d52f4.0 npm diff
@codecademy/gamut-illustrations 0.58.10-alpha.6d52f4.0 npm diff
@codecademy/gamut-kit 0.6.593-alpha.6d52f4.0 npm diff
@codecademy/gamut-patterns 0.10.29-alpha.6d52f4.0 npm diff
@codecademy/gamut-styles 17.13.2-alpha.6d52f4.0 npm diff
@codecademy/gamut-tests 5.3.4-alpha.6d52f4.0 npm diff
@codecademy/variance 0.26.2-alpha.6d52f4.0 npm diff
eslint-plugin-gamut 2.4.4-alpha.6d52f4.0 npm diff

@github-actions
Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Member

@sh0ji sh0ji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heads up that I only really reviewed the API, not the implementation. looks really good in general! but I did have quite a few questions and there are three things that could use refinement:

  1. every prop needs a description and we should be very picky about those descriptions. prop descriptions are our most important docs since they can clarify usage, help in situations where naming is hard, and because they're our best just-in-time docs since they're visible during development thanks to IDE type hinting.
  2. naming conventions: I'm of the opinion that callback props should always be named on${Event}, and that seems to be the case across Gamut. it's possible that some of the props I commented on aren't actually callback props, which would reinforce my first point (better descriptions).
  3. the focus management API feels too big to me. happy to brainstorm ideas to solve it, but my instinct is to remove as much of it as possible and just handle it internally. it's also easier to add props later than it is to remove them after they're out.

Comment thread packages/gamut/src/DatePicker/index.tsx
Comment thread packages/gamut/src/index.tsx
Comment thread packages/gamut/src/DatePicker/types.ts
Comment thread packages/gamut/src/DatePicker/types.ts
Comment thread packages/gamut/src/DatePicker/types.ts
Comment on lines +68 to +78
/** Move focus from the input into the grid when the calendar is already open (e.g. ArrowDown). */
focusCalendarGrid: () => void;
/**
* Flips on each grid focus request so `CalendarBody` effects re-run when `focusTarget` is unchanged.
* Not a semantic true/false — only the change matters; pair with `gridFocusRequested`.
*/
focusGridSignal: boolean;
/** When true, `CalendarBody` runs a one-shot move of DOM focus into the grid if it is not already there. */
gridFocusRequested: boolean;
/** Clears `gridFocusRequested` after focus has moved into the grid (or call when closing). */
clearGridFocusRequest: () => void;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these four props plus moveFocusIntoCalendar in OpenCalendarOptions is five props just for focus management, which feels like a huge API for something that I don't personally think developers want to spend that much time thinking about. how much of it do we need to expose to users?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think any of this needs to be exposed to users

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is the context props so we're handling all of this internally within DatePicker, this is what is within the context. i can move this type into a different file if thats clearer?

Comment thread packages/gamut/src/DatePicker/types.ts
/** Which input is active (start/end focused); null = selection mode. */
activeRangePart: ActiveRangePart;
/** Set which input is active (e.g. when input receives focus). */
setActiveRangePart: (part: ActiveRangePart) => void;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused. "active" = focused? and "part" = the two different inputs (start/end)? the semantics and names of these props could use some refinement. and if these are part of the focus management API, I also wonder if there's just some better way to handle this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is for the logic when you specifically click on one of the inputs and then select a date in the calendar

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lmk if you have a better naming suggestion or way to handle this

Comment thread packages/gamut/src/DatePicker/types.ts
Comment thread packages/gamut/src/DatePicker/types.ts
@aresnik11
Copy link
Copy Markdown
Contributor Author

heads up that I only really reviewed the API, not the implementation. looks really good in general! but I did have quite a few questions and there are three things that could use refinement:

  1. every prop needs a description and we should be very picky about those descriptions. prop descriptions are our most important docs since they can clarify usage, help in situations where naming is hard, and because they're our best just-in-time docs since they're visible during development thanks to IDE type hinting.
  2. naming conventions: I'm of the opinion that callback props should always be named on${Event}, and that seems to be the case across Gamut. it's possible that some of the props I commented on aren't actually callback props, which would reinforce my first point (better descriptions).
  3. the focus management API feels too big to me. happy to brainstorm ideas to solve it, but my instinct is to remove as much of it as possible and just handle it internally. it's also easier to add props later than it is to remove them after they're out.

@sh0ji will update prop descriptions, naming conventions, and clean up what we're exporting (i think this will help with the focus management stuff too)

Copy link
Copy Markdown
Contributor

@LinKCoding LinKCoding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't review the docs fwiw -- but I did test the functionality (not including VO) and it worked well using mouse!

I think my review can be grouped into 3 things:

  1. Clean-up around naming and comments (comments esp could be part of a later ticket)
  2. Some RTL clean-up, albeit, you're blocked by current RTL fixes
  3. Echoing Cass's point about using objects in function parameters/arguments

All in all, looking really good :)

Comment thread packages/gamut/src/DatePicker/Calendar/utils/dateGrid.ts
Comment thread packages/gamut/src/DatePicker/Calendar/types.ts
Comment thread packages/gamut/src/DatePicker/utils/dateSelect.ts
Comment thread packages/gamut/src/DatePicker/utils/dateSelect.ts
if (startDate && endDate) {
// if start date is end date and is clicked, clears everything
if (
startDate.getTime() === endDate.getTime() &&
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

purely stylistic, but you could also use isSameDay here

delta: 1 | -1
) => {
const { min, max } = getSegmentSpinBounds(field, segments);
let cur = parseSegmentNumericString(segments[field]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe currentSegementVal to be more accurate?

fieldOrder: DatePartKind[]
): SegmentValues => {
let rest = digits;
const out: SegmentValues = { month: '', day: '', year: '' };
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out => segmentDigits? something more descriptive?

size={inputSize}
/>
<Box alignSelf="center" mt={32}>
<MiniArrowRightIcon />
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: could add a TODO comment for later, but this will need to adjust for RTL (waiting for the useElementDir hook in another PR)

);

const setButtonRef = useCallback((date: Date, el: HTMLElement | null) => {
const k = new Date(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover robo naming? k => key? dateRef?

Comment thread packages/gamut/src/DatePicker/Calendar/CalendarFooter.tsx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants