@aarongustafson/form-show-if 2.0.4 → 3.0.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 (4) hide show
  1. package/README.md +20 -21
  2. package/define.js +15 -2
  3. package/index.js +0 -1
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -16,26 +16,12 @@ npm install @aarongustafson/form-show-if
16
16
 
17
17
  ## Usage
18
18
 
19
- ### Option 1: Auto-define the custom element (easiest)
20
-
21
- Import the package to automatically define the `<form-show-if>` custom element:
22
-
23
- ```javascript
24
- import '@aarongustafson/form-show-if';
25
- ```
26
-
27
- Or use the define-only script in HTML:
28
-
29
- ```html
30
- <script src="./node_modules/@aarongustafson/form-show-if/define.js" type="module"></script>
31
- ```
32
-
33
- ### Option 2: Import the class and define manually
19
+ ### Option 1: Import the class and define manually
34
20
 
35
21
  Import the class and define the custom element with your preferred tag name:
36
22
 
37
23
  ```javascript
38
- import { FormShowIfElement } from '@aarongustafson/form-show-if/form-show-if.js';
24
+ import { FormShowIfElement } from '@aarongustafson/form-show-if';
39
25
 
40
26
  // Define with default name
41
27
  customElements.define('form-show-if', FormShowIfElement);
@@ -44,13 +30,26 @@ customElements.define('form-show-if', FormShowIfElement);
44
30
  customElements.define('my-conditional-field', FormShowIfElement);
45
31
  ```
46
32
 
47
- ### Option 3: Import everything (class + auto-define)
33
+ ### Option 2: Auto-define the custom element (browser environments only)
48
34
 
49
- Get both the class export and automatic element definition:
35
+ Use the guarded definition helper to register the element when `customElements` is available:
50
36
 
51
37
  ```javascript
52
- import { FormShowIfElement } from '@aarongustafson/form-show-if';
53
- // The custom element is now defined AND you have access to the class
38
+ import '@aarongustafson/form-show-if/define.js';
39
+ ```
40
+
41
+ If you prefer to control when the element is registered, call the helper directly:
42
+
43
+ ```javascript
44
+ import { defineFormShowIf } from '@aarongustafson/form-show-if/define.js';
45
+
46
+ defineFormShowIf();
47
+ ```
48
+
49
+ You can also include the guarded script from HTML:
50
+
51
+ ```html
52
+ <script src="./node_modules/@aarongustafson/form-show-if/define.js" type="module"></script>
54
53
  ```
55
54
 
56
55
  ### CDN Usage
@@ -58,7 +57,7 @@ import { FormShowIfElement } from '@aarongustafson/form-show-if';
58
57
  You can also use the component directly from a CDN:
59
58
 
60
59
  ```html
61
- <script src="https://unpkg.com/@aarongustafson/form-show-if/define.js" type="module"></script>
60
+ <script src="https://unpkg.com/@aarongustafson/form-show-if@latest/define.js" type="module"></script>
62
61
  ```
63
62
 
64
63
  ## API
package/define.js CHANGED
@@ -1,5 +1,18 @@
1
1
  import { FormShowIfElement } from './form-show-if.js';
2
2
 
3
- if (!!customElements) {
4
- customElements.define('form-show-if', FormShowIfElement);
3
+ export function defineFormShowIf(tagName = 'form-show-if') {
4
+ const hasWindow = typeof window !== 'undefined';
5
+ const registry = hasWindow ? window.customElements : undefined;
6
+
7
+ if (!registry || typeof registry.define !== 'function') {
8
+ return false;
9
+ }
10
+
11
+ if (!registry.get(tagName)) {
12
+ registry.define(tagName, FormShowIfElement);
13
+ }
14
+
15
+ return true;
5
16
  }
17
+
18
+ defineFormShowIf();
package/index.js CHANGED
@@ -1,2 +1 @@
1
1
  export { FormShowIfElement } from './form-show-if.js';
2
- import './define.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aarongustafson/form-show-if",
3
- "version": "2.0.4",
3
+ "version": "3.0.0",
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",