@spartan-ng/brain 1.10.0-beta.1 → 1.10.0-beta.3

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/README.md CHANGED
@@ -89,7 +89,6 @@ spartan/ui requires Tailwind CSS v4.
89
89
 
90
90
  - Angular `>=21.0.0 <23.0.0` (core, common, forms, cdk)
91
91
  - `rxjs` `>=6.6.0`
92
- - `clsx` `>=2.0.0`
93
92
  - `tailwindcss` `>=4.0.0`
94
93
  - `tw-animate-css` `>=1.0.0` _(imported by the Tailwind preset)_
95
94
  - `luxon` `>=3.0.0` _(optional - only required for `@spartan-ng/brain/date-time-luxon`)_
@@ -119,6 +119,32 @@ const compareDays = (a, b, dateAdapter) => {
119
119
  return a.every((day, i) => dateAdapter.isSameDay(day, b[i]));
120
120
  };
121
121
 
122
+ /** Find the first enabled day from the target, moving in the requested direction. */
123
+ function findEnabledDate(start, direction, adapter, isDisabled, bounds) {
124
+ let date = start;
125
+ // Allow multi-year disabled ranges, but cap searches with no far boundary.
126
+ // Explicit calendar boundaries stop the search as soon as they are crossed.
127
+ for (let step = 0; step < 3660; step++) {
128
+ const beforeMin = bounds?.min !== undefined && adapter.isBefore(date, adapter.startOfDay(bounds.min));
129
+ const afterMax = bounds?.max !== undefined && adapter.isAfter(date, adapter.endOfDay(bounds.max));
130
+ // Only stop at a bound when moving farther out, not toward the allowed range.
131
+ if ((beforeMin && direction === -1) || (afterMax && direction === 1))
132
+ return undefined;
133
+ if (!beforeMin && !afterMax && !isDisabled(date))
134
+ return date;
135
+ date = direction === 1 ? adapter.add(date, { days: 1 }) : adapter.subtract(date, { days: 1 });
136
+ }
137
+ return undefined;
138
+ }
139
+ /** Find a usable initial tab stop if the requested focused day is disabled. */
140
+ function findInitialFocusedDate(date, adapter, isDisabled, bounds) {
141
+ if (!isDisabled(date))
142
+ return date;
143
+ return (findEnabledDate(date, 1, adapter, isDisabled, bounds) ??
144
+ findEnabledDate(date, -1, adapter, isDisabled, bounds) ??
145
+ date);
146
+ }
147
+
122
148
  class BrnCalendar {
123
149
  _i18n = injectBrnCalendarI18n();
124
150
  /** Access the date adapter */
@@ -150,7 +176,32 @@ class BrnCalendar {
150
176
  /**
151
177
  * The focused date.
152
178
  */
153
- focusedDate = linkedSignal(() => this.constrainDate(this.defaultFocusedDate() ?? this.date() ?? this._dateAdapter.now()), ...(ngDevMode ? [{ debugName: "focusedDate" }] : /* istanbul ignore next */ []));
179
+ focusedDate = linkedSignal({ ...(ngDevMode ? { debugName: "focusedDate" } : /* istanbul ignore next */ {}), source: () => ({
180
+ initialDate: this.constrainDate(this.defaultFocusedDate() ?? this.date() ?? this._dateAdapter.now()),
181
+ startingDate: this.defaultFocusedDate(),
182
+ selectedDate: this.date(),
183
+ min: this.min(),
184
+ max: this.max(),
185
+ disabled: this.disabled(),
186
+ dateDisabled: this.dateDisabled(),
187
+ }),
188
+ computation: (source, previous) => {
189
+ if (source.disabled)
190
+ return source.initialDate;
191
+ // Availability changes must not reset keyboard focus to the default date.
192
+ // A new initial date or changed limits should still reset it.
193
+ const preserveFocus = previous !== undefined &&
194
+ previous.source.startingDate === source.startingDate &&
195
+ previous.source.selectedDate === source.selectedDate &&
196
+ previous.source.min === source.min &&
197
+ previous.source.max === source.max &&
198
+ previous.source.disabled === source.disabled;
199
+ const target = preserveFocus ? previous.value : source.initialDate;
200
+ return findInitialFocusedDate(target, this._dateAdapter, (day) => this.isDateDisabled(day), {
201
+ min: source.min,
202
+ max: source.max,
203
+ });
204
+ } });
154
205
  /** Emits whenever the focused date changes. */
155
206
  focusedDateChange = output();
156
207
  _setFocusedDate(date) {
@@ -343,7 +394,7 @@ class BrnCalendarCellButton {
343
394
  this._destroyRef.onDestroy(() => this._calendar.unregisterCalendarCell(this));
344
395
  }
345
396
  /** Whether this date is focusable */
346
- focusable = computed(() => this._dateAdapter.isSameDay(this._calendar.focusedDate(), this.date()), ...(ngDevMode ? [{ debugName: "focusable" }] : /* istanbul ignore next */ []));
397
+ focusable = computed(() => !this.disabled() && this._dateAdapter.isSameDay(this._calendar.focusedDate(), this.date()), ...(ngDevMode ? [{ debugName: "focusable" }] : /* istanbul ignore next */ []));
347
398
  outside = computed(() => {
348
399
  const focusedDate = this._calendar.focusedDate();
349
400
  return !this._dateAdapter.isSameMonth(this.date(), focusedDate);
@@ -362,7 +413,7 @@ class BrnCalendarCellButton {
362
413
  const targetDate = this._dateAdapter.add(this._calendar.focusedDate(), {
363
414
  days: this.getDirection() === 'rtl' ? 1 : -1,
364
415
  });
365
- this._calendar.setFocusedDate(targetDate);
416
+ this.focusEnabledDate(targetDate, this.getDirection() === 'rtl' ? 1 : -1);
366
417
  }
367
418
  /**
368
419
  * Focus the next cell.
@@ -373,7 +424,7 @@ class BrnCalendarCellButton {
373
424
  const targetDate = this._dateAdapter.add(this._calendar.focusedDate(), {
374
425
  days: this.getDirection() === 'rtl' ? -1 : 1,
375
426
  });
376
- this._calendar.setFocusedDate(targetDate);
427
+ this.focusEnabledDate(targetDate, this.getDirection() === 'rtl' ? -1 : 1);
377
428
  }
378
429
  /**
379
430
  * Focus the above cell.
@@ -381,7 +432,7 @@ class BrnCalendarCellButton {
381
432
  focusAbove(event) {
382
433
  event.preventDefault();
383
434
  event.stopPropagation();
384
- this._calendar.setFocusedDate(this._dateAdapter.subtract(this._calendar.focusedDate(), { days: 7 }));
435
+ this.focusEnabledDate(this._dateAdapter.subtract(this._calendar.focusedDate(), { days: 7 }), -1);
385
436
  }
386
437
  /**
387
438
  * Focus the below cell.
@@ -389,7 +440,7 @@ class BrnCalendarCellButton {
389
440
  focusBelow(event) {
390
441
  event.preventDefault();
391
442
  event.stopPropagation();
392
- this._calendar.setFocusedDate(this._dateAdapter.add(this._calendar.focusedDate(), { days: 7 }));
443
+ this.focusEnabledDate(this._dateAdapter.add(this._calendar.focusedDate(), { days: 7 }), 1);
393
444
  }
394
445
  /**
395
446
  * Focus the first date of the month.
@@ -397,7 +448,7 @@ class BrnCalendarCellButton {
397
448
  focusFirst(event) {
398
449
  event.preventDefault();
399
450
  event.stopPropagation();
400
- this._calendar.setFocusedDate(this._dateAdapter.startOfMonth(this._calendar.focusedDate()));
451
+ this.focusEnabledDate(this._dateAdapter.startOfMonth(this._calendar.focusedDate()), 1, this._dateAdapter.endOfMonth(this._calendar.focusedDate()));
401
452
  }
402
453
  /**
403
454
  * Focus the last date of the month.
@@ -405,7 +456,7 @@ class BrnCalendarCellButton {
405
456
  focusLast(event) {
406
457
  event.preventDefault();
407
458
  event.stopPropagation();
408
- this._calendar.setFocusedDate(this._dateAdapter.endOfMonth(this._calendar.focusedDate()));
459
+ this.focusEnabledDate(this._dateAdapter.endOfMonth(this._calendar.focusedDate()), -1, this._dateAdapter.startOfMonth(this._calendar.focusedDate()));
409
460
  }
410
461
  /**
411
462
  * Focus the same date in the previous month.
@@ -419,10 +470,10 @@ class BrnCalendarCellButton {
419
470
  const lastDay = this._dateAdapter.endOfMonth(previousMonthTarget);
420
471
  // if we are on a date that does not exist in the previous month, we should focus the last day of the month.
421
472
  if (date > this._dateAdapter.getDate(lastDay)) {
422
- this._calendar.setFocusedDate(lastDay);
473
+ this.focusEnabledDate(lastDay, -1);
423
474
  }
424
475
  else {
425
- this._calendar.setFocusedDate(this._dateAdapter.set(previousMonthTarget, { day: date }));
476
+ this.focusEnabledDate(this._dateAdapter.set(previousMonthTarget, { day: date }), -1);
426
477
  }
427
478
  }
428
479
  /**
@@ -437,12 +488,28 @@ class BrnCalendarCellButton {
437
488
  const lastDay = this._dateAdapter.endOfMonth(nextMonthTarget);
438
489
  // if we are on a date that does not exist in the next month, we should focus the last day of the month.
439
490
  if (date > this._dateAdapter.getDate(lastDay)) {
440
- this._calendar.setFocusedDate(lastDay);
491
+ this.focusEnabledDate(lastDay, 1);
441
492
  }
442
493
  else {
443
- this._calendar.setFocusedDate(this._dateAdapter.set(nextMonthTarget, { day: date }));
494
+ this.focusEnabledDate(this._dateAdapter.set(nextMonthTarget, { day: date }), 1);
444
495
  }
445
496
  }
497
+ focusEnabledDate(target, direction, boundary) {
498
+ let min = this._calendar.min();
499
+ let max = this._calendar.max();
500
+ if (boundary !== undefined) {
501
+ if (direction === 1 && (max === undefined || this._dateAdapter.isBefore(boundary, max)))
502
+ max = boundary;
503
+ if (direction === -1 && (min === undefined || this._dateAdapter.isAfter(boundary, min)))
504
+ min = boundary;
505
+ }
506
+ const date = findEnabledDate(target, direction, this._dateAdapter, (day) => this._calendar.isDateDisabled(day), {
507
+ min,
508
+ max,
509
+ });
510
+ if (date !== undefined)
511
+ this._calendar.setFocusedDate(date);
512
+ }
446
513
  /**
447
514
  * Get the direction of the element.
448
515
  */
@@ -564,6 +631,30 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.16", ngImpo
564
631
  }]
565
632
  }], ctorParameters: () => [] });
