mirror of
https://github.com/bitbaum/whenkit.git
synced 2026-12-04 05:57:06 +01:00
Day chips, time chips, length chips, a readable date field over the native
picker, and a booking request that resolves to {startsAt, endsAt} in the
provider's zone.
- Core (no React): wall-clock YYYY-MM-DD / HH:MM strings, one zonedToUtc
(DST gaps resolve forward), todayIn(zone), slots inside opening hours,
lengths that fit, resolveRequest / describeRequest for three choices:
slot, days, flexible.
- React: BookingRequestPicker, DayStrip, ChipGroup (real radio groups),
DateField (native input transparent over a readable display).
- styles.css: --wk-* variables defaulting to @bitbaum/design-tokens; 44px
targets, 16px input, strip scrolls inside itself.
- Tests: 15 core + 8 server-render, and a Chromium check at 1280/390,
light/dark that taps bookings end to end. dist/ committed for github: installs.
Signed-off-by: Cato <41178744+catomean@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
56 lines
2.2 KiB
JavaScript
56 lines
2.2 KiB
JavaScript
/**
|
|
* How a day, a time and a length read to a person — in their language.
|
|
*
|
|
* Every string here comes from `Intl`, so "Today", "morgen" and "demain" are
|
|
* the platform's words rather than a table this package would have to keep.
|
|
* Days are formatted from their UTC anchor with `timeZone: "UTC"`, so the
|
|
* label is always the day that was picked.
|
|
*/
|
|
import { dayAnchor, daysBetween, isWallTime } from "./calendar.js";
|
|
function capitalise(text, locale) {
|
|
return text.charAt(0).toLocaleUpperCase(locale) + text.slice(1);
|
|
}
|
|
export function dayLabel(date, opts = {}) {
|
|
const locale = opts.locale ?? "en";
|
|
const anchor = dayAnchor(date);
|
|
const f = (o) => new Intl.DateTimeFormat(locale, { timeZone: "UTC", ...o }).format(anchor);
|
|
let relative = null;
|
|
if (opts.today) {
|
|
const diff = daysBetween(opts.today, date);
|
|
if (diff === 0 || diff === 1) {
|
|
relative = capitalise(new Intl.RelativeTimeFormat(locale, { numeric: "auto" }).format(diff, "day"), locale);
|
|
}
|
|
}
|
|
return {
|
|
date,
|
|
weekdayShort: f({ weekday: "short" }),
|
|
dayOfMonth: f({ day: "numeric" }),
|
|
monthShort: f({ month: "short" }),
|
|
relative,
|
|
long: f({ weekday: "long", day: "numeric", month: "long" }),
|
|
short: f({ weekday: "short", day: "numeric", month: "short" }),
|
|
};
|
|
}
|
|
/** "14:00" or "2:00 PM", whichever the locale uses. */
|
|
export function formatTime(time, locale = "en") {
|
|
if (!isWallTime(time))
|
|
return time;
|
|
const [h, m] = time.split(":").map(Number);
|
|
return new Intl.DateTimeFormat(locale, {
|
|
timeZone: "UTC",
|
|
hour: "numeric",
|
|
minute: "2-digit",
|
|
}).format(new Date(Date.UTC(2000, 0, 1, h, m)));
|
|
}
|
|
/** "30 min", "2 hr", "1 day" — the locale's own short unit words. */
|
|
export function formatLength(minutes, locale = "en") {
|
|
const unit = (value, u) => new Intl.NumberFormat(locale, { style: "unit", unit: u, unitDisplay: "short" }).format(value);
|
|
if (minutes % (24 * 60) === 0)
|
|
return unit(minutes / (24 * 60), "day");
|
|
if (minutes % 60 === 0)
|
|
return unit(minutes / 60, "hour");
|
|
if (minutes > 60)
|
|
return unit(Math.round((minutes / 60) * 10) / 10, "hour");
|
|
return unit(minutes, "minute");
|
|
}
|
|
//# sourceMappingURL=labels.js.map
|