@cahyo-dimas/freeday 3.16.1 → 3.18.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +113 -0
  2. package/COMPONENTS.md +60 -20
  3. package/README.id.md +1 -1
  4. package/README.md +1 -1
  5. package/adapters/blazor/FdyAutocomplete.razor +1 -1
  6. package/adapters/blazor/FdyAutocomplete.razor.cs +6 -1
  7. package/adapters/blazor/FdyCascade.razor.cs +1 -1
  8. package/adapters/blazor/FdyCfl.razor +2 -2
  9. package/adapters/blazor/FdyCfl.razor.cs +13 -0
  10. package/adapters/blazor/FdyCombo.razor.cs +1 -1
  11. package/adapters/blazor/FdyDateRange.razor +2 -2
  12. package/adapters/blazor/FdyDateRange.razor.cs +14 -0
  13. package/adapters/blazor/FdyDatepicker.razor.cs +1 -1
  14. package/adapters/blazor/FdyMonthpicker.razor.cs +1 -1
  15. package/adapters/blazor/FdyTimepicker.razor.cs +1 -1
  16. package/adapters/blazor/freeday-blazor.js +13 -4
  17. package/adapters/react/components/FdyAutocomplete.tsx +5 -0
  18. package/adapters/react/components/FdyCfl.tsx +11 -1
  19. package/adapters/react/components/FdyDateRange.tsx +5 -0
  20. package/adapters/react/index.d.ts +26 -0
  21. package/adapters/react/index.js +1 -0
  22. package/adapters/react/useTheme.js +35 -0
  23. package/adapters/vue/components/FdyAutocomplete.vue +5 -0
  24. package/adapters/vue/components/FdyCfl.vue +11 -1
  25. package/adapters/vue/components/FdyDateRange.vue +5 -0
  26. package/adapters/vue/index.d.ts +26 -0
  27. package/adapters/vue/index.js +1 -0
  28. package/adapters/vue/useTheme.js +47 -0
  29. package/dist/freeday-autocomplete.js +6 -1
  30. package/dist/freeday-cascade.js +13 -2
  31. package/dist/freeday-cfl.js +12 -1
  32. package/dist/freeday-datepicker.js +13 -2
  33. package/dist/freeday-form.js +21 -5
  34. package/dist/freeday-select.js +8 -1
  35. package/dist/freeday-theme.js +115 -0
  36. package/dist/freeday-timepicker.js +13 -2
  37. package/dist/freeday.js +202 -14
  38. package/docs/getting-started.md +4 -3
  39. package/package.json +2 -2
@@ -1,4 +1,5 @@
1
1
  export { useFreeday } from './useFreeday.js';
2
+ export { useTheme } from './useTheme.js';
2
3
  export { usePopover } from './usePopover.ts';
3
4
  export { FdyCombo } from './components/FdyCombo.tsx';
4
5
  export { FdyDatepicker } from './components/FdyDatepicker.tsx';
