@cahyo-dimas/freeday 3.19.1 → 3.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/freeday.css CHANGED
@@ -1284,6 +1284,21 @@ a { color: var(--color-primary-strong); }
1284
1284
  .fdy-daterange .fdy-datepicker{flex:0 1 11rem;min-width:7rem;}
1285
1285
  .fdy-daterange__sep{flex:none;color:var(--color-text-subtle);}
1286
1286
 
1287
+ /* Range in one field (`data-layout="single"` / `layout="single"`): one trigger holding both dates.
1288
+ Wider than a single picker's cap, which is sized for one date and would cut the second one off. */
1289
+ .fdy-datepicker--range{max-width:18rem;}
1290
+ /* The band. Day cells sit 2px apart, so a band painted on the cells alone is striped; each cell in it
1291
+ spreads 1px into the gap on both sides, which closes it. The ends are square on their inner side
1292
+ and keep their radius on the outer one, so the run reads as one bar from start to end. */
1293
+ .fdy-cal__day.in-range{box-shadow:-1px 0 0 var(--color-primary-soft),1px 0 0 var(--color-primary-soft);}
1294
+ .fdy-cal__day.in-range.is-today{box-shadow:inset 0 0 0 1.5px var(--color-border-strong),-1px 0 0 var(--color-primary-soft),1px 0 0 var(--color-primary-soft);}
1295
+ .fdy-cal__day.is-range-start{border-top-right-radius:0;border-bottom-right-radius:0;box-shadow:1px 0 0 var(--color-primary-soft);}
1296
+ .fdy-cal__day.is-range-end{border-top-left-radius:0;border-bottom-left-radius:0;box-shadow:-1px 0 0 var(--color-primary-soft);}
1297
+ .fdy-cal__day.is-range-end.in-range{border-top-right-radius:var(--radius-sm);border-bottom-right-radius:var(--radius-sm);}
1298
+ /* The band rules above weigh what the focus ring weighs and come later, so without this the day the
1299
+ keyboard is previewing from would be the one cell in the grid with no visible focus. */
1300
+ .fdy-cal__day.in-range:focus-visible,.fdy-cal__day.is-range-start:focus-visible,.fdy-cal__day.is-range-end:focus-visible{box-shadow:0 0 0 3px rgba(var(--color-primary-rgb),.26);}
1301
+
1287
1302
  @media (prefers-reduced-motion:no-preference){.fdy-datepicker__panel:not([hidden]){animation:fdy-combo-in var(--dur-fast) var(--ease-standard);}}
1288
1303
 
1289
1304
  /* Freeday. Datetime picker (composed: date picker + time picker → one value).
package/dist/freeday.js CHANGED
@@ -2170,6 +2170,11 @@
2170
2170
  * <div data-fdy-datepicker data-role="to" data-placeholder="To"></div>
2171
2171
  * </div>
2172
2172
  * The range links the two: the end can never precede the start (out-of-range days disable).
2173
+ * - Range in ONE field: <div data-fdy-daterange data-layout="single" data-value="2026-11-01/2026-11-30"
2174
+ * data-label="Period"></div>
2175
+ * One trigger, one calendar: the first day picked is the start, the second the end. The value is
2176
+ * "start/end" (ISO 8601 interval) or "", and it changes only when the range is whole, announced
2177
+ * by "fdy-daterange-settled" {from, to} on the root. Escape or a click outside drops half a range.
2173
2178
  * - Month: <div data-fdy-datepicker data-mode="month" data-value="2026-10"></div>
2174
2179
  * The calendar opens ON the month grid and stops there; the value is "YYYY-MM".
2175
2180
  *
@@ -2213,7 +2218,14 @@
2213
2218
  nextYears: 'Next years',
2214
2219
  chooseMonth: '{label}, choose month',
2215
2220
  chooseYear: '{label}, choose year',
2216
- backToMonths: '{start} to {end}, back to months'
2221
+ backToMonths: '{start} to {end}, back to months',
2222
+ /* The single-field range's own pair, for the same reason month mode has one: "Choose a date"
2223
+ on a field that wants two of them tells the reader half the job. */
2224
+ labelRange: 'Date range',
2225
+ placeholderRange: 'Choose dates',
2226
+ /* Appended to a day's accessible name, so an end of the range is not told by colour alone. */
2227
+ rangeStart: 'range start',
2228
+ rangeEnd: 'range end'
2217
2229
  };
