Calendar
A month grid for picking a date, with full keyboard navigation, localized names and optional limits; single date or a date range.
Installation
bash
npx bootcn add calendarbash
pnpm dlx bootcn add calendarbash
yarn dlx bootcn add calendarbash
bunx --bun bootcn add calendarThe CLI also installs the design tokens and these components: none. It prints the <link> and <script> tags to add to your layout.
Usage
The script draws the month grid; the markup only holds the header (with the month as text, which the script keeps up to date), an empty table and the hidden input. Month and weekday names come from the browser's Intl API in the page's language. For a date inside a form field, use the Date picker.
JavaScript API
data-bootcn-calendaron the wrapper. Options:data-value(YYYY-MM-DD, the selected date),data-min/data-max,data-week-start(0 = Sunday … 6; default 1, Monday) anddata-locale(default: the nearestlangattribute).- The hidden input marked
data-bootcn-calendar-inputgets the value (YYYY-MM-DD) for forms. - Event
bootcn:calendar:select(bubbles),detail.valueanddetail.date. - Range mode:
data-mode="range"(add the classbootcn-calendar--range), withdata-start/data-endand two hidden inputs markeddata-bootcn-calendar-input="start"and="end". The first pick sets the start, the next the end (a pick before the start starts over). The event'sdetailis{ start, end, complete }; every day in the range hasaria-selected="true". Bootcn.calendar.setValue(root, 'YYYY-MM-DD'),focus(root),init(root)/destroy(root).
Accessibility
- WAI-ARIA date grid: one tab stop (the focused day); Arrow keys move by a day or a week, Home/End to the start or end of the week, Page Up/Down by a month (with Shift by a year), Enter or Space selects.
- Each day is a button named with the full date ("Tuesday, 24 November 2026"); the selected day's cell has
aria-selected, today hasaria-current="date"; dates outside the limits are disabled. - The month title is a polite live region, so changing month is announced.
Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css.
Source
The exact files the CLI installs (calendar@1.1.0). View on GitHub · Report an issue
html
<div
class="bootcn-calendar"
data-bootcn-calendar
data-value="2026-11-24"
data-min="2026-01-01"
data-max="2027-12-31"
>
<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-calendar-demo-title" aria-live="polite">
November 2026
</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-calendar-demo-title">
<thead>
<tr></tr>
</thead>
<tbody></tbody>
</table>
<input type="hidden" name="date" data-bootcn-calendar-input />
</div>css
.bootcn-calendar {
display: inline-block;
padding: 0.75rem;
border: var(--bootcn-border-width) solid var(--bootcn-border-color);
border-radius: var(--bootcn-radius-lg);
background-color: var(--bootcn-background);
color: var(--bootcn-foreground);
box-shadow: var(--bootcn-shadow-sm);
}
.bootcn-calendar__header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.5rem;
margin-bottom: 0.5rem;
}
.bootcn-calendar__title {
margin: 0;
font-size: 0.875rem;
font-weight: 500;
}
.bootcn-calendar__nav {
display: inline-flex;
align-items: center;
justify-content: center;
width: 1.75rem;
height: 1.75rem;
border: var(--bootcn-border-width) solid var(--bootcn-input);
border-radius: var(--bootcn-radius);
background: transparent;
color: var(--bootcn-foreground);
}
.bootcn-calendar__nav:hover {
background-color: var(--bootcn-accent);
}
.bootcn-calendar__nav:focus-visible {
outline: 0;
box-shadow: var(--bootcn-focus-ring);
}
.bootcn-calendar__grid {
border-collapse: collapse;
}
.bootcn-calendar__grid th {
width: 2.25rem;
padding: 0 0 0.25rem;
color: var(--bootcn-muted-foreground);
font-size: 0.75rem;
font-weight: 400;
text-align: center;
}
.bootcn-calendar__grid td {
padding: 0.0625rem;
text-align: center;
}
.bootcn-calendar__day {
width: 2.25rem;
height: 2.25rem;
border: 0;
border-radius: var(--bootcn-radius);
background: transparent;
color: var(--bootcn-foreground);
font-size: 0.875rem;
font-variant-numeric: tabular-nums;
}
.bootcn-calendar__day:hover {
background-color: var(--bootcn-accent);
}
.bootcn-calendar__day:focus-visible {
outline: 0;
box-shadow: var(--bootcn-focus-ring);
}
.bootcn-calendar__day.is-outside {
color: var(--bootcn-muted-foreground);
}
.bootcn-calendar__day[aria-current='date'] {
background-color: var(--bootcn-accent);
font-weight: 600;
}
[aria-selected='true'] > .bootcn-calendar__day {
background-color: var(--bootcn-primary);
color: var(--bootcn-primary-foreground);
}
.bootcn-calendar__day:disabled {
opacity: 0.5;
text-decoration: line-through;
cursor: not-allowed;
}
/* Range mode: the days between start and end share a band. */
[aria-selected='true'] > .bootcn-calendar__day.is-range-middle {
border-radius: 0;
background-color: var(--bootcn-accent);
color: var(--bootcn-accent-foreground);
}
[aria-selected='true'] > .bootcn-calendar__day.is-range-start:not(.is-range-end) {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
[aria-selected='true'] > .bootcn-calendar__day.is-range-end:not(.is-range-start) {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.bootcn-calendar--range td {
padding-inline: 0;
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.calendar) return; // already loaded: idempotent
const ROOT = '[data-bootcn-calendar]';
const INIT = 'data-bootcn-calendar-initialized';
const state = new WeakMap(); // root -> { month: Date, focus: Date }
let listening = false;
// Dates are local calendar days; ISO strings are YYYY-MM-DD.
function parse(value) {
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value || '');
return match ? new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3])) : null;
}
function iso(date) {
const pad = (n) => String(n).padStart(2, '0');
return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate());
}
function addDays(date, days) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + days);
}
function addMonths(date, months) {
const target = new Date(date.getFullYear(), date.getMonth() + months, 1);
const last = new Date(target.getFullYear(), target.getMonth() + 1, 0).getDate();
return new Date(target.getFullYear(), target.getMonth(), Math.min(date.getDate(), last));
}
function sameDay(a, b) {
return !!a && !!b && iso(a) === iso(b);
}
function locale(root) {
const owner = root.closest('[lang]');
return root.getAttribute('data-locale') || (owner && owner.getAttribute('lang')) || 'en';
}
// 0 = Sunday … 6 = Saturday; Monday when the attribute is missing or invalid.
function weekStart(root) {
const raw = root.getAttribute('data-week-start');
const value = raw === null || raw.trim() === '' ? NaN : Number(raw);
return Number.isInteger(value) && value >= 0 && value <= 6 ? value : 1;
}
function bounds(root) {
return { min: parse(root.getAttribute('data-min')), max: parse(root.getAttribute('data-max')) };
}
function clamp(root, date) {
const { min, max } = bounds(root);
if (min && date < min) return min;
if (max && date > max) return max;
return date;
}
function render(root) {
const s = state.get(root);
const loc = locale(root);
const start = weekStart(root);
const selected = parse(root.getAttribute('data-value'));
const range = isRange(root);
const rangeStart = parse(root.getAttribute('data-start'));
const rangeEnd = parse(root.getAttribute('data-end'));
const today = new Date();
const { min, max } = bounds(root);
const title = root.querySelector('.bootcn-calendar__title');
const body = root.querySelector('tbody');
const head = root.querySelector('thead tr');
title.textContent = new Intl.DateTimeFormat(loc, { month: 'long', year: 'numeric' }).format(
s.month,
);
if (head.children.length === 0) {
const short = new Intl.DateTimeFormat(loc, { weekday: 'short' });
const long = new Intl.DateTimeFormat(loc, { weekday: 'long' });
for (let i = 0; i < 7; i += 1) {
const day = new Date(2024, 0, 7 + ((start + i) % 7)); // 7 Jan 2024 is a Sunday
const th = document.createElement('th');
th.scope = 'col';
th.abbr = long.format(day);
th.textContent = short.format(day).slice(0, 2);
head.appendChild(th);
}
}
const full = new Intl.DateTimeFormat(loc, { dateStyle: 'full' });
const first = new Date(s.month.getFullYear(), s.month.getMonth(), 1);
let cursor = addDays(first, -((first.getDay() - start + 7) % 7));
body.textContent = '';
for (let week = 0; week < 6; week += 1) {
const row = document.createElement('tr');
for (let i = 0; i < 7; i += 1) {
const cell = document.createElement('td');
cell.setAttribute('role', 'gridcell');
const button = document.createElement('button');
button.type = 'button';
button.className = 'bootcn-calendar__day';
button.textContent = String(cursor.getDate());
button.setAttribute('data-date', iso(cursor));
button.setAttribute('aria-label', full.format(cursor));
button.tabIndex = sameDay(cursor, s.focus) ? 0 : -1;
if (cursor.getMonth() !== s.month.getMonth()) button.classList.add('is-outside');
if (sameDay(cursor, today)) button.setAttribute('aria-current', 'date');
if ((min && cursor < min) || (max && cursor > max)) button.disabled = true;
if (range) {
const inside =
!!rangeStart &&
(sameDay(cursor, rangeStart) ||
(!!rangeEnd && cursor >= rangeStart && cursor <= rangeEnd));
cell.setAttribute('aria-selected', String(inside));
if (sameDay(cursor, rangeStart)) button.classList.add('is-range-start');
if (sameDay(cursor, rangeEnd)) button.classList.add('is-range-end');
if (inside && !sameDay(cursor, rangeStart) && !sameDay(cursor, rangeEnd)) {
button.classList.add('is-range-middle');
}
} else {
cell.setAttribute('aria-selected', String(sameDay(cursor, selected)));
}
cell.appendChild(button);
row.appendChild(cell);
cursor = addDays(cursor, 1);
}
body.appendChild(row);
}
}
function focusDate(root, date, moveFocus) {
const s = state.get(root);
const next = clamp(root, date);
s.focus = next;
if (next.getMonth() !== s.month.getMonth() || next.getFullYear() !== s.month.getFullYear()) {
s.month = new Date(next.getFullYear(), next.getMonth(), 1);
}
render(root);
if (moveFocus) {
const button = root.querySelector('[data-date="' + iso(next) + '"]');
if (button) button.focus();
}
}
// Range mode: data-mode="range"; the first pick is the start, the next the
// end (a pick before the start becomes the new start).
function isRange(root) {
return root.getAttribute('data-mode') === 'range';
}
function selectRange(root, date) {
const start = parse(root.getAttribute('data-start'));
const end = parse(root.getAttribute('data-end'));
if (!start || end || date < start) {
root.setAttribute('data-start', iso(date));
root.removeAttribute('data-end');
} else {
root.setAttribute('data-end', iso(date));
}
const startInput = root.querySelector('[data-bootcn-calendar-input="start"]');
const endInput = root.querySelector('[data-bootcn-calendar-input="end"]');
if (startInput) startInput.value = root.getAttribute('data-start') || '';
if (endInput) endInput.value = root.getAttribute('data-end') || '';
focusDate(root, date, true);
root.dispatchEvent(
new CustomEvent('bootcn:calendar:select', {
bubbles: true,
detail: {
start: root.getAttribute('data-start'),
end: root.getAttribute('data-end'),
complete: root.hasAttribute('data-end'),
},
}),
);
}
function select(root, date) {
if (isRange(root)) {
selectRange(root, date);
return;
}
const value = iso(date);
root.setAttribute('data-value', value);
const input = root.querySelector('[data-bootcn-calendar-input]');
if (input) input.value = value;
focusDate(root, date, true);
root.dispatchEvent(
new CustomEvent('bootcn:calendar:select', {
bubbles: true,
detail: { value: value, date: date },
}),
);
}
function changeMonth(root, step) {
const s = state.get(root);
s.month = new Date(s.month.getFullYear(), s.month.getMonth() + step, 1);
s.focus = clamp(root, addMonths(s.focus, step));
render(root);
}
function setup(root) {
const selected = parse(root.getAttribute(isRange(root) ? 'data-start' : 'data-value'));
const base = selected || clamp(root, new Date());
state.set(root, { month: new Date(base.getFullYear(), base.getMonth(), 1), focus: base });
if (isRange(root)) {
const startInput = root.querySelector('[data-bootcn-calendar-input="start"]');
const endInput = root.querySelector('[data-bootcn-calendar-input="end"]');
if (startInput) startInput.value = root.getAttribute('data-start') || '';
if (endInput) endInput.value = root.getAttribute('data-end') || '';
} else {
const input = root.querySelector('[data-bootcn-calendar-input]');
if (input && selected) input.value = iso(selected);
}
root.setAttribute(INIT, '');
render(root);
}
function onClick(event) {
const root = event.target.closest && event.target.closest(ROOT);
if (!root || !state.has(root)) return;
const day = event.target.closest('.bootcn-calendar__day');
if (day && !day.disabled) {
select(root, parse(day.getAttribute('data-date')));
} else if (event.target.closest('[data-bootcn-calendar-prev]')) {
changeMonth(root, -1);
} else if (event.target.closest('[data-bootcn-calendar-next]')) {
changeMonth(root, 1);
}
}
function onKeydown(event) {
const day = event.target.closest && event.target.closest('.bootcn-calendar__day');
const root = day && day.closest(ROOT);
if (!root || !state.has(root)) return;
const current = parse(day.getAttribute('data-date'));
const start = weekStart(root);
let next;
switch (event.key) {
case 'ArrowLeft':
next = addDays(current, -1);
break;
case 'ArrowRight':
next = addDays(current, 1);
break;
case 'ArrowUp':
next = addDays(current, -7);
break;
case 'ArrowDown':
next = addDays(current, 7);
break;
case 'Home':
next = addDays(current, -((current.getDay() - start + 7) % 7));
break;
case 'End':
next = addDays(current, 6 - ((current.getDay() - start + 7) % 7));
break;
case 'PageUp':
next = addMonths(current, event.shiftKey ? -12 : -1);
break;
case 'PageDown':
next = addMonths(current, event.shiftKey ? 12 : 1);
break;
default:
return;
}
// Handled here, so containers (such as a Bootstrap dropdown) do not react.
event.preventDefault();
event.stopPropagation();
focusDate(root, next, true);
}
function init(root = document) {
root.querySelectorAll(ROOT + ':not([' + INIT + '])').forEach(setup);
if (!listening) {
document.addEventListener('click', onClick);
document.addEventListener('keydown', onKeydown, true);
listening = true;
}
}
function destroy(root = document) {
root.querySelectorAll(ROOT + '[' + INIT + ']').forEach((element) => {
element.removeAttribute(INIT);
state.delete(element);
});
if (root === document && listening) {
document.removeEventListener('click', onClick);
document.removeEventListener('keydown', onKeydown, true);
listening = false;
}
}
// Shows the month of `value` (YYYY-MM-DD) and selects it, or today.
function setValue(root, value) {
if (!state.has(root)) setup(root);
const date = parse(value);
if (date) select(root, date);
}
function focus(root) {
const s = state.get(root);
if (s) focusDate(root, s.focus, true);
}
ns.calendar = { init: init, destroy: destroy, setValue: setValue, focus: focus };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => init());
} else {
init();
}
})();
