@aarongustafson/form-show-if 3.1.3 → 3.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/@aarongustafson/form-show-if.svg)](https://www.npmjs.com/package/@aarongustafson/form-show-if) [![Build Status](https://img.shields.io/github/actions/workflow/status/aarongustafson/form-show-if/ci.yml?branch=main)](https://github.com/aarongustafson/form-show-if/actions)
4
4
 
5
- Currently, HTML provides no mechanism to show & hide dependent fields. Sometimes you only want a field to show when certain other fields have a (particular) value. The `form-show-if` web component enables that.
5
+ `form-show-if` shows or hides native form fields according to other fields’ values.
6
+ It uses light DOM, native `hidden` and `disabled` behavior, and authored labels.
7
+ Before enhancement, the fields remain available unless your markup or CSS hides them.
6
8
 
7
9
  ## TypeScript Support
8
10
 
@@ -46,7 +48,9 @@ Use the guarded definition helper to register the element when `customElements`
46
48
  import '@aarongustafson/form-show-if/define.js';
47
49
  ```
48
50
 
49
- If you prefer to control when the element is registered, call the helper directly:
51
+ Importing `define.js` registers the default name immediately when a browser
52
+ registry is available, even when you import the helper by name. You can also call
53
+ the helper again; it preserves an existing registration:
50
54
 
51
55
  ```javascript
52
56
  import { defineFormShowIf } from '@aarongustafson/form-show-if/define.js';
@@ -57,7 +61,10 @@ defineFormShowIf();
57
61
  You can also include the guarded script from HTML:
58
62
 
59
63
  ```html
60
- <script src="./node_modules/@aarongustafson/form-show-if/define.js" type="module"></script>
64
+ <script
65
+ src="./node_modules/@aarongustafson/form-show-if/define.js"
66
+ type="module"
67
+ ></script>
61
68
  ```
62
69
 
63
70
  ### CDN Usage
@@ -65,38 +72,69 @@ You can also include the guarded script from HTML:
65
72
  You can also use the component directly from a CDN:
66
73
 
67
74
  ```html
68
- <script src="https://unpkg.com/@aarongustafson/form-show-if@latest/define.js" type="module"></script>
75
+ <script
76
+ src="https://unpkg.com/@aarongustafson/form-show-if@latest/define.js"
77
+ type="module"
78
+ ></script>
69
79
  ```
70
80
 
71
81
  ## API
72
82
 
73
- <ul>
74
- <li><code>conditions</code><br> A double pipe (||) separated list of <code>name</code>/<code>value</code> pairs. When fields with the provided <code>name</code> values are updated, the value of those fields will be compared against the values you provided (or * for anything). If any resolve to true, the field will be shown, otherwise it will be hidden.</li>
75
- <li><code>disabled-class</code> (optional)<br> By default the field & <code>label</code> wrapper will be marked <code>hidden</code> when disabled, but you can apply a custom <code>class</code> to be used instead, when the field is disabled.</li>
76
- <li><code>enabled-class</code> (optional)<br> By default the field & <code>label</code> wrapper will not be <code>hidden</code> when enabled, but you can apply a custom <code>class</code> to the wrapper, when the field is enabled.</li>
77
- </ul>
83
+ | Attribute | Property | Default | Behavior |
84
+ | ---------------- | --------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
85
+ | `conditions` | `conditions` | `null` | OR-separated `name=value` comparisons, such as `email=*\|\|choose=3`. `*` matches a nonempty value. With no match, the managed fields are disabled. |
86
+ | `disabled-class` | `disabledClass` | `null` | A single class token applied to the unavailable wrapper **instead of** setting `hidden`. The fields remain natively disabled; visibility is your CSS’s responsibility. |
87
+ | `enabled-class` | `enabledClass` | `null` | A single class token applied to the available wrapper. Alone, this does **not** replace native hiding when unavailable. |
88
+
89
+ These string properties reflect their attributes. Setting a property to `null`
90
+ removes the attribute. Native `input`, `change`, and form `reset` events trigger
91
+ reevaluation; changing `conditions` also reevaluates an initialized component.
92
+ The component does not emit custom events.
93
+
94
+ Set `disabled-class` and `enabled-class` in markup before initialization.
95
+ Their properties reflect later attribute changes, but those changes do not
96
+ fully migrate existing DOM state: adding `disabled-class` does not clear an
97
+ existing `hidden` attribute, and changing or removing a class token does not
98
+ remove previously applied tokens. A field can stay hidden or retain stale
99
+ state styling after its conditions match. Reconnection alone does not clear
100
+ those stale attributes or classes. Prefer configuring the class tokens before
101
+ upgrade; if you change them later, explicitly reconcile the old classes and
102
+ visibility while preserving entered values and focus.
103
+
104
+ Conditions support OR, not AND. Names and values cannot contain the `||` or `=`
105
+ delimiters; there is no escaping syntax. Comparisons use current values, not the
106
+ referenced control’s enabled state. A disabled controller can retain a value that
107
+ still matches. Prefer direct dependencies on an available controller instead of
108
+ assuming that hiding one dependent field will disable its dependents too.
78
109
 
79
110
  ## Markup Assumptions
80
111
 
81
- This web component assumes the fields you reference in `conditions` exist in the DOM when the component is loaded. If they don't, they will be ignored.
112
+ Put each dependent field and its label inside a `form-show-if`. The component
113
+ manages the first eligible `input`, `select`, or `textarea`, plus same-name
114
+ siblings inside the component. It does not disable every differently named
115
+ descendant. Use a separate wrapper for each dependent field.
82
116
 
83
- ## Implementation notes
117
+ The wrapper is the nearest ancestor inside the component that contains a label,
118
+ or the component itself. Keep both the label and field inside that boundary.
84
119
 
85
- The "wrapper" mentioned below refers to the nearest mutual parent of the field & its label. It may be the `form-show-if` element itself.
120
+ Referenced controls are looked up in the nearest form, or the document body when
121
+ there is no form. Missing names do not match; a later input/change can find newly
122
+ added controls. Successful lookups are cached. If you replace a referenced node,
123
+ reconnect the component or change its conditions to clear that cache.
86
124
 
87
- 1. **Field markup changes.** When the field is hidden (conditions not met), it will receive the `disabled` attribute and all fields within the wrapper are disabled.
125
+ ## Implementation notes
88
126
 
89
- 2. **Visual state management.** If `disabled-class` and/or `enabled-class` **are not** defined:
90
- * When **no** condition is met: The wrapper has a `hidden` attribute
91
- * When **any** condition is met: The `hidden` attribute is removed from the wrapper
127
+ Initialization waits for an animation frame so authored children are available.
128
+ Unavailable managed fields are disabled and excluded from native FormData and
129
+ constraint validation; their values are not cleared. When a condition matches,
130
+ the fields are enabled and their wrapper is revealed unless `disabled-class`
131
+ delegates visibility to your styles.
92
132
 
93
- 3. **Custom class management.** If `disabled-class` and/or `enabled-class` **are** defined:
94
- * When **no** condition is met:
95
- * If `enabled-class` is defined, it is removed from the wrapper
96
- * If `disabled-class` is defined, it is added to the wrapper
97
- * When **any** condition is met:
98
- * If `enabled-class` is defined, it is added to the wrapper
99
- * If `disabled-class` is defined, it is removed from the wrapper
133
+ `enabled-class` is added only while available; `disabled-class` is added only while
134
+ unavailable. The component does not supply either class’s styles. Disconnection
135
+ cancels pending initialization, removes delegated listeners, and clears cached
136
+ references. Reconnection reevaluates the existing fields; disconnection alone
137
+ does not undo their last `hidden` or `disabled` state.
100
138
 
101
139
  ## Examples
102
140
 
@@ -104,15 +142,15 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
104
142
 
105
143
  ```html
106
144
  <form>
107
- <label for="email">Email</label>
108
- <input type="email" id="email" name="email">
145
+ <label for="email">Email</label>
146
+ <input type="email" id="email" name="email" />
109
147
 
110
- <form-show-if conditions="email=*">
111
- <label for="phone">Phone (shown if email provided)</label>
112
- <input type="tel" id="phone" name="phone">
113
- </form-show-if>
148
+ <form-show-if conditions="email=*">
149
+ <label for="phone">Phone (shown if email provided)</label>
150
+ <input type="tel" id="phone" name="phone" />
151
+ </form-show-if>
114
152
 
115
- <button type="submit">Submit</button>
153
+ <button type="submit">Submit</button>
116
154
  </form>
117
155
  ```
118
156
 
@@ -120,8 +158,8 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
120
158
 
121
159
  ```html
122
160
  <form-show-if conditions="email=*||phone=*">
123
- <label for="name">Name (shown if email OR phone provided)</label>
124
- <input type="text" id="name" name="name">
161
+ <label for="name">Name (shown if email OR phone provided)</label>
162
+ <input type="text" id="name" name="name" />
125
163
  </form-show-if>
126
164
  ```
127
165
 
@@ -129,8 +167,8 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
129
167
 
130
168
  ```html
131
169
  <form-show-if conditions="contact-method=email">
132
- <label for="email">Email Address</label>
133
- <input type="email" id="email" name="email">
170
+ <label for="email">Email Address</label>
171
+ <input type="email" id="email" name="email" />
134
172
  </form-show-if>
135
173
  ```
136
174
 
@@ -138,23 +176,97 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
138
176
 
139
177
  ```html
140
178
  <form-show-if conditions="newsletter=yes">
141
- <label for="email">Email (shown for newsletter)</label>
142
- <input type="email" id="email" name="email">
179
+ <label for="email">Email (shown for newsletter)</label>
180
+ <input type="email" id="email" name="email" />
143
181
  </form-show-if>
144
182
  ```
145
183
 
146
184
  ### Using Custom Classes
147
185
 
148
186
  ```html
149
- <form-show-if conditions="email=*" disabled-class="visually-hidden" enabled-class="is-visible">
150
- <label for="phone">Phone</label>
151
- <input type="tel" id="phone" name="phone">
187
+ <form-show-if
188
+ conditions="email=*"
189
+ disabled-class="disabled"
190
+ enabled-class="enabled"
191
+ >
192
+ <label for="phone"
193
+ >Phone
194
+ <input
195
+ type="tel"
196
+ id="phone"
197
+ name="phone"
198
+ aria-describedby="phone-state"
199
+ />
200
+ </label>
201
+ <small id="phone-state">
202
+ <span class="state-on">Available</span>
203
+ <span class="state-off">Unavailable: enter an email above</span>
204
+ </small>
152
205
  </form-show-if>
153
206
  ```
154
207
 
208
+ The demo styles keep reading colors paired. Solid/dashed outlines and authored
209
+ availability text convey the state; green/red accents are supplementary:
210
+
211
+ ```css
212
+ .enabled label,
213
+ [disabled-class='disabled'] label,
214
+ .disabled label {
215
+ outline: 2px solid currentColor;
216
+ outline-offset: 0.25rem;
217
+ background: Canvas;
218
+ color: CanvasText;
219
+ border-inline-start: 0.25rem solid #17652e;
220
+ padding: 0.75rem;
221
+ }
222
+ .disabled label {
223
+ outline-style: dashed;
224
+ border-inline-start-color: #982b39;
225
+ }
226
+ .state-off,
227
+ .disabled .state-on {
228
+ display: none;
229
+ }
230
+ .disabled .state-off {
231
+ display: inline;
232
+ }
233
+ ```
234
+
235
+ Translate or replace the authored labels and status text as needed. The component
236
+ generates no user-facing strings. Keeping descriptions outside labels preserves
237
+ stable accessible names; `aria-describedby` associates the current explanation.
238
+ Without enhancement, the fields remain usable and the default “Available” text
239
+ remains truthful.
240
+
241
+ ## Styling and accessibility
242
+
243
+ The component injects no CSS, colors, focus ring, shadow DOM, or CSS parts.
244
+ Use normal light-DOM selectors and the optional state classes. Pair backgrounds
245
+ and foregrounds, retain readable labels and necessary boundaries, and do not rely
246
+ only on color or faded opacity to convey availability. The demos use Canvas
247
+ reading pairs, ButtonFace/ButtonText controls, and Field/FieldText inputs, with
248
+ foreground-colored necessary borders and browser-native focus. Supporting footer
249
+ text uses an 80% CanvasText/Canvas mix with a Canvas backing and full-strength
250
+ forced-color/increased-contrast fallback.
251
+
252
+ Do not override native hiding with an unconditional `form-show-if { display:
253
+ block; }`. Use `form-show-if:not([hidden])` if you need a block wrapper.
254
+
255
+ Revealing fields does not move focus. Native keyboard navigation reaches enabled
256
+ fields and skips disabled ones. If application code invalidates a focused field
257
+ programmatically, move focus to a relevant visible control before hiding or
258
+ disabling it. Custom-class availability text is not an automatic live announcement;
259
+ provide deliberate, restrained announcements if your application needs them.
260
+
261
+ Keep required instructions outside conditionally hidden wrappers. Authored fields
262
+ remain visible and usable without JavaScript or after an import failure unless
263
+ your own markup/CSS hides them. Do not pre-hide essential fields merely to avoid
264
+ the initial enhancement transition.
265
+
155
266
  ## Browser Support
156
267
 
157
268
  This web component works in all modern browsers that support:
269
+
158
270
  - Custom Elements v1
159
271
  - ES Modules (for module usage)
160
272
 
@@ -176,8 +288,24 @@ npm run test:ui
176
288
 
177
289
  # Run tests with coverage
178
290
  npm run test:coverage
291
+
292
+ # Install browser engines once, then run real-browser checks
293
+ npx playwright install chromium firefox webkit
294
+ npm run test:browser
179
295
  ```
180
296
 
297
+ Browser tests substitute the study’s 19 distinct palettes and cover native
298
+ conditions, form submission values, reset, keyboard focus, non-color status,
299
+ reflow, and no-script/module-failure paths. Palette substitution does not
300
+ reproduce an operating system’s forced-color painting. Firefox custom colors,
301
+ real Windows Contrast themes, platform zoom, mobile hardware, and manual
302
+ assistive-technology acceptance still need testing; automated checks do not
303
+ establish conformance.
304
+
305
+ Reflow checks cover 320–1400px viewports and enlarged root text at 320px, with
306
+ measured field widths rather than only page-overflow assertions. Enlarging root
307
+ text is not a substitute for testing browser zoom.
308
+
181
309
  ### Linting and Formatting
182
310
 
183
311
  ```bash
@@ -11,26 +11,57 @@
11
11
  "customElement": true,
12
12
  "name": "FormShowIfElement",
13
13
  "tagName": "form-show-if",
14
- "description": "Web component that enables you to make one HTML form field show (or not) based on the value(s) of another field.",
15
- "members": [],
14
+ "description": "Light-DOM conditional form fields. Unmatched managed fields are disabled and their wrapper is hidden unless disabled-class supplies author-controlled visibility. No colors, status strings, or focus changes are generated. Configure state-class tokens before initialization; later changes do not clear stale classes or hidden state.",
15
+ "members": [
16
+ {
17
+ "kind": "field",
18
+ "name": "conditions",
19
+ "attribute": "conditions",
20
+ "reflects": true,
21
+ "type": { "text": "string | null" },
22
+ "default": "null",
23
+ "description": "OR-separated name=value conditions. * matches a nonempty value. Null removes the attribute; no matching condition disables the managed fields."
24
+ },
25
+ {
26
+ "kind": "field",
27
+ "name": "disabledClass",
28
+ "attribute": "disabled-class",
29
+ "reflects": true,
30
+ "type": { "text": "string | null" },
31
+ "default": "null",
32
+ "description": "A single authored class token applied to the wrapper when unavailable, instead of setting hidden. Managed fields remain natively disabled."
33
+ },
34
+ {
35
+ "kind": "field",
36
+ "name": "enabledClass",
37
+ "attribute": "enabled-class",
38
+ "reflects": true,
39
+ "type": { "text": "string | null" },
40
+ "default": "null",
41
+ "description": "A single authored class token applied when available. Alone, it does not replace native hiding when unavailable."
42
+ }
43
+ ],
16
44
  "attributes": [
17
45
  {
18
46
  "name": "conditions",
19
- "description": "A double pipe (||) separated list of `name`/`value` pairs that, if true, would make this field show",
47
+ "fieldName": "conditions",
48
+ "description": "OR-separated name=value comparisons, with * for any nonempty value. Missing names do not match. Conditions use current values, not the controller's enabled state.",
20
49
  "type": {
21
50
  "text": "string"
22
51
  }
23
52
  },
24
53
  {
25
54
  "name": "disabled-class",
26
- "description": "An optional attribute declaring the `class` to use when the field is disabled. Used in lieu of the `hidden` attribute.",
55
+ "fieldName": "disabledClass",
56
+ "description": "A single class token used instead of hidden on the unavailable wrapper. Native disabling still excludes managed fields from submission.",
27
57
  "type": {
28
58
  "text": "string"
29
59
  }
30
60
  },
31
61
  {
32
62
  "name": "enabled-class",
33
- "description": "An optional attribute declaring the `class` to use when the field is enabled.",
63
+ "fieldName": "enabledClass",
64
+ "description": "A single class token for the available wrapper. Does not replace hidden when disabled-class is absent.",
34
65
  "type": {
35
66
  "text": "string"
36
67
  }
@@ -48,12 +79,54 @@
48
79
  "declaration": {
49
80
  "name": "FormShowIfElement"
50
81
  }
82
+ }
83
+ ]
84
+ },
85
+ {
86
+ "kind": "javascript-module",
87
+ "path": "index.js",
88
+ "exports": [
89
+ {
90
+ "kind": "js",
91
+ "name": "FormShowIfElement",
92
+ "declaration": {
93
+ "name": "FormShowIfElement",
94
+ "module": "form-show-if.js"
95
+ }
96
+ }
97
+ ]
98
+ },
99
+ {
100
+ "kind": "javascript-module",
101
+ "path": "define.js",
102
+ "description": "Importing this module calls the guarded definition helper automatically.",
103
+ "declarations": [
104
+ {
105
+ "kind": "function",
106
+ "name": "defineFormShowIf",
107
+ "parameters": [
108
+ {
109
+ "name": "tagName",
110
+ "type": { "text": "string" },
111
+ "default": "'form-show-if'"
112
+ }
113
+ ],
114
+ "return": { "type": { "text": "boolean" } },
115
+ "description": "Registers the class when a browser custom-elements registry exists. Returns false without a registry, otherwise true. Existing registrations are retained."
116
+ }
117
+ ],
118
+ "exports": [
119
+ {
120
+ "kind": "js",
121
+ "name": "defineFormShowIf",
122
+ "declaration": { "name": "defineFormShowIf" }
51
123
  },
52
124
  {
53
125
  "kind": "custom-element-definition",
54
126
  "name": "form-show-if",
55
127
  "declaration": {
56
- "name": "FormShowIfElement"
128
+ "name": "FormShowIfElement",
129
+ "module": "form-show-if.js"
57
130
  }
58
131
  }
59
132
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aarongustafson/form-show-if",
3
- "version": "3.1.3",
3
+ "version": "3.1.4",
4
4
  "description": "Web component that enables you to make one HTML form field show (or not) based on the value(s) of another field.",
5
5
  "keywords": [
6
6
  "html",
@@ -59,16 +59,18 @@
59
59
  "test:ui": "vitest --ui",
60
60
  "test:run": "vitest run",
61
61
  "test:coverage": "vitest run --coverage",
62
+ "test:browser": "playwright test",
62
63
  "lint": "npm run lint:eslint && npm run lint:prettier",
63
64
  "format": "npm run format:eslint && npm run format:prettier",
64
65
  "lint:eslint": "eslint .",
65
66
  "format:eslint": "eslint . --fix",
66
- "lint:prettier": "prettier \"**/*.js\" --check --ignore-path .gitignore",
67
- "format:prettier": "prettier \"**/*.js\" --write --ignore-path .gitignore",
67
+ "lint:prettier": "prettier \"**/*.{js,ts,css,html,md,json,yml}\" --check --ignore-path .gitignore",
68
+ "format:prettier": "prettier \"**/*.{js,ts,css,html,md,json,yml}\" --write --ignore-path .gitignore",
68
69
  "version": "node scripts/update-demo-versions.js && git add demo/esm.html",
69
70
  "postversion": "git push --follow-tags"
70
71
  },
71
72
  "devDependencies": {
73
+ "@playwright/test": "^1.64.0",
72
74
  "@open-wc/eslint-config": "^13.1.0",
73
75
  "@testing-library/dom": "^10.4.2",
74
76
  "@testing-library/user-event": "^14.6.7",