Date picker
A button that opens a calendar in a popover and shows the chosen date, with keyboard support and form value.
Installation
Usage
Load calendar.js before date-picker.js. Set data-value on the calendar to start with a date. For a quick native alternative, <input type="date"> with the Input component also works well.
JavaScript API
data-bootcn-date-pickeron the wrapper (a Bootstrap.dropdown); the trigger opens the popover; inside is a Calendar component.- The text marked
data-bootcn-date-picker-textshows the chosen date (long format, page language) or its original placeholder. - The form value is the calendar's hidden input (YYYY-MM-DD); listen for
bootcn:calendar:selectfor changes. Bootcn.datePicker.init(root)/destroy().
Accessibility
- The trigger's name combines its label and the current date (
aria-labelledby), for example "Delivery date, 24 November 2026". - Opening moves focus to the selected day (or today); choosing a day closes the popover and returns focus to the trigger; Escape closes it too (Bootstrap).
- The popover is a labelled, non-modal dialog.
Dependencies
- Bootstrap 5.3+ CSS and the JS bundle.
themes/tokens/tokens.css.- The
calendarcomponent. - The
buttoncomponent. - The
labelcomponent.
Source
The exact files the CLI installs (date-picker@1.0.0). View on GitHub · Report an issue
html
<div class="dropdown bootcn-date-picker" data-bootcn-date-picker>
<span class="bootcn-label" id="bootcn-date-picker-label">Delivery date</span>
<button
type="button"
class="btn bootcn-button bootcn-button--outline bootcn-date-picker__trigger"
data-bs-toggle="dropdown"
data-bs-auto-close="outside"
aria-expanded="false"
aria-haspopup="dialog"
aria-labelledby="bootcn-date-picker-label bootcn-date-picker-text"
>
<svg
class="bootcn-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<rect x="3" y="5" width="18" height="16" rx="2" />
<path d="M16 3v4M8 3v4M3 11h18" />
</svg>
<span id="bootcn-date-picker-text" data-bootcn-date-picker-text>Pick a date</span>
</button>
<div
class="dropdown-menu bootcn-date-picker__popover"
role="dialog"
aria-label="Choose a delivery date"
>
<div class="bootcn-calendar" data-bootcn-calendar>
<div class="bootcn-calendar__header">
<button
type="button"
class="bootcn-calendar__nav"
data-bootcn-calendar-prev
aria-label="Previous month"
>
<svg
class="bootcn-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="m15 18-6-6 6-6" />
</svg>
</button>
<h2
class="bootcn-calendar__title"
id="bootcn-date-picker-calendar-title"
aria-live="polite"
>
Calendar
</h2>
<button
type="button"
class="bootcn-calendar__nav"
data-bootcn-calendar-next
aria-label="Next month"
>
<svg
class="bootcn-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="m9 18 6-6-6-6" />
</svg>
</button>
</div>
<table
class="bootcn-calendar__grid"
role="grid"
aria-labelledby="bootcn-date-picker-calendar-title"
>
<thead>
<tr></tr>
</thead>
<tbody></tbody>
</table>
<input type="hidden" name="delivery" data-bootcn-calendar-input />
</div>
</div>
</div>css
.bootcn-date-picker {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.5rem;
}
.bootcn-date-picker__trigger {
justify-content: flex-start;
min-width: 15rem;
font-weight: 400;
}
.bootcn-date-picker__trigger[data-empty] {
color: var(--bootcn-muted-foreground);
}
.bootcn-date-picker__popover {
padding: 0;
border: 0;
background: transparent;
}
.bootcn-date-picker__popover .bootcn-calendar {
box-shadow: var(--bootcn-shadow-md);
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.datePicker) return; // already loaded: idempotent
const ROOT = '[data-bootcn-date-picker]';
let listening = false;
function parts(root) {
return {
trigger: root.querySelector('[data-bs-toggle="dropdown"]'),
text: root.querySelector('[data-bootcn-date-picker-text]'),
calendar: root.querySelector('[data-bootcn-calendar]'),
};
}
function format(root, value) {
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value || '');
if (!match) return null;
const owner = root.closest('[lang]');
const locale = (owner && owner.getAttribute('lang')) || 'en';
const date = new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
return new Intl.DateTimeFormat(locale, { dateStyle: 'long' }).format(date);
}
function sync(root) {
const { trigger, text, calendar } = parts(root);
if (!trigger || !text || !calendar) return;
const label = format(root, calendar.getAttribute('data-value'));
if (!text.hasAttribute('data-placeholder'))
text.setAttribute('data-placeholder', text.textContent);
text.textContent = label || text.getAttribute('data-placeholder');
trigger.toggleAttribute('data-empty', !label);
}
function onSelect(event) {
const root = event.target.closest(ROOT);
if (!root) return;
sync(root);
const { trigger } = parts(root);
if (window.bootstrap && trigger) window.bootstrap.Dropdown.getOrCreateInstance(trigger).hide();
if (trigger) trigger.focus();
}
// Move focus into the calendar (the selected day, or today) when it opens.
function onShown(event) {
const root = event.target.closest && event.target.closest(ROOT);
if (!root || !ns.calendar) return;
ns.calendar.focus(parts(root).calendar);
}
function init(root = document) {
if (!window.bootstrap || !window.bootstrap.Dropdown) {
console.error(
'[bootcn] date-picker: Bootstrap JavaScript is required. Load the Bootstrap bundle before bootcn date-picker.js.',
);
return;
}
root.querySelectorAll(ROOT).forEach(sync);
if (listening) return;
document.addEventListener('bootcn:calendar:select', onSelect);
document.addEventListener('shown.bs.dropdown', onShown);
listening = true;
}
function destroy() {
if (!listening) return;
document.removeEventListener('bootcn:calendar:select', onSelect);
document.removeEventListener('shown.bs.dropdown', onShown);
listening = false;
}
ns.datePicker = { init: init, destroy: destroy };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => init());
} else {
init();
}
})();