2218
2230
  /* HTML lowercases attribute names, so a camelCase key like `prevMonth` can only ever be written
2219
2231
  as `data-fdy-text-prevmonth`, while the kebab form anybody would reach for,
@@ -2254,6 +2266,16 @@
2254
2266
  var d = new Date(Number(p[0]), Number(p[1]) - 1, p.length > 2 ? Number(p[2]) : 1);
2255
2267
  return isNaN(d.getTime()) ? null : d;
2256
2268
  }
2269
+ /* "start/end", the ISO 8601 interval form, so a range is ONE `data-value` like every other kit
2270
+ control and `freeday-form.js` can require it without knowing it is a range. Half an interval,
2271
+ or an end before its start, reads as empty: the field never shows a range it would not commit. */
2272
+ function parseInterval(s) {
2273
+ var p = String(s || '').split('/');
2274
+ var a = p.length === 2 ? parseISO(p[0]) : null;
2275
+ var b = p.length === 2 ? parseISO(p[1]) : null;
2276
+ if (!a || !b || b.getTime() < a.getTime()) return { start: null, end: null };
2277
+ return { start: a, end: b };
2278
+ }
2257
2279
  function startOfDay(d) { return new Date(d.getFullYear(), d.getMonth(), d.getDate()); }
2258
2280
  function sameDay(a, b) { return !!a && !!b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate(); }
2259
2281
  function addDays(d, n) { var x = new Date(d); x.setDate(x.getDate() + n); return x; }
@@ -2285,10 +2307,26 @@
2285
2307
  opens on it, Enter and a click commit from it, the day grid is never built, and the value is
2286
2308
  "YYYY-MM". Anything else, including the attribute's absence, is the day picker unchanged. */
2287
2309
  var monthOnly = wrap.getAttribute('data-mode') === 'month';
2310
+ /* A `data-fdy-daterange` root reaches this function only for `data-layout="single"`: the range
2311
+ in one field. It is the same calendar with a different answer, so it lives here rather than
2312
+ in a second copy of everything below. `selected` holds the start and `selectedEnd` the end;
2313
+ `anchor` is a start picked in this opening and not yet answered by an end, and `preview` the
2314
+ day the pointer or the keyboard is on while it waits. */
2315
+ var rangeMode = wrap.hasAttribute('data-fdy-daterange');
2288
2316
  var parseVal = monthOnly ? parseISOMonth : parseISO;
2289
- var placeholder = wrap.getAttribute('data-placeholder') || textOf(wrap, monthOnly ? 'placeholderMonth' : 'placeholder');
2290
- var label = wrap.getAttribute('data-label') || textOf(wrap, monthOnly ? 'labelMonth' : 'label');
2291
- var selected = parseVal(wrap.getAttribute('data-value'));
2317
+ var placeholder = wrap.getAttribute('data-placeholder') || textOf(wrap, monthOnly ? 'placeholderMonth' : rangeMode ? 'placeholderRange' : 'placeholder');
2318
+ var label = wrap.getAttribute('data-label') || (rangeMode && wrap.getAttribute('aria-label')) || textOf(wrap, monthOnly ? 'labelMonth' : rangeMode ? 'labelRange' : 'label');
2319
+ var seed = rangeMode ? parseInterval(wrap.getAttribute('data-value')) : null;
2320
+ var selected = rangeMode ? seed.start : parseVal(wrap.getAttribute('data-value'));
2321
+ var selectedEnd = rangeMode ? seed.end : null;
2322
+ var anchor = null, preview = null;
2323
+ if (rangeMode) {
2324
+ wrap.classList.add('fdy-datepicker--range');
2325
+ /* The group role the split layout needs would wrap one button here; the trigger carries
2326
+ the name instead. */
2327
+ wrap.removeAttribute('role');
2328
+ wrap.removeAttribute('aria-label');
2329
+ }
2292
2330
  var minDate = parseVal(wrap.getAttribute('data-min'));
2293
2331
  var maxDate = parseVal(wrap.getAttribute('data-max'));
2294
2332
  var rangeStart = null, rangeEnd = null; // for in-range shading
@@ -2334,7 +2372,22 @@
2334
2372
  wrap.appendChild(trigger);
2335
2373
  wrap.appendChild(panel);
2336
2374
 
