Skip to content
Star

Date picker

A field that opens a calendar, with quick picks and natural language. Free and MIT licensed, as HTML, a typed React component or an Astro component, in three styles with light and dark mode.

$npx designme add date-picker
$npx shadcn@latest add https://designme.dev/r/shadcn/date-picker.json

Need this designed into your product? Book a call

01

Install

Add it to your project with the CLI.

HTML, CSS and JS

$npx designme add date-picker

React

$npx designme add date-picker --react

Astro

$npx designme add date-picker --astro

shadcn, React

$npx shadcn@latest add https://designme.dev/r/shadcn/date-picker.json

shadcn, Astro

$npx shadcn@latest add https://designme.dev/r/shadcn/astro/date-picker.json

The CLI copies the source into your project, so you own it and can change anything. Where each version lands, what it needs and the shadcn theme bridge: Installation.

02

About

What it is for and what it does.

A date field that opens a small calendar, with a text box at the top that reads plain English like "next friday" or "in 2 weeks". Quick picks such as Today and Tomorrow sit under the box, and the month grid below works with mouse or keyboard. It suits agent products where people set a due date or launch date the way they would say it.

  • When people pick one date, such as a deadline, launch or reminder.
  • When typing "tomorrow" or "in 3 days" is quicker than clicking through months.
  • When the same few dates come up often and deserve one-click quick picks.
03

Usage

Import it in React and drive it from your own state, or place it in an Astro page.

React

import { DatePicker } from '@/components/designme/date-picker/DatePicker';

React example

The demo above as a React component, 31 lines you can copy.

'use client';
// The preview from designme.dev, rebuilt from the React component: the calendar opens, someone types "next friday", and it
// closes on the day it found.
import { useRef, useState } from 'react';
import { DatePicker } from './DatePicker';
import { useStory } from '../lib/useScript';

/* ---------- Date picker: a field that opens a calendar and reads plain English ---------- */
// today is the real today, so the calendar opens on this month and "next friday" is the coming Friday
export default function DatePickerDemo() {
  const [open, setOpen] = useState(false);
  const [query, setQuery] = useState('');
  // the preview opens it and types a date on its own; clicking, focusing or typing in it takes over, and what it had typed goes
  const ref = useRef<HTMLDivElement>(null);
  useStory(ref, async ({ wait, taken }) => {
    await wait(800); setOpen(true);
    await wait(500);
    const s = 'next friday';
    let typing = true;
    taken(() => { if (typing) setQuery(''); });
    for (let k = 1; k <= s.length; k++) { setQuery(s.slice(0, k)); await wait(70); }
    typing = false;
    await wait(2400); setOpen(false);
  });
  return (
    <div ref={ref} style={{ width: '100%' }}>
      <DatePicker placeholder="Pick a launch date" open={open} onOpenChange={setOpen} query={query} onQueryChange={setQuery} />
    </div>
  );
}

Astro

In a page in src/pages. It plays the demo above; change its date-picker.js to show your own data. Attributes like class go on its root element.

---
import DatePicker from '../components/designme/date-picker/DatePicker.astro';
---
<DatePicker />
<DatePicker data-style="soft" class="my-class" />
04

Props

Everything DatePicker takes.

PropTypeWhat it does
valueDate | nullThe picked day (a Date at local midnight), or null before anything is picked
defaultValueDate | null
onValueChange(date: Date | null) => void
monthDateThe month on show, as its first day. Defaults to the picked day's month, or today's
defaultMonthDate
onMonthChange(month: Date) => void
openbooleanWhether the calendar is open. Leave it out to let the field toggle it
defaultOpenboolean
onOpenChange(open: boolean) => void
querystringWhat is typed in the box at the top, like "next friday". A date it reads is picked straight away
defaultQuerystring
onQueryChange(text: string) => void
parse(text: string, today: Date) => Date | nullTurns typed text into a day. Defaults to plain English: weekdays, "tomorrow", "in 2 weeks"
todayDateThe day marked with a dot. Defaults to the real today; pass one when rendering on a server
placeholderstringShown in the field before a day is picked
queryPlaceholderstringThe hint in the text box
presetsDatePickerPreset[]Quick picks under the text box. Defaults to Today, Tomorrow and In a week
format(date: Date) => stringHow the picked day reads in the field. Defaults to "Fri, Oct 23, 2026"
localestringUsed for the month name, the weekday letters and the default format
weekStartsOnWeekStart1 starts the weeks on Monday (the default), 0 on Sunday
classNamestring
05

Code

The full source, as the CLI installs it.

DatePicker.tsxReact · 152 lines
'use client';
import { useEffect, useRef, useState, type KeyboardEvent } from 'react';
import { addDays, formatMonth, fromIsoDay, isoDay, monthGrid, moveDay, parseRelativeDay, sameDay, sameMonth, startOfDay, startOfMonth, weekdayNames, type WeekStart } from '../lib/calendar';
import { cx } from '../lib/cx';
import { useControllable, useIsoLayoutEffect } from '../lib/hooks';
import { Icon } from '../lib/icons';
import { Swap } from '../lib/motion';
import { useHoverGlide } from '../lib/glide';
import { usePopover } from '../lib/popover';
import '../designme.css';
import './date-picker.css';

/** A quick pick under the text box, like Tomorrow: `days` after today */
export interface DatePickerPreset {
  label: string;
  days: number;
}

