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
@@ -9,16 +9,28 @@
9
9
  * - Property change events via 'prop:change' (separate from user 'change' events)
10
10
  */
11
11
  import { PropertyManager, PropertyConfig, PropertyChange } from '../utils/property-manager.js';
12
+ import { upgradeProperties } from '../utils/upgrade-properties.js';
13
+ export { upgradeProperties };
12
14
  /**
13
15
  * Base class for all Ty components with unified property lifecycle
14
16
  */
15
17
  export declare abstract class TyComponent<T = any> extends HTMLElement {
16
18
  static formAssociated: boolean;
17
19
  protected static properties: Record<string, PropertyConfig>;
20
+ /** attachShadow delegatesFocus: the host itself counts as focusable when
21
+ * its focusable role element lives in the shadow (checkbox/switch) —
22
+ * Firefox checks the host, not the validation anchor, on a blocked submit. */
23
+ protected static delegatesFocus: boolean;
24
+ /** A wrapping <label> delegates clicks to the host (see watchLabelForwards). */
25
+ protected static labelClicks: boolean;
18
26
  protected props: PropertyManager<T>;
19
27
  protected _internals: ElementInternals;
28
+ private _validity;
20
29
  protected _connected: boolean;
21
30
  protected _rendered: boolean;
31
+ /** The user edited the field since it was created or its form reset:
32
+ * blur then shows its validation message (like native :user-invalid). */
33
+ protected _userEdited: boolean;
22
34
  constructor();
23
35
  /**
24
36
  * Define which attributes to observe
@@ -54,11 +66,14 @@ export declare abstract class TyComponent<T = any> extends HTMLElement {
54
66
  * Disconnected callback
55
67
  */
56
68
  disconnectedCallback(): void;
69
+ /** The form owner changed (moved, `form` attribute): see ValidityController.track(). */
70
+ formAssociatedCallback(): void;
57
71
  /**
58
- * Capture properties that were set before connectedCallback
59
- * (React, Vue, Reagent set properties before element is connected)
72
+ * upgradeProperties(), plus declared properties without an accessor of
73
+ * their own name (e.g. ty-input's `minlength`): those go straight to
74
+ * setProperty().
60
75
  */
61
- private _capturePreSetProperties;
76
+ private _upgradeProperties;
62
77
  /**
63
78
  * THE CORE LIFECYCLE METHOD
64
79
  * All property changes flow through here
@@ -113,6 +128,22 @@ export declare abstract class TyComponent<T = any> extends HTMLElement {
113
128
  * Get the form element this component is associated with
114
129
  */
115
130
  get form(): HTMLFormElement | null;
131
+ /** Form-associated? Non-FACE internals throw on validity access. */
132
+ private get _validates();
133
+ /** Subclasses report their own validity here, not via _internals directly,
134
+ * so a user's setCustomValidity() survives re-validation. */
135
+ protected applyValidity(flags: ValidityStateFlags, message?: string, anchor?: HTMLElement): void;
136
+ /** Show the field's current validation message in its error display, if
137
+ * it's invalid. Called on blur after the user edited the field. */
138
+ protected revealValidity(): void;
139
+ get validity(): ValidityState | undefined;
140
+ get validationMessage(): string;
141
+ get willValidate(): boolean;
142
+ checkValidity(): boolean;
143
+ /** Like native, but shows the message in the component's `error` display
144
+ * when it has one (and clears it once valid) instead of the browser bubble. */
145
+ reportValidity(): boolean;
146
+ setCustomValidity(message: string): void;
116
147
  /**
117
148
  * a11y helper for components whose ARIA role lives on an inner shadow element
118
149
  * (checkbox/switch/radio). A wrapping <label> names the form-associated HOST,
@@ -120,18 +151,6 @@ export declare abstract class TyComponent<T = any> extends HTMLElement {
120
151
  * the role element as aria-label, giving it an accessible name.
121
152
  */
122
153
  protected applyLabelName(roleEl: HTMLElement): void;
123
- private _handlingActivationClick;
124
- /**
125
- * Guard for click-driven toggle handlers (checkbox/switch) on components
126
- * whose whole host is the click target AND that expect a wrapping
127
- * <label> to delegate to them. Some browsers still forward a label's own
128
- * synthetic click to the associated control even when the original click
129
- * already targeted that control directly, firing 'click' twice for one
130
- * user gesture. Both arrive synchronously in the same task, so a flag
131
- * cleared on the next microtask catches only the spurious duplicate —
132
- * two genuinely separate clicks are always in different tasks.
133
- */
134
- protected isDuplicateActivationClick(): boolean;
135
154
  private _resetDefaults;
136
155
  /**
137
156
  * Form reset callback - called when form.reset() is invoked.
@@ -140,6 +159,9 @@ export declare abstract class TyComponent<T = any> extends HTMLElement {
140
159
  * semantics). Non-form state (e.g. a checkbox's submission `value`, a
141
160
  * ty-radio's option `value`) is deliberately left alone — the old
142
161
  * blanket 'value' reset corrupted radio groups by wiping child values.
162
+ * Like a native control the field also looks untouched again: the
163
+ * validation message it showed is gone (an `error` the author set stays)
164
+ * and a blur shows nothing until the user edits it.
143
165
  */
144
166
  formResetCallback(): void;
145
167
  /**
@@ -1,16 +1,47 @@
1
1
  import { PropertyManager } from '../utils/property-manager.js';
2
+ import { ValidityController } from '../utils/validity.js';
3
+ import { upgradeProperties } from '../utils/upgrade-properties.js';
4
+ export { upgradeProperties };
5
+ const labelClickHosts = new WeakSet();
6
+ const directClicks = new WeakMap();
7
+ let labelForwardsWatched = false;
8
+ function watchLabelForwards() {
9
+ if (labelForwardsWatched)
10
+ return;
11
+ labelForwardsWatched = true;
12
+ window.addEventListener('click', (e) => {
13
+ const path = e.composedPath();
14
+ const host = path.find((n) => labelClickHosts.has(n));
15
+ if (!host)
16
+ return;
17
+ const at = `${e.clientX},${e.clientY}`;
18
+ if (path[0] !== host) {
19
+ directClicks.set(host, at);
20
+ setTimeout(() => { if (directClicks.get(host) === at)
21
+ directClicks.delete(host); });
22
+ }
23
+ else if (directClicks.get(host) === at) {
24
+ e.stopImmediatePropagation();
25
+ }
26
+ }, true);
27
+ }
2
28
  export class TyComponent extends HTMLElement {
3
29
  constructor() {
4
30
  super();
5
31
  this._connected = false;
6
32
  this._rendered = false;
7
- this._handlingActivationClick = false;
33
+ this._userEdited = false;
8
34
  this._resetDefaults = null;
9
35
  const ctor = this.constructor;
10
36
  this.props = new PropertyManager(ctor.properties);
11
37
  this._internals = this.attachInternals();
38
+ this._validity = new ValidityController(this, this._internals);
12
39
  if (!this.shadowRoot) {
13
- this.attachShadow({ mode: 'open' });
40
+ this.attachShadow({ mode: 'open', delegatesFocus: ctor.delegatesFocus });
41
+ }
42
+ if (ctor.labelClicks) {
43
+ labelClickHosts.add(this);
44
+ watchLabelForwards();
14
45
  }
15
46
  }
16
47
  static get observedAttributes() {
@@ -79,7 +110,6 @@ this._handlePropertyChanges([change]);
79
110
  }
80
111
  }
81
112
  connectedCallback() {
82
- this._connected = true;
83
113
  if (!this._resetDefaults) {
84
114
  this._resetDefaults = new Map();
85
115
  const ctor = this.constructor;
@@ -87,13 +117,13 @@ for (const [name, config] of Object.entries(ctor.properties)) {
87
117
  if (!config.formValue)
88
118
  continue;
89
119
  const attr = this.getAttribute(this._toKebabCase(name));
90
- this._resetDefaults.set(name, attr ?? config.default ?? null);
120
+ this._resetDefaults.set(name, attr ?? (typeof config.default === 'function' ? config.default() : config.default) ?? null);
91
121
  }
92
122
  }
93
- const preSetProps = this._capturePreSetProperties();
94
- if (preSetProps.length > 0) {
95
- this._handlePropertyChanges(preSetProps);
96
- }
123
+ this._upgradeProperties();
124
+ this._connected = true;
125
+ if (this._validates)
126
+ this._validity.track();
97
127
  this.onConnect();
98
128
  if (!this._rendered) {
99
129
  this.render();
@@ -102,21 +132,28 @@ this._rendered = true;
102
132
  }
103
133
  disconnectedCallback() {
104
134
  this._connected = false;
135
+ if (this._validates)
136
+ this._validity.track();
105
137
  this.onDisconnect();
106
138
  }
107
- _capturePreSetProperties() {
108
- const changes = [];
109
- const ctor = this.constructor;
110
- for (const propName of Object.keys(ctor.properties)) {
111
- const descriptor = Object.getOwnPropertyDescriptor(this, propName);
112
- if (descriptor && descriptor.value !== undefined) {
113
- const change = this.props.updateProperty(propName, descriptor.value, 'property');
114
- if (change) {
115
- changes.push(change);
116
- }
139
+ formAssociatedCallback() {
140
+ if (this._validates)
141
+ this._validity.track();
117
142
  }
143
+ _upgradeProperties() {
144
+ for (const name of Object.keys(this.constructor.properties)) {
145
+ if (!Object.prototype.hasOwnProperty.call(this, name))
146
+ continue;
147
+ const value = this[name];
148
+ delete this[name];
149
+ if (value === undefined)
150
+ continue;
151
+ if (name in this)
152
+ this[name] = value;
153
+ else
154
+ this.setProperty(name, value);
118
155
  }
119
- return changes;
156
+ upgradeProperties(this);
120
157
  }
121
158
  _handlePropertyChanges(changes) {
122
159
  if (changes.length === 0)
@@ -163,6 +200,35 @@ onDisconnect() {
163
200
  get form() {
164
201
  return this._internals.form;
165
202
  }
203
+ get _validates() {
204
+ return this.constructor.formAssociated === true;
205
+ }
206
+ applyValidity(flags, message, anchor) {
207
+ this._validity.set(flags, message, anchor);
208
+ }
209
+ revealValidity() {
210
+ if (this._validates)
211
+ this._validity.reveal();
212
+ }
213
+ get validity() {
214
+ return this._validates ? this._internals.validity : undefined;
215
+ }
216
+ get validationMessage() {
217
+ return this._validates ? this._internals.validationMessage : '';
218
+ }
219
+ get willValidate() {
220
+ return this._validates ? this._internals.willValidate : false;
221
+ }
222
+ checkValidity() {
223
+ return this._validates ? this._internals.checkValidity() : true;
224
+ }
225
+ reportValidity() {
226
+ return this._validates ? this._validity.report() : true;
227
+ }
228
+ setCustomValidity(message) {
229
+ if (this._validates)
230
+ this._validity.setCustom(message);
231
+ }
166
232
  applyLabelName(roleEl) {
167
233
  let text = Array.from(this._internals.labels ?? [])
168
234
  .map((l) => l.textContent?.trim())
@@ -181,14 +247,10 @@ roleEl.setAttribute('aria-label', text);
181
247
  else
182
248
  roleEl.removeAttribute('aria-label');
183
249
  }
184
- isDuplicateActivationClick() {
185
- if (this._handlingActivationClick)
186
- return true;
187
- this._handlingActivationClick = true;
188
- queueMicrotask(() => { this._handlingActivationClick = false; });
189
- return false;
190
- }
191
250
  formResetCallback() {
251
+ this._userEdited = false;
252
+ if (this._validates)
253
+ this._validity.reset();
192
254
  for (const [name, def] of this._resetDefaults ?? []) {
193
255
  this.setProperty(name, def);
194
256
  }
@@ -199,3 +261,5 @@ onFormReset() {
199
261
  }
200
262
  TyComponent.formAssociated = true;
201
263
  TyComponent.properties = {};
264
+ TyComponent.delegatesFocus = false;
265
+ TyComponent.labelClicks = false;
@@ -89,11 +89,25 @@ export declare class TyButton extends TyComponent<ButtonState> implements TyButt
89
89
  };
90
90
  };
91
91
  private _structureInitialized;
92
+ private _formDisabled;
92
93
  private _customFlavorSheet;
94
+ private _ownClick;
93
95
  constructor();
96
+ /**
97
+ * A real press inside the host's content box that missed the button: the
98
+ * empty host below a wide button that a min-height made taller, or around
99
+ * one a page's display: flex stretched. Nothing there looks like the
100
+ * button, so it's no press. Host padding and border are outside the
101
+ * content box.
102
+ */
103
+ private _pressOffButton;
94
104
  protected onConnect(): void;
95
105
  protected onDisconnect(): void;
96
106
  protected onPropertiesChanged(changes: PropertyChange[]): void;
107
+ formDisabledCallback(disabled: boolean): void;
108
+ private get _isDisabled();
109
+ /** Focus the inner <button>. The host itself isn't focusable. */
110
+ focus(options?: FocusOptions): void;
97
111
  /**
98
112
  * Custom flavors: adopt generated per-flavor rules pointed at the user's
99
113
  * design tokens (--ty-solid-X / --ty-color-X / --ty-bg-X), falling back to
@@ -4,12 +4,37 @@ import { ensureStyles, buildClassList } from "../utils/styles.js";
4
4
  import { buttonStyles, buttonCustomFlavorCss } from "../styles/button.js";
5
5
  import { syncCustomFlavorSheet } from "../utils/flavor-sheet.js";
6
6
  import { getLoaderSvg } from "../utils/loader-registry.js";
7
+ import { asSubmitAttempt } from "../utils/validity.js";
7
8
  export class TyButton extends TyComponent {
8
9
  constructor() {
9
10
  super();
10
11
  this._structureInitialized = false;
12
+ this._formDisabled = false;
11
13
  this._customFlavorSheet = null;
14
+ this._ownClick = null;
12
15
  ensureStyles(this.shadowRoot, { css: buttonStyles, id: "ty-button" });
16
+ this.addEventListener("click", (e) => {
17
+ const button = this.shadowRoot.querySelector("button");
18
+ if (!button || e === this._ownClick)
19
+ return;
20
+ if (e.composedPath().includes(button))
21
+ return;
22
+ e.stopImmediatePropagation();
23
+ if (!this._pressOffButton(e))
24
+ button.click();
25
+ }, true);
26
+ }
27
+ _pressOffButton(e) {
28
+ if (!e.isTrusted || !(e instanceof MouseEvent))
29
+ return false;
30
+ const r = this.getBoundingClientRect();
31
+ const s = getComputedStyle(this);
32
+ const px = (p) => parseFloat(s.getPropertyValue(p)) || 0;
33
+ const left = r.left + px("border-left-width") + px("padding-left");
34
+ const right = r.right - px("border-right-width") - px("padding-right");
35
+ const top = r.top + px("border-top-width") + px("padding-top");
36
+ const bottom = r.bottom - px("border-bottom-width") - px("padding-bottom");
37
+ return e.clientX >= left && e.clientX < right && e.clientY >= top && e.clientY < bottom;
13
38
  }
14
39
  onConnect() {
15
40
  this._syncCustomFlavor();
@@ -20,6 +45,21 @@ if (changes.some((c) => c.name === "flavor")) {
20
45
  this._syncCustomFlavor();
21
46
  }
22
47
  }
48
+ formDisabledCallback(disabled) {
49
+ this._formDisabled = disabled;
50
+ if (this._structureInitialized)
51
+ this.render();
52
+ }
53
+ get _isDisabled() {
54
+ return this.disabled || this._formDisabled;
55
+ }
56
+ focus(options) {
57
+ const button = this.shadowRoot?.querySelector("button");
58
+ if (button)
59
+ button.focus(options);
60
+ else
61
+ super.focus(options);
62
+ }
23
63
  _syncCustomFlavor() {
24
64
  const { base } = this.parseFlavor();
25
65
  this._customFlavorSheet = syncCustomFlavorSheet(this.shadowRoot, this._customFlavorSheet, base, () => buttonCustomFlavorCss(base));
@@ -117,7 +157,13 @@ case "submit":
117
157
  if (this.name && this.value) {
118
158
  this._internals.setFormValue(this.value);
119
159
  }
120
- form.requestSubmit();
160
+ try {
161
+ asSubmitAttempt(() => form.requestSubmit());
162
+ }
163
+ finally {
164
+ if (this.name && this.value)
165
+ this._internals.setFormValue(null);
166
+ }
121
167
  break;
122
168
  case "reset":
123
169
  form.reset();
@@ -130,7 +176,7 @@ initializeButtonStructure() {
130
176
  const shadow = this.shadowRoot;
131
177
  const classes = this.buildClasses();
132
178
  const button = document.createElement("button");
133
- button.disabled = this.disabled;
179
+ button.disabled = this._isDisabled;
134
180
  button.className = classes;
135
181
  button.setAttribute("part", "button");
136
182
  const loader = document.createElement("span");
@@ -149,15 +195,21 @@ button.appendChild(startSlot);
149
195
  button.appendChild(defaultSlot);
150
196
  button.appendChild(endSlot);
151
197
  button.addEventListener("click", (e) => {
152
- if (this.disabled || this.loading)
198
+ const path = e.composedPath();
199
+ if (path.slice(0, path.indexOf(button)).some((n) => n instanceof Element && n.localName === "ty-popup"))
153
200
  return;
154
201
  e.stopPropagation();
155
- this.handleFormAction();
156
- this.dispatchEvent(new CustomEvent("click", {
202
+ if (this._isDisabled || this.loading)
203
+ return;
204
+ const click = new CustomEvent("click", {
157
205
  bubbles: true,
158
206
  composed: true,
207
+ cancelable: true,
159
208
  detail: { originalEvent: e },
160
- }));
209
+ });
210
+ this._ownClick = click;
211
+ if (this.dispatchEvent(click))
212
+ this.handleFormAction();
161
213
  });
162
214
  this.applyLoadingState(button);
163
215
  shadow.appendChild(button);
@@ -168,12 +220,14 @@ const isLoading = this.loading;
168
220
  button.classList.toggle("loading", isLoading);
169
221
  if (isLoading) {
170
222
  button.setAttribute("aria-busy", "true");
223
+ button.setAttribute("aria-disabled", "true");
171
224
  const loader = button.querySelector(".loader-icon");
172
225
  if (loader)
173
226
  loader.innerHTML = getLoaderSvg();
174
227
  }
175
228
  else {
176
229
  button.removeAttribute("aria-busy");
230
+ button.removeAttribute("aria-disabled");
177
231
  }
178
232
  }
179
233
  render() {
@@ -185,7 +239,7 @@ return;
185
239
  const classes = this.buildClasses();
186
240
  const button = shadow.querySelector("button");
187
241
  if (button) {
188
- button.disabled = this.disabled;
242
+ button.disabled = this._isDisabled;
189
243
  button.className = classes;
190
244
  this.applyLoadingState(button);
191
245
  }
@@ -32,7 +32,7 @@ export interface DayClickDetail {
32
32
  export declare class TyCalendarMonth extends HTMLElement {
33
33
  private _displayYear;
34
34
  private _displayMonth;
35
- private _locale;
35
+ private _locale?;
36
36
  private _size;
37
37
  private _width?;
38
38
  private _minWidth?;
@@ -97,6 +97,12 @@ export declare class TyCalendarMonth extends HTMLElement {
97
97
  * Useful when external data changes (e.g., async event loading)
98
98
  */
99
99
  refresh(): void;
100
+ /**
101
+ * A day was picked (click, Enter, Space): it becomes the grid's tab stop and
102
+ * takes the focus, which render() keeps on it when the pick re-renders the
103
+ * grid. Then the events go out.
104
+ */
105
+ private pickDay;
100
106
  private dispatchDayClick;
101
107
  /**
102
108
  * WAI-ARIA grid pattern: gridcell + roving tabindex (exactly one cell in
@@ -1,7 +1,8 @@
1
1
  import { ensureStyles } from '../utils/styles.js';
2
+ import { liftProperties, upgradeProperties } from '../utils/upgrade-properties.js';
2
3
  import { syncCustomFlavorSheet } from '../utils/flavor-sheet.js';
3
4
  import { calendarMonthStyles, calendarMonthCustomFlavorCss } from '../styles/calendar-month.js';
4
- import { getCalendarMonthDays, getLocalizedWeekdays, parseISODate } from '../utils/calendar-utils.js';
5
+ import { getCalendarMonthDays, getLocalizedWeekdays, localMidnight, parseDateDay, utcMidnight } from '../utils/calendar-utils.js';
5
6
  import { getEffectiveLocale, observeLocaleChanges } from '../utils/locale.js';
6
7
  function defaultDayContent(dayContext) {
7
8
  return dayContext.dayInMonth.toString();
@@ -24,10 +25,10 @@ return value instanceof HTMLElement;
24
25
  function isoToUTCTimestamp(iso) {
25
26
  if (!iso)
26
27
  return null;
27
- const parsed = parseISODate(iso);
28
+ const parsed = parseDateDay(iso);
28
29
  if (!parsed)
29
30
  return null;
30
- return Date.UTC(parsed.year, parsed.month - 1, parsed.day);
31
+ return utcMidnight(parsed.year, parsed.month, parsed.day);
31
32
  }
32
33
  function normalizeSizeValue(value) {
33
34
  if (!value || value === '')
@@ -44,7 +45,6 @@ return ['display-year', 'display-month', 'locale', 'size', 'min', 'max', 'flavor
44
45
  }
45
46
  constructor() {
46
47
  super();
47
- this._locale = 'en-US';
48
48
  this._size = 'md';
49
49
  this._customFlavorSheet = null;
50
50
  this._focusedIndex = null;
@@ -53,6 +53,7 @@ const today = new Date();
53
53
  this._displayYear = today.getFullYear();
54
54
  this._displayMonth = today.getMonth() + 1;
55
55
  this.attachShadow({ mode: 'open' });
56
+ liftProperties(this);
56
57
  }
57
58
  connectedCallback() {
58
59
  if (!this._displayMonth) {
@@ -62,23 +63,15 @@ if (!this._displayYear) {
62
63
  this._displayYear = new Date().getFullYear();
63
64
  }
64
65
  if (!this._locale) {
65
- this._locale = this.getAttribute('locale') || 'en-US';
66
+ this._locale = this.getAttribute('locale') || undefined;
66
67
  }
67
68
  const sizeAttr = this.getAttribute('size');
68
69
  if (sizeAttr && (sizeAttr === 'sm' || sizeAttr === 'md' || sizeAttr === 'lg')) {
69
70
  this._size = sizeAttr;
70
71
  }
71
- const plainDayContentFn = this.dayContentFn;
72
- if (plainDayContentFn && !this._dayContentFn) {
73
- this._dayContentFn = plainDayContentFn;
74
- delete this.dayContentFn;
75
- }
76
- const plainCustomCSS = this.customCSS;
77
- if (plainCustomCSS && !this._customCSS) {
78
- this._customCSS = plainCustomCSS;
79
- delete this.customCSS;
80
- }
72
+ upgradeProperties(this);
81
73
  this._localeObserver = observeLocaleChanges(this, () => {
74
+ if (!this._locale)
82
75
  this.render();
83
76
  });
84
77
  this._syncCustomFlavor();
@@ -102,7 +95,7 @@ this.displayMonth = n;
102
95
  }
103
96
  }
104
97
  else if (name === 'locale') {
105
- this._locale = newValue || 'en-US';
98
+ this._locale = newValue || undefined;
106
99
  this.render();
107
100
  }
108
101
  else if (name === 'size') {
@@ -143,11 +136,11 @@ this.render();
143
136
  }
144
137
  }
145
138
  get locale() {
146
- return getEffectiveLocale(this, this.getAttribute('locale'));
139
+ return getEffectiveLocale(this, this._locale);
147
140
  }
148
141
  set locale(value) {
149
142
  if (this._locale !== value) {
150
- this._locale = value;
143
+ this._locale = value || undefined;
151
144
  this.render();
152
145
  }
153
146
  }
@@ -239,6 +232,7 @@ set value(timestamp) {
239
232
  const newValue = timestamp ?? undefined;
240
233
  if (this._value !== newValue) {
241
234
  this._value = newValue;
235
+ this._focusedIndex = null;
242
236
  this.render();
243
237
  }
244
238
  }
@@ -276,6 +270,15 @@ this.shadowRoot.adoptedStyleSheets = [...existing, this._customCSS];
276
270
  refresh() {
277
271
  this.render();
278
272
  }
273
+ pickDay(cell, dayContext, domEvent) {
274
+ const index = this._cellElements.indexOf(cell);
275
+ if (index !== -1) {
276
+ this._cellElements.forEach((c) => c.setAttribute('tabindex', c === cell ? '0' : '-1'));
277
+ this._focusedIndex = index;
278
+ cell.focus({ preventScroll: true });
279
+ }
280
+ this.dispatchDayClick(dayContext, domEvent);
281
+ }
279
282
  dispatchDayClick(dayContext, _domEvent) {
280
283
  const detail = {
281
284
  dayContext,
@@ -297,7 +300,7 @@ cancelable: true,
297
300
  this.dispatchEvent(event);
298
301
  }
299
302
  }
300
- renderDayCell(dayContext, disabled, isRoving) {
303
+ renderDayCell(dayContext, disabled, isRoving, label) {
301
304
  const dayElement = document.createElement('div');
302
305
  const content = this._dayContentFn
303
306
  ? this._dayContentFn(dayContext)
@@ -309,6 +312,8 @@ throw new Error(`Custom dayContentFn must return a DOM element or string, got: $
309
312
  const classes = getDefaultDayClasses(dayContext);
310
313
  if (disabled)
311
314
  classes.push('disabled');
315
+ if (!this._dayContentFn)
316
+ classes.push('default-content');
312
317
  dayElement.className = classes.join(' ');
313
318
  if (typeof content === 'string') {
314
319
  dayElement.textContent = content;
@@ -317,8 +322,7 @@ else if (isDOMElement(content)) {
317
322
  dayElement.appendChild(content);
318
323
  }
319
324
  dayElement.setAttribute('role', 'gridcell');
320
- dayElement.setAttribute('aria-label', new Intl.DateTimeFormat(this._locale, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })
321
- .format(new Date(dayContext.localValue)));
325
+ dayElement.setAttribute('aria-label', label.format(new Date(dayContext.localValue)));
322
326
  dayElement.setAttribute('aria-selected', String(!!dayContext.isSelected));
323
327
  if (dayContext.today)
324
328
  dayElement.setAttribute('aria-current', 'date');
@@ -328,15 +332,14 @@ dayElement.setAttribute('tabindex', '-1');
328
332
  }
329
333
  else {
330
334
  dayElement.setAttribute('tabindex', isRoving ? '0' : '-1');
331
- dayElement.addEventListener('pointerdown', (event) => {
332
- event.preventDefault();
333
- this.dispatchDayClick(dayContext, event);
335
+ dayElement.addEventListener('click', (event) => {
336
+ this.pickDay(dayElement, dayContext, event);
334
337
  });
335
338
  dayElement.addEventListener('keydown', (event) => {
336
339
  const ke = event;
337
340
  if (ke.key === 'Enter' || ke.key === ' ') {
338
341
  ke.preventDefault();
339
- this.dispatchDayClick(dayContext, event);
342
+ this.pickDay(dayElement, dayContext, event);
340
343
  }
341
344
  });
342
345
  }
@@ -350,7 +353,7 @@ ensureStyles(root, { css: calendarMonthStyles, id: 'ty-calendar-month' });
350
353
  this.applyCustomCSS();
351
354
  this.applyWidthProperties();
352
355
  let selection;
353
- if (this._value) {
356
+ if (this._value !== undefined) {
354
357
  const date = new Date(this._value);
355
358
  selection = {
356
359
  year: date.getFullYear(),
@@ -359,13 +362,16 @@ day: date.getDate()
359
362
  };
360
363
  }
361
364
  const days = getCalendarMonthDays(this._displayYear, this._displayMonth, selection);
362
- const weekdays = getLocalizedWeekdays(this._locale, 'short');
365
+ const locale = this.locale;
366
+ const weekdays = getLocalizedWeekdays(locale, 'short');
367
+ const dayLabel = new Intl.DateTimeFormat(locale, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
368
+ const hadFocus = !!root.activeElement;
363
369
  root.innerHTML = '';
364
370
  const calendarContainer = document.createElement('div');
365
371
  calendarContainer.className = `calendar-flex-container calendar-size-${this._size}`;
366
372
  calendarContainer.setAttribute('role', 'grid');
367
- calendarContainer.setAttribute('aria-label', new Intl.DateTimeFormat(this._locale, { month: 'long', year: 'numeric' })
368
- .format(new Date(this._displayYear, this._displayMonth - 1, 1)));
373
+ calendarContainer.setAttribute('aria-label', new Intl.DateTimeFormat(locale, { month: 'long', year: 'numeric' })
374
+ .format(localMidnight(this._displayYear, this._displayMonth, 1)));
369
375
  const headerRow = document.createElement('div');
370
376
  headerRow.className = 'calendar-row calendar-header-row';
371
377
  headerRow.setAttribute('role', 'row');
@@ -420,7 +426,7 @@ dayRow.setAttribute('role', 'row');
420
426
  week.forEach(dayContext => {
421
427
  const disabled = (minTs !== null && dayContext.value < minTs) ||
422
428
  (maxTs !== null && dayContext.value > maxTs);
423
- const dayCell = this.renderDayCell(dayContext, disabled, flatIndex === effectiveFocusedIndex);
429
+ const dayCell = this.renderDayCell(dayContext, disabled, flatIndex === effectiveFocusedIndex, dayLabel);
424
430
  dayCell.className = `${dayCell.className} calendar-cell calendar-day-cell`;
425
431
  dayRow.appendChild(dayCell);
426
432
  this._cellElements[flatIndex] = dayCell;
@@ -451,6 +457,8 @@ nextCell.focus();
451
457
  }
452
458
  });
453
459
  root.appendChild(calendarContainer);
460
+ if (hadFocus)
461
+ this._cellElements[effectiveFocusedIndex]?.focus({ preventScroll: true });
454
462
  }
455
463
  }
456
464
  if (!customElements.get('ty-calendar-month')) {
@@ -19,6 +19,8 @@ interface NavigationState {
19
19
  displayMonth: number;
20
20
  displayYear: number;
21
21
  }
22
+ /** Which grid the calendar shows: days of a month, or the months of a year. */
23
+ export type CalendarView = 'days' | 'months';
22
24
  /**
23
25
  * TyCalendarNavigation Web Component
24
26
  */
@@ -55,6 +57,13 @@ export declare class TyCalendarNavigation extends TyComponent<NavigationState> {
55
57
  visual: boolean;
56
58
  default: string;
57
59
  };
60
+ view: {
61
+ type: "string";
62
+ visual: boolean;
63
+ default: string;
64
+ validate: (v: any) => boolean;
65
+ coerce: (v: any) => "days" | "months";
66
+ };
58
67
  min: {
59
68
  type: "string";
60
69
  visual: boolean;
@@ -86,6 +95,8 @@ export declare class TyCalendarNavigation extends TyComponent<NavigationState> {
86
95
  set size(value: 'sm' | 'md' | 'lg');
87
96
  get width(): string;
88
97
  set width(value: string);
98
+ get view(): CalendarView;
99
+ set view(value: CalendarView);
89
100
  get min(): string;
90
101
  set min(value: string);
91
102
  get max(): string;