566
633
 
634
+ /**
635
+ * Enabled day in `month` closest to `preferred`'s day-of-month.
636
+ * Undefined when every day in the month is disabled.
637
+ * An earlier day wins a distance tie.
638
+ */
639
+ function nearestEnabledDateInMonth(month, preferred, adapter, isDisabled) {
640
+ const preferredDay = adapter.getDate(preferred);
641
+ let cursor = adapter.startOfMonth(month);
642
+ const end = adapter.endOfMonth(month);
643
+ let best;
644
+ let bestDistance = Number.POSITIVE_INFINITY;
645
+ for (let step = 0; step < 31 && !adapter.isAfter(cursor, end); step++) {
646
+ if (!isDisabled(cursor)) {
647
+ const distance = Math.abs(adapter.getDate(cursor) - preferredDay);
648
+ if (distance < bestDistance) {
649
+ best = cursor;
650
+ bestDistance = distance;
651
+ }
652
+ }
653
+ cursor = adapter.add(cursor, { days: 1 });
654
+ }
655
+ return best;
656
+ }
657
+
567
658
  class BrnCalendarNextButton {
568
659
  /** Access the calendar */
569
660
  _calendar = injectBrnCalendar();
@@ -571,27 +662,26 @@ class BrnCalendarNextButton {
571
662
  _dateAdapter = injectDateAdapter();
572
663
  /** Access the calendar i18n */
573
664
  _i18n = injectBrnCalendarI18n();
665
+ /**
666
+ * An enabled day in the next month, closest to the focused day number.
667
+ * Undefined when the whole month is outside min/max or rejected by `dateDisabled`.
668
+ */
669
+ _enabledTarget = computed(() => {
670
+ const targetDate = this.nextMonthDate();
671
+ return nearestEnabledDateInMonth(targetDate, targetDate, this._dateAdapter, (date) => this._calendar.isDateDisabled(date));
672
+ }, ...(ngDevMode ? [{ debugName: "_enabledTarget" }] : /* istanbul ignore next */ []));
574
673
  /** Whether navigating to the next month is disabled. */
575
- _disabled = computed(() => {
576
- const { targetDate, possibleDate } = this.nextMonthTarget();
577
- const effective = this._dateAdapter.isSameMonth(possibleDate, targetDate) ? possibleDate : targetDate;
578
- return this._calendar.isDateDisabled(effective);
579
- }, ...(ngDevMode ? [{ debugName: "_disabled" }] : /* istanbul ignore next */ []));
674
+ _disabled = computed(() => this._enabledTarget() === undefined, ...(ngDevMode ? [{ debugName: "_disabled" }] : /* istanbul ignore next */ []));
580
675
  /** Focus the next month */
581
676
  focusNextMonth() {
582
- if (this._disabled()) {
677
+ const date = this._enabledTarget();
678
+ if (date === undefined) {
583
679
  return;
584
680
  }
585
- const { targetDate, possibleDate } = this.nextMonthTarget();
586
- if (this._dateAdapter.isSameMonth(possibleDate, targetDate)) {
587
- // if this date is within the same month, then focus it
588
- this._calendar.setFocusedDate(possibleDate);
589
- return;
590
- }
591
- this._calendar.setFocusedDate(targetDate);
681
+ this._calendar.setFocusedDate(date);
592
682
  }
593
- /** @internal The target date in the next month and its constrained counterpart. */
594
- nextMonthTarget() {
683
+ /** The same day number in the next month, clamped to that month's length. */
684
+ nextMonthDate() {
595
685
  const focusedDate = this._calendar.focusedDate();
596
686
  const date = this._dateAdapter.getDate(focusedDate);
597
687
  // go to start of month first, then add 1 month to avoid day overflow
@@ -599,15 +689,10 @@ class BrnCalendarNextButton {
599
689
  nextMonthTarget = this._dateAdapter.add(nextMonthTarget, { months: 1 });
600
690
  const lastDay = this._dateAdapter.endOfMonth(nextMonthTarget);
601
691
  // if we are on a date that does not exist in the next month, clamp to the last day of the month.
602
- let targetDate;
603
692
  if (date > this._dateAdapter.getDate(lastDay)) {
604
- targetDate = lastDay;
605
- }
606
- else {
607
- targetDate = this._dateAdapter.set(nextMonthTarget, { day: date });
693
+ return lastDay;
608
694
  }
609
- // if the date is disabled, but there are available dates in the month, focus the constrained date.
610
- return { targetDate, possibleDate: this._calendar.constrainDate(targetDate) };
695
+ return this._dateAdapter.set(nextMonthTarget, { day: date });
611
696
  }
612
697
  /** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnCalendarNextButton, deps: [], target: i0.ɵɵFactoryTarget.Directive });
613
698
  /** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.16", type: BrnCalendarNextButton, isStandalone: true, selector: "button[brnCalendarNextButton]", host: { attributes: { "type": "button", "data-slot": "calendar-next-button" }, listeners: { "click": "focusNextMonth()" }, properties: { "attr.aria-label": "_i18n.config().labelNext()", "attr.aria-disabled": "_disabled() ? true : null", "disabled": "_disabled()" } }, exportAs: ["brnCalendarNextButton"], ngImport: i0 });
@@ -635,27 +720,26 @@ class BrnCalendarPreviousButton {
635
720
  _dateAdapter = injectDateAdapter();
636
721
  /** Access the calendar i18n */
637
722
  _i18n = injectBrnCalendarI18n();
723
+ /**
724
+ * An enabled day in the previous month, closest to the focused day number.
725
+ * Undefined when the whole month is outside min/max or rejected by `dateDisabled`.
726
+ */
727
+ _enabledTarget = computed(() => {
728
+ const targetDate = this.previousMonthDate();
729
+ return nearestEnabledDateInMonth(targetDate, targetDate, this._dateAdapter, (date) => this._calendar.isDateDisabled(date));
730
+ }, ...(ngDevMode ? [{ debugName: "_enabledTarget" }] : /* istanbul ignore next */ []));
638
731
  /** Whether navigating to the previous month is disabled. */
639
- _disabled = computed(() => {
640
- const { targetDate, possibleDate } = this.previousMonthTarget();
641
- const effective = this._dateAdapter.isSameMonth(possibleDate, targetDate) ? possibleDate : targetDate;
642
- return this._calendar.isDateDisabled(effective);
643
- }, ...(ngDevMode ? [{ debugName: "_disabled" }] : /* istanbul ignore next */ []));
732
+ _disabled = computed(() => this._enabledTarget() === undefined, ...(ngDevMode ? [{ debugName: "_disabled" }] : /* istanbul ignore next */ []));
644
733
  /** Focus the previous month */
645
734
  focusPreviousMonth() {
646
- if (this._disabled()) {
647
- return;
648
- }
649
- const { targetDate, possibleDate } = this.previousMonthTarget();
650
- if (this._dateAdapter.isSameMonth(possibleDate, targetDate)) {
651
- // if this date is within the same month, then focus it
652
- this._calendar.setFocusedDate(possibleDate);
735
+ const date = this._enabledTarget();
736
+ if (date === undefined) {
653
737
  return;
654
738
  }
655
- this._calendar.setFocusedDate(targetDate);
739
+ this._calendar.setFocusedDate(date);
656
740
  }
657
- /** @internal The target date in the previous month and its constrained counterpart. */
658
- previousMonthTarget() {
741
+ /** The same day number in the previous month, clamped to that month's length. */
742
+ previousMonthDate() {
659
743
  const focusedDate = this._calendar.focusedDate();
660
744
  const date = this._dateAdapter.getDate(focusedDate);
661
745
  // go to start of month first, then subtract 1 month to avoid day overflow
@@ -663,15 +747,10 @@ class BrnCalendarPreviousButton {
663
747
  previousMonthTarget = this._dateAdapter.subtract(previousMonthTarget, { months: 1 });
664
748
  const lastDay = this._dateAdapter.endOfMonth(previousMonthTarget);
665
749
  // if we are on a date that does not exist in the previous month, clamp to the last day of the month.
666
- let targetDate;
667
750
  if (date > this._dateAdapter.getDate(lastDay)) {
668
- targetDate = lastDay;
751
+ return lastDay;
669
752
  }
670
- else {
671
- targetDate = this._dateAdapter.set(previousMonthTarget, { day: date });
672
- }
673
- // if the date is disabled, but there are available dates in the month, focus the constrained date.
674
- return { targetDate, possibleDate: this._calendar.constrainDate(targetDate) };
753
+ return this._dateAdapter.set(previousMonthTarget, { day: date });
675
754
  }
676
755
  /** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnCalendarPreviousButton, deps: [], target: i0.ɵɵFactoryTarget.Directive });
677
756
  /** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.16", type: BrnCalendarPreviousButton, isStandalone: true, selector: "button[brnCalendarPreviousButton]", host: { attributes: { "type": "button", "data-slot": "calendar-previous-button" }, listeners: { "click": "focusPreviousMonth()" }, properties: { "attr.aria-label": "_i18n.config().labelPrevious()", "attr.aria-disabled": "_disabled() ? true : null", "disabled": "_disabled()" } }, exportAs: ["brnCalendarPreviousButton"], ngImport: i0 });
@@ -929,7 +1008,32 @@ class BrnCalendarMulti {
929
1008
  /**
930
1009
  * The focused date.
931
1010
  */
932
- focusedDate = linkedSignal(() => this.constrainDate(this.defaultFocusedDate() ?? this._dateAdapter.now()), ...(ngDevMode ? [{ debugName: "focusedDate" }] : /* istanbul ignore next */ []));
1011
+ focusedDate = linkedSignal({ ...(ngDevMode ? { debugName: "focusedDate" } : /* istanbul ignore next */ {}), source: () => ({
1012
+ initialDate: this.constrainDate(this.defaultFocusedDate() ?? this._dateAdapter.now()),
1013
+ startingDate: this.defaultFocusedDate(),
1014
+ selectedDate: this.defaultFocusedDate(),
1015
+ min: this.min(),
1016
+ max: this.max(),
1017
+ disabled: this.disabled(),
1018
+ dateDisabled: this.dateDisabled(),
1019
+ }),
1020
+ computation: (source, previous) => {
1021
+ if (source.disabled)
1022
+ return source.initialDate;
1023
+ // Availability changes must not reset keyboard focus to the default date.
1024
+ // A new initial date or changed limits should still reset it.
1025
+ const preserveFocus = previous !== undefined &&
1026
+ previous.source.startingDate === source.startingDate &&
1027
+ previous.source.selectedDate === source.selectedDate &&
1028
+ previous.source.min === source.min &&
1029
+ previous.source.max === source.max &&
1030
+ previous.source.disabled === source.disabled;
1031
+ const target = preserveFocus ? previous.value : source.initialDate;
1032
+ return findInitialFocusedDate(target, this._dateAdapter, (day) => this.isDateDisabled(day), {
1033
+ min: source.min,
1034
+ max: source.max,
1035
+ });
1036
+ } });
933
1037
  /** Emits whenever the focused date changes. */
934
1038
  focusedDateChange = output();
935
1039
  _setFocusedDate(date) {
@@ -1101,7 +1205,32 @@ class BrnCalendarRange {
1101
1205
  /**
1102
1206
  * The focused date.
1103
1207
  */
1104
- focusedDate = linkedSignal(() => this.constrainDate(this.defaultFocusedDate() ?? this.startDate() ?? this._dateAdapter.now()), ...(ngDevMode ? [{ debugName: "focusedDate" }] : /* istanbul ignore next */ []));
1208
+ focusedDate = linkedSignal({ ...(ngDevMode ? { debugName: "focusedDate" } : /* istanbul ignore next */ {}), source: () => ({
1209
+ initialDate: this.constrainDate(this.defaultFocusedDate() ?? this.startDate() ?? this._dateAdapter.now()),
1210
+ startingDate: this.defaultFocusedDate(),
1211
+ selectedDate: this.startDate(),
1212
+ min: this.min(),
1213
+ max: this.max(),
1214
+ disabled: this.disabled(),
1215
+ dateDisabled: this.dateDisabled(),
1216
+ }),
1217
+ computation: (source, previous) => {
1218
+ if (source.disabled)
1219
+ return source.initialDate;
1220
+ // Availability changes must not reset keyboard focus to the default date.
1221
+ // A new initial date or changed limits should still reset it.
1222
+ const preserveFocus = previous !== undefined &&
1223
+ previous.source.startingDate === source.startingDate &&
1224
+ previous.source.selectedDate === source.selectedDate &&
1225
+ previous.source.min === source.min &&
1226
+ previous.source.max === source.max &&
1227
+ previous.source.disabled === source.disabled;
1228
+ const target = preserveFocus ? previous.value : source.initialDate;
1229
+ return findInitialFocusedDate(target, this._dateAdapter, (day) => this.isDateDisabled(day), {
1230
+ min: source.min,
1231
+ max: source.max,
1232
+ });
1233
+ } });
1105
1234
  /** Emits whenever the focused date changes. */
1106
1235
  focusedDateChange = output();
1107
1236
  _setFocusedDate(date) {