tyrell-components 1.0.0-RC15 → 1.0.0-RC17

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.
Files changed (108) hide show
  1. package/README.md +44 -52
  2. package/css/tyrell-colors-static.css +14 -0
  3. package/css/tyrell-theme.css +13 -7
  4. package/css/tyrell.css +15 -22
  5. package/dist/tyrell-colors-static.css +1 -1
  6. package/dist/tyrell-theme.css +1 -1
  7. package/dist/tyrell.css +1 -1
  8. package/dist/tyrell.js +1 -1
  9. package/lib/base/ty-component.d.ts +37 -15
  10. package/lib/base/ty-component.js +90 -26
  11. package/lib/components/button.d.ts +14 -0
  12. package/lib/components/button.js +61 -7
  13. package/lib/components/calendar-month.d.ts +7 -1
  14. package/lib/components/calendar-month.js +38 -30
  15. package/lib/components/calendar-navigation.d.ts +11 -0
  16. package/lib/components/calendar-navigation.js +68 -44
  17. package/lib/components/calendar.d.ts +52 -3
  18. package/lib/components/calendar.js +274 -61
  19. package/lib/components/checkbox.d.ts +8 -1
  20. package/lib/components/checkbox.js +29 -8
  21. package/lib/components/copy.d.ts +2 -0
  22. package/lib/components/copy.js +24 -33
  23. package/lib/components/date-picker.d.ts +68 -34
  24. package/lib/components/date-picker.js +386 -389
  25. package/lib/components/file-upload.d.ts +7 -0
  26. package/lib/components/file-upload.js +36 -4
  27. package/lib/components/input.d.ts +73 -3
  28. package/lib/components/input.js +159 -24
  29. package/lib/components/modal.d.ts +4 -8
  30. package/lib/components/modal.js +103 -33
  31. package/lib/components/option.js +7 -5
  32. package/lib/components/popup.js +117 -14
  33. package/lib/components/radio.d.ts +24 -0
  34. package/lib/components/radio.js +55 -32
  35. package/lib/components/resize-observer.js +2 -0
  36. package/lib/components/scroll-container.d.ts +3 -2
  37. package/lib/components/scroll-container.js +7 -8
  38. package/lib/components/select.d.ts +68 -11
  39. package/lib/components/select.js +196 -44
  40. package/lib/components/selected-tags.d.ts +7 -1
  41. package/lib/components/selected-tags.js +10 -1
  42. package/lib/components/switch.d.ts +9 -1
  43. package/lib/components/switch.js +29 -8
  44. package/lib/components/tab.d.ts +49 -1
  45. package/lib/components/tab.js +151 -10
  46. package/lib/components/tabs.d.ts +14 -2
  47. package/lib/components/tabs.js +185 -136
  48. package/lib/components/tag.d.ts +4 -7
  49. package/lib/components/tag.js +6 -7
  50. package/lib/components/textarea.d.ts +51 -5
  51. package/lib/components/textarea.js +95 -30
  52. package/lib/components/tooltip.d.ts +4 -3
  53. package/lib/components/tooltip.js +34 -24
  54. package/lib/components/wizard.d.ts +5 -4
  55. package/lib/components/wizard.js +120 -156
  56. package/lib/index.d.ts +2 -1
  57. package/lib/index.js +1 -0
  58. package/lib/styles/button.js +21 -19
  59. package/lib/styles/calendar-month.d.ts +3 -0
  60. package/lib/styles/calendar-month.js +43 -27
  61. package/lib/styles/calendar-navigation.d.ts +1 -1
  62. package/lib/styles/calendar-navigation.js +55 -26
  63. package/lib/styles/calendar.d.ts +3 -2
  64. package/lib/styles/calendar.js +77 -0
  65. package/lib/styles/checkbox.js +31 -13
  66. package/lib/styles/copy.js +5 -1
  67. package/lib/styles/date-picker.js +68 -26
  68. package/lib/styles/file-upload.d.ts +1 -1
  69. package/lib/styles/file-upload.js +3 -3
  70. package/lib/styles/icon.d.ts +1 -1
  71. package/lib/styles/icon.js +2 -2
  72. package/lib/styles/input.js +6 -10
  73. package/lib/styles/option.d.ts +1 -1
  74. package/lib/styles/option.js +7 -3
  75. package/lib/styles/popup.d.ts +1 -1
  76. package/lib/styles/popup.js +10 -5
  77. package/lib/styles/radio.js +18 -6
  78. package/lib/styles/scroll-container.d.ts +1 -1
  79. package/lib/styles/scroll-container.js +1 -1
  80. package/lib/styles/select-base.js +24 -3
  81. package/lib/styles/select.d.ts +1 -1
  82. package/lib/styles/select.js +10 -0
  83. package/lib/styles/step.d.ts +1 -1
  84. package/lib/styles/step.js +1 -9
  85. package/lib/styles/switch.js +14 -6
  86. package/lib/styles/tab.d.ts +1 -1
  87. package/lib/styles/tab.js +67 -3
  88. package/lib/styles/tabs.d.ts +1 -1
  89. package/lib/styles/tabs.js +7 -3
  90. package/lib/styles/tag.js +3 -3
  91. package/lib/styles/textarea.d.ts +1 -1
  92. package/lib/styles/textarea.js +7 -0
  93. package/lib/styles/wizard.d.ts +9 -5
  94. package/lib/styles/wizard.js +211 -99
  95. package/lib/types/common.d.ts +2 -2
  96. package/lib/utils/calendar-utils.d.ts +49 -0
  97. package/lib/utils/calendar-utils.js +63 -6
  98. package/lib/utils/property-manager.d.ts +7 -1
  99. package/lib/utils/property-manager.js +16 -4
  100. package/lib/utils/scroll-lock.d.ts +10 -4
  101. package/lib/utils/scroll-lock.js +102 -2
  102. package/lib/utils/upgrade-properties.d.ts +21 -0
  103. package/lib/utils/upgrade-properties.js +47 -0
  104. package/lib/utils/validity.d.ts +122 -0
  105. package/lib/utils/validity.js +289 -0
  106. package/lib/version.d.ts +1 -1
  107. package/lib/version.js +1 -1
  108. package/package.json +27 -5