export interface DatePickerProps {
  /** The picked day (a Date at local midnight), or null before anything is picked */
  value?: Date | null;
  defaultValue?: Date | null;
  onValueChange?: (date: Date | null) => void;
  /** The month on show, as its first day. Defaults to the picked day's month, or today's */
  month?: Date;
  defaultMonth?: Date;
  onMonthChange?: (month: Date) => void;
  /** Whether the calendar is open. Leave it out to let the field toggle it */
  open?: boolean;
  defaultOpen?: boolean;
  onOpenChange?: (open: boolean) => void;
  /** What is typed in the box at the top, like "next friday". A date it reads is picked straight away */
  query?: string;
  defaultQuery?: string;
  onQueryChange?: (text: string) => void;
  /** Turns typed text into a day. Defaults to plain English: weekdays, "tomorrow", "in 2 weeks" */
  parse?: (text: string, today: Date) => Date | null;
  /** The day marked with a dot. Defaults to the real today; pass one when rendering on a server */
  today?: Date;
  /** Shown in the field before a day is picked */
  placeholder?: string;
  /** The hint in the text box */
  queryPlaceholder?: string;
  /** Quick picks under the text box. Defaults to Today, Tomorrow and In a week */
  presets?: DatePickerPreset[];
  /** How the picked day reads in the field. Defaults to "Fri, Oct 23, 2026" */
  format?: (date: Date) => string;
  /** Used for the month name, the weekday letters and the default format */
  locale?: string;
  /** 1 starts the weeks on Monday (the default), 0 on Sunday */
  weekStartsOn?: WeekStart;
  className?: string;
}

const PRESETS: DatePickerPreset[] = [{ label: 'Today', days: 0 }, { label: 'Tomorrow', days: 1 }, { label: 'In a week', days: 7 }];

/** A date field that opens a calendar, with quick picks and a box that understands "next friday". */
export function DatePicker({
  value, defaultValue = null, onValueChange, month, defaultMonth, onMonthChange, open, defaultOpen = false, onOpenChange,
  query, defaultQuery = '', onQueryChange, parse = parseRelativeDay, today: todayProp, placeholder = 'Pick a date',
  queryPlaceholder = 'Try “next friday” or “in 2 weeks”', presets = PRESETS, format, locale = 'en-US', weekStartsOn = 1, className
}: DatePickerProps) {
  const [today] = useState(() => startOfDay(todayProp ?? new Date()));
  const now = todayProp ? startOfDay(todayProp) : today;
  const [picked, setPicked] = useControllable(value, defaultValue, onValueChange);
  const [view, setView] = useControllable(month, startOfMonth(defaultMonth ?? defaultValue ?? now), onMonthChange);
  const [text, setText] = useControllable(query, defaultQuery, onQueryChange);
  const menu = usePopover({ side: 'down', open, defaultOpen, onOpenChange });
  const show = format ?? ((d: Date) => d.toLocaleDateString(locale, { weekday: 'short', month: 'short', day: 'numeric', year: 'numeric' }));

  // a new month slides in from the side it comes from
  const viewKey = isoDay(startOfMonth(view));
  const [shown, setShown] = useState(viewKey);
  const [dir, setDir] = useState(0);
  if (shown !== viewKey) { setDir(viewKey > shown ? 1 : -1); setShown(viewKey); }

  const closing = useRef<ReturnType<typeof setTimeout>>(undefined);
  useEffect(() => () => clearTimeout(closing.current), []);
  const pick = (d: Date, keep = false) => {
    setPicked(d);
    if (!sameMonth(d, view)) setView(startOfMonth(d));
    if (!keep) { clearTimeout(closing.current); closing.current = setTimeout(() => menu.setOpen(false), 260); }
  };

  // typed text picks a day as soon as it reads as one, and the calendar stays open
  const read = useRef(text);
  useEffect(() => {
    if (read.current === text) return;
    read.current = text;
    const d = parse(text, now);
    if (d) pick(d, true);
  }, [text]); // eslint-disable-line react-hooks/exhaustive-deps

  // arrow keys move the focus a day or a week, across months too
  const grid = useRef<HTMLDivElement>(null);
  // Studio's one hover: a neutral circle that glides from day to day
  const vp = useRef<HTMLDivElement>(null);
  useHoverGlide(vp, '.dm-dpk-d', { keep: '.dm-s' });
  const [focus, setFocus] = useState<string | null>(null);
  useIsoLayoutEffect(() => {
    if (!focus) return;
    grid.current?.querySelector<HTMLButtonElement>(`[data-date="${focus}"]`)?.focus();
    setFocus(null);
  }, [focus, viewKey]);
  // one day takes Tab: the one the arrows last reached, or the picked day, today or the 1st
  const [cursor, setCursor] = useState<Date | null>(null);
  const tabDay = [cursor, picked, now].find(d => d && sameMonth(d, view)) ?? startOfMonth(view);
  const keys = (e: KeyboardEvent<HTMLDivElement>) => {
    const at = (e.target as HTMLElement).dataset.date; if (!at) return;
    const next = moveDay(fromIsoDay(at), e.key, weekStartsOn, e.shiftKey); if (!next) return;
    e.preventDefault();
    if (!sameMonth(next, view)) setView(startOfMonth(next));
    setCursor(next); setFocus(isoDay(next));
  };
  // opened with Enter or Space, the calendar takes the focus on the picked day, or today
  const keyOpen = useRef(false);
  useIsoLayoutEffect(() => { if (menu.open && keyOpen.current) { keyOpen.current = false; setCursor(null); grid.current?.querySelector<HTMLButtonElement>('[tabindex="0"]')?.focus({ preventScroll: true }); } }, [menu.open]);

  return (
    <div data-dm="" className={cx('dm-dpk', className)}>
      <button type="button" className="dm-dpk-f" {...menu.trigger} aria-haspopup="dialog" onClick={e => { keyOpen.current = !menu.open && e.detail === 0; menu.trigger.onClick(); }}>
        <Icon name="cal" size="xs" />
        <Swap className={cx('dm-dpk-val', !picked && 'dm-ph')}>{picked ? show(picked) : placeholder}</Swap>
        <Icon name="chev-d" size="xs" />
      </button>
      <div {...menu.pop} className={cx(menu.pop.className, 'dm-dpk-pop')} role="dialog" aria-label="Choose a date">
        <input className="dm-dpk-nl" placeholder={queryPlaceholder} spellCheck={false} aria-label="Type a date" value={text}
          onChange={e => setText(e.target.value)} onKeyDown={e => { if (e.key === 'Enter' && picked) menu.setOpen(false); }} />
        <div className="dm-dpk-qk">
          {presets.map(p => <button type="button" key={p.label} className="dm-chip dm-sm" onClick={() => pick(addDays(now, p.days))}>{p.label}</button>)}
        </div>
        <div className="dm-dpk-mh">
          <button type="button" className="dm-ib dm-dpk-pv" aria-label="Previous month" onClick={() => setView(startOfMonth(new Date(view.getFullYear(), view.getMonth() - 1, 1)))}><Icon name="chev-l" size="xs" /></button>
          <b className="dm-dpk-m" aria-live="polite">{formatMonth(view, locale)}</b>
          <button type="button" className="dm-ib dm-dpk-nx" aria-label="Next month" onClick={() => setView(startOfMonth(new Date(view.getFullYear(), view.getMonth() + 1, 1)))}><Icon name="chev-r" size="xs" /></button>
        </div>
        <div className="dm-dpk-wd dm-muted" aria-hidden="true">{weekdayNames(locale, weekStartsOn).map((w, i) => <span key={i}>{w}</span>)}</div>
        <div ref={vp} className="dm-dpk-vp">
          <div key={viewKey} ref={grid} className={cx('dm-dpk-g', dir < 0 && 'dm-in-l', dir > 0 && 'dm-in-r')} onKeyDown={keys}>
            {monthGrid(view, weekStartsOn).map(d => (
              <button type="button" key={isoDay(d)} data-date={isoDay(d)}
                className={cx('dm-dpk-d', !sameMonth(d, view) && 'dm-o', sameDay(d, now) && 'dm-t', sameDay(d, picked) && 'dm-s')}
                aria-pressed={sameDay(d, picked)} aria-current={sameDay(d, now) ? 'date' : undefined} aria-label={show(d)} tabIndex={sameDay(d, tabDay) ? 0 : -1}
                onClick={() => pick(d)}>{d.getDate()}</button>
            ))}
          </div>
        </div>
      </div>
    </div>
  );
}
date-picker.cssReact · 74 lines
/* Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync. */
/* ---------- Date picker ---------- */
/* one field, as wide as the other single fields; its script keeps it centred in its stage and glides it up as the calendar opens */
.dm-dpk{position:relative;width:100%;max-width:var(--dm-field-w);margin:0 auto}
.dm-dpk-f{display:flex;align-items:center;gap:10px;width:100%;height:var(--dm-field-h);padding:0 var(--dm-s-3);border-radius:var(--dm-r-field);background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line) 50%,transparent);font-size:13.5px;transition:box-shadow var(--dm-t-fast)}
@supports (corner-shape:squircle){.dm-dpk-f{corner-shape:squircle;border-radius:var(--dm-r-field-sq)}}
/* a dropdown's field steps up its edge while it has the focus or its calendar is open; it takes no ring */
.dm-dpk-f:focus-visible,.dm-dpk-f[aria-expanded="true"]{outline:0;box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line-strong) 50%,transparent)}
.dm-dpk-f>svg:first-child{color:var(--dm-muted)}
.dm-dpk-val{flex:1;text-align:left;color:var(--dm-fg)}
.dm-dpk-val.dm-ph{color:var(--dm-muted)}
.dm-dpk-f>svg:last-child{color:var(--dm-muted);transition:transform var(--dm-t) var(--dm-ease)}
.dm-dpk-f[aria-expanded="true"]>svg:last-child{transform:rotate(180deg)}
.dm-dpk-pop{width:100%;padding:10px;background:var(--dm-surface)}
/* the calendar under the field: its content 16px in from the popover's edges in every style, and the month's arrows centred
   over the first and last columns of days (a column is a seventh of the content's width) */