@@ -0,0 +1,35 @@
1
+ /* Freeday. React theme preference (CATATAN-FREEDAY §23). A thin view over window.FreedayTheme,
2
+ * which this file loads itself, so it needs no `import 'freeday'` first and a page still has one
3
+ * preference if the root import is there too (the core registers once).
4
+ *
5
+ * Usage:
6
+ * const { theme, resolved, setTheme, toggleTheme } = useTheme();
7
+ * // <button onClick={toggleTheme} aria-label={resolved === 'dark' ? 'Light theme' : 'Dark theme'}>
8
+ *
9
+ * Pass a ref to also put the preference on that element, for a region whose markup fixes its own
10
+ * data-theme. Every instance reads the same store through useSyncExternalStore, so two toggles on
11
+ * one page never disagree.
12
+ */
13
+ import { useEffect, useSyncExternalStore } from 'react';
14
+ import '../../dist/freeday-theme.js';
15
+
16
+ function subscribe(onChange) {
17
+ document.addEventListener('fdy-theme-change', onChange);
18
+ return function () { document.removeEventListener('fdy-theme-change', onChange); };
19
+ }
20
+ function getTheme() { return window.FreedayTheme.get(); }
21
+ function getResolved() { return window.FreedayTheme.resolved(); }
22
+ function serverTheme() { return 'system'; }
23
+ function serverResolved() { return 'light'; }
24
+ function setTheme(next) { window.FreedayTheme.set(next); }
25
+ function toggleTheme() { window.FreedayTheme.toggle(); }
26
+
27
+ export function useTheme(rootRef) {
28
+ var theme = useSyncExternalStore(subscribe, getTheme, serverTheme);
29
+ var resolved = useSyncExternalStore(subscribe, getResolved, serverResolved);
30
+ useEffect(function () {
31
+ if (rootRef && rootRef.current) return window.FreedayTheme.attach(rootRef.current);
32
+ return undefined;
33
+ }, [rootRef]);
34
+ return { theme: theme, resolved: resolved, setTheme: setTheme, toggleTheme: toggleTheme };
35
+ }
@@ -21,6 +21,10 @@ const props = defineProps<{
21
21
  /** Locked/view mode: the input is not editable and the list won't open, but it stays focusable and shows its value. Unlike `disabled`, it keeps tab order and isn't greyed. */
22
22
  readonly?: boolean;
23
23
  invalid?: boolean;
24
+ /** The native `required`, on the input itself: it is editable, so constraint validation (and so a
25
+ * `data-fdy-validate` form) applies to it as to any input. Not `data-fdy-required`, which is for
26
+ * controls whose trigger is a button (#114). */
27
+ required?: boolean;
24
28
  describedby?: string;
25
29
  }>();
26
30
 
@@ -148,6 +152,7 @@ watch(active, async (i: number): Promise<void> => {
148
152
  :placeholder="placeholder"
149
153
  :disabled="isDisabled"
150
154
  :readonly="isReadonly"
155
+ :required="required === true"
151
156
  :value="modelValue"
152
157
  @input="onInput"
153
158
  @focus="openList"
@@ -64,6 +64,13 @@ const props = defineProps<{
64
64
  /** Locked/view mode: shows the picked value (focusable, copyable), but the search dialog can't be opened. Unlike `disabled`, it keeps tab order and isn't greyed. */
65
65
  readonly?: boolean;
66
66
  invalid?: boolean;
67
+ /** Let a `data-fdy-validate` form demand a row be picked. It renders `data-fdy-required` on the
68
+ * field group, which mirrors the picked keys in `data-value`; `freeday-form.js` refuses the
69
+ * submit, paints the message in the field and focuses the open button. Not the native
70
+ * `required`: the field's input is readonly, and a readonly input is barred from constraint
71
+ * validation, so `required` on it never refused anything (#114). Ignored with `dialogOnly`,
72
+ * which has no field to paint. */
73
+ required?: boolean;
67
74
  /** Render a clear button when a row is picked, so an OPTIONAL foreign key can be unset. Without it
68
75
  * `modelValue` accepts `Row | null` but the component can only ever produce a `Row`. */
69
76
  clearable?: boolean;
@@ -122,6 +129,8 @@ const isInvalid: ComputedRef<boolean> = computed((): boolean => props.invalid ==
122
129
  const currentRows: ComputedRef<Row[]> = computed((): Row[] =>
123
130
  Array.isArray(props.modelValue) ? props.modelValue : props.modelValue !== null ? [props.modelValue as Row] : [],
124
131
  );
132
+ /* The live value on the root, as every value-bearing kit control mirrors it: the picked keys. */
133
+ const valueAttr: ComputedRef<string> = computed((): string => currentRows.value.map((r: Row): string => props.rowKey(r)).join(','));
125
134
  const selectedTextFor = (n: number): string => (props.selectedText ?? '{n} selected').replace('{n}', String(n));
126
135
  const displayValue: ComputedRef<string> = computed((): string => {
127
136
  if (props.multiple === true) return currentRows.value.length === 0 ? '' : selectedTextFor(currentRows.value.length);
@@ -346,7 +355,8 @@ onBeforeUnmount((): void => {
346
355
  </script>
347
356
 
348
357
  <template>
349
- <div :class="isDialogOnly ? 'fdy-cfl__host' : 'fdy-input-group'">
358
+ <div :class="isDialogOnly ? 'fdy-cfl__host' : 'fdy-input-group'" :data-value="valueAttr"
359
+ :data-fdy-required="required === true && !isDialogOnly ? '' : undefined">
350
360
  <template v-if="!isDialogOnly">
351
361
  <input
352
362
  :id="fieldId"
@@ -24,6 +24,9 @@ const props = defineProps<{
24
24
  /** Locked/view mode for both pickers, focusable, values shown, but can't be opened or changed. */
25
25
  readonly?: boolean;
26
26
  invalid?: boolean;
27
+ /** Both ends must be filled for a `data-fdy-validate` form to submit. Forwarded to BOTH pickers,
28
+ * each refusing on its own; the two share the field's one message slot (#114). */
29
+ required?: boolean;
27
30
  describedby?: string;
28
31
  startPlaceholder?: string;
29
32
  endPlaceholder?: string;
@@ -80,6 +83,7 @@ function setEnd(end: string): void {
80
83
  :disabled="disabled"
81
84
  :readonly="readonly"
82
85
  :invalid="invalid"
86
+ :required="required"
83
87
  :describedby="describedby"
84
88
  :placeholder="startPlaceholder ?? 'Dari'"
85
89
  :prev-month-label="prevMonthLabel"
@@ -101,6 +105,7 @@ function setEnd(end: string): void {
101
105
  :disabled="disabled"
102
106
  :readonly="readonly"
103
107
  :invalid="invalid"
108
+ :required="required"
104
109
  :describedby="describedby"
105
110
  :placeholder="endPlaceholder ?? 'Sampai'"
106
111
  :prev-month-label="prevMonthLabel"
@@ -13,6 +13,32 @@ export interface UseFreedayReturn {
13
13
  */
14
14
  export declare function useFreeday(rootRef?: Ref<HTMLElement | null>): UseFreedayReturn;
15
15
 
16
+ /** A theme preference. `'system'` removes `data-theme`, so the tokens follow the OS, live. */
17
+ export type FdyTheme = 'light' | 'dark' | 'system';
18
+ /** What is actually on screen once `'system'` is resolved against the OS. */
19
+ export type FdyResolvedTheme = 'light' | 'dark';
20
+ /** `event.detail` of `fdy-theme-change`, fired on `document`. */
21
+ export interface FdyThemeChangeDetail { theme: FdyTheme; resolved: FdyResolvedTheme }
22
+
23
+ export interface UseThemeReturn {
24
+ /** The stored preference, per device; `'system'` until one is chosen. */
25
+ theme: Readonly<Ref<FdyTheme>>;
26
+ /** The theme on screen. Use it for the toggle's icon and label, not `theme`. */
27
+ resolved: Readonly<Ref<FdyResolvedTheme>>;
28
+ /** Store and apply a preference. Throws a TypeError for any other value. */
29
+ setTheme: (theme: FdyTheme) => void;
30
+ /** Switch to the opposite of what is on screen, so the first press always changes it. */
31
+ toggleTheme: () => void;
32
+ }
33
+
34
+ /**
35
+ * The device's light/dark preference, applied to `<html>`, kept in `localStorage` and shared by
36
+ * every caller on the page. Pass a ref to also apply it to that element, for a region whose markup
37
+ * fixes its own `data-theme`. Loads the theme core itself: no `import '@cahyo-dimas/freeday'`
38
+ * needed. Apply the stored value before first paint with the `<head>` snippet in COMPONENTS.md.
39
+ */
40
+ export declare function useTheme(rootRef?: Ref<HTMLElement | null>): UseThemeReturn;
41
+
16
42
  /**
17
43
  * Which of the panel's edges lines up with the trigger's. `'start'` (the default) puts the panel's
18
44
  * LEFT edge on the trigger's left edge, right for an input-shaped dropdown; `'end'` puts its RIGHT
@@ -1,4 +1,5 @@
1
1
  export { useFreeday } from './useFreeday.js';
2
+ export { useTheme } from './useTheme.js';
2
3
  export { usePopover } from './usePopover.ts';
3
4
  export { default as FdyCombo } from './components/FdyCombo.vue';
4
5
  export { default as FdyDatepicker } from './components/FdyDatepicker.vue';
@@ -0,0 +1,47 @@
1
+ /* Freeday. Vue 3 theme preference (CATATAN-FREEDAY §23). A thin view over window.FreedayTheme,
2
+ * which this file loads itself, so it needs no `import 'freeday'` first and a page still has one
3
+ * preference if the root import is there too (the core registers once).
4
+ *
5
+ * Usage:
6
+ * const { theme, resolved, setTheme, toggleTheme } = useTheme();
7
+ * // <button @click="toggleTheme" :aria-label="resolved === 'dark' ? 'Light theme' : 'Dark theme'">
8
+ *
9
+ * Pass a template ref to also put the preference on that element, for a region whose markup fixes
10
+ * its own data-theme. Every instance stays in step through the core's fdy-theme-change event.
11
+ */
12
+ import { onBeforeUnmount, onMounted, readonly, ref } from 'vue';
13
+ import '../../dist/freeday-theme.js';
14
+
15
+ function core() {
16
+ return typeof window === 'undefined' ? null : window.FreedayTheme;
17
+ }
18
+
19
+ export function useTheme(rootRef) {
20
+ var api = core();
21
+ var theme = ref(api ? api.get() : 'system');
22
+ var resolved = ref(api ? api.resolved() : 'light');
23
+ var detach = null;
24
+
25
+ function sync(e) {
26
+ theme.value = e.detail.theme;
27
+ resolved.value = e.detail.resolved;
28
+ }
29
+
30
+ onMounted(function () {
31
+ if (rootRef && rootRef.value) detach = api.attach(rootRef.value);
32
+ theme.value = api.get();
33
+ resolved.value = api.resolved();
34
+ document.addEventListener('fdy-theme-change', sync);
35
+ });
36
+ onBeforeUnmount(function () {
37
+ document.removeEventListener('fdy-theme-change', sync);
38
+ if (detach) detach();
39
+ });
40
+
41
+ return {
42
+ theme: readonly(theme),
43
+ resolved: readonly(resolved),
44
+ setTheme: function (next) { core().set(next); },
45
+ toggleTheme: function () { core().toggle(); }
46
+ };
47
+ }
@@ -22,6 +22,7 @@
22
22
  root.dataset.fdyAcReady = '1';
23
23
 
24
24
  var input = root.querySelector('input[role="combobox"]') || root.querySelector('input');
25
+ root._fdyInvalid = !!input && input.getAttribute('aria-invalid') === 'true';
25
26
  var listbox = root.querySelector('[role="listbox"]');
26
27
  if (!input || !listbox) return;
27
28
  var options = Array.prototype.slice.call(listbox.querySelectorAll('[role="option"]'));
@@ -145,10 +146,14 @@
145
146
  if (!input || !state) return;
146
147
  if (state.disabled != null) input.disabled = !!state.disabled;
147
148
  if (state.readonly != null) input.readOnly = !!state.readonly;
148
- if (state.invalid != null) {
149
+ /* Only when the HOST changes it: an unchanged push must not erase what freeday-form.js painted (#114). */
150
+ if (state.invalid != null && !!state.invalid !== root._fdyInvalid) {
151
+ root._fdyInvalid = !!state.invalid;
149
152
  if (state.invalid) input.setAttribute('aria-invalid', 'true');
150
153
  else input.removeAttribute('aria-invalid');
151
154
  }
155
+ /* Native `required` on the input itself: it is editable, so constraint validation applies (#114). */
156
+ if (state.required != null) input.required = !!state.required;
152
157
  }
153
158
 
154
159
  window.FreedayAutocomplete = { init: initAutocomplete, initAll: initAll, setState: setState };
@@ -236,16 +236,26 @@
236
236
  trigger.disabled = stateDisabled;
237
237
  if (stateReadonly) trigger.setAttribute('aria-readonly', 'true');
238
238
  else trigger.removeAttribute('aria-readonly');
239
+ if ((stateDisabled || stateReadonly) && !panel.hidden) close(false);
240
+ }
241
+ /* `invalid` is written only when the HOST changes it. A render-once host (Blazor) pushes every
242
+ state on every parent re-render, and rewriting an unchanged `false` erased the mark
243
+ freeday-form.js had painted, leaving its message under a field no longer red (#114). */
244
+ function applyInvalid() {
239
245
  if (stateInvalid) trigger.setAttribute('aria-invalid', 'true');
240
246
  else trigger.removeAttribute('aria-invalid');
241
247
  wrap.classList.toggle('fdy-cascade--error', stateInvalid);
242
- if ((stateDisabled || stateReadonly) && !panel.hidden) close(false);
243
248
  }
244
249
  function setState(next) {
245
250
  if (!next) return;
246
251
  if (next.disabled != null) stateDisabled = !!next.disabled;
247
252
  if (next.readonly != null) stateReadonly = !!next.readonly;
248
- if (next.invalid != null) stateInvalid = !!next.invalid;
253
+ if (next.invalid != null && !!next.invalid !== stateInvalid) {
254
+ stateInvalid = !!next.invalid;
255
+ applyInvalid();
256
+ }
257
+ /* `required` too: a render-once host (Blazor) has no other way to change it later (#114). */
258
+ if (next.required != null) wrap.toggleAttribute('data-fdy-required', !!next.required);
249
259
  applyState();
250
260
  }
251
261
 
@@ -306,6 +316,7 @@
306
316
  }
307
317
 
308
318
  applyState();
319
+ applyInvalid();
309
320
 
310
321
  var api = {
311
322
  wrap: wrap,
@@ -99,15 +99,26 @@
99
99
  countEl.textContent = textOf(dialog, 'selected', { n: n });
100
100
  }
101
101
 
102
+ /* The row's `data-value`, else the text the field shows: what the group mirrors as its live
103
+ value, so `freeday-form.js` can require a choose-from-list. Its input is readonly, which
104
+ bars it from constraint validation, so a native `required` on it never refuses (#114). */
105
+ function rowValue(row, key) {
106
+ if (row.value != null && row.value !== '') return String(row.value);
107
+ return row[key] != null ? String(row[key]) : '';
108
+ }
109
+
102
110
  function fillGroup(detail) {
103
111
  if (!activeGroup) return;
112
+ var key = activeGroup.getAttribute('data-fdy-cfl-display') || 'value';
113
+ activeGroup.setAttribute('data-value', detail.rows
114
+ ? detail.rows.map(function (r) { return rowValue(r, key); }).join(',')
115
+ : rowValue(detail.row, key));
104
116
  var input = activeGroup.querySelector('.fdy-input');
105
117
  if (input) {
106
118
  if (detail.rows) {
107
119
  var tpl = activeGroup.getAttribute('data-fdy-cfl-summary') || '{n} selected';
108
120
  input.value = detail.rows.length ? tpl.replace('{n}', String(detail.rows.length)) : '';
109
121
  } else {
110
- var key = activeGroup.getAttribute('data-fdy-cfl-display') || 'value';
111
122
  input.value = detail.row[key] != null ? detail.row[key] : (detail.row.value || '');
112
123
  }
113
124
  }
@@ -582,16 +582,26 @@
582
582
  trigger.disabled = stateDisabled;
583
583
  if (stateReadonly) trigger.setAttribute('aria-readonly', 'true');
584
584
  else trigger.removeAttribute('aria-readonly');
585
+ if ((stateDisabled || stateReadonly) && !panel.hidden) close(false);
586
+ }
587
+ /* `invalid` is written only when the HOST changes it. A render-once host (Blazor) pushes every
588
+ state on every parent re-render, and rewriting an unchanged `false` erased the mark
589
+ freeday-form.js had painted, leaving its message under a field no longer red (#114). */
590
+ function applyInvalid() {
585
591
  if (stateInvalid) trigger.setAttribute('aria-invalid', 'true');
586
592
  else trigger.removeAttribute('aria-invalid');
587
593
  wrap.classList.toggle('fdy-datepicker--error', stateInvalid);
588
- if ((stateDisabled || stateReadonly) && !panel.hidden) close(false);
589
594
  }
590
595
  function setState(next) {
591
596
  if (!next) return;
592
597
  if (next.disabled != null) stateDisabled = !!next.disabled;
593
598
  if (next.readonly != null) stateReadonly = !!next.readonly;
594
- if (next.invalid != null) stateInvalid = !!next.invalid;
599
+ if (next.invalid != null && !!next.invalid !== stateInvalid) {
600
+ stateInvalid = !!next.invalid;
601
+ applyInvalid();
602
+ }
603
+ /* `required` too: a render-once host (Blazor) has no other way to change it later (#114). */
604
+ if (next.required != null) wrap.toggleAttribute('data-fdy-required', !!next.required);
595
605
  applyState();
596
606
  }
597
607
 
@@ -628,6 +638,7 @@
628
638
  updateDisplay();
629
639
 
630
640
  applyState();
641
+ applyInvalid();
631
642
 
632
643
  var api = {
633
644
  wrap: wrap,
@@ -82,10 +82,11 @@
82
82
  * data-fdy-required the app asking for it, on the control's ROOT
83
83
  * data-value the live value, which every value-bearing kit control now mirrors
84
84
  * and the element that takes aria-invalid and focus is found by `aria-haspopup`, which every
85
- * popup-style kit trigger already sets. `FdyAutocomplete` and `FdyCfl` are absent on purpose:
86
- * they render a real <input>, so the NATIVE `required` is what fits them, and routing them
87
- * through here would give one field two sources of truth. No prop exposes that yet, which is a
88
- * separate gap and is written down in NEXT-UP.md rather than half-closed here. */
85
+ * popup-style kit trigger already sets. A choose-from-list belongs here too: its <input> is
86
+ * `readonly`, and a readonly input is barred from constraint validation, so a native `required`
87
+ * on it never refuses anything; its open button carries `aria-haspopup="dialog"`. An
88
+ * autocomplete does NOT: its input is editable, so the native `required` is the one that fits,
89
+ * and routing it through here would give one field two sources of truth (#114). */
89
90
  function kitRoots(form) {
90
91
  return Array.prototype.slice.call(form.querySelectorAll('[data-fdy-required]'));
91
92
  }
@@ -97,6 +98,9 @@
97
98
  }
98
99
 
99
100
  function kitMessage(root) {
101
+ /* Read again on every pass, not only when the root was collected: a field required only under
102
+ a condition loses the attribute while its error is still painted (#114). */
103
+ if (!root.hasAttribute('data-fdy-required')) return '';
100
104
  if ((root.getAttribute('data-value') || '') !== '') return '';
101
105
  var form = root.closest('form');
102
106
  return root.getAttribute('data-fdy-msg-required')
@@ -143,6 +147,17 @@
143
147
  return custom || el.getAttribute('data-fdy-msg') || (alias && textOf(form, alias)) || el.validationMessage || textOf(form, 'invalid');
144
148
  }
145
149
 
150
+ /* Two controls can share one `.fdy-field` and so one message slot: a date range's two ends, or two
151
+ native inputs side by side. The slot is found per FIELD, so clearing the end that was filled
152
+ used to erase the message the other end still needed, leaving it red with nothing to say why
153
+ (#114). The slot is kept while any other control that points at it is still invalid. */
154
+ function stillClaimed(errEl, except) {
155
+ var scope = except.closest('form') || document;
156
+ return Array.prototype.some.call(scope.querySelectorAll('[aria-invalid="true"]'), function (other) {
157
+ return other !== except && (other.getAttribute('aria-describedby') || '').split(/\s+/).indexOf(errEl.id) !== -1;
158
+ });
159
+ }
160
+
146
161
  function paint(el, message) {
147
162
  var errEl = ensureErrorEl(el);
148
163
  if (message) {
@@ -151,6 +166,7 @@
151
166
  errEl.hidden = false;
152
167
  } else {
153
168
  el.removeAttribute('aria-invalid');
169
+ if (stillClaimed(errEl, el)) return;
154
170
  errEl.textContent = '';
155
171
  errEl.hidden = true;
156
172
  }
@@ -208,7 +224,7 @@
208
224
  root.dataset.fdyKitBound = '1';
209
225
  new MutationObserver(function () {
210
226
  if (submitted || eager) validateKit(root);
211
- }).observe(root, { attributes: true, attributeFilter: ['data-value'] });
227
+ }).observe(root, { attributes: true, attributeFilter: ['data-value', 'data-fdy-required'] });
212
228
  }
213
229
  function validateOne(el) {
214
230
  var msg = messageFor(el);
@@ -39,6 +39,7 @@
39
39
  var options = Array.prototype.slice.call(root.querySelectorAll('.fdy-combo__option'));
40
40
  if (!button || !listbox || !valueEl || options.length === 0) return;
41
41
  root.dataset.fdyComboReady = '1';
42
+ root._fdyInvalid = button.getAttribute('aria-invalid') === 'true';
42
43
 
43
44
  seq += 1;
44
45
  if (!listbox.id) listbox.id = 'fdy-combo-list-' + seq;
@@ -222,10 +223,16 @@
222
223
  if (state.readonly) button.setAttribute('aria-readonly', 'true');
223
224
  else button.removeAttribute('aria-readonly');
224
225
  }
225
- if (state.invalid != null) {
226
+ /* `invalid` is written only when the HOST changes it. A render-once host (Blazor) pushes every
227
+ state on every parent re-render, and rewriting an unchanged `false` erased the mark
228
+ freeday-form.js had painted, leaving its message under a field no longer red (#114). */
229
+ if (state.invalid != null && !!state.invalid !== root._fdyInvalid) {
230
+ root._fdyInvalid = !!state.invalid;
226
231
  if (state.invalid) button.setAttribute('aria-invalid', 'true');
227
232
  else button.removeAttribute('aria-invalid');
228
233
  }
234
+ /* `required` too: a render-once host (Blazor) has no other way to change it later (#114). */
235
+ if (state.required != null) root.toggleAttribute('data-fdy-required', !!state.required);
229
236
  }
230
237
 
231
238
  window.FreedayCombo = { init: initCombo, initAll: initAll, setValue: setValue, setState: setState };
@@ -0,0 +1,115 @@
1
+ /* Freeday, theme preference (optional, zero-dependency, no markup of its own).
2
+ * The light/dark switch every app needs and each one used to write for itself, guessing the kit's
3
+ * contract from the docs (CATATAN-FREEDAY §23). One preference per device: 'light', 'dark' or
4
+ * 'system'.
5
+ *
6
+ * 'system' REMOVES data-theme instead of computing one. The tokens already follow
7
+ * prefers-color-scheme whenever no data-theme says otherwise, so the page tracks an OS switch live,
8
+ * with no listener deciding anything.
9
+ *
10
+ * The preference always lands on <html>. attach(el) adds another root that gets it too: a region
11
+ * that carries its own data-theme in markup (a dark kiosk shell inside a light app) would otherwise
12
+ * keep that fixed value, because a subtree's attribute wins over the document's. Attaching hands the
13
+ * region to the user: with the preference at 'system' its fixed value is removed and it follows the
14
+ * OS like the rest of the page.
15
+ *
16
+ * Every change fires "fdy-theme-change" on document, detail {theme, resolved}; so does an OS switch
17
+ * while the preference is 'system', since `resolved` (what is actually shown) moved. The Vue and
18
+ * React useTheme() import this same file, so a page has one preference whichever way it loaded.
19
+ *
20
+ * Only a script in <head> can apply the stored value before the first paint; this file runs too
21
+ * late for that, and catches up on load. COMPONENTS.md has the one-line snippet. Exposes
22
+ * window.FreedayTheme.
23
+ */
24
+ (function () {
25
+ 'use strict';
26
+ if (typeof window === 'undefined' || window.FreedayTheme) return;
27
+
28
+ var KEY = 'fdy-theme';
29
+ var THEMES = ['light', 'dark', 'system'];
30
+ var roots = [];
31
+ var memory = null;
32
+ var media = window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : null;
33
+
34
+ function valid(theme) {
35
+ return THEMES.indexOf(theme) !== -1;
36
+ }
37
+
38
+ /* Storage can throw (private mode, a blocked origin); the preference then lasts the page. */
39
+ function read() {
40
+ try {
41
+ var stored = window.localStorage.getItem(KEY);
42
+ if (valid(stored)) return stored;
43
+ } catch (e) { /* fall through to the in-memory value */ }
44
+ return memory !== null ? memory : 'system';
45
+ }
46
+ function write(theme) {
47
+ memory = theme;
48
+ try {
49
+ if (theme === 'system') window.localStorage.removeItem(KEY);
50
+ else window.localStorage.setItem(KEY, theme);
51
+ } catch (e) { /* kept in memory only */ }
52
+ }
53
+
54
+ function applyTo(el, theme) {
55
+ if (theme === 'system') el.removeAttribute('data-theme');
56
+ else el.setAttribute('data-theme', theme);
57
+ }
58
+ function targets() {
59
+ return [document.documentElement].concat(roots);
60
+ }
61
+
62
+ /* What <html> shows: its own attribute if it has one, the OS otherwise. Read from the DOM, not
63
+ from the preference, so markup that ships data-theme="light" before anything is stored is
64
+ reported as light, which is what the user sees. */
65
+ function resolved() {
66
+ var own = document.documentElement.getAttribute('data-theme');
67
+ if (own === 'light' || own === 'dark') return own;
68
+ return media && media.matches ? 'dark' : 'light';
69
+ }
70
+
71
+ function emit() {
72
+ document.dispatchEvent(new CustomEvent('fdy-theme-change', { detail: { theme: read(), resolved: resolved() } }));
73
+ }
74
+
75
+ function set(theme) {
76
+ if (!valid(theme)) {
77
+ throw new TypeError('FreedayTheme.set: theme must be one of ' + THEMES.join(', ') + ', got ' + JSON.stringify(theme));
78
+ }
79
+ write(theme);
80
+ targets().forEach(function (el) { applyTo(el, theme); });
81
+ emit();
82
+ }
83
+
84
+ /* Flips what is SHOWN, so the first press always changes the screen, even from 'system'. */
85
+ function toggle() {
86
+ set(resolved() === 'dark' ? 'light' : 'dark');
87
+ }
88
+
89
+ function attach(el) {
90
+ if (!el || roots.indexOf(el) !== -1) return detacher(el);
91
+ roots.push(el);
92
+ applyTo(el, read());
93
+ return detacher(el);
94
+ }
95
+ function detacher(el) {
96
+ return function () {
97
+ var i = roots.indexOf(el);
98
+ if (i !== -1) roots.splice(i, 1);
99
+ };
100
+ }
101
+
102
+ /* A stored choice is applied on load as well. The <head> snippet does it before first paint;
103
+ without it the page shows the CSS default for a moment, and then still ends up right. Nothing
104
+ stored means nothing is touched, so markup that ships its own data-theme keeps it. */
105
+ var initial = read();
106
+ if (initial !== 'system') applyTo(document.documentElement, initial);
107
+
108
+ if (media) {
109
+ var onOs = function () { if (read() === 'system') emit(); };
110
+ if (media.addEventListener) media.addEventListener('change', onOs);
111
+ else if (media.addListener) media.addListener(onOs);
112
+ }
113
+
114
+ window.FreedayTheme = { get: read, resolved: resolved, set: set, toggle: toggle, attach: attach };
115
+ })();
@@ -117,16 +117,26 @@
117
117
  trigger.disabled = stateDisabled;
118
118
  if (stateReadonly) trigger.setAttribute('aria-readonly', 'true');
119
119
  else trigger.removeAttribute('aria-readonly');
120
+ if ((stateDisabled || stateReadonly) && !panel.hidden) close(false);
121
+ }
122
+ /* `invalid` is written only when the HOST changes it. A render-once host (Blazor) pushes every
123
+ state on every parent re-render, and rewriting an unchanged `false` erased the mark
124
+ freeday-form.js had painted, leaving its message under a field no longer red (#114). */
125
+ function applyInvalid() {
120
126
  if (stateInvalid) trigger.setAttribute('aria-invalid', 'true');
121
127
  else trigger.removeAttribute('aria-invalid');
122
128
  wrap.classList.toggle('fdy-timepicker--error', stateInvalid);
123
- if ((stateDisabled || stateReadonly) && !panel.hidden) close(false);
124
129
  }
125
130
  function setState(next) {
126
131
  if (!next) return;
127
132
  if (next.disabled != null) stateDisabled = !!next.disabled;
128
133
  if (next.readonly != null) stateReadonly = !!next.readonly;
129
- if (next.invalid != null) stateInvalid = !!next.invalid;
134
+ if (next.invalid != null && !!next.invalid !== stateInvalid) {
135
+ stateInvalid = !!next.invalid;
136
+ applyInvalid();
137
+ }
138
+ /* `required` too: a render-once host (Blazor) has no other way to change it later (#114). */
139
+ if (next.required != null) wrap.toggleAttribute('data-fdy-required', !!next.required);
130
140
  applyState();
131
141
  }
132
142
 
@@ -214,6 +224,7 @@
214
224
 
215
225
  updateDisplay();
216
226
  applyState();
227
+ applyInvalid();
217
228
 
218
229
  var api = {
219
230
  wrap: wrap,