@@ -1,9 +1,11 @@
1
1
  import { ensureStyles } from '../utils/styles.js';
2
+ import { liftProperties, upgradeProperties } from '../utils/upgrade-properties.js';
2
3
  import { calendarStyles } from '../styles/calendar.js';
3
4
  import './calendar-month.js';
4
5
  import './calendar-navigation.js';
5
- import { parseISODate } from '../utils/calendar-utils.js';
6
+ import { parseDateDay, formatISODay, toISODay, getMonthName, localMidnight, utcMidnight } from '../utils/calendar-utils.js';
6
7
  import { getEffectiveLocale, observeLocaleChanges } from '../utils/locale.js';
8
+ import { ValidityController, nativeMessage } from '../utils/validity.js';
7
9
  function getCurrentDate() {
8
10
  const now = new Date();
9
11
  return {
@@ -30,14 +32,12 @@ const day = parseInt(dayStr, 10);
30
32
  const daysInMonth = new Date(year, month, 0).getDate();
31
33
  return day >= 1 && day <= daysInMonth ? day : null;
32
34
  }
33
- function formatDateISO(year, month, day) {
34
- const monthStr = month.toString().padStart(2, '0');
35
- const dayStr = day.toString().padStart(2, '0');
36
- return `${year}-${monthStr}-${dayStr}`;
35
+ function formatYear(year) {
36
+ return year.toString().padStart(4, '0');
37
37
  }
38
38
  export class TyCalendar extends HTMLElement {
39
39
  static get observedAttributes() {
40
- return ['value', 'year', 'month', 'day', 'hide-navigation', 'stateless', 'locale', 'name', 'size', 'width', 'min', 'max', 'flavor'];
40
+ return ['value', 'year', 'month', 'day', 'hide-navigation', 'stateless', 'locale', 'name', 'size', 'width', 'min', 'max', 'flavor', 'required'];
41
41
  }
42
42
  constructor() {
43
43
  super();
@@ -45,49 +45,26 @@ this._hideNavigation = false;
45
45
  this._stateless = false;
46
46
  this._size = 'md';
47
47
  this._flavor = 'primary';
48
+ this._view = 'days';
49
+ this._dayGridHeight = 0;
48
50
  this._initialValue = '';
51
+ this._valueSets = 0;
52
+ this._pressing = false;
49
53
  const current = getCurrentDate();
50
54
  this._state = {
51
55
  displayYear: current.year,
52
56
  displayMonth: current.month,
53
57
  };
54
- this.attachShadow({ mode: 'open' });
58
+ this.attachShadow({ mode: 'open', delegatesFocus: true });
59
+ liftProperties(this);
55
60
  if ('attachInternals' in this) {
56
61
  this._internals = this.attachInternals();
62
+ this._validity = new ValidityController(this, this._internals);
57
63
  }
58
64
  }
59
65
  connectedCallback() {
60
66
  this.initializeFromAttributes();
61
- const plainDayContentFn = this.dayContentFn;
62
- if (plainDayContentFn && !this._dayContentFn) {
63
- this._dayContentFn = plainDayContentFn;
64
- delete this.dayContentFn;
65
- }
66
- const plainCustomCSS = this.customCSS;
67
- if (plainCustomCSS && !this._customCSS) {
68
- this._customCSS = plainCustomCSS;
69
- delete this.customCSS;
70
- }
71
- const plainValue = this.value;
72
- if (plainValue && typeof plainValue === 'string' && plainValue !== this.value) {
73
- const match = plainValue.match(/^(\d{4})-(\d{2})-(\d{2})$/);
74
- if (match) {
75
- const year = parseInt(match[1], 10);
76
- const month = parseInt(match[2], 10);
77
- const day = parseInt(match[3], 10);
78
- this._state.selectedYear = year;
79
- this._state.selectedMonth = month;
80
- this._state.selectedDay = day;
81
- this._state.displayYear = year;
82
- this._state.displayMonth = month;
83
- if (!this._stateless) {
84
- this.setAttribute('year', year.toString());
85
- this.setAttribute('month', month.toString());
86
- this.setAttribute('day', day.toString());
87
- }
88
- }
89
- delete this.value;
90
- }
67
+ upgradeProperties(this);
91
68
  this.render();
92
69
  this.updateFormValue();
93
70
  this._localeObserver = observeLocaleChanges(this, () => {
@@ -101,6 +78,9 @@ this._localeObserver = undefined;
101
78
  }
102
79
  this._navigation = undefined;
103
80
  this._monthDisplay = undefined;
81
+ this._monthGrid = undefined;
82
+ this._container = undefined;
83
+ this._view = 'days';
104
84
  }
105
85
  attributeChangedCallback(name, oldValue, newValue) {
106
86
  if (oldValue === newValue)
@@ -151,6 +131,9 @@ this[name === 'min' ? '_min' : '_max'] = newValue || undefined;
151
131
  this.syncChildComponents();
152
132
  this.updateValidity();
153
133
  break;
134
+ case 'required':
135
+ this.updateValidity();
136
+ break;
154
137
  case 'flavor':
155
138
  this._flavor = newValue || 'primary';
156
139
  this.syncChildComponents();
@@ -184,6 +167,12 @@ this.setAttribute('flavor', value);
184
167
  else
185
168
  this.removeAttribute('flavor');
186
169
  }
170
+ get required() {
171
+ return this.hasAttribute('required');
172
+ }
173
+ set required(value) {
174
+ this.toggleAttribute('required', !!value);
175
+ }
187
176
  get year() {
188
177
  return this._state.selectedYear;
189
178
  }
@@ -191,12 +180,12 @@ set year(value) {
191
180
  if (value !== undefined) {
192
181
  if (this._stateless) {
193
182
  this._state.displayYear = value;
194
- this.setAttribute('year', value.toString());
183
+ this.setAttribute('year', formatYear(value));
195
184
  }
196
185
  else {
197
186
  this._state.selectedYear = value;
198
187
  this._state.displayYear = value;
199
- this.setAttribute('year', value.toString());
188
+ this.setAttribute('year', formatYear(value));
200
189
  }
201
190
  }
202
191
  else {
@@ -325,35 +314,43 @@ this.syncChildComponents();
325
314
  get value() {
326
315
  const { selectedYear, selectedMonth, selectedDay } = this._state;
327
316
  if (selectedYear && selectedMonth && selectedDay) {
328
- return formatDateISO(selectedYear, selectedMonth, selectedDay);
317
+ return formatISODay(selectedYear, selectedMonth, selectedDay);
329
318
  }
330
319
  return '';
331
320
  }
332
321
  set value(isoDate) {
333
- if (!isoDate) {
322
+ const set = ++this._valueSets;
323
+ const superseded = () => set !== this._valueSets;
324
+ if (isoDate === null || isoDate === undefined || isoDate === '') {
334
325
  delete this._state.selectedYear;
335
326
  delete this._state.selectedMonth;
336
327
  delete this._state.selectedDay;
337
328
  if (!this._stateless) {
338
329
  this.removeAttribute('year');
330
+ if (superseded())
331
+ return;
339
332
  this.removeAttribute('month');
333
+ if (superseded())
334
+ return;
340
335
  this.removeAttribute('day');
341
336
  }
342
337
  }
343
338
  else {
344
- const match = isoDate.match(/^(\d{4})-(\d{2})-(\d{2})$/);
345
- if (match) {
346
- const year = parseInt(match[1], 10);
347
- const month = parseInt(match[2], 10);
348
- const day = parseInt(match[3], 10);
339
+ const parsed = parseDateDay(isoDate);
340
+ if (parsed) {
341
+ const { year, month, day } = parsed;
349
342
  this._state.selectedYear = year;
350
343
  this._state.selectedMonth = month;
351
344
  this._state.selectedDay = day;
352
345
  this._state.displayYear = year;
353
346
  this._state.displayMonth = month;
354
347
  if (!this._stateless) {
355
- this.setAttribute('year', year.toString());
348
+ this.setAttribute('year', formatYear(year));
349
+ if (superseded())
350
+ return;
356
351
  this.setAttribute('month', month.toString());
352
+ if (superseded())
353
+ return;
357
354
  this.setAttribute('day', day.toString());
358
355
  }
359
356
  }
@@ -442,6 +439,10 @@ this._navigation.size = this._size;
442
439
  this._navigation.min = this._min ?? '';
443
440
  this._navigation.max = this._max ?? '';
444
441
  this._navigation.width = this._width;
442
+ this._navigation.view = this._view;
443
+ }
444
+ if (this._monthGrid) {
445
+ this.showBody();
445
446
  }
446
447
  if (this._monthDisplay) {
447
448
  this._monthDisplay.displayMonth = this._state.displayMonth;
@@ -460,13 +461,13 @@ this._monthDisplay.customCSS = this._customCSS;
460
461
  }
461
462
  const { selectedYear, selectedMonth, selectedDay } = this._state;
462
463
  if (selectedYear && selectedMonth && selectedDay) {
463
- const date = new Date(selectedYear, selectedMonth - 1, selectedDay);
464
- this._monthDisplay.value = date.getTime();
464
+ this._monthDisplay.value = localMidnight(selectedYear, selectedMonth, selectedDay).getTime();
465
465
  }
466
466
  else {
467
467
  this._monthDisplay.value = null;
468
468
  }
469
469
  }
470
+ this.updateValidity();
470
471
  }
471
472
  updateFormValue() {
472
473
  if (this._stateless)
@@ -476,7 +477,7 @@ return;
476
477
  const elementName = this.getAttribute('name');
477
478
  const { selectedYear, selectedMonth, selectedDay } = this._state;
478
479
  if (elementName && selectedYear && selectedMonth && selectedDay) {
479
- const isoDate = formatDateISO(selectedYear, selectedMonth, selectedDay);
480
+ const isoDate = formatISODay(selectedYear, selectedMonth, selectedDay);
480
481
  this._internals.setFormValue(isoDate);
481
482
  }
482
483
  else {
@@ -487,26 +488,42 @@ this.updateValidity();
487
488
  updateValidity() {
488
489
  if (!this._internals)
489
490
  return;
491
+ const anchor = this.gridCell() ?? undefined;
490
492
  const { selectedYear, selectedMonth, selectedDay } = this._state;
491
493
  if (selectedYear && selectedMonth && selectedDay) {
492
- const sel = Date.UTC(selectedYear, selectedMonth - 1, selectedDay);
493
- const min = this._min ? parseISODate(this._min) : null;
494
- const max = this._max ? parseISODate(this._max) : null;
495
- if (min && sel < Date.UTC(min.year, min.month - 1, min.day)) {
496
- this._internals.setValidity({ rangeUnderflow: true }, `Date must be ${this._min} or later`);
494
+ const sel = utcMidnight(selectedYear, selectedMonth, selectedDay);
495
+ const min = this._min ? parseDateDay(this._min) : null;
496
+ const max = this._max ? parseDateDay(this._max) : null;
497
+ const message = () => nativeMessage('input', { type: 'date', min: toISODay(this._min) ?? undefined, max: toISODay(this._max) ?? undefined }, formatISODay(selectedYear, selectedMonth, selectedDay));
498
+ if (min && sel < utcMidnight(min.year, min.month, min.day)) {
499
+ this._validity.set({ rangeUnderflow: true }, message(), anchor);
497
500
  return;
498
501
  }
499
- if (max && sel > Date.UTC(max.year, max.month - 1, max.day)) {
500
- this._internals.setValidity({ rangeOverflow: true }, `Date must be ${this._max} or earlier`);
502
+ if (max && sel > utcMidnight(max.year, max.month, max.day)) {
503
+ this._validity.set({ rangeOverflow: true }, message(), anchor);
501
504
  return;
502
505
  }
503
506
  }
504
- this._internals.setValidity({});
507
+ else if (this.required) {
508
+ this._validity.set({ valueMissing: true }, nativeMessage('input', { type: 'date', required: true }), anchor);
509
+ return;
510
+ }
511
+ this._validity.set({}, undefined, anchor);
505
512
  }
513
+ get form() { return this._internals?.form ?? null; }
514
+ get validity() { return this._internals?.validity; }
515
+ get validationMessage() { return this._internals?.validationMessage ?? ''; }
516
+ get willValidate() { return this._internals?.willValidate ?? false; }
517
+ checkValidity() { return this._internals?.checkValidity() ?? true; }
518
+ reportValidity() { return this._validity?.report() ?? true; }
519
+ setCustomValidity(message) { this._validity?.setCustom(message); }
506
520
  handleNavigationChange(event) {
507
521
  event.preventDefault();
508
522
  event.stopPropagation();
509
523
  const { month, year } = event.detail;
524
+ this.goTo(month, year);
525
+ }
526
+ goTo(month, year) {
510
527
  this._state.displayMonth = month;
511
528
  this._state.displayYear = year;
512
529
  this.syncChildComponents();
@@ -541,7 +558,7 @@ this._state.selectedMonth = month;
541
558
  this._state.selectedDay = day;
542
559
  this._state.displayYear = year;
543
560
  this._state.displayMonth = month;
544
- this.setAttribute('year', year.toString());
561
+ this.setAttribute('year', formatYear(year));
545
562
  this.setAttribute('month', month.toString());
546
563
  this.setAttribute('day', day.toString());
547
564
  this.updateFormValue();
@@ -566,6 +583,7 @@ this.syncChildComponents();
566
583
  if (this._monthDisplay && typeof this._monthDisplay.refresh === 'function') {
567
584
  this._monthDisplay.refresh();
568
585
  }
586
+ this.updateValidity();
569
587
  }
570
588
  createNavigation() {
571
589
  const nav = document.createElement('ty-calendar-navigation');
@@ -578,7 +596,12 @@ nav.max = this._max ?? '';
578
596
  if (this._width) {
579
597
  nav.width = this._width;
580
598
  }
599
+ nav.view = this._view;
581
600
  nav.addEventListener('change', (e) => this.handleNavigationChange(e));
601
+ nav.addEventListener('view-toggle', (e) => {
602
+ this.setView(e.detail.view);
603
+ this.focusNavTitle();
604
+ });
582
605
  this._navigation = nav;
583
606
  return nav;
584
607
  }
@@ -596,8 +619,7 @@ month.width = this._width;
596
619
  }
597
620
  const { selectedYear, selectedMonth, selectedDay } = this._state;
598
621
  if (selectedYear && selectedMonth && selectedDay) {
599
- const date = new Date(selectedYear, selectedMonth - 1, selectedDay);
600
- month.value = date.getTime();
622
+ month.value = localMidnight(selectedYear, selectedMonth, selectedDay).getTime();
601
623
  }
602
624
  if (this._dayContentFn) {
603
625
  month.dayContentFn = this._dayContentFn;
@@ -606,6 +628,7 @@ if (this._customCSS) {
606
628
  month.customCSS = this._customCSS;
607
629
  }
608
630
  month.addEventListener('day-click', (e) => this.handleDayClick(e));
631
+ month.shadowRoot?.addEventListener('focusin', () => this.updateValidity());
609
632
  this._monthDisplay = month;
610
633
  return month;
611
634
  }
@@ -615,8 +638,27 @@ if (!root)
615
638
  return;
616
639
  ensureStyles(root, { css: calendarStyles, id: 'ty-calendar' });
617
640
  root.innerHTML = '';
641
+ this._monthGrid = undefined;
618
642
  const container = document.createElement('div');
619
643
  container.className = 'calendar-container';
644
+ container.tabIndex = -1;
645
+ const press = () => {
646
+ this._pressing = true;
647
+ setTimeout(() => { this._pressing = false; });
648
+ };
649
+ container.addEventListener('pointerdown', press);
650
+ container.addEventListener('mousedown', press);
651
+ container.addEventListener('focus', () => {
652
+ if (this._pressing)
653
+ return;
654
+ queueMicrotask(() => {
655
+ if (this.shadowRoot?.activeElement === container)
656
+ this.gridCell()?.focus();
657
+ });
658
+ });
659
+ container.addEventListener('keydown', (e) => this.handleKeydown(e));
660
+ container.addEventListener('focusin', () => this.updateValidity());
661
+ this._container = container;
620
662
  if (!this._hideNavigation) {
621
663
  const nav = this.createNavigation();
622
664
  container.appendChild(nav);
@@ -624,6 +666,177 @@ container.appendChild(nav);
624
666
  const month = this.createMonthDisplay();
625
667
  container.appendChild(month);
626
668
  root.appendChild(container);
669
+ if (this._view === 'months')
670
+ this.showBody();
671
+ this.updateValidity();
672
+ }
673
+ setView(view) {
674
+ if (view === this._view)
675
+ return;
676
+ if (view === 'months' && this._monthDisplay) {
677
+ this._dayGridHeight = this._monthDisplay.getBoundingClientRect().height;
678
+ }
679
+ this._view = view;
680
+ if (this._navigation)
681
+ this._navigation.view = view;
682
+ this.showBody();
683
+ this.updateValidity();
684
+ }
685
+ showBody() {
686
+ const container = this._container;
687
+ if (!container || !this._monthDisplay)
688
+ return;
689
+ if (this._view === 'days') {
690
+ this._monthGrid?.remove();
691
+ this._monthGrid = undefined;
692
+ if (!this._monthDisplay.isConnected)
693
+ container.appendChild(this._monthDisplay);
694
+ return;
695
+ }
696
+ const grid = this.buildMonthGrid();
697
+ if (this._monthGrid)
698
+ this._monthGrid.replaceWith(grid);
699
+ else {
700
+ this._monthDisplay.remove();
701
+ container.appendChild(grid);
702
+ }
703
+ this._monthGrid = grid;
704
+ }
705
+ static monthIndex(year, month) {
706
+ return year * 12 + (month - 1);
707
+ }
708
+ boundIndices() {
709
+ const min = this._min ? parseDateDay(this._min) : null;
710
+ const max = this._max ? parseDateDay(this._max) : null;
711
+ return {
712
+ minIdx: min ? TyCalendar.monthIndex(min.year, min.month) : null,
713
+ maxIdx: max ? TyCalendar.monthIndex(max.year, max.month) : null,
714
+ };
715
+ }
716
+ buildMonthGrid() {
717
+ const { displayYear, displayMonth, selectedYear, selectedMonth } = this._state;
718
+ const today = getCurrentDate();
719
+ const { minIdx, maxIdx } = this.boundIndices();
720
+ const grid = document.createElement('div');
721
+ grid.className = `month-grid size-${this._size}`;
722
+ grid.setAttribute('role', 'grid');
723
+ grid.setAttribute('aria-label', String(displayYear));
724
+ if (this._dayGridHeight)
725
+ grid.style.minHeight = `${this._dayGridHeight}px`;
726
+ if (this._width)
727
+ grid.style.setProperty('--calendar-width', this._width);
728
+ for (let month = 1; month <= 12; month++) {
729
+ const idx = TyCalendar.monthIndex(displayYear, month);
730
+ const disabled = (minIdx !== null && idx < minIdx) || (maxIdx !== null && idx > maxIdx);
731
+ const selected = selectedYear === displayYear && selectedMonth === month;
732
+ const cell = document.createElement('button');
733
+ cell.type = 'button';
734
+ cell.className = 'month-cell';
735
+ cell.setAttribute('role', 'gridcell');
736
+ cell.setAttribute('aria-selected', String(selected));
737
+ cell.setAttribute('aria-label', `${getMonthName(month, this.locale, 'long')} ${displayYear}`);
738
+ cell.dataset.month = String(month);
739
+ cell.tabIndex = month === displayMonth ? 0 : -1;
740
+ cell.disabled = disabled;
741
+ if (selected)
742
+ cell.classList.add('selected');
743
+ if (today.year === displayYear && today.month === month)
744
+ cell.classList.add('current');
745
+ cell.textContent = getMonthName(month, this.locale, 'short');
746
+ cell.addEventListener('click', () => this.pickMonth(month));
747
+ grid.appendChild(cell);
748
+ }
749
+ grid.addEventListener('keydown', (e) => {
750
+ const delta = e.key === 'ArrowLeft' ? -1 :
751
+ e.key === 'ArrowRight' ? 1 :
752
+ e.key === 'ArrowUp' ? -3 :
753
+ e.key === 'ArrowDown' ? 3 :
754
+ 0;
755
+ if (!delta)
756
+ return;
757
+ const cells = Array.from(grid.querySelectorAll('.month-cell'));
758
+ const current = cells.findIndex(c => c === this.shadowRoot.activeElement);
759
+ const next = cells[(current === -1 ? displayMonth - 1 : current) + delta];
760
+ if (!next || next.disabled)
761
+ return;
762
+ e.preventDefault();
763
+ cells.forEach(c => (c.tabIndex = -1));
764
+ next.tabIndex = 0;
765
+ next.focus();
766
+ });
767
+ return grid;
768
+ }
769
+ pickMonth(month) {
770
+ this.setView('days');
771
+ this.goTo(month, this._state.displayYear);
772
+ this.focusDayGrid();
773
+ }
774
+ handleKeydown(e) {
775
+ if (e.target === this._container && e.key.startsWith('Arrow')) {
776
+ e.preventDefault();
777
+ this.gridCell()?.focus();
778
+ return;
779
+ }
780
+ if (e.key === 'Escape' && this._view === 'months') {
781
+ e.preventDefault();
782
+ e.stopPropagation();
783
+ this.setView('days');
784
+ this.focusNavTitle();
785
+ return;
786
+ }
787
+ if (e.key !== 'PageUp' && e.key !== 'PageDown')
788
+ return;
789
+ e.preventDefault();
790
+ const sign = e.key === 'PageUp' ? -1 : 1;
791
+ const step = e.shiftKey || this._view === 'months' ? 12 : 1;
792
+ const current = TyCalendar.monthIndex(this._state.displayYear, this._state.displayMonth);
793
+ const { minIdx, maxIdx } = this.boundIndices();
794
+ let target = current + sign * step;
795
+ if (minIdx !== null && target < minIdx)
796
+ target = minIdx;
797
+ if (maxIdx !== null && target > maxIdx)
798
+ target = maxIdx;
799
+ if (target === current)
800
+ return;
801
+ this.goTo((target % 12) + 1, Math.floor(target / 12));
802
+ if (this._view === 'months') {
803
+ const cell = this._monthGrid?.querySelector('.month-cell[tabindex="0"]');
804
+ cell?.focus();
805
+ }
806
+ else {
807
+ this.focusDayGrid();
808
+ }
809
+ }
810
+ focusNavTitle() {
811
+ requestAnimationFrame(() => {
812
+ this._navigation?.shadowRoot?.querySelector('.nav-title')?.focus();
813
+ });
814
+ }
815
+ gridCell() {
816
+ if (this._view === 'months') {
817
+ return this._monthGrid?.querySelector('.month-cell[tabindex="0"]:not(:disabled)')
818
+ ?? this._monthGrid?.querySelector('.month-cell:not(:disabled)')
819
+ ?? this._navigation?.shadowRoot?.querySelector('.nav-title')
820
+ ?? null;
821
+ }
822
+ const root = this._monthDisplay?.shadowRoot;
823
+ return root?.querySelector('.calendar-day-cell[tabindex="0"]')
824
+ ?? root?.querySelector('.calendar-day-cell.selected')
825
+ ?? root?.querySelector('.calendar-day-cell')
826
+ ?? null;
827
+ }
828
+ focus(options) {
829
+ const cell = this.gridCell();
830
+ if (cell)
831
+ cell.focus(options);
832
+ else
833
+ super.focus(options);
834
+ }
835
+ focusDayGrid() {
836
+ requestAnimationFrame(() => {
837
+ const cell = this._monthDisplay?.shadowRoot?.querySelector('.calendar-day-cell[tabindex="0"]');
838
+ cell?.focus();
839
+ });
627
840
  }
628
841
  }
629
842
  TyCalendar.formAssociated = true;
@@ -32,6 +32,8 @@ export interface TyCheckboxElement extends HTMLElement {
32
32
  */
33
33
  export declare class TyCheckbox extends TyComponent<CheckboxState> implements TyCheckboxElement {
34
34
  static formAssociated: boolean;
35
+ protected static delegatesFocus: boolean;
36
+ protected static labelClicks: boolean;
35
37
  protected static properties: {
36
38
  checked: {
37
39
  type: "boolean";
@@ -98,6 +100,9 @@ export declare class TyCheckbox extends TyComponent<CheckboxState> implements Ty
98
100
  protected getFormValue(): FormDataEntryValue | null;
99
101
  private buildClassList;
100
102
  private handleCheckboxClick;
103
+ /** The ring is for keyboard use: keys show it, a click hides it — also a
104
+ * <label>'s, which in Chromium focuses the box as :focus-visible. */
105
+ private showFocusRing;
101
106
  private handleCheckboxKeydown;
102
107
  private removeEventListeners;
103
108
  /**
@@ -110,7 +115,9 @@ export declare class TyCheckbox extends TyComponent<CheckboxState> implements Ty
110
115
  * Label, required asterisk, and error UI are the consumer's responsibility.
111
116
  */
112
117
  protected render(): void;
113
- /** required + unchecked => valueMissing; otherwise valid. */
118
+ /** Focus the checkbox itself (the host is not focusable; its role element is). */
119
+ focus(options?: FocusOptions): void;
120
+ /** required + unchecked => valueMissing (the browser's own message); otherwise valid. */
114
121
  private updateValidity;
115
122
  get checked(): boolean;
116
123
  set checked(value: boolean);
@@ -3,6 +3,7 @@ import { TyComponent } from "../base/ty-component.js";
3
3
  import { ensureStyles } from "../utils/styles.js";
4
4
  import { syncCustomFlavorSheet } from "../utils/flavor-sheet.js";
5
5
  import { checkboxStyles, checkboxCustomFlavorCss } from "../styles/checkbox.js";
6
+ import { nativeMessage } from "../utils/validity.js";
6
7
  const CHECK_ICON = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></svg>`;
7
8
  const DASH_ICON = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/></svg>`;
8
9
  export class TyCheckbox extends TyComponent {
@@ -20,6 +21,9 @@ ensureStyles(shadow, { css: checkboxStyles, id: "ty-checkbox" });
20
21
  onConnect() {
21
22
  this.setupEventListeners();
22
23
  this._syncCustomFlavor();
24
+ const el = this.shadowRoot.querySelector(".checkbox-container");
25
+ if (el && this.shadowRoot.activeElement !== el)
26
+ el.classList.remove("focused");
23
27
  }
24
28
  _syncCustomFlavor() {
25
29
  this._customFlavorSheet = syncCustomFlavorSheet(this.shadowRoot, this._customFlavorSheet, this.flavor, ({ base }) => checkboxCustomFlavorCss(base));
@@ -46,8 +50,7 @@ return classes.join(" ");
46
50
  handleCheckboxClick(e) {
47
51
  if (this.disabled)
48
52
  return;
49
- if (this.isDuplicateActivationClick())
50
- return;
53
+ this.showFocusRing(e instanceof KeyboardEvent);
51
54
  const newValue = this.indeterminate ? true : !this.checked;
52
55
  if (this.indeterminate)
53
56
  this.indeterminate = false;
@@ -71,6 +74,9 @@ composed: true,
71
74
  }));
72
75
  }, 0);
73
76
  }
77
+ showFocusRing(on) {
78
+ this.shadowRoot?.querySelector(".checkbox-container")?.classList.toggle("focused", on);
79
+ }
74
80
  handleCheckboxKeydown(e) {
75
81
  if (this.disabled)
76
82
  return;
@@ -123,7 +129,7 @@ this._keydownHandler = (e) => {
123
129
  this.handleCheckboxKeydown(e);
124
130
  };
125
131
  this._focusHandler = () => {
126
- checkboxEl.classList.add("focused");
132
+ checkboxEl.classList.toggle("focused", checkboxEl.matches(":focus-visible"));
127
133
  };
128
134
  this._blurHandler = () => {
129
135
  checkboxEl.classList.remove("focused");
@@ -142,7 +148,8 @@ const ariaChecked = this.indeterminate ? "mixed" : String(this.checked);
142
148
  if (!checkboxEl) {
143
149
  checkboxEl = document.createElement("div");
144
150
  checkboxEl.className = "checkbox-container " + classes;
145
- checkboxEl.tabIndex = this.disabled ? -1 : 0;
151
+ if (!this.disabled)
152
+ checkboxEl.tabIndex = 0;
146
153
  checkboxEl.setAttribute("role", "checkbox");
147
154
  checkboxEl.setAttribute("aria-checked", ariaChecked);
148
155
  checkboxEl.setAttribute("aria-disabled", String(this.disabled));
@@ -153,8 +160,13 @@ checkboxEl.appendChild(checkboxIcon);
153
160
  shadow.appendChild(checkboxEl);
154
161
  }
155
162
  else {
163
+ const focused = checkboxEl.classList.contains("focused") && shadow.activeElement === checkboxEl;
156
164
  checkboxEl.className = "checkbox-container " + classes;
157
- checkboxEl.tabIndex = this.disabled ? -1 : 0;
165
+ checkboxEl.classList.toggle("focused", focused && !this.disabled);
166
+ if (this.disabled)
167
+ checkboxEl.removeAttribute("tabindex");
168
+ else
169
+ checkboxEl.tabIndex = 0;
158
170
  checkboxEl.setAttribute("aria-checked", ariaChecked);
159
171
  checkboxEl.setAttribute("aria-disabled", String(this.disabled));
160
172
  checkboxEl.setAttribute("aria-required", String(this.required));
@@ -165,17 +177,24 @@ this.setupEventListeners();
165
177
  this.applyLabelName(checkboxEl);
166
178
  this.updateValidity();
167
179
  }
180
+ focus(options) {
181
+ const el = this.shadowRoot?.querySelector(".checkbox-container");
182
+ if (el)
183
+ el.focus(options);
184
+ else
185
+ super.focus(options);
186
+ }
168
187
  updateValidity() {
169
188
  const anchor = this.shadowRoot?.querySelector(".checkbox-container");
170
189
  if (this.disabled) {
171
- this._internals.setValidity({});
190
+ this.applyValidity({});
172
191
  return;
173
192
  }
174
193
  if (this.required && !this.checked) {
175
- this._internals.setValidity({ valueMissing: true }, "Please check this box", anchor ?? undefined);
194
+ this.applyValidity({ valueMissing: true }, nativeMessage("input", { type: "checkbox", required: true }), anchor ?? undefined);
176
195
  }
177
196
  else {
178
- this._internals.setValidity({});
197
+ this.applyValidity({});
179
198
  }
180
199
  }
181
200
  get checked() {
@@ -231,6 +250,8 @@ return this._internals.form;
231
250
  }
232
251
  }
233
252
  TyCheckbox.formAssociated = true;
253
+ TyCheckbox.delegatesFocus = true;
254
+ TyCheckbox.labelClicks = true;
234
255
  TyCheckbox.properties = {
235
256
  checked: {
236
257
  type: "boolean",
@@ -109,6 +109,8 @@ export declare class TyCopy extends TyComponent<CopyState> implements TyCopyElem
109
109
  * Uses state flag so render() can show the correct icon
110
110
  */
111
111
  private showCopySuccess;
112
+ /** Feedback over: the copy icon again. */
113
+ private resetCopyButton;
112
114
  /**
113
115
  * Show copy error animation
114
116
  * Swaps copy icon → red X icon → copy icon