.dm-dpk>.dm-dpk-pop{padding:var(--dm-s-4)}
.dm-dpk>.dm-dpk-pop .dm-dpk-mh{padding-inline:calc((100% / 7 - 26px) / 2)}
/* nothing in it grows or shrinks when it is pressed (owner, 18 Nov) */
.dm-dpk :is(.dm-ib,.dm-chip,.dm-dpk-d):active,:root[data-style="studio"] .dm-dpk .dm-ib:active,:root [data-style="studio"] .dm-dpk .dm-ib:active,:root [data-style="studio"].dm-dpk .dm-ib:active{transform:none}
/* Studio (owner, 18 Nov): the popover takes the card's 22px round corners */
:root[data-style="studio"] .dm-dpk>.dm-dpk-pop,:root [data-style="studio"] .dm-dpk>.dm-dpk-pop,:root [data-style="studio"].dm-dpk>.dm-dpk-pop{border-radius:var(--dm-r-card)}
/* Studio (owner, 21 Nov: "date picker studio is not its style, its soft with rounded corners, change it more"). The field is a
   flat dropdown trigger (an off-white fill under a hairline that deepens on hover and while it is open). The popover opens
   from it with Studio's scale and fade; its head is an off-white strip under a hairline (its background, 92px tall) holding the
   typed date and the quick picks as flat hairline pills. The days are circles: the picked one filled with the accent (white
   with dark words in dark), today a small dot under its number, and the hover a neutral circle that glides from day to day
   (hoverGlide). The weekday letters are muted at 12px, ‹ and › ghost buttons, and a new month slides in with Studio's ease */
