सुलभता पैटर्न · फ़ॉर्म इनपुट
डेट पिकर
तारीख़ टाइप करने की छूट हमेशा है, फ़ॉर्मैट दिखाया और जाँचा जाता है; कैलेंडर दूसरा रास्ता है, अकेला कभी नहीं। कैलेंडर एक ग्रिड के चारों ओर बना मोडल डायलॉग है: Tab क्रम में एक ही दिन रहता है, ऐरो कुंजियाँ दिन और हफ़्ते के हिसाब से चलती हैं, Page Up और Page Down महीने के हिसाब से, और Enter दिन चुनकर फ़ोकस वापस भेजता है।
- डब्ल्यूसीएजी मानदंड
- 7
- कीबोर्ड नियम
- 9
- जाँच का तरीक़ा
- axe, कीबोर्ड और इंस्पेक्टर
लाइव डेमो
आज़माएँ
इसे माउस, कीबोर्ड या स्क्रीन रीडर से इस्तेमाल करें। साथ का इंस्पेक्टर दिखाता है कि आपके इस्तेमाल के दौरान ब्राउज़र सहायक तकनीक को क्या बताता है: फ़ोकस, स्थिति में बदलाव और घोषणाएँ।
मुंबई CSMT से पुणे जंक्शन
डेक्कन क्वीन, चेयर कार
फ़ॉर्मैट: DD/MM/YYYY। ट्रेन आज से आगे की तारीख़ों के लिए बुक की जा सकती है।
जावास्क्रिप्ट के बिना डेमो उतना ही काम करता है जितना उसका एचटीएमएल; इंस्पेक्टर के लिए जावास्क्रिप्ट चाहिए।
कीबोर्ड
जिन कुंजियों पर यह काम करता है
हर क्रिया पॉइंटर के बिना काम करती है।
| कुंजी | यह क्या करती है |
|---|---|
| EnterयाSpace | कैलेंडर बटन पर, डायलॉग खोलता है और फ़ोकस चुनी गई तारीख़ पर, या आज पर रखता है। |
| Arrow LeftयाArrow Right | ग्रिड में, पिछले या अगले दिन पर जाता है, ज़रूरत हो तो अगले महीने में भी। |
| Arrow UpयाArrow Down | पिछले या अगले हफ़्ते के उसी दिन पर जाता है। |
| HomeयाEnd | हफ़्ते के पहले (सोमवार) या आख़िरी (रविवार) दिन पर जाता है। |
| Page UpयाPage Down | पिछला या अगला महीना दिखाता है, जहाँ हो सके महीने की वही तारीख़ रखते हुए। |
| ShiftPage UpयाShiftPage Down | पिछले या अगले साल का वही महीना दिखाता है। |
| EnterयाSpace | किसी दिन पर, उसे चुनता है, फ़ील्ड भरता है, डायलॉग बंद करता है और फ़ोकस कैलेंडर बटन पर लौटाता है। |
| Escape | तारीख़ बदले बिना डायलॉग बंद करता है; फ़ोकस कैलेंडर बटन पर लौटता है। |
| TabयाShiftTab | सिर्फ़ डायलॉग के अंदर, महीने और साल के बटनों, ग्रिड और रद्द करें के बीच चलता है। |
स्क्रीन रीडर
यह क्या घोषित करता है
मार्कअप की भूमिकाओं, नामों और स्थितियों से लिखा गया।
| कब | अपेक्षित घोषणा |
|---|---|
| फ़ोकस कैलेंडर बटन पर पहुँचता है | तारीख़ चुनें, बटन, डायलॉग खोलता है |
| डायलॉग खुलता है | यात्रा की तारीख़ चुनें, डायलॉग। ऐरो कुंजियाँ दिन और हफ़्ते के हिसाब से चलती हैं। सोमवार, 5 अक्तूबर 2026, आज की तारीख़ |
| Page Down अगला महीना दिखाता है | नवंबर 2026 (लाइव महीने के नाम से), फिर फ़ोकस वाला दिन |
| Enter कोई दिन चुनता है | तारीख़ बदलें, शनिवार, 14 नवंबर 2026, बटन |
| टाइप की गई तारीख़ पढ़ी नहीं जा सकती | तारीख़ DD/MM/YYYY के रूप में लिखें। |
ये अपेक्षित घोषणाएँ हैं, रिकॉर्डिंग नहीं। स्क्रीन रीडर और ब्राउज़र के हिसाब से शब्द और क्रम बदलते हैं।
कोड
कोड कॉपी करें
वही फ़ाइलें जिन पर यह डेमो चलता है। स्टाइल एक प्रीफ़िक्स के साथ परोसी जाती हैं जो इस साइट की अपनी स्टाइल को डेमो से दूर रखता है; आप जो कॉपी करते हैं वह मूल है।
<div class="ap-date-picker" data-ap-date-picker data-region="IN">
<div class="ap-date-picker__trip">
<span class="ap-date-picker__badge" aria-hidden="true"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="6" y="3" width="12" height="14" rx="3"/><path d="M6 11h12M9 21l1.5-4M15 21l-1.5-4"/><path d="M9.5 14h.01M14.5 14h.01"/></svg></span>
<div>
<p class="ap-date-picker__route">Mumbai CSMT to Pune Junction</p>
<p class="ap-date-picker__train">Deccan Queen, chair car</p>
</div>
</div>
<div class="ap-field">
<label class="ap-label" for="date-picker-input">Journey date</label>
<p class="ap-hint" id="date-picker-hint">Format: DD/MM/YYYY. Trains can be booked from today.</p>
<div class="ap-date-picker__group" data-ap-group>
<input class="ap-input ap-date-picker__input" id="date-picker-input" type="text" autocomplete="off" spellcheck="false" aria-describedby="date-picker-hint date-picker-error" />
<button type="button" class="ap-date-picker__open" data-ap-open aria-haspopup="dialog">
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="3.5" y="5" width="17" height="15" rx="2.5"/><path d="M3.5 10h17M8 3v4M16 3v4"/><path d="M8 14h.01M12 14h.01M16 14h.01M8 17h.01M12 17h.01"/></svg>
<span class="ap-date-picker__sr" data-ap-word>Choose date</span>
<span class="ap-date-picker__sr" data-ap-chosen></span>
</button>
</div>
<p class="ap-error ap-date-picker__error" id="date-picker-error" aria-live="polite">
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><circle cx="12" cy="12" r="9"/><path d="M12 7.5v5.5M12 16.5h.01"/></svg>
<span data-ap-error></span>
</p>
</div>
<dialog class="ap-date-picker__dialog" aria-label="Choose journey date" aria-describedby="date-picker-keys">
<div class="ap-date-picker__panel">
<div class="ap-date-picker__head">
<button type="button" class="ap-date-picker__nav" data-ap-step="-12" aria-label="Previous year"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="m12 17-5-5 5-5M18 17l-5-5 5-5"/></svg></button>
<button type="button" class="ap-date-picker__nav" data-ap-step="-1" aria-label="Previous month"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="m15 18-6-6 6-6"/></svg></button>
<h2 class="ap-date-picker__month" id="date-picker-month" aria-live="polite" data-ap-month></h2>
<button type="button" class="ap-date-picker__nav" data-ap-step="1" aria-label="Next month"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="m9 18 6-6-6-6"/></svg></button>
<button type="button" class="ap-date-picker__nav" data-ap-step="12" aria-label="Next year"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="m12 17 5-5-5-5M6 17l5-5-5-5"/></svg></button>
</div>
<table class="ap-date-picker__grid" role="grid" aria-labelledby="date-picker-month">
<thead>
<tr>
<th scope="col"><span aria-hidden="true">Mo</span><span class="ap-date-picker__sr">Monday</span></th>
<th scope="col"><span aria-hidden="true">Tu</span><span class="ap-date-picker__sr">Tuesday</span></th>
<th scope="col"><span aria-hidden="true">We</span><span class="ap-date-picker__sr">Wednesday</span></th>
<th scope="col"><span aria-hidden="true">Th</span><span class="ap-date-picker__sr">Thursday</span></th>
<th scope="col"><span aria-hidden="true">Fr</span><span class="ap-date-picker__sr">Friday</span></th>
<th scope="col"><span aria-hidden="true">Sa</span><span class="ap-date-picker__sr">Saturday</span></th>
<th scope="col"><span aria-hidden="true">Su</span><span class="ap-date-picker__sr">Sunday</span></th>
</tr>
</thead>
<tbody data-ap-days></tbody>
</table>
<div class="ap-date-picker__foot">
<ul class="ap-date-picker__legend">
<li><span class="ap-date-picker__key ap-date-picker__key--today" aria-hidden="true"></span>Today</li>
<li><span class="ap-date-picker__key ap-date-picker__key--selected" aria-hidden="true"></span>Selected</li>
<li><span class="ap-date-picker__key ap-date-picker__key--past" aria-hidden="true">8</span>Unavailable</li>
</ul>
</div>
<div class="ap-date-picker__end">
<p class="ap-date-picker__keys" id="date-picker-keys">Arrow keys move by day and week. Page Up and Page Down change the month.</p>
<button type="button" class="ap-btn ap-date-picker__cancel" data-ap-cancel>Cancel</button>
</div>
</div>
</dialog>
</div>
/* Shared primitives: buttons and form fields used across the patterns.
Uses the --ap-* design tokens. */
.ap-btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
min-height: 40px;
padding: 0 16px;
border: 1px solid var(--ap-border-strong);
border-radius: var(--ap-radius-sm);
background: var(--ap-surface);
color: var(--ap-text);
font: inherit;
font-weight: 600;
line-height: 1.2;
text-decoration: none;
white-space: nowrap;
cursor: pointer;
transition: background-color var(--ap-duration) var(--ap-ease), border-color var(--ap-duration) var(--ap-ease), box-shadow var(--ap-duration) var(--ap-ease);
}
.ap-btn:hover {
background: var(--ap-surface-2);
}
.ap-btn:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 2px;
}
.ap-btn:disabled,
.ap-btn[aria-disabled="true"] {
opacity: .55;
cursor: not-allowed;
}
.ap-btn--primary {
border-color: var(--ap-accent);
background: var(--ap-accent);
color: var(--ap-on-accent);
box-shadow: var(--ap-shadow-sm);
}
.ap-btn--primary:hover {
border-color: var(--ap-accent-hover);
background: var(--ap-accent-hover);
}
.ap-btn--danger {
border-color: var(--ap-danger);
background: var(--ap-danger);
color: var(--ap-surface);
}
.ap-btn--danger:hover {
filter: brightness(1.08);
}
.ap-btn--ghost {
border-color: transparent;
background: transparent;
}
.ap-btn--ghost:hover {
background: var(--ap-surface-2);
}
.ap-btn__icon {
width: 18px;
height: 18px;
flex-shrink: 0;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
.ap-field {
display: grid;
gap: 6px;
}
.ap-label {
color: var(--ap-text);
font-weight: 600;
font-size: .9375rem;
}
.ap-hint {
margin: 0;
color: var(--ap-text-3);
font-size: .875rem;
line-height: 1.45;
}
.ap-error {
display: flex;
align-items: flex-start;
gap: 6px;
margin: 0;
color: var(--ap-danger);
font-size: .875rem;
font-weight: 600;
line-height: 1.45;
}
.ap-input {
width: 100%;
min-height: 44px;
padding: 10px 12px;
border: 1px solid var(--ap-border-strong);
border-radius: var(--ap-radius-sm);
background: var(--ap-surface);
color: var(--ap-text);
font: inherit;
line-height: 1.4;
transition: border-color var(--ap-duration) var(--ap-ease), box-shadow var(--ap-duration) var(--ap-ease);
}
.ap-input::placeholder {
color: var(--ap-text-3);
}
.ap-input:hover {
border-color: var(--ap-text-2);
}
.ap-input:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 1px;
border-color: var(--ap-focus);
}
.ap-input[aria-invalid="true"] {
border-color: var(--ap-danger);
box-shadow: inset 4px 0 0 var(--ap-danger);
}
textarea.ap-input {
resize: vertical;
min-height: 88px;
}
@media (prefers-reduced-motion: reduce) {
.ap-btn,
.ap-input { transition: none; }
}
/* Date picker. Uses the --ap-* design tokens and the shared primitives. */
.ap-date-picker {
display: grid;
gap: 20px;
width: min(100%, 420px);
margin-inline: auto;
padding: 22px 24px 24px;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius-lg);
background: var(--ap-surface);
color: var(--ap-text);
box-shadow: var(--ap-shadow-md);
font-family: var(--ap-font);
}
/* The journey the date is for */
.ap-date-picker__trip {
display: flex;
align-items: center;
gap: 14px;
padding-bottom: 18px;
border-bottom: 1px solid var(--ap-border);
}
.ap-date-picker__badge {
display: grid;
place-items: center;
width: 44px;
height: 44px;
flex-shrink: 0;
border-radius: var(--ap-radius);
background: var(--ap-accent-soft);
color: var(--ap-accent-soft-text);
}
.ap-date-picker__badge svg {
width: 24px;
height: 24px;
fill: none;
stroke: currentColor;
stroke-width: 1.8;
stroke-linecap: round;
stroke-linejoin: round;
}
.ap-date-picker__route {
margin: 0;
font-weight: 650;
line-height: 1.35;
}
.ap-date-picker__train {
margin: 2px 0 0;
color: var(--ap-text-3);
font-size: .875rem;
}
/* The field and its calendar button, joined */
.ap-date-picker__group {
display: flex;
}
.ap-date-picker__input {
flex: 1;
min-width: 0;
border-start-end-radius: 0;
border-end-end-radius: 0;
font-variant-numeric: tabular-nums;
letter-spacing: .02em;
}
.ap-date-picker__input:focus-visible {
position: relative;
z-index: 1;
}
.ap-date-picker__open {
display: grid;
place-items: center;
width: 48px;
min-height: 44px;
flex-shrink: 0;
margin-inline-start: -1px;
padding: 0;
border: 1px solid var(--ap-border-strong);
border-start-end-radius: var(--ap-radius-sm);
border-end-end-radius: var(--ap-radius-sm);
background: var(--ap-surface-2);
color: var(--ap-text-2);
cursor: pointer;
transition: background-color var(--ap-duration) var(--ap-ease), color var(--ap-duration) var(--ap-ease);
}
.ap-date-picker__open:hover {
background: var(--ap-surface-3);
color: var(--ap-text);
}
.ap-date-picker__open:focus-visible {
position: relative;
z-index: 1;
outline: 2px solid var(--ap-focus);
outline-offset: 1px;
}
.ap-date-picker__open svg {
width: 22px;
height: 22px;
fill: none;
stroke: currentColor;
stroke-width: 1.8;
stroke-linecap: round;
stroke-linejoin: round;
}
/* The error line takes no room until it has words. */
.ap-date-picker__error svg {
width: 18px;
height: 18px;
flex-shrink: 0;
margin-top: 1px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
}
.ap-date-picker__error:has(> span:empty) svg {
display: none;
}
.ap-date-picker__error:has(> span:empty) {
margin-top: -6px;
}
.ap-date-picker__sr {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
/* The calendar dialog */
.ap-date-picker__dialog {
width: 336px;
max-width: calc(100vw - 16px);
padding: 0;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius-lg);
background: var(--ap-surface);
color: var(--ap-text);
box-shadow: var(--ap-shadow-lg);
font-family: var(--ap-font);
opacity: 1;
transform: none;
transition: opacity 160ms var(--ap-ease), transform 160ms var(--ap-ease), overlay 160ms allow-discrete, display 160ms allow-discrete;
}
.ap-date-picker__dialog:not([open]) {
opacity: 0;
transform: translateY(-4px);
}
@starting-style {
.ap-date-picker__dialog[open] {
opacity: 0;
transform: translateY(-4px);
}
}
.ap-date-picker__dialog::backdrop {
background: var(--ap-scrim, rgb(9 9 11 / .5));
opacity: .4;
}
.ap-date-picker__panel {
padding: 14px 16px 16px;
}
.ap-date-picker__head {
display: flex;
align-items: center;
gap: 2px;
margin-bottom: 8px;
}
.ap-date-picker__month {
flex: 1;
margin: 0;
font-family: var(--ap-font);
font-size: 1rem;
font-weight: 650;
line-height: 1.3;
text-align: center;
}
.ap-date-picker__nav {
display: grid;
place-items: center;
width: 36px;
height: 36px;
flex-shrink: 0;
padding: 0;
border: 0;
border-radius: var(--ap-radius-sm);
background: transparent;
color: var(--ap-text-2);
cursor: pointer;
}
.ap-date-picker__nav:hover {
background: var(--ap-surface-2);
color: var(--ap-text);
}
.ap-date-picker__nav:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 1px;
}
.ap-date-picker__nav[aria-disabled="true"] {
opacity: .4;
cursor: not-allowed;
}
.ap-date-picker__nav[aria-disabled="true"]:hover {
background: transparent;
}
.ap-date-picker__nav svg {
width: 18px;
height: 18px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
.ap-date-picker__grid {
width: 100%;
border: 0;
border-collapse: separate;
border-spacing: 2px;
table-layout: fixed;
background: transparent;
}
.ap-date-picker__grid th,
.ap-date-picker__grid td {
padding: 0;
border: 0;
background: transparent;
text-align: center;
}
.ap-date-picker__grid th {
height: 30px;
color: var(--ap-text-3);
font-size: .75rem;
font-weight: 650;
letter-spacing: .04em;
text-transform: uppercase;
}
.ap-date-picker__grid td {
height: 40px;
border-radius: var(--ap-radius-full);
color: var(--ap-text);
font-size: .9375rem;
font-variant-numeric: tabular-nums;
}
.ap-date-picker__grid td[data-date] {
cursor: pointer;
transition: background-color var(--ap-duration) var(--ap-ease);
}
.ap-date-picker__grid td[data-date]:hover {
background: var(--ap-surface-2);
}
.ap-date-picker__grid td[data-date]:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 1px;
}
/* Today: a ring and bold type */
.ap-date-picker__grid td[aria-current="date"] {
box-shadow: inset 0 0 0 1.5px var(--ap-border-strong);
font-weight: 750;
}
/* Chosen: a solid fill */
.ap-date-picker__grid td[aria-selected="true"],
.ap-date-picker__grid td[aria-selected="true"]:hover {
background: var(--ap-accent);
color: var(--ap-on-accent);
font-weight: 700;
}
/* Unavailable: struck through */
.ap-date-picker__grid td[aria-disabled="true"],
.ap-date-picker__grid td[aria-disabled="true"]:hover {
background: transparent;
color: var(--ap-text-3);
text-decoration: line-through;
cursor: default;
}
.ap-date-picker__foot {
margin-top: 8px;
padding-top: 12px;
border-top: 1px solid var(--ap-border);
}
.ap-date-picker__end {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
}
.ap-date-picker__legend {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: 6px 12px;
margin: 0;
padding: 0;
list-style: none;
color: var(--ap-text-2);
font-size: .8125rem;
}
.ap-date-picker__legend li {
display: inline-flex;
align-items: center;
gap: 6px;
}
.ap-date-picker__key {
display: inline-grid;
place-items: center;
width: 18px;
height: 18px;
border-radius: var(--ap-radius-full);
font-size: .75rem;
font-weight: 600;
}
.ap-date-picker__key--today {
box-shadow: inset 0 0 0 1.5px var(--ap-border-strong);
}
.ap-date-picker__key--selected {
background: var(--ap-accent);
}
.ap-date-picker__key--past {
color: var(--ap-text-3);
text-decoration: line-through;
}
.ap-date-picker__cancel {
flex-shrink: 0;
min-height: 36px;
padding: 0 14px;
}
.ap-date-picker__keys {
margin: 0;
color: var(--ap-text-3);
font-size: .8125rem;
line-height: 1.45;
}
@media (prefers-reduced-motion: reduce) {
.ap-date-picker__dialog,
.ap-date-picker__open,
.ap-date-picker__grid td[data-date] { transition: none; }
}
/**
* Date picker: a text field that takes a typed date (DD/MM/YYYY), and a
* button that opens a modal calendar dialog with a keyboard grid of days.
*
* Markup: [data-ap-date-picker] holding an input, a wrapper [data-ap-group]
* that the dialog is anchored to, a button[data-ap-open] with hidden name
* parts [data-ap-word] and [data-ap-chosen], an error [data-ap-error] in a
* polite live region, and a dialog with buttons [data-ap-step] (months to
* move), a live month name [data-ap-month], a table[role=grid] whose tbody
* [data-ap-days] the script fills, and a button[data-ap-cancel].
* Days before today cannot be chosen. data-region on the root (for example
* "IN") is added to a bare page language when dates are formatted.
*/
const MESSAGES = {
format: "Enter the date as DD/MM/YYYY.",
real: "There is no such date. Check the day and month.",
past: "Choose today or a later date.",
};
const startOfDay = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate());
const sameDay = (a, b) => !!a && !!b && a.getTime() === b.getTime();
const addDays = (d, n) => new Date(d.getFullYear(), d.getMonth(), d.getDate() + n);
const daysIn = (y, m) => new Date(y, m + 1, 0).getDate();
const pad = (n) => String(n).padStart(2, "0");
const iso = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
const dmy = (d) => `${pad(d.getDate())}/${pad(d.getMonth() + 1)}/${d.getFullYear()}`;
/** The same day of the month, months away, or the last day when that month is shorter. */
function addMonths(d, n) {
const y = d.getFullYear();
const m = d.getMonth() + n;
return new Date(y, m, Math.min(d.getDate(), daysIn(y, m)));
}
/** Reads DD/MM/YYYY (or with dots, dashes or spaces). Returns { date } or { error }. */
function parse(text, min) {
const m = text.trim().match(/^(\d{1,2})[/.\- ](\d{1,2})[/.\- ](\d{4})$/);
if (!m) return { error: "format" };
const [day, month, year] = [Number(m[1]), Number(m[2]), Number(m[3])];
const date = new Date(year, month - 1, day);
if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) return { error: "real" };
if (date < min) return { error: "past" };
return { date };
}
export function init(root) {
const input = root.querySelector("input");
const group = root.querySelector("[data-ap-group]");
const opener = root.querySelector("[data-ap-open]");
const word = opener.querySelector("[data-ap-word]");
const chosen = opener.querySelector("[data-ap-chosen]");
const error = root.querySelector("[data-ap-error]");
const dialog = root.querySelector("dialog");
const monthName = dialog.querySelector("[data-ap-month]");
const days = dialog.querySelector("[data-ap-days]");
const steps = [...dialog.querySelectorAll("[data-ap-step]")];
const html = document.documentElement;
const events = new AbortController();
let selected = null;
let focusDate = null;
let shownMonth = "";
let scrollWas = "";
const today = () => startOfDay(new Date());
// Formatted in the page's language, so a translated page gets its own month names.
function formatter(options) {
const lang = root.closest("[lang]")?.lang || navigator.language;
const region = root.dataset.region;
const locale = region && !lang.includes("-") ? `${lang}-${region}` : lang;
try {
return new Intl.DateTimeFormat(locale, options);
} catch {
return new Intl.DateTimeFormat(undefined, options);
}
}
/* ── The field ── */
function showError(kind) {
const message = kind ? MESSAGES[kind] : "";
if (error.textContent !== message) error.textContent = message;
if (kind) input.setAttribute("aria-invalid", "true");
else input.removeAttribute("aria-invalid");
}
function setSelected(date) {
selected = date;
if (date) {
word.textContent = "Change date,";
chosen.textContent = formatter({ weekday: "long", day: "numeric", month: "long", year: "numeric" }).format(date);
} else {
word.textContent = "Choose date";
chosen.textContent = "";
}
}
function checkField() {
const text = input.value.trim();
if (!text) {
showError(null);
setSelected(null);
return;
}
const result = parse(text, today());
if (result.error) {
showError(result.error);
setSelected(null);
return;
}
input.value = dmy(result.date);
showError(null);
setSelected(result.date);
}
function onFieldKey(event) {
if (event.key === "Enter") checkField();
}
/* ── The grid ── */
function render() {
const y = focusDate.getFullYear();
const m = focusDate.getMonth();
const min = today();
const now = today();
const name = formatter({ weekday: "long", day: "numeric", month: "long", year: "numeric" });
const key = `${y}-${m}`;
// The month name is a live region: write it only when the month changes.
if (key !== shownMonth) {
monthName.textContent = formatter({ month: "long", year: "numeric" }).format(focusDate);
shownMonth = key;
}
const lead = (new Date(y, m, 1).getDay() + 6) % 7; // Monday first
const total = daysIn(y, m);
const rows = [];
for (let r = 0; r < 6; r++) {
const tr = document.createElement("tr");
for (let c = 0; c < 7; c++) {
const n = r * 7 + c - lead + 1;
const td = document.createElement("td");
if (n >= 1 && n <= total) {
const date = new Date(y, m, n);
td.textContent = String(n);
td.dataset.date = iso(date);
td.setAttribute("aria-label", name.format(date));
td.tabIndex = sameDay(date, focusDate) ? 0 : -1;
// showModal() focuses the [autofocus] element, so focus lands on the day, not the first button.
if (td.tabIndex === 0) td.autofocus = true;
if (sameDay(date, now)) td.setAttribute("aria-current", "date");
if (sameDay(date, selected)) td.setAttribute("aria-selected", "true");
if (date < min) td.setAttribute("aria-disabled", "true");
}
tr.append(td);
}
rows.push(tr);
}
days.replaceChildren(...rows);
// Earlier months hold nothing that can be chosen.
const atStart = y === min.getFullYear() && m === min.getMonth();
for (const b of steps) {
if (Number(b.dataset.apStep) < 0) b.setAttribute("aria-disabled", String(atStart));
}
}
function cellFor(date) {
return days.querySelector(`[data-date="${iso(date)}"]`);
}
function moveTo(date, focus = true) {
const min = today();
focusDate = date < min ? min : date;
render();
if (focus) cellFor(focusDate)?.focus();
}
function choose(date) {
if (date < today()) return;
input.value = dmy(date);
showError(null);
setSelected(date);
dialog.close();
}
function onGridKey(event) {
const cell = event.target.closest("td[data-date]");
if (!cell) return;
const d = focusDate;
let next = null;
switch (event.key) {
case "ArrowRight": next = addDays(d, 1); break;
case "ArrowLeft": next = addDays(d, -1); break;
case "ArrowDown": next = addDays(d, 7); break;
case "ArrowUp": next = addDays(d, -7); break;
case "Home": next = addDays(d, -((d.getDay() + 6) % 7)); break;
case "End": next = addDays(d, 6 - ((d.getDay() + 6) % 7)); break;
case "PageUp": next = addMonths(d, event.shiftKey ? -12 : -1); break;
case "PageDown": next = addMonths(d, event.shiftKey ? 12 : 1); break;
case "Enter":
case " ":
event.preventDefault();
choose(d);
return;
default: return;
}
event.preventDefault();
moveTo(next);
}
function onDialogClick(event) {
const cell = event.target.closest("td[data-date]");
if (cell) {
const [y, m, n] = cell.dataset.date.split("-").map(Number);
choose(new Date(y, m - 1, n));
return;
}
const step = event.target.closest("[data-ap-step]");
if (step) {
if (step.getAttribute("aria-disabled") === "true") return;
// Focus stays on the button; the live month name says where we are.
moveTo(addMonths(focusDate, Number(step.dataset.apStep)), false);
return;
}
if (event.target.closest("[data-ap-cancel]")) {
dialog.close();
return;
}
// The dialog element itself, outside its panel, is the backdrop.
if (event.target === dialog) dialog.close();
}
/* ── Opening and closing ── */
function place() {
dialog.style.margin = "";
dialog.style.inset = "";
const box = group.getBoundingClientRect();
const w = dialog.offsetWidth;
const h = dialog.offsetHeight;
const vw = html.clientWidth;
const vh = window.innerHeight;
// No room under the field (a phone, or the field low on screen): stay centred, as showModal() puts it.
const top = box.bottom + 8;
if (vw < w + 32 || top + h > vh - 8) return;
const left = Math.min(Math.max(8, box.right - w), vw - w - 8);
dialog.style.margin = "0";
dialog.style.inset = `${Math.round(top)}px auto auto ${Math.round(left)}px`;
}
function open() {
checkField();
scrollWas = html.style.overflow;
html.style.overflow = "hidden";
shownMonth = "";
focusDate = selected || today();
render();
dialog.showModal();
place();
cellFor(focusDate)?.focus();
}
function onClose() {
html.style.overflow = scrollWas;
opener.focus();
}
input.addEventListener("change", checkField);
input.addEventListener("keydown", onFieldKey);
opener.addEventListener("click", open);
days.addEventListener("keydown", onGridKey);
dialog.addEventListener("click", onDialogClick);
dialog.addEventListener("close", onClose);
window.addEventListener("resize", () => { if (dialog.open) place(); }, { signal: events.signal });
return () => {
events.abort();
if (dialog.open) dialog.close();
html.style.overflow = scrollWas;
input.removeEventListener("change", checkField);
input.removeEventListener("keydown", onFieldKey);
opener.removeEventListener("click", open);
days.removeEventListener("keydown", onGridKey);
dialog.removeEventListener("click", onDialogClick);
dialog.removeEventListener("close", onClose);
};
}
for (const root of document.querySelectorAll("[data-ap-date-picker]")) init(root);
/* Design tokens for the pattern components. Light by default, dark when the
system asks for it; set data-theme="dark" on :root to force dark. */
:root {
--ap-radius-sm: 8px;
--ap-radius: 12px;
--ap-radius-lg: 16px;
--ap-radius-full: 999px;
--ap-font: "Instrument Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
--ap-mono: "JetBrains Mono", ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
--ap-ease: cubic-bezier(.2, .8, .2, 1);
--ap-duration: 180ms;
--ap-target: 44px;
--ap-bg: #F4F4F5;
--ap-surface: #FFFFFF;
--ap-surface-2: #F4F4F5;
--ap-surface-3: #E4E4E7;
--ap-border: #E4E4E7;
--ap-border-strong: #76767F;
--ap-text: #18181B;
--ap-text-2: #3F3F46;
--ap-text-3: #5E5E66;
--ap-accent: #4F46E5;
--ap-accent-hover: #4338CA;
--ap-on-accent: #FFFFFF;
--ap-accent-text: #4338CA;
--ap-accent-soft: #EEF2FF;
--ap-accent-soft-text: #3730A3;
--ap-focus: #4F46E5;
--ap-danger: #B91C1C;
--ap-danger-soft: #FEF2F2;
--ap-success: #15803D;
--ap-success-soft: #F0FDF4;
--ap-warning: #A15C07;
--ap-warning-soft: #FEFCE8;
--ap-info: #1D4ED8;
--ap-info-soft: #EFF6FF;
--ap-scrim: rgb(9 9 11 / .48);
--ap-shadow-sm: 0 1px 2px rgb(9 9 11 / .06);
--ap-shadow-md: 0 1px 2px rgb(9 9 11 / .05), 0 6px 16px -4px rgb(9 9 11 / .1);
--ap-shadow-lg: 0 2px 6px rgb(9 9 11 / .06), 0 20px 40px -12px rgb(9 9 11 / .22);
--ap-light-bg: #F4F4F5;
--ap-light-surface: #FFFFFF;
--ap-light-surface-2: #F4F4F5;
--ap-light-surface-3: #E4E4E7;
--ap-light-border: #E4E4E7;
--ap-light-border-strong: #76767F;
--ap-light-text: #18181B;
--ap-light-text-2: #3F3F46;
--ap-light-text-3: #5E5E66;
--ap-light-accent: #4F46E5;
--ap-light-accent-hover: #4338CA;
--ap-light-on-accent: #FFFFFF;
--ap-light-accent-text: #4338CA;
--ap-light-accent-soft: #EEF2FF;
--ap-light-accent-soft-text: #3730A3;
--ap-light-focus: #4F46E5;
--ap-light-danger: #B91C1C;
--ap-light-danger-soft: #FEF2F2;
--ap-light-success: #15803D;
--ap-light-success-soft: #F0FDF4;
--ap-light-warning: #A15C07;
--ap-light-warning-soft: #FEFCE8;
--ap-light-info: #1D4ED8;
--ap-light-info-soft: #EFF6FF;
--ap-light-scrim: rgb(9 9 11 / .48);
--ap-light-shadow-sm: 0 1px 2px rgb(9 9 11 / .06);
--ap-light-shadow-md: 0 1px 2px rgb(9 9 11 / .05), 0 6px 16px -4px rgb(9 9 11 / .1);
--ap-light-shadow-lg: 0 2px 6px rgb(9 9 11 / .06), 0 20px 40px -12px rgb(9 9 11 / .22);
--ap-dark-bg: #09090B;
--ap-dark-surface: #18181B;
--ap-dark-surface-2: #27272A;
--ap-dark-surface-3: #3F3F46;
--ap-dark-border: #2E2E33;
--ap-dark-border-strong: #8E8E97;
--ap-dark-text: #FAFAFA;
--ap-dark-text-2: #D4D4D8;
--ap-dark-text-3: #A1A1AA;
--ap-dark-accent: #818CF8;
--ap-dark-accent-hover: #A5B4FC;
--ap-dark-on-accent: #0C0A1F;
--ap-dark-accent-text: #A5B4FC;
--ap-dark-accent-soft: #1E1B4B;
--ap-dark-accent-soft-text: #C7D2FE;
--ap-dark-focus: #A5B4FC;
--ap-dark-danger: #F87171;
--ap-dark-danger-soft: #2A1215;
--ap-dark-success: #4ADE80;
--ap-dark-success-soft: #0F2A1A;
--ap-dark-warning: #FACC15;
--ap-dark-warning-soft: #2A2410;
--ap-dark-info: #60A5FA;
--ap-dark-info-soft: #0F1D33;
--ap-dark-scrim: rgb(0 0 0 / .62);
--ap-dark-shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
--ap-dark-shadow-md: 0 1px 2px rgb(0 0 0 / .4), 0 8px 20px -6px rgb(0 0 0 / .5);
--ap-dark-shadow-lg: 0 2px 8px rgb(0 0 0 / .45), 0 24px 48px -12px rgb(0 0 0 / .7);
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--ap-bg: #09090B;
--ap-surface: #18181B;
--ap-surface-2: #27272A;
--ap-surface-3: #3F3F46;
--ap-border: #2E2E33;
--ap-border-strong: #8E8E97;
--ap-text: #FAFAFA;
--ap-text-2: #D4D4D8;
--ap-text-3: #A1A1AA;
--ap-accent: #818CF8;
--ap-accent-hover: #A5B4FC;
--ap-on-accent: #0C0A1F;
--ap-accent-text: #A5B4FC;
--ap-accent-soft: #1E1B4B;
--ap-accent-soft-text: #C7D2FE;
--ap-focus: #A5B4FC;
--ap-danger: #F87171;
--ap-danger-soft: #2A1215;
--ap-success: #4ADE80;
--ap-success-soft: #0F2A1A;
--ap-warning: #FACC15;
--ap-warning-soft: #2A2410;
--ap-info: #60A5FA;
--ap-info-soft: #0F1D33;
--ap-scrim: rgb(0 0 0 / .62);
--ap-shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
--ap-shadow-md: 0 1px 2px rgb(0 0 0 / .4), 0 8px 20px -6px rgb(0 0 0 / .5);
--ap-shadow-lg: 0 2px 8px rgb(0 0 0 / .45), 0 24px 48px -12px rgb(0 0 0 / .7);
}
}
:root[data-theme="dark"] {
--ap-bg: #09090B;
--ap-surface: #18181B;
--ap-surface-2: #27272A;
--ap-surface-3: #3F3F46;
--ap-border: #2E2E33;
--ap-border-strong: #8E8E97;
--ap-text: #FAFAFA;
--ap-text-2: #D4D4D8;
--ap-text-3: #A1A1AA;
--ap-accent: #818CF8;
--ap-accent-hover: #A5B4FC;
--ap-on-accent: #0C0A1F;
--ap-accent-text: #A5B4FC;
--ap-accent-soft: #1E1B4B;
--ap-accent-soft-text: #C7D2FE;
--ap-focus: #A5B4FC;
--ap-danger: #F87171;
--ap-danger-soft: #2A1215;
--ap-success: #4ADE80;
--ap-success-soft: #0F2A1A;
--ap-warning: #FACC15;
--ap-warning-soft: #2A2410;
--ap-info: #60A5FA;
--ap-info-soft: #0F1D33;
--ap-scrim: rgb(0 0 0 / .62);
--ap-shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
--ap-shadow-md: 0 1px 2px rgb(0 0 0 / .4), 0 8px 20px -6px rgb(0 0 0 / .5);
--ap-shadow-lg: 0 2px 8px rgb(0 0 0 / .45), 0 24px 48px -12px rgb(0 0 0 / .7);
}
डब्ल्यूसीएजी 2.2
यह किन मानदंडों को पूरा करता है
वे सफलता मानदंड जिनका यह पैटर्न ध्यान रखता है, और कैसे।
-
1.3.1 जानकारी और संबंध
स्तर A
कैलेंडर कॉलम हेडर वाली असली तालिका है, इसलिए हर दिन अपने वार के साथ सुनाई देता है, और ग्रिड का नाम महीने से मिलता है।
-
1.4.1 रंग का उपयोग
स्तर A
आज के चारों ओर रिंग और मोटे अक्षर हैं, चुने गए दिन में ठोस भराव है, और अनुपलब्ध दिनों पर रेखा खिंची है; संकेत-सूची यह शब्दों में बताती है।
-
2.1.1 कीबोर्ड
स्तर A
हर तारीख़ टाइप की जा सकती है, और कैलेंडर का हर हिस्सा कीबोर्ड से चलता है।
-
2.4.3 फ़ोकस क्रम
स्तर A
डायलॉग खुलने पर फ़ोकस चुने गए दिन पर जाता है और बंद होने पर वापस कैलेंडर बटन पर।
-
3.3.1 त्रुटि पहचान
स्तर A
जो तारीख़ पढ़ी न जा सके, मौजूद न हो या बीत चुकी हो, उसकी बात फ़ील्ड के पास टेक्स्ट में कही जाती है और फ़ील्ड अमान्य चिह्नित होता है।
-
3.3.2 लेबल या निर्देश
स्तर A
फ़ील्ड का लेबल और फ़ॉर्मैट संकेत दिखते हैं और कोड में फ़ील्ड से जुड़े हैं।
-
4.1.2 नाम, भूमिका, मान
स्तर A
दिनों पर aria-selected, aria-current="date" और aria-disabled हैं; बटन का नाम बताता है कि कौन सी तारीख़ चुनी गई है।
उपयोग
इसे कब इस्तेमाल करें
इसे अपनाएँ
- आज के आसपास की तारीख़ें जिन्हें लोग कैलेंडर पर सोचते हैं, जैसे यात्रा, बुकिंग या डिलीवरी का समय।
- जब चुनाव में वार मायने रखता हो, जैसे सप्ताहांत से बचना।
कुछ और इस्तेमाल करें
- वे तारीख़ें जो लोगों को याद हैं, जैसे जन्मतिथि: दिन, महीने और साल के लिए तीन टेक्स्ट फ़ील्ड इस्तेमाल करें।
- शुरुआत और अंत वाली अवधि: ऐसा रेंज पिकर इस्तेमाल करें जो दोनों को दिखाए रखे।
- ऐसा फ़ील्ड जहाँ कैलेंडर ही अकेला रास्ता हो; टाइप करना हमेशा चलना चाहिए।
आम विफलताएँ
आमतौर पर यह कहाँ गलत होता है
तारीख़ टाइप करने का कोई रास्ता नहीं
सिर्फ़ कैलेंडर खोलने वाला केवल-पढ़ने योग्य फ़ील्ड सबको महीने-दर-महीने पलटने पर मजबूर करता है, और उन लोगों को विफल करता है जिनके टूल फ़ील्ड ख़ुद भरते हैं। यहाँ का टेक्स्ट फ़ील्ड टाइप की गई तारीख़ लेता है।
Tab क्रम में हर दिन
रद्द करें बटन से पहले बयालीस टैब स्टॉप इस्तेमाल के लायक नहीं। Tab क्रम में एक ही दिन है और ऐरो कुंजियाँ दिनों के बीच चलती हैं।
आज और चुनी गई तारीख़ सिर्फ़ रंग से दिखाना
रंगीन घेरा उन लोगों को कुछ नहीं बताता जो फ़र्क़ नहीं देख पाते। आज के चारों ओर रिंग और मोटे अक्षर हैं, चुने गए दिन में भराव है, और दोनों संकेत-सूची में हैं और घोषित भी होते हैं।
दिनों के ख़ाने जिनका नाम सिर्फ़ उनकी संख्या है
अकेला "14" नहीं बताता कि कौन सा महीना या वार है। हर दिन का नाम उसकी पूरी तारीख़ है।
महीने के बदलाव जो घोषित नहीं होते
Page Down चुपचाप ग्रिड बदल देता है। महीने का नाम एक polite लाइव रीजन है, इसलिए नया महीना सुनाई देता है।
फ़ॉर्मैट को छिपाए रखना
प्लेसहोल्डर टाइप शुरू होते ही ग़ायब हो जाता है। फ़ॉर्मैट लेबल के नीचे संकेत के रूप में है, और त्रुटियाँ उसे दोहराती हैं।
नोट्स
इसे बनाना
- तारीख़ें पेज की भाषा में Intl.DateTimeFormat से लिखी जाती हैं, इसलिए महीने और वार के नाम बिना शब्दकोश के अनुवादित पेज के साथ बदल जाते हैं।
- जगह हो तो डायलॉग फ़ील्ड के नीचे खुलता है, वरना बीच में; showModal() दोनों हालात में उसे मोडल रखता है।
- आज से पहले के दिनों पर रेखा खिंची है और वे aria-disabled चिह्नित हैं; ऐरो कुंजियाँ आज पर रुक जाती हैं, इसलिए फ़ोकस कभी ऐसे दिन पर नहीं पहुँचता जिसे चुना न जा सके।
- चुनने के बाद, कैलेंडर बटन का नाम "तारीख़ बदलें" और तारीख़ हो जाता है, और फ़ोकस लौटने पर स्क्रीन रीडर यही सुनाता है।
- हमेशा छह पंक्तियाँ बनती हैं, इसलिए डायलॉग का आकार हर महीने एक-सा रहता है और बटन खिसकते नहीं।
स्रोत: WAI-एआरआईए ऑथरिंग प्रैक्टिसेज़: डेट पिकर डायलॉग का उदाहरण · GOV.UK डिज़ाइन सिस्टम: डेट इनपुट
लाइट और डार्क थीम में, डेस्कटॉप और फ़ोन की चौड़ाई पर axe से और कीबोर्ड से जाँचा गया। अभी हर स्क्रीन रीडर और ब्राउज़र जोड़ी के साथ नहीं परखा गया। सुधार बताएँ