Forms

Time Field

Hour and minute segments you type or arrow through, 12 or 24-hour.

react-aria-componentslucide-react@internationalized/date

Preview

Send window starts
900AM
Send window ends
1730

Installation

Components are copied into your project — no runtime package.

  1. Install the following dependencies:

    terminal
    npm install react-aria-components lucide-react @internationalized/date
  2. Create a time-field.tsx file and paste the following code into it.

    src/components/ui/time-field.tsx
    "use client";
    
    import { Clock } from "lucide-react";
    import { DateInput, TimeField as AriaTimeField, type TimeFieldProps as AriaTimeFieldProps, type TimeValue } from "react-aria-components";
    import { cx } from "@/utils/cx";
    import { Segment } from "./date-picker";
    import { Description, ErrorMessage, Label, fieldBase } from "./field";
    
    export interface TimeFieldProps<T extends TimeValue> extends Omit<AriaTimeFieldProps<T>, "className" | "children"> {
      label?: string;
      description?: string;
      errorMessage?: string;
      className?: string;
    }
    
    /** Hour/minute (and AM/PM) segments you type or arrow through. */
    export function TimeField<T extends TimeValue>({ label, description, errorMessage, className, ...props }: TimeFieldProps<T>) {
      return (
        <AriaTimeField {...props} isInvalid={props.isInvalid ?? Boolean(errorMessage)} className={cx("flex flex-col gap-2", className)}>
          {label && <Label>{label}</Label>}
          <div className={cx(fieldBase, "flex h-10 items-center gap-2 px-3 focus-within:border-accent focus-within:shadow-focus")}>
            <Clock className="size-4 shrink-0 text-fg-muted" aria-hidden />
            <DateInput className="flex flex-1 text-base">{(seg) => <Segment segment={seg} />}</DateInput>
          </div>
          {description && !errorMessage && <Description>{description}</Description>}
          <ErrorMessage>{errorMessage}</ErrorMessage>
        </AriaTimeField>
      );
    }
  3. Create a date-picker.tsx file and paste the following code into it.

    src/components/ui/date-picker.tsx
    "use client";
    
    import { CalendarDays } from "lucide-react";
    import {
      Button as AriaButton,
      DateInput,
      DatePicker as AriaDatePicker,
      type DatePickerProps as AriaDatePickerProps,
      DateSegment,
      type DateSegmentProps,
      type DateValue,
      Dialog,
      Group,
      Popover,
    } from "react-aria-components";
    import { cx } from "@/utils/cx";
    import { DateCalendar } from "./date-calendar";
    import { Description, ErrorMessage, Label, fieldBase } from "./field";
    
    export const segment =
      "rounded-[4px] px-0.5 tabular text-fg outline-none data-placeholder:text-fg-muted data-focused:bg-accent data-focused:text-white data-[type=literal]:px-0 data-[type=literal]:text-fg-muted data-disabled:text-fg-disabled";
    
    /**
     * Date/time segment. Node and browsers ship different ICU data: Node formats "9:00 AM"
     * with a narrow no-break space (U+202F), Chrome with a plain space, which breaks hydration.
     * Normalising literal separators keeps server and client output identical.
     */
    export function Segment({ segment: seg }: { segment: DateSegmentProps["segment"] }) {
      return (
        <DateSegment segment={seg} className={segment}>
          {seg.type === "literal" ? seg.text.replace(/[\u202f\u00a0]/g, " ") : undefined}
        </DateSegment>
      );
    }
    
    export interface DatePickerProps<T extends DateValue> extends Omit<AriaDatePickerProps<T>, "className" | "children"> {
      label?: string;
      description?: string;
      errorMessage?: string;
      className?: string;
    }
    
    /** Typed date segments plus a calendar popover. Fully keyboard accessible. */
    export function DatePicker<T extends DateValue>({ label, description, errorMessage, className, ...props }: DatePickerProps<T>) {
      return (
        <AriaDatePicker {...props} isInvalid={props.isInvalid ?? Boolean(errorMessage)} className={cx("group flex flex-col gap-2", className)}>
          {label && <Label>{label}</Label>}
          <Group className={cx(fieldBase, "flex h-10 items-center gap-2 pl-3 pr-1 group-data-open:border-accent group-data-open:shadow-focus")}>
            <DateInput className="flex flex-1 text-base">{(seg) => <Segment segment={seg} />}</DateInput>
            <AriaButton className="inline-flex size-8 items-center justify-center rounded-sm text-fg-muted outline-none data-hovered:bg-muted data-hovered:text-fg data-focus-visible:shadow-focus">
              <CalendarDays className="size-4" aria-hidden />
            </AriaButton>
          </Group>
          {description && !errorMessage && <Description>{description}</Description>}
          <ErrorMessage>{errorMessage}</ErrorMessage>
          <Popover offset={8} placement="bottom start" className="rounded-md bg-surface p-3 shadow-popover outline-none">
            <Dialog className="outline-none">
              <DateCalendar />
            </Dialog>
          </Popover>
        </AriaDatePicker>
      );
    }
  4. Create a field.tsx file and paste the following code into it.

    src/components/ui/field.tsx
    "use client";
    
    import type { ReactNode } from "react";
    import { FieldError, Label as AriaLabel, Text } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    /** Shared field chrome: 40px tall, radius 12, 1px border-strong, accent border + 3px ring on focus. */
    export const fieldBase =
      "w-full rounded-md border border-line-strong bg-surface text-base text-fg outline-none transition-[border,box-shadow] " +
      "placeholder:text-fg-muted data-hovered:border-fg-disabled " +
      "data-focused:border-accent data-focused:shadow-focus data-focus-within:border-accent data-focus-within:shadow-focus " +
      "data-invalid:border-error data-invalid:data-focused:shadow-focus-error " +
      "data-disabled:border-line data-disabled:bg-subtle data-disabled:text-fg-disabled data-disabled:placeholder:text-fg-disabled";
    
    export function Label({ className, children }: { className?: string; children: ReactNode }) {
      return <AriaLabel className={cx("text-sm font-medium text-fg", className)}>{children}</AriaLabel>;
    }
    
    export function Description({ children }: { children: ReactNode }) {
      return (
        <Text slot="description" className="text-sm text-fg-muted">
          {children}
        </Text>
      );
    }
    
    export function ErrorMessage({ children }: { children?: ReactNode }) {
      return <FieldError className="text-sm text-error-text">{children}</FieldError>;
    }
  5. Add the theme tokens and the cx helper from Installation if you haven’t yet, then update the import paths to match your project setup.