:root[data-style="studio"] :is(.dm-dpk-f),:root [data-style="studio"] :is(.dm-dpk-f),:root [data-style="studio"]:is(.dm-dpk-f){background:var(--dm-strip);box-shadow:var(--dm-btn2-depth);transition:box-shadow var(--dm-t-fast) var(--dm-ease),background-color var(--dm-t-fast) var(--dm-ease)}
@media (hover:hover) and (pointer:fine){:root[data-style="studio"] :is(.dm-dpk-f):hover,:root [data-style="studio"] :is(.dm-dpk-f):hover,:root [data-style="studio"]:is(.dm-dpk-f):hover{background:var(--dm-chip);box-shadow:var(--dm-btn2-hover)}}
:root[data-style="studio"] :is(.dm-dpk-f):is(:focus-visible,[aria-expanded="true"]),:root [data-style="studio"] :is(.dm-dpk-f):is(:focus-visible,[aria-expanded="true"]),:root [data-style="studio"]:is(.dm-dpk-f):is(:focus-visible,[aria-expanded="true"]){background:var(--dm-chip);box-shadow:var(--dm-btn2-hover)}
:root[data-style="studio"] .dm-dpk>.dm-dpk-pop,:root [data-style="studio"] .dm-dpk>.dm-dpk-pop,:root [data-style="studio"].dm-dpk>.dm-dpk-pop{padding:12px 16px 16px;background:linear-gradient(var(--dm-strip) 0 92px,var(--dm-line) 92px 93px,var(--dm-surface) 93px)}
:root[data-style="studio"] .dm-dpk .dm-dpk-qk,:root [data-style="studio"] .dm-dpk .dm-dpk-qk,:root [data-style="studio"].dm-dpk .dm-dpk-qk{margin:8px 0 0}
:root[data-style="studio"] .dm-dpk .dm-dpk-qk .dm-chip,:root [data-style="studio"] .dm-dpk .dm-dpk-qk .dm-chip,:root [data-style="studio"].dm-dpk .dm-dpk-qk .dm-chip{height:28px;padding:0 12px;font-size:12.5px;color:var(--dm-fg);background:linear-gradient(180deg,var(--dm-btn2-1),var(--dm-btn2-2));box-shadow:var(--dm-btn2-depth);transition:box-shadow var(--dm-t-fast) var(--dm-ease)}
@media (hover:hover) and (pointer:fine){:root[data-style="studio"] .dm-dpk .dm-dpk-qk .dm-chip:hover,:root [data-style="studio"] .dm-dpk .dm-dpk-qk .dm-chip:hover,:root [data-style="studio"].dm-dpk .dm-dpk-qk .dm-chip:hover{background:linear-gradient(180deg,var(--dm-btn2-1),var(--dm-btn2-2));box-shadow:var(--dm-btn2-hover)}}
:root[data-style="studio"] .dm-dpk>.dm-dpk-pop .dm-dpk-mh,:root [data-style="studio"] .dm-dpk>.dm-dpk-pop .dm-dpk-mh,:root [data-style="studio"].dm-dpk>.dm-dpk-pop .dm-dpk-mh{margin:25px 0 4px;padding-inline:calc((100% / 7 - 28px) / 2)}
:root[data-style="studio"] .dm-dpk .dm-dpk-mh .dm-ib,:root [data-style="studio"] .dm-dpk .dm-dpk-mh .dm-ib,:root [data-style="studio"].dm-dpk .dm-dpk-mh .dm-ib{width:28px;height:28px;border-radius:50%}
:root[data-style="studio"] .dm-dpk .dm-dpk-wd,:root [data-style="studio"] .dm-dpk .dm-dpk-wd,:root [data-style="studio"].dm-dpk .dm-dpk-wd{font-size:12px;line-height:20px;margin-bottom:4px}
:root[data-style="studio"] .dm-dpk .dm-dpk-d,:root [data-style="studio"] .dm-dpk .dm-dpk-d,:root [data-style="studio"].dm-dpk .dm-dpk-d{width:32px;height:32px;justify-self:center;border-radius:50%;transition:background-color var(--dm-t-fast) var(--dm-ease),color var(--dm-t-fast) var(--dm-ease)}
:root[data-style="studio"] .dm-dpk .dm-dpk-d.dm-t::after,:root [data-style="studio"] .dm-dpk .dm-dpk-d.dm-t::after,:root [data-style="studio"].dm-dpk .dm-dpk-d.dm-t::after{bottom:3px;width:3px;height:3px;margin-left:-1.5px}
:root[data-style="studio"] .dm-dpk .dm-dpk-g.dm-in-l,:root [data-style="studio"] .dm-dpk .dm-dpk-g.dm-in-l,:root [data-style="studio"].dm-dpk .dm-dpk-g.dm-in-l{animation:dm-clpkl var(--dm-t) var(--dm-ease) both}
:root[data-style="studio"] .dm-dpk .dm-dpk-g.dm-in-r,:root [data-style="studio"] .dm-dpk .dm-dpk-g.dm-in-r,:root [data-style="studio"].dm-dpk .dm-dpk-g.dm-in-r{animation:dm-clpkr var(--dm-t) var(--dm-ease) both}
@keyframes dm-clpkl{from{opacity:0;transform:translateX(-16px)}}
@keyframes dm-clpkr{from{opacity:0;transform:translateX(16px)}}
.dm-dpk-nl{width:100%;height:32px;border:0;outline:0;border-radius:var(--dm-r-field);background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line) 50%,transparent);padding:0 10px;font:inherit;font-size:13px;color:var(--dm-fg);transition:box-shadow var(--dm-t-fast)}
@supports (corner-shape:squircle){.dm-dpk-nl{corner-shape:squircle;border-radius:var(--dm-r-field-sq)}}
.dm-dpk-nl::placeholder{color:var(--dm-muted)}
.dm-dpk-nl:focus{box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line-strong) 50%,transparent)}
.dm-dpk-qk{display:flex;gap:var(--dm-s-1);margin:var(--dm-s-2) 0}
.dm-dpk-mh{display:flex;align-items:center;justify-content:space-between;margin:var(--dm-s-1) 0}
.dm-dpk-mh b{font-weight:500;font-size:13px}
.dm-dpk-mh .dm-ib{width:26px;height:26px}
.dm-dpk-wd,.dm-dpk-g{display:grid;grid-template-columns:repeat(7,1fr);text-align:center}
.dm-dpk-wd{font-size:11px;margin-bottom:2px}
.dm-dpk-vp{overflow:hidden}
.dm-dpk-g{gap:2px 0}
.dm-dpk-g.dm-in-l{animation:dm-dpkl .4s var(--dm-ease) both}.dm-dpk-g.dm-in-r{animation:dm-dpkr .4s var(--dm-ease) both}
@keyframes dm-dpkl{from{opacity:0;transform:translateX(-24px);filter:blur(3px)}}
@keyframes dm-dpkr{from{opacity:0;transform:translateX(24px);filter:blur(3px)}}
.dm-dpk-d{height:32px;border-radius:calc(9px * var(--dm-rk-m,var(--dm-rk)));font-size:12.5px;color:var(--dm-fg);transition:background .2s,color .2s;position:relative}
@media (hover:hover) and (pointer:fine){.dm-dpk-d:hover{background:var(--dm-row)}}
/* the days fill the month's sliding view, which would cut an outside ring: theirs is drawn inside */
.dm-dpk-d:focus-visible{outline-offset:-2px}
.dm-dpk-d.dm-o{color:var(--dm-muted)}
.dm-dpk-d.dm-t::after{content:"";position:absolute;left:50%;bottom:4px;width:4px;height:4px;margin-left:-2px;border-radius:50%;background:var(--dm-accent-mark,var(--dm-accent))}
.dm-dpk-d.dm-s{background:var(--dm-accent);color:var(--dm-on-accent)}
.dm-dpk-d.dm-s::after{background:var(--dm-on-accent)}
.dm-dpk-d.dm-s{animation:dm-dpks .35s var(--dm-ease)}
/* in the date picker a picked day takes its fill without growing (owner, 18 Nov: nothing scales on a click) */
.dm-dpk .dm-dpk-d.dm-s{animation:none}
/* on the picked day the inside ring would sit on the accent fill: a surface gap inside it keeps it seen */
.dm-dpk-d.dm-s:focus-visible{box-shadow:inset 0 0 0 3.5px var(--dm-surface)}
@keyframes dm-dpks{from{transform:scale(.8)}}
DatePicker.astroAstro · 33 lines
---
// Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync.
// Date picker: A field that opens a calendar, with quick picks and natural language.
// The markup, CSS and script are designme.dev's own, run by the shared core in ../core, which mounts the component when it scrolls
// into view and plays its demo. To show your own data, change date-picker.js. The markup is kept exactly as it is (is:raw).
import type { HTMLAttributes } from 'astro/types';
import '../core/designme.css';
import './date-picker.css';

