@aarongustafson/form-show-if 3.1.3 → 3.1.5

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
@@ -1,8 +1,14 @@
1
1
  # Show If Web Component
2
2
 
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)
3
+ [![npm version](https://img.shields.io/npm/v/@aarongustafson/form-show-if.svg)](https://www.npmjs.com/package/@aarongustafson/form-show-if)
4
+ [![CI status](https://img.shields.io/github/actions/workflow/status/aarongustafson/form-show-if/ci.yml?branch=main&label=CI)](https://github.com/aarongustafson/form-show-if/actions/workflows/ci.yml)
5
+ [![Minified + gzipped size](https://img.shields.io/bundlejs/size/@aarongustafson/form-show-if?format=minzip&label=min%2Bgzip)](https://bundlejs.com/?q=@aarongustafson/form-show-if)
6
+ [![License](https://img.shields.io/npm/l/@aarongustafson/form-show-if)](https://github.com/aarongustafson/form-show-if/blob/main/LICENSE)
7
+ [![Type definitions](https://img.shields.io/npm/types/@aarongustafson/form-show-if)](https://github.com/aarongustafson/form-show-if/blob/main/form-show-if.d.ts)
4
8
 
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.
9
+ `form-show-if` shows or hides native form fields according to other fields’ values.
10
+ It uses light DOM, native `hidden` and `disabled` behavior, and authored labels.
11
+ Before enhancement, the fields remain available unless your markup or CSS hides them.
6
12
 
7
13
  ## TypeScript Support
8
14
 
@@ -46,7 +52,9 @@ Use the guarded definition helper to register the element when `customElements`
46
52
  import '@aarongustafson/form-show-if/define.js';
47
53
  ```
48
54
 
49
- If you prefer to control when the element is registered, call the helper directly:
55
+ Importing `define.js` registers the default name immediately when a browser
56
+ registry is available, even when you import the helper by name. You can also call
57
+ the helper again; it preserves an existing registration:
50
58
 
51
59
  ```javascript
52
60
  import { defineFormShowIf } from '@aarongustafson/form-show-if/define.js';
@@ -57,7 +65,10 @@ defineFormShowIf();
57
65
  You can also include the guarded script from HTML:
58
66
 
59
67
  ```html
60
- <script src="./node_modules/@aarongustafson/form-show-if/define.js" type="module"></script>
68
+ <script
69
+ src="./node_modules/@aarongustafson/form-show-if/define.js"
70
+ type="module"
71
+ ></script>
61
72
  ```
62
73
 
63
74
  ### CDN Usage
@@ -65,38 +76,69 @@ You can also include the guarded script from HTML:
65
76
  You can also use the component directly from a CDN:
66
77
 
67
78
  ```html
68
- <script src="https://unpkg.com/@aarongustafson/form-show-if@latest/define.js" type="module"></script>
79
+ <script
80
+ src="https://unpkg.com/@aarongustafson/form-show-if@latest/define.js"
81
+ type="module"
82
+ ></script>
69
83
  ```
70
84
 
71
85
  ## API
72
86
 
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>
87
+ | Attribute | Property | Default | Behavior |
88
+ | ---------------- | --------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
89
+ | `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. |
90
+ | `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. |
91
+ | `enabled-class` | `enabledClass` | `null` | A single class token applied to the available wrapper. Alone, this does **not** replace native hiding when unavailable. |
92
+
93
+ These string properties reflect their attributes. Setting a property to `null`
94
+ removes the attribute. Native `input`, `change`, and form `reset` events trigger
95
+ reevaluation; changing `conditions` also reevaluates an initialized component.
96
+ The component does not emit custom events.
97
+
98
+ Set `disabled-class` and `enabled-class` in markup before initialization.
99
+ Their properties reflect later attribute changes, but those changes do not
100
+ fully migrate existing DOM state: adding `disabled-class` does not clear an
101
+ existing `hidden` attribute, and changing or removing a class token does not
102
+ remove previously applied tokens. A field can stay hidden or retain stale
103
+ state styling after its conditions match. Reconnection alone does not clear
104
+ those stale attributes or classes. Prefer configuring the class tokens before
105
+ upgrade; if you change them later, explicitly reconcile the old classes and
106
+ visibility while preserving entered values and focus.
107
+
108
+ Conditions support OR, not AND. Names and values cannot contain the `||` or `=`
109
+ delimiters; there is no escaping syntax. Comparisons use current values, not the
110
+ referenced control’s enabled state. A disabled controller can retain a value that
111
+ still matches. Prefer direct dependencies on an available controller instead of
112
+ assuming that hiding one dependent field will disable its dependents too.
78
113
 
79
114
  ## Markup Assumptions
80
115
 
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.
116
+ Put each dependent field and its label inside a `form-show-if`. The component
117
+ manages the first eligible `input`, `select`, or `textarea`, plus same-name
118
+ siblings inside the component. It does not disable every differently named
119
+ descendant. Use a separate wrapper for each dependent field.
82
120
 
83
- ## Implementation notes
121
+ The wrapper is the nearest ancestor inside the component that contains a label,
122
+ or the component itself. Keep both the label and field inside that boundary.
84
123
 
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.
124
+ Referenced controls are looked up in the nearest form, or the document body when
125
+ there is no form. Missing names do not match; a later input/change can find newly
126
+ added controls. Successful lookups are cached. If you replace a referenced node,
127
+ reconnect the component or change its conditions to clear that cache.
86
128
 
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.
129
+ ## Implementation notes
88
130
 
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
131
+ Initialization waits for an animation frame so authored children are available.
132
+ Unavailable managed fields are disabled and excluded from native FormData and
133
+ constraint validation; their values are not cleared. When a condition matches,
134
+ the fields are enabled and their wrapper is revealed unless `disabled-class`
135
+ delegates visibility to your styles.
92
136
 
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
137
+ `enabled-class` is added only while available; `disabled-class` is added only while
138
+ unavailable. The component does not supply either class’s styles. Disconnection
139
+ cancels pending initialization, removes delegated listeners, and clears cached
140
+ references. Reconnection reevaluates the existing fields; disconnection alone
141
+ does not undo their last `hidden` or `disabled` state.
100
142
 
101
143
  ## Examples
102
144
 
@@ -104,15 +146,15 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
104
146
 
105
147
  ```html
106
148
  <form>
107
- <label for="email">Email</label>
108
- <input type="email" id="email" name="email">
149
+ <label for="email">Email</label>
150
+ <input type="email" id="email" name="email" />
109
151
 
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>
152
+ <form-show-if conditions="email=*">
153
+ <label for="phone">Phone (shown if email provided)</label>
154
+ <input type="tel" id="phone" name="phone" />
155
+ </form-show-if>
114
156
 
115
- <button type="submit">Submit</button>
157
+ <button type="submit">Submit</button>
116
158
  </form>
117
159
  ```
118
160
 
@@ -120,8 +162,8 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
120
162
 
121
163
  ```html
122
164
  <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">
165
+ <label for="name">Name (shown if email OR phone provided)</label>
166
+ <input type="text" id="name" name="name" />
125
167
  </form-show-if>
126
168
  ```
127
169
 
@@ -129,8 +171,8 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
129
171
 
130
172
  ```html
131
173
  <form-show-if conditions="contact-method=email">
132
- <label for="email">Email Address</label>
133
- <input type="email" id="email" name="email">
174
+ <label for="email">Email Address</label>
175
+ <input type="email" id="email" name="email" />
134
176
  </form-show-if>
135
177
  ```
136
178
 
@@ -138,23 +180,97 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
138
180
 
139
181
  ```html
140
182
  <form-show-if conditions="newsletter=yes">
141
- <label for="email">Email (shown for newsletter)</label>
142
- <input type="email" id="email" name="email">
183
+ <label for="email">Email (shown for newsletter)</label>
184
+ <input type="email" id="email" name="email" />
143
185
  </form-show-if>
144
186
  ```
145
187
 
146
188
  ### Using Custom Classes
147
189
 
148
190
  ```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">
191
+ <form-show-if
192
+ conditions="email=*"
193
+ disabled-class="disabled"
194
+ enabled-class="enabled"
195
+ >
196
+ <label for="phone"
197
+ >Phone
198
+ <input
199
+ type="tel"
200
+ id="phone"
201
+ name="phone"
202
+ aria-describedby="phone-state"
203
+ />
204
+ </label>
205
+ <small id="phone-state">
206
+ <span class="state-on">Available</span>
207
+ <span class="state-off">Unavailable: enter an email above</span>
208
+ </small>
152
209
  </form-show-if>
153
210
  ```
154
211
 
212
+ The demo styles keep reading colors paired. Solid/dashed outlines and authored
213
+ availability text convey the state; green/red accents are supplementary:
214
+
215
+ ```css
216
+ .enabled label,
217
+ [disabled-class='disabled'] label,
218
+ .disabled label {
219
+ outline: 2px solid currentColor;
220
+ outline-offset: 0.25rem;
221
+ background: Canvas;
222
+ color: CanvasText;
223
+ border-inline-start: 0.25rem solid #17652e;
224
+ padding: 0.75rem;
225
+ }
226
+ .disabled label {
227
+ outline-style: dashed;
228
+ border-inline-start-color: #982b39;
229
+ }
230
+ .state-off,
231
+ .disabled .state-on {
232
+ display: none;
233
+ }
234
+ .disabled .state-off {
235
+ display: inline;
236
+ }
237
+ ```
238
+
239
+ Translate or replace the authored labels and status text as needed. The component
240
+ generates no user-facing strings. Keeping descriptions outside labels preserves
241
+ stable accessible names; `aria-describedby` associates the current explanation.
242
+ Without enhancement, the fields remain usable and the default “Available” text
243
+ remains truthful.
244
+
245
+ ## Styling and accessibility
246
+
247
+ The component injects no CSS, colors, focus ring, shadow DOM, or CSS parts.
248
+ Use normal light-DOM selectors and the optional state classes. Pair backgrounds
249
+ and foregrounds, retain readable labels and necessary boundaries, and do not rely
250
+ only on color or faded opacity to convey availability. The demos use Canvas
251
+ reading pairs, ButtonFace/ButtonText controls, and Field/FieldText inputs, with
252
+ foreground-colored necessary borders and browser-native focus. Supporting footer
253
+ text uses an 80% CanvasText/Canvas mix with a Canvas backing and full-strength
254
+ forced-color/increased-contrast fallback.
255
+
256
+ Do not override native hiding with an unconditional `form-show-if { display:
257
+ block; }`. Use `form-show-if:not([hidden])` if you need a block wrapper.
258
+
259
+ Revealing fields does not move focus. Native keyboard navigation reaches enabled
260
+ fields and skips disabled ones. If application code invalidates a focused field
261
+ programmatically, move focus to a relevant visible control before hiding or
262
+ disabling it. Custom-class availability text is not an automatic live announcement;
263
+ provide deliberate, restrained announcements if your application needs them.
264
+
265
+ Keep required instructions outside conditionally hidden wrappers. Authored fields
266
+ remain visible and usable without JavaScript or after an import failure unless
267
+ your own markup/CSS hides them. Do not pre-hide essential fields merely to avoid
268
+ the initial enhancement transition.
269
+
155
270
  ## Browser Support
156
271
 
157
272
  This web component works in all modern browsers that support:
273
+
158
274
  - Custom Elements v1
159
275
  - ES Modules (for module usage)
160
276
 
@@ -176,8 +292,24 @@ npm run test:ui
176
292
 
177
293
  # Run tests with coverage
178
294
  npm run test:coverage
295
+
296
+ # Install browser engines once, then run real-browser checks
297
+ npx playwright install chromium firefox webkit
298
+ npm run test:browser
179
299
  ```
180
300
 
301
+ Browser tests substitute the study’s 19 distinct palettes and cover native
302
+ conditions, form submission values, reset, keyboard focus, non-color status,
303
+ reflow, and no-script/module-failure paths. Palette substitution does not
304
+ reproduce an operating system’s forced-color painting. Firefox custom colors,
305
+ real Windows Contrast themes, platform zoom, mobile hardware, and manual
306
+ assistive-technology acceptance still need testing; automated checks do not
307
+ establish conformance.
308
+
309
+ Reflow checks cover 320–1400px viewports and enlarged root text at 320px, with
310
+ measured field widths rather than only page-overflow assertions. Enlarging root
311
+ text is not a substitute for testing browser zoom.
312
+
181
313
  ### Linting and Formatting
182
314
 
183
315
  ```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.5",
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",