2375
+ function rangeValue() {
2376
+ return selected && selectedEnd ? toISO(selected) + '/' + toISO(selectedEnd) : '';
2377
+ }
2378
+
2337
2379
  function updateDisplay() {
2380
+ if (rangeMode) {
2381
+ wrap.setAttribute('data-value', rangeValue());
2382
+ if (selected && selectedEnd) {
2383
+ valueSpan.textContent = valueFmt.format(selected) + ' – ' + valueFmt.format(selectedEnd);
2384
+ valueSpan.classList.remove('fdy-datepicker__value--placeholder');
2385
+ } else {
2386
+ valueSpan.textContent = placeholder;
2387
+ valueSpan.classList.add('fdy-datepicker__value--placeholder');
2388
+ }
2389
+ return;
2390
+ }
2338
2391
  /* Mirror the value onto the ROOT, which until now only seeded it. `freeday-select.js` has
2339
2392
  always done this, so this makes the family agree rather than inventing a second idea, and
2340
2393
  it is what lets `freeday-form.js` require a kit control without knowing which control it
@@ -2443,7 +2496,8 @@
2443
2496
  btn.textContent = d.getDate();
2444
2497
  if (d.getMonth() !== view.getMonth()) btn.classList.add('is-outside');
2445
2498
  if (sameDay(d, today)) btn.classList.add('is-today');
2446
- if (selected && sameDay(d, selected)) { btn.classList.add('is-selected'); btn.setAttribute('aria-selected', 'true'); }
2499
+ if (rangeMode) paintRange(btn, d);
2500
+ else if (selected && sameDay(d, selected)) { btn.classList.add('is-selected'); btn.setAttribute('aria-selected', 'true'); }
2447
2501
  if (rangeStart && rangeEnd) {
2448
2502
  var t = startOfDay(d).getTime();
2449
2503
  if (t > startOfDay(rangeStart).getTime() && t < startOfDay(rangeEnd).getTime()) btn.classList.add('in-range');
@@ -2455,9 +2509,84 @@
2455
2509
  grid.appendChild(btn);
2456
2510
  }
2457
2511
  grid.addEventListener('keydown', onGridKey);
2512
+ if (rangeMode) grid.addEventListener('mouseover', onRangeHover);
2458
2513
  panel.appendChild(grid);
2459
2514
  }
2460
2515
 
2516
+ /* The two ends the grid shows. While a start waits for its end, the end is the day under the
2517
+ pointer or the keyboard, and only when it does not precede the start: a day before it would
2518
+ become the new start, so drawing a band backwards would promise something a click will not do. */
2519
+ function shownEnds() {
2520
+ if (anchor) {
2521
+ var to = preview && startOfDay(preview).getTime() >= anchor.getTime() ? startOfDay(preview) : null;
2522
+ return { from: anchor, to: to, pending: true };
2523
+ }
2524
+ return { from: selected, to: selectedEnd, pending: false };
2525
+ }
2526
+
2527
+ /* Every range state of one day cell, written in full each time, so the same function serves the
2528
+ render and the cheaper repaint a hover asks for (rebuilding the grid under a moving pointer
2529
+ would replace the element the pointer is on). The ends are rounded on their outer side only,
2530
+ and a committed end is filled like a picked day. A previewed end is not filled: it is not an
2531
+ answer yet. Each end also says so in its accessible name, so the range is not colour alone. */
2532
+ function paintRange(btn, d) {
2533
+ var ends = shownEnds();
2534
+ var t = startOfDay(d).getTime();
2535
+ var isFrom = !!ends.from && sameDay(d, ends.from);
2536
+ var isTo = !!ends.to && sameDay(d, ends.to);
2537
+ var span = !!ends.from && !!ends.to && !sameDay(ends.from, ends.to);
2538
+ var filled = isFrom || (isTo && !ends.pending);
2539
+ btn.classList.toggle('is-selected', filled);
2540
+ if (filled) btn.setAttribute('aria-selected', 'true'); else btn.removeAttribute('aria-selected');
2541
+ btn.classList.toggle('is-range-start', isFrom && span);
2542
+ btn.classList.toggle('is-range-end', isTo && span);
2543
+ btn.classList.toggle('in-range', span && ((t > ends.from.getTime() && t < ends.to.getTime()) || (isTo && ends.pending)));
2544
+ var name = dayLabelFmt.format(d);
2545
+ if (isFrom) name += ', ' + textOf(wrap, 'rangeStart');
2546
+ else if (isTo && !ends.pending) name += ', ' + textOf(wrap, 'rangeEnd');
2547
+ btn.setAttribute('aria-label', name);
2548
+ }
2549
+
2550
+ function repaintRange() {
2551
+ Array.prototype.forEach.call(panel.querySelectorAll('.fdy-cal__day'), function (btn) {
2552
+ if (btn._date) paintRange(btn, btn._date);
2553
+ });
2554
+ }
2555
+
2556
+ function onRangeHover(e) {
2557
+ if (!anchor) return;
2558
+ var cell = e.target && e.target.closest ? e.target.closest('.fdy-cal__day') : null;
2559
+ if (!cell || !cell._date || cell.disabled) return;
2560
+ if (preview && sameDay(preview, cell._date)) return;
2561
+ preview = cell._date;
2562
+ repaintRange();
2563
+ }
2564
+
2565
+ /* First pick: the start, and the panel stays open for the end. Second pick on or after it: the
2566
+ range is whole, committed and announced, and the panel closes. A second pick BEFORE the start
2567
+ becomes the new start instead of silently swapping the two, which would leave the reader
2568
+ wondering which end they just moved. */
2569
+ function pickRange(d) {
2570
+ var day = startOfDay(d);
2571
+ focusDate = day;
2572
+ if (!anchor || day.getTime() < anchor.getTime()) {
2573
+ anchor = day;
2574
+ preview = day;
2575
+ render();
2576
+ focusFocusDate();
2577
+ return;
2578
+ }
2579
+ selected = anchor;
2580
+ selectedEnd = day;
2581
+ anchor = null;
2582
+ preview = null;
2583
+ updateDisplay();
2584
+ wrap.dispatchEvent(new CustomEvent('fdy-daterange-settled', {
2585
+ bubbles: true, detail: { from: toISO(selected), to: toISO(selectedEnd) }
2586
+ }));
2587
+ close(true);
2588
+ }
2589
+
2461
2590
  function renderMonths() {
2462
2591
  panel.innerHTML = '';
2463
2592
  var year = view.getFullYear();
@@ -2677,6 +2806,9 @@
2677
2806
 
2678
2807
  function moveFocus(next) {
2679
2808
  focusDate = next;
2809
+ /* The keyboard previews the band exactly as the pointer does, or a keyboard user would pick
2810
+ the end without ever seeing the range they are about to commit. */
2811
+ if (anchor) preview = startOfDay(next);
2680
2812
  if (next.getMonth() !== view.getMonth() || next.getFullYear() !== view.getFullYear()) {
2681
2813
  view = new Date(next.getFullYear(), next.getMonth(), 1);
2682
2814
  }
@@ -2705,6 +2837,7 @@
2705
2837
 
2706
2838
  function pick(d) {
2707
2839
  if (isDisabled(d)) return;
2840
+ if (rangeMode) { pickRange(d); return; }
2708
2841
  selected = startOfDay(d);
2709
2842
  focusDate = selected;
2710
2843
  updateDisplay();
@@ -2781,6 +2914,10 @@
2781
2914
 
2782
2915
  function close(returnFocus) {
2783
2916
  if (panel.hidden) return;
2917
+ /* Every way out that is not a second pick drops half a range: the value never held it, so
2918
+ there is nothing to restore, only the start this opening was waiting on. */
2919
+ anchor = null;
2920
+ preview = null;
2784
2921
  var p = popCtl(); if (p) p.hide(); else panel.hidden = true;
2785
2922
  trigger.setAttribute('aria-expanded', 'false');
2786
2923
  trigger.classList.remove('is-open');
@@ -2802,8 +2939,11 @@
2802
2939
  var api = {
2803
2940
  wrap: wrap,
2804
2941
  setState: setState,
2805
- getValue: function () { return selected ? (monthOnly ? toISOMonth(selected) : toISO(selected)) : ''; },
2806
- clear: function () { selected = null; updateDisplay(); if (!panel.hidden) render(); },
2942
+ getValue: function () {
2943
+ if (rangeMode) return rangeValue();
2944
+ return selected ? (monthOnly ? toISOMonth(selected) : toISO(selected)) : '';
2945
+ },
2946
+ clear: function () { selected = null; selectedEnd = null; updateDisplay(); if (!panel.hidden) render(); },
2807
2947
  setMin: function (iso) { minDate = parseVal(iso); if (!panel.hidden) render(); },
2808
2948
  setMax: function (iso) { maxDate = parseVal(iso); if (!panel.hidden) render(); },
2809
2949
  setRange: function (fromIso, toIso) {
@@ -2820,6 +2960,9 @@
2820
2960
  if (root.dataset.fdyDrReady === '1') return;
2821
2961
  if (root.closest('[data-fdy-ignore]')) return;
2822
2962
  root.dataset.fdyDrReady = '1';
2963
+ /* One field: the root IS the picker, and the range logic lives in initPicker beside the
2964
+ calendar it draws on. The split layout below is unchanged. */
2965
+ if (root.getAttribute('data-layout') === 'single') { initPicker(root); return; }
2823
2966
  var fromEl = root.querySelector('[data-fdy-datepicker][data-role="from"]');
2824
2967
  var toEl = root.querySelector('[data-fdy-datepicker][data-role="to"]');
2825
2968
  if (!fromEl || !toEl) return;