export interface Props extends HTMLAttributes<'div'> {
  /** "soft" or "studio" draws it in that style. Leave it out for Blueprint, or for the style set on <html> (data-style="soft" or "studio") */
  'data-style'?: 'soft' | 'studio';
}
const { class: className, ...rest } = Astro.props;
---
<div class:list={['card', className]} data-c="date-picker" {...rest}>
  <div class="stage-in"><Fragment is:raw>
<div class="dpk">
  <button class="dpk-f" data-pop=".dpk-pop" data-pop-pos="down"><svg class="i xs"><use href="#i-cal"/></svg><span class="dpk-val">Pick a launch date</span><svg class="i xs"><use href="#i-chev-d"/></svg></button>
  <div class="pop dpk-pop" role="dialog" aria-label="Choose a date">
    <input class="dpk-nl" placeholder="Try “next friday” or “in 2 weeks”" spellcheck="false">
    <div class="dpk-qk"><button class="chip sm" data-d="0">Today</button><button class="chip sm" data-d="1">Tomorrow</button><button class="chip sm" data-d="7">In a week</button></div>
    <div class="dpk-mh"><button class="ib dpk-pv" aria-label="Previous month"><svg class="i xs"><use href="#i-chev-l"/></svg></button><b class="dpk-m"></b><button class="ib dpk-nx" aria-label="Next month"><svg class="i xs"><use href="#i-chev-r"/></svg></button></div>
    <div class="dpk-wd muted"><span>M</span><span>T</span><span>W</span><span>T</span><span>F</span><span>S</span><span>S</span></div>
    <div class="dpk-vp"><div class="dpk-g"></div></div>
  </div>
</div>
  </Fragment></div>
</div>
<script>
  import './date-picker.js';
</script>
date-picker.htmlHTML · 15 lines
<div class="card" data-c="date-picker">
  <div class="stage-in">
    <div class="dpk">
      <button class="dpk-f" data-pop=".dpk-pop" data-pop-pos="down"><svg class="i xs"><use href="#i-cal"/></svg><span class="dpk-val">Pick a launch date</span><svg class="i xs"><use href="#i-chev-d"/></svg></button>
      <div class="pop dpk-pop" role="dialog" aria-label="Choose a date">
        <input class="dpk-nl" placeholder="Try “next friday” or “in 2 weeks”" spellcheck="false">
        <div class="dpk-qk"><button class="chip sm" data-d="0">Today</button><button class="chip sm" data-d="1">Tomorrow</button><button class="chip sm" data-d="7">In a week</button></div>
        <div class="dpk-mh"><button class="ib dpk-pv" aria-label="Previous month"><svg class="i xs"><use href="#i-chev-l"/></svg></button><b class="dpk-m"></b><button class="ib dpk-nx" aria-label="Next month"><svg class="i xs"><use href="#i-chev-r"/></svg></button></div>
        <div class="dpk-wd muted"><span>M</span><span>T</span><span>W</span><span>T</span><span>F</span><span>S</span><span>S</span></div>
        <div class="dpk-vp"><div class="dpk-g"></div></div>
      </div>
    </div>
  </div>
</div>
date-picker.cssHTML · 73 lines
/* ---------- Date picker ---------- */
/* one field, as wide as the other single fields; its script keeps it centred in its stage and glides it up as the calendar opens */
.dpk{position:relative;width:100%;max-width:var(--dm-field-w);margin:0 auto}
.dpk-f{display:flex;align-items:center;gap:10px;width:100%;height:var(--dm-field-h);padding:0 var(--dm-s-3);border-radius:var(--dm-r-field);background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line) 50%,transparent);font-size:13.5px;transition:box-shadow var(--dm-t-fast)}
@supports (corner-shape:squircle){.dpk-f{corner-shape:squircle;border-radius:var(--dm-r-field-sq)}}
/* a dropdown's field steps up its edge while it has the focus or its calendar is open; it takes no ring */
.dpk-f:focus-visible,.dpk-f[aria-expanded="true"]{outline:0;box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line-strong) 50%,transparent)}
.dpk-f>svg:first-child{color:var(--dm-muted)}
.dpk-val{flex:1;text-align:left;color:var(--dm-fg)}
.dpk-val.ph{color:var(--dm-muted)}
.dpk-f>svg:last-child{color:var(--dm-muted);transition:transform var(--dm-t) var(--dm-ease)}
.dpk-f[aria-expanded="true"]>svg:last-child{transform:rotate(180deg)}
.dpk-pop{width:100%;padding:10px;background:var(--dm-surface)}
/* the calendar under the field: its content 16px in from the popover's edges in every style, and the month's arrows centred
   over the first and last columns of days (a column is a seventh of the content's width) */
