Skip to content

DatePicker

A headless, accessible DatePicker component for React. Composed of multiple subcomponents for flexible usage.

Features

  • Accessible (keyboard navigation, ARIA attributes)
  • Headless (minimal default styles, fully customizable)
  • Composable API (DatePicker, DatePickerTrigger, DatePickerPanel)
  • Context-powered hooks
  • Built-in styled calendar UI or fully custom

Installation

bash
npm install react-headless-ui-kit

Usage

Basic Usage

tsx
import { DatePicker } from "react-headless-ui-kit";

function ExampleDatePicker() {
  return (
    <DatePicker 
      placeholder="Select a date"
      onDateChange={(date) => console.log("Selected:", date)}
    />
  );
}

Custom Styled Input

tsx
import { DatePicker, DatePickerTrigger } from "react-headless-ui-kit";

function CustomDatePicker() {
  return (
    <DatePicker 
      custom
      onDateChange={(date) => console.log("Selected:", date)}
    >
      <DatePickerTrigger
        placeholder="Select a date"
        style={{ 
          width: "250px",
          padding: "0.5rem",
          border: "1px solid #93c5fd",
          borderRadius: "6px"
        }}
      />
      <DatePickerPanel />
    </DatePicker>
  );
}

API Reference

<DatePicker />

PropTypeDescription
childrenReactNodeCustom trigger or content (when using custom)
defaultDateDate | nullInitial selected date
onDateChange(date: Date | null) => voidCallback when date changes
placeholderstringPlaceholder text for default input
widthstringWidth of default input (e.g., "200px")
custombooleanIf true, uses children as custom trigger

<DatePickerTrigger />

Input field with calendar icon to open/close the date picker.

PropTypeDescription
placeholderstringPlaceholder text (default: "Select date")
styleCSSPropertiesCustom styles for the input

<DatePickerPanel />

DatePicker panel container. Includes a default styled calendar UI or accepts custom children.

PropTypeDescription
childrenReactNodeCustom panel content (optional)

useDatePicker()

Hook to access date picker context values inside subcomponents.

Return valueDescription
openDatePicker open state
setOpenSetter for open
selectedDateCurrently selected date
setSelectedDateSetter for selected date
triggerRefRef to trigger button
panelRefRef to panel element

Accessibility

  • Pressing Escape closes the date picker.
  • ARIA attributes are set for screen readers.
  • Click outside closes the date picker.
  • Calendar navigation with keyboard support.

Customization

The component provides a styled default calendar UI. You can fully customize the appearance and behavior by using the custom prop and providing your own trigger and panel components as children, or by overriding styles via the style prop.