@threadlabs/looma 0.53.17 → 0.53.18

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.
@@ -2,7 +2,7 @@
2
2
  <defs>
3
3
  <prop name="invalid" type="boolean" default="false"
4
4
  >Marks the field's control invalid by setting aria-invalid on it. Only while invalid, the error slot appears and becomes part of the control's
5
- description.</prop>
5
+ description. Clearing the field error restores the control's own validation state.</prop>
6
6
  <prop name="disabled" type="boolean" default="false"
7
7
  >Disables the control named by the label's for attribute, or the first field control inside it. Composite controls supply their focus target and form value.
8
8
  The field writes this to the control whenever it updates, so set it here rather than on the control.</prop>
@@ -15,8 +15,48 @@ export function connect(host) {
15
15
  const element = host.element;
16
16
  let activeInput = null;
17
17
  let owned = new Set();
18
+ // Save the control's state only while the field overrides it.
19
+ let controlInvalid;
18
20
  let queued = false;
19
21
  const warned = new Set();
22
+ const readControlInvalid = (records) => {
23
+ if (records.some((record) => record.attributeName === "aria-invalid")) {
24
+ controlInvalid = activeInput.getAttribute("aria-invalid") ?? "false";
25
+ } else if (records.some(({ attributeName }) =>
26
+ /^data-ui-[a-z-]+-prop-invalid$/.test(attributeName) && activeInput.getAttribute(attributeName) === "true")) {
27
+ // A control's new true prop can leave aria-invalid unchanged while the field holds it true.
28
+ controlInvalid = "true";
29
+ }
30
+ };
31
+ const setNativeInvalid = (value) => {
32
+ if (activeInput.getAttribute("aria-invalid") === value) return;
33
+ activeInput.setAttribute("aria-invalid", value);
34
+ // Only changes from the control contribute to the value we restore.
35
+ invalidObserver.takeRecords();
36
+ };
37
+ const invalidObserver = new MutationObserver((records) => {
38
+ readControlInvalid(records);
39
+ if (host.props.invalid.value) setNativeInvalid("true");
40
+ });
41
+ const releaseInvalid = () => {
42
+ if (controlInvalid === undefined) return;
43
+ readControlInvalid(invalidObserver.takeRecords());
44
+ invalidObserver.disconnect();
45
+ setNativeInvalid(controlInvalid);
46
+ controlInvalid = undefined;
47
+ };
48
+ const updateInvalid = () => {
49
+ if (!host.props.invalid.value) {
50
+ releaseInvalid();
51
+ return;
52
+ }
53
+ if (controlInvalid === undefined) {
54
+ controlInvalid = activeInput.getAttribute("aria-invalid") ?? "false";
55
+ invalidObserver.observe(activeInput, { attributes: true });
56
+ }
57
+ readControlInvalid(invalidObserver.takeRecords());
58
+ setNativeInvalid("true");
59
+ };
20
60
  const removeOwned = (input) => {
21
61
  if (!input || owned.size === 0) return;
22
62
  const remaining = descriptionIds(input).filter((value) => !owned.has(value));
@@ -35,6 +75,7 @@ export function connect(host) {
35
75
  const errorRegion = element.querySelector(':scope > .error');
36
76
  const error = element.querySelector(':scope > .error > *, [slot="error"], [role="alert"]') ?? (errorRegion?.textContent.trim() ? errorRegion : null);
37
77
  if (input !== activeInput) {
78
+ releaseInvalid();
38
79
  removeOwned(activeInput);
39
80
  activeInput = input;
40
81
  owned = new Set();
@@ -62,7 +103,7 @@ export function connect(host) {
62
103
  for (const value of element.querySelectorAll("[data-ui-field-value]")) value.disabled = input.disabled;
63
104
  }
64
105
  input.required = Boolean(host.props.required.value);
65
- input.setAttribute("aria-invalid", String(Boolean(host.props.invalid.value)));
106
+ updateInvalid();
66
107
  };
67
108
  const schedule = () => {
68
109
  if (queued) return;
@@ -85,6 +126,7 @@ export function connect(host) {
85
126
  return () => {
86
127
  stop();
87
128
  observer.disconnect();
129
+ releaseInvalid();
88
130
  removeOwned(activeInput);
89
131
  };
90
132
  }