.dpk>.dpk-pop{padding:var(--dm-s-4)}
.dpk>.dpk-pop .dpk-mh{padding-inline:calc((100% / 7 - 26px) / 2)}
/* nothing in it grows or shrinks when it is pressed (owner, 18 Nov) */
.dpk :is(.ib,.chip,.dpk-d):active,:root[data-style="studio"] .dpk .ib:active,:root [data-style="studio"] .dpk .ib:active,:root [data-style="studio"].dpk .ib:active{transform:none}
/* Studio (owner, 18 Nov): the popover takes the card's 22px round corners */
:root[data-style="studio"] .dpk>.dpk-pop,:root [data-style="studio"] .dpk>.dpk-pop,:root [data-style="studio"].dpk>.dpk-pop{border-radius:var(--dm-r-card)}
/* Studio (owner, 21 Nov: "date picker studio is not its style, its soft with rounded corners, change it more"). The field is a
   flat dropdown trigger (an off-white fill under a hairline that deepens on hover and while it is open). The popover opens
   from it with Studio's scale and fade; its head is an off-white strip under a hairline (its background, 92px tall) holding the
   typed date and the quick picks as flat hairline pills. The days are circles: the picked one filled with the accent (white
   with dark words in dark), today a small dot under its number, and the hover a neutral circle that glides from day to day
   (hoverGlide). The weekday letters are muted at 12px, ‹ and › ghost buttons, and a new month slides in with Studio's ease */
:root[data-style="studio"] :is(.dpk-f),:root [data-style="studio"] :is(.dpk-f),:root [data-style="studio"]:is(.dpk-f){background:var(--dm-strip);box-shadow:var(--dm-btn2-depth);transition:box-shadow var(--dm-t-fast) var(--dm-ease),background-color var(--dm-t-fast) var(--dm-ease)}
@media (hover:hover) and (pointer:fine){:root[data-style="studio"] :is(.dpk-f):hover,:root [data-style="studio"] :is(.dpk-f):hover,:root [data-style="studio"]:is(.dpk-f):hover{background:var(--dm-chip);box-shadow:var(--dm-btn2-hover)}}
:root[data-style="studio"] :is(.dpk-f):is(:focus-visible,[aria-expanded="true"]),:root [data-style="studio"] :is(.dpk-f):is(:focus-visible,[aria-expanded="true"]),:root [data-style="studio"]:is(.dpk-f):is(:focus-visible,[aria-expanded="true"]){background:var(--dm-chip);box-shadow:var(--dm-btn2-hover)}
:root[data-style="studio"] .dpk>.dpk-pop,:root [data-style="studio"] .dpk>.dpk-pop,:root [data-style="studio"].dpk>.dpk-pop{padding:12px 16px 16px;background:linear-gradient(var(--dm-strip) 0 92px,var(--dm-line) 92px 93px,var(--dm-surface) 93px)}
:root[data-style="studio"] .dpk .dpk-qk,:root [data-style="studio"] .dpk .dpk-qk,:root [data-style="studio"].dpk .dpk-qk{margin:8px 0 0}
:root[data-style="studio"] .dpk .dpk-qk .chip,:root [data-style="studio"] .dpk .dpk-qk .chip,:root [data-style="studio"].dpk .dpk-qk .chip{height:28px;padding:0 12px;font-size:12.5px;color:var(--dm-fg);background:linear-gradient(180deg,var(--dm-btn2-1),var(--dm-btn2-2));box-shadow:var(--dm-btn2-depth);transition:box-shadow var(--dm-t-fast) var(--dm-ease)}
@media (hover:hover) and (pointer:fine){:root[data-style="studio"] .dpk .dpk-qk .chip:hover,:root [data-style="studio"] .dpk .dpk-qk .chip:hover,:root [data-style="studio"].dpk .dpk-qk .chip:hover{background:linear-gradient(180deg,var(--dm-btn2-1),var(--dm-btn2-2));box-shadow:var(--dm-btn2-hover)}}
:root[data-style="studio"] .dpk>.dpk-pop .dpk-mh,:root [data-style="studio"] .dpk>.dpk-pop .dpk-mh,:root [data-style="studio"].dpk>.dpk-pop .dpk-mh{margin:25px 0 4px;padding-inline:calc((100% / 7 - 28px) / 2)}
:root[data-style="studio"] .dpk .dpk-mh .ib,:root [data-style="studio"] .dpk .dpk-mh .ib,:root [data-style="studio"].dpk .dpk-mh .ib{width:28px;height:28px;border-radius:50%}
:root[data-style="studio"] .dpk .dpk-wd,:root [data-style="studio"] .dpk .dpk-wd,:root [data-style="studio"].dpk .dpk-wd{font-size:12px;line-height:20px;margin-bottom:4px}
:root[data-style="studio"] .dpk .dpk-d,:root [data-style="studio"] .dpk .dpk-d,:root [data-style="studio"].dpk .dpk-d{width:32px;height:32px;justify-self:center;border-radius:50%;transition:background-color var(--dm-t-fast) var(--dm-ease),color var(--dm-t-fast) var(--dm-ease)}
:root[data-style="studio"] .dpk .dpk-d.t::after,:root [data-style="studio"] .dpk .dpk-d.t::after,:root [data-style="studio"].dpk .dpk-d.t::after{bottom:3px;width:3px;height:3px;margin-left:-1.5px}
:root[data-style="studio"] .dpk .dpk-g.in-l,:root [data-style="studio"] .dpk .dpk-g.in-l,:root [data-style="studio"].dpk .dpk-g.in-l{animation:clpkl var(--dm-t) var(--dm-ease) both}
:root[data-style="studio"] .dpk .dpk-g.in-r,:root [data-style="studio"] .dpk .dpk-g.in-r,:root [data-style="studio"].dpk .dpk-g.in-r{animation:clpkr var(--dm-t) var(--dm-ease) both}
@keyframes clpkl{from{opacity:0;transform:translateX(-16px)}}
@keyframes clpkr{from{opacity:0;transform:translateX(16px)}}
.dpk-nl{width:100%;height:32px;border:0;outline:0;border-radius:var(--dm-r-field);background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line) 50%,transparent);padding:0 10px;font:inherit;font-size:13px;color:var(--dm-fg);transition:box-shadow var(--dm-t-fast)}
@supports (corner-shape:squircle){.dpk-nl{corner-shape:squircle;border-radius:var(--dm-r-field-sq)}}
.dpk-nl::placeholder{color:var(--dm-muted)}
.dpk-nl:focus{box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line-strong) 50%,transparent)}
.dpk-qk{display:flex;gap:var(--dm-s-1);margin:var(--dm-s-2) 0}
.dpk-mh{display:flex;align-items:center;justify-content:space-between;margin:var(--dm-s-1) 0}
.dpk-mh b{font-weight:500;font-size:13px}
.dpk-mh .ib{width:26px;height:26px}
.dpk-wd,.dpk-g{display:grid;grid-template-columns:repeat(7,1fr);text-align:center}
.dpk-wd{font-size:11px;margin-bottom:2px}
.dpk-vp{overflow:hidden}
.dpk-g{gap:2px 0}
.dpk-g.in-l{animation:dpkl .4s var(--dm-ease) both}.dpk-g.in-r{animation:dpkr .4s var(--dm-ease) both}
@keyframes dpkl{from{opacity:0;transform:translateX(-24px);filter:blur(3px)}}
@keyframes dpkr{from{opacity:0;transform:translateX(24px);filter:blur(3px)}}
.dpk-d{height:32px;border-radius:calc(9px * var(--dm-rk-m,var(--dm-rk)));font-size:12.5px;color:var(--dm-fg);transition:background .2s,color .2s;position:relative}
@media (hover:hover) and (pointer:fine){.dpk-d:hover{background:var(--dm-row)}}
/* the days fill the month's sliding view, which would cut an outside ring: theirs is drawn inside */
.dpk-d:focus-visible{outline-offset:-2px}
.dpk-d.o{color:var(--dm-muted)}
.dpk-d.t::after{content:"";position:absolute;left:50%;bottom:4px;width:4px;height:4px;margin-left:-2px;border-radius:50%;background:var(--dm-accent-mark,var(--dm-accent))}
.dpk-d.s{background:var(--dm-accent);color:var(--dm-on-accent)}
.dpk-d.s::after{background:var(--dm-on-accent)}
.dpk-d.s{animation:dpks .35s var(--dm-ease)}
/* in the date picker a picked day takes its fill without growing (owner, 18 Nov: nothing scales on a click) */
.dpk .dpk-d.s{animation:none}
/* on the picked day the inside ring would sit on the accent fill: a surface gap inside it keeps it seen */
.dpk-d.s:focus-visible{box-shadow:inset 0 0 0 3.5px var(--dm-surface)}
@keyframes dpks{from{transform:scale(.8)}}
date-picker.jsHTML · 73 lines
DM.def('date-picker', (r, c) => {
  r.innerHTML = r.innerHTML.replace('{cal}', I('cal','xs')).replace('{chev}', I('chev-d','xs')).replace('{l}', I('chev-l','xs')).replace('{r}', I('chev-r','xs'));
  // today is the real today: the calendar opens on its month and the quick picks and typed dates count from it
  const now = new Date(), TODAY = new Date(now.getFullYear(), now.getMonth(), now.getDate()); let view = new Date(TODAY.getFullYear(), TODAY.getMonth(), 1), sel = null;
  const g = $('.dpk-g', r), m = $('.dpk-m', r), val = $('.dpk-val', r), f = $('.dpk-f', r); let cursor = null;
  // Studio's one hover: a neutral circle that glides from day to day (hoverGlide)
  c.offs.push(hoverGlide($('.dpk-vp', r), '.dpk-d', { keep:'.s' }));
  val.classList.add('ph');
  const same = (a, b) => a && b && a.toDateString() === b.toDateString();
  const fmt = d => d.toLocaleDateString('en-US', { weekday:'short', month:'short', day:'numeric', year:'numeric' });
  function paint(dir){
    m.textContent = view.toLocaleDateString('en-US', { month:'long', year:'numeric' });
    const first = new Date(view.getFullYear(), view.getMonth(), 1), start = new Date(first); start.setDate(1 - ((first.getDay() + 6) % 7));
    // one day takes Tab, the one the arrows last reached, or the picked day, today or the 1st; the arrows move from it
    const inView = d => d && d.getMonth() === view.getMonth() && d.getFullYear() === view.getFullYear(), tab = [cursor, sel, TODAY].find(inView) || first;
    let h = ''; for (let i = 0; i < 42; i++){ const d = new Date(start); d.setDate(start.getDate() + i); h += `<button class="dpk-d${d.getMonth() !== view.getMonth() ? ' o' : ''}${same(d, TODAY) ? ' t' : ''}${same(d, sel) ? ' s' : ''}" data-t="${d.getTime()}" tabindex="${same(d, tab) ? 0 : -1}" aria-label="${fmt(d)}" aria-pressed="${!!same(d, sel)}"${same(d, TODAY) ? ' aria-current="date"' : ''}>${d.getDate()}</button>`; }
    g.innerHTML = h; if (dir){ g.classList.remove('in-l','in-r'); void g.offsetWidth; g.classList.add(dir < 0 ? 'in-l' : 'in-r'); }
    $$('.dpk-d', g).forEach(b => b.onclick = () => pick(new Date(+b.dataset.t)));
  }
  function pick(d, keep){ sel = d; val.classList.remove('ph'); swap(val, fmt(d)); if (d.getMonth() !== view.getMonth() || d.getFullYear() !== view.getFullYear()){ const dir = d > view ? 1 : -1; view = new Date(d.getFullYear(), d.getMonth(), 1); paint(dir); } else paint(); if (!keep) c.later(260, () => closePops()); }
  $('.dpk-pv', r).onclick = () => { view = new Date(view.getFullYear(), view.getMonth() - 1, 1); paint(-1); };
  $('.dpk-nx', r).onclick = () => { view = new Date(view.getFullYear(), view.getMonth() + 1, 1); paint(1); };
  $$('.dpk-qk .chip', r).forEach(b => b.onclick = () => { const d = new Date(TODAY); d.setDate(d.getDate() + +b.dataset.d); pick(d); });
  const W = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];
  $('.dpk-nl', r).oninput = e => { const s = e.target.value.toLowerCase().trim(); let d = null;
    const wd = W.findIndex(w => s.includes(w)); const n = s.match(/in (\d+) (day|week|month)s?/);
    if (n){ d = new Date(TODAY); if (n[2] === 'day') d.setDate(d.getDate() + +n[1]); if (n[2] === 'week') d.setDate(d.getDate() + n[1] * 7); if (n[2] === 'month') d.setMonth(d.getMonth() + +n[1]); }
    else if (wd >= 0){ d = new Date(TODAY); let add = (wd - d.getDay() + 7) % 7 || 7; if (s.includes('next') && add < 7) add += 0; d.setDate(d.getDate() + add); }
    else if (s === 'tomorrow'){ d = new Date(TODAY); d.setDate(d.getDate() + 1); }
    if (d) pick(d, true); };
  $('.dpk-nl', r).onkeydown = e => { if (e.key === 'Enter' && sel) closePops(); };
  // the keyboard in the calendar: the arrows move a day or a week, across months too; Home and End go to the start and end
  // of the week; Page Up and Page Down move a month, or a year with Shift; Enter or Space picks the day
  const dayBtn = d => $$('.dpk-d', g).find(b => same(new Date(+b.dataset.t), d));
  function goDay(d){
    cursor = d;
    if (d.getMonth() !== view.getMonth() || d.getFullYear() !== view.getFullYear()){ const dir = d > view ? 1 : -1; view = new Date(d.getFullYear(), d.getMonth(), 1); paint(dir); } else paint();
    const b = dayBtn(d); if (b) b.focus({ preventScroll:true });
  }
  c.on(g, 'keydown', e => {
    const b = e.target.closest('.dpk-d'); if (!b) return;
    const d = new Date(+b.dataset.t), wd = (d.getDay() + 6) % 7, at = n => new Date(d.getFullYear(), d.getMonth(), d.getDate() + n);
    const mon = (y, mo) => new Date(y, mo, Math.min(d.getDate(), new Date(y, mo + 1, 0).getDate()));
    const n = { ArrowLeft:at(-1), ArrowRight:at(1), ArrowUp:at(-7), ArrowDown:at(7), Home:at(-wd), End:at(6 - wd),
      PageUp:e.shiftKey ? mon(d.getFullYear() - 1, d.getMonth()) : mon(d.getFullYear(), d.getMonth() - 1),
      PageDown:e.shiftKey ? mon(d.getFullYear() + 1, d.getMonth()) : mon(d.getFullYear(), d.getMonth() + 1) }[e.key];
    if (!n) return; e.preventDefault(); goDay(n);
  });
  // opened with the keyboard, the calendar takes the focus on the picked day, or today
  c.on(f, 'click', e => { if (e.isTrusted && !e.detail) requestAnimationFrame(() => { if (f.getAttribute('aria-expanded') !== 'true') return; cursor = null; paint(); const b = $('.dpk-d[tabindex="0"]', g); if (b) b.focus({ preventScroll:true }); }); });
  paint();
  // the field sits centred on its own, and glides up as the calendar opens under it, so the two sit centred together; on
  // closing, it waits for the calendar to fade before it glides back to the middle
  const box = $('.dpk', r), pop = $('.dpk-pop', r), isOpen = () => f.getAttribute('aria-expanded') === 'true';
  const centred = centre(c, box, () => isOpen() ? Math.max(box.offsetHeight, pop.offsetTop + pop.offsetHeight) : box.offsetHeight);
  // (the wait is the glide's own transition delay, so a busy page can't hold the field up there longer than the fade)
  const mo = new MutationObserver(() => { box.style.transitionDelay = isOpen() || REDUCE ? '' : '.18s'; centred(); });
  mo.observe(f, { attributes:true, attributeFilter:['aria-expanded'] }); c.offs.push(() => mo.disconnect());
  // the preview opens it, types a date, and closes it on the day it found: the closed field shows the date, centred again.
  // Clicking, focusing or typing in it takes over, and what it had typed goes. One popover is open at a time on the page, so
  // the preview waits while another card's demo has its menu open, and never takes it from that card; if another card closes
  // this calendar while it types, the typing waits and goes on once the calendar is open again
  const free = async () => { for (let k = 0; k < 40 && openPop && openPop.pop !== pop; k++) await c.step(250); };
  const shown = async () => { if (isOpen()) return; await free(); f.click(); await c.step(400); };
  c.auto(800, () => c.run(async () => {
    const i = $('.dpk-nl', r); let typed = '';
    c.taken(() => { if (typed && i.isConnected && i.value === typed){ i.value = ''; i.oninput({ target:i }); } });
    await shown(); await c.step(100);
    for (const ch of 'next friday'){ await shown(); await c.step(70); typed += ch; i.value = typed; i.oninput({ target:i }); }
    typed = '';
    await shown(); await c.step(2400); if (isOpen()) closePops(); }));
});
06

Questions

What people ask about it.

What does "friday" pick if today is Friday?
The default parser always picks the next one, so it returns the Friday a week later, never today. "next friday" works the same way.
Can I use my own natural language parser?
Yes. Pass parse, a function that takes the typed text and today's date and returns a Date or null. You could call your agent or a library like chrono from there, as long as it returns synchronously.
Why should I pass today when rendering on the server?
The today dot and the presets are based on the current date. Passing today keeps server and client output the same and makes demos and tests repeatable.
Can I change the quick picks?
Yes. Pass presets as a list of a label and a number of days after today, like { label: 'Next sprint', days: 14 }.
07

More in Dates

Pickers for single dates and date ranges.

Segmented controlSwitch between views of the same thing, with a sliding indicator.
Event calendarA week view where the agent finds a free slot for you.
Meeting cardThe next meeting from countdown to wrap-up, with the time already gone hatched out and a join that stays open.