@ramstack/alpinegear-when 1.4.2 → 1.4.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
@@ -1,144 +1,111 @@
1
- # @ramstack/alpinegear-when
2
- [![NPM](https://img.shields.io/npm/v/@ramstack/alpinegear-when)](https://www.npmjs.com/package/@ramstack/alpinegear-when)
3
- [![MIT](https://img.shields.io/github/license/rameel/ramstack.alpinegear.js)](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE)
4
-
5
- `@ramstack/alpinegear-when` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-when` directive.
6
-
7
- This directive is used for conditionally rendering elements on the page, similar to `x-if`.
8
-
9
- However, unlike `x-if`, it supports multiple root elements inside the `<template>` tag.
10
- Essentially, `x-when` works like a combination of `x-if` and [x-fragment](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/fragment), but with cleaner syntax. There's no need to nest multiple `<template>` tags, which makes the markup simpler and easier to manage.
11
-
12
- > [!Note]
13
- > This package is part of the **[`@ramstack/alpinegear-main`](https://www.npmjs.com/package/@ramstack/alpinegear-main)** bundle.
14
- > If you are using the main bundle, you don't need to install this package separately.
15
-
16
- ## Installation
17
-
18
- ### Using CDN
19
- To include the CDN version of this plugin, add the following `<script>` tag before the core `alpine.js` file:
20
-
21
- ```html
22
- <!-- alpine.js plugin -->
23
- <script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-when@1/alpinegear-when.min.js" defer></script>
24
-
25
- <!-- alpine.js -->
26
- <script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
27
- ```
28
-
29
- ### Using NPM
30
- Alternatively, you can install the plugin via `npm`:
31
-
32
- ```bash
33
- npm install --save @ramstack/alpinegear-when
34
- ```
35
-
36
- Then initialize it in your bundle:
37
-
38
- ```js
39
- import Alpine from "alpinejs";
40
- import when from "@ramstack/alpinegear-when";
41
-
42
- Alpine.plugin(when);
43
- Alpine.start();
44
- ```
45
-
46
- ## Usage
47
- The `x-when` directive functions similarly to `x-if`, but allows multiple root elements in the `<template>` tag:
48
-
49
- ```html
50
- <div x-data="{ show: false }">
51
- <button @click="show = !show">Show more</button>
52
-
53
- <ul>
54
- <li>Apple</li>
55
- <li>Banana</li>
56
-
57
- <template x-when="show">
58
- <li>Orange</li>
59
- <li>Grape</li>
60
- <li>Mango</li>
61
- </template>
62
- </ul>
63
- </div>
64
- ```
65
- 🚀 [Live demo | Alpine.js x-when: Multiple root elements](https://jsfiddle.net/rameel/91zhsLqp/)
66
-
67
- ### Using with `x-for`
68
- The `x-when` directive can also be used with the directive `x-for` to conditionally render multiple items:
69
-
70
- ```html
71
- <div x-data="{
72
- items: [
73
- { term: 'Star', description: 'Luminous plasma sphere.' },
74
- { term: 'Planet', description: 'Body orbiting a star.' },
75
- { term: 'Galaxy', description: 'Stars and dust system.' },
76
- { term: 'Nebula', description: 'Cloud of gas in space.' }
77
- ]}">
78
- <button @click="items.reverse()">Reverse</button>
79
-
80
- <dl>
81
- <template x-for="item in items" :key="item.term">
82
- <template x-when="true">
83
- <dt x-text="item.term"></dt>
84
- <dd x-text="item.description"></dd>
85
- </template>
86
- </template>
87
- </dl>
88
- </div>
89
- ```
90
- 🚀 [Live demo | Alpine.js x-when: Multiple root elements with x-for](https://jsfiddle.net/rameel/cuoh3297/)
91
-
92
- ## Source code
93
- You can find the source code for this plugin on GitHub:
94
-
95
- https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/when
96
-
97
- ## Related projects
98
-
99
- **[@ramstack/alpinegear-main](https://www.npmjs.com/package/@ramstack/alpinegear-main)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/main))<br>
100
- Provides a combined plugin that includes several useful directives.
101
- This package aggregates multiple individual plugins, offering a convenient all-in-one bundle.
102
- Included directives: `x-bound`, `x-format`, `x-fragment`, `x-match`, `x-template`, and `x-when`.
103
-
104
- **[@ramstack/alpinegear-bound](https://www.npmjs.com/package/@ramstack/alpinegear-bound)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/bound))<br>
105
- Provides the `x-bound` directive, which allows for two-way binding of input elements and their associated data properties.
106
- It works similarly to the binding provided by [Svelte](https://svelte.dev/docs/element-directives#bind-property)
107
- and also supports synchronizing values between two `Alpine.js` data properties.
108
-
109
- **[@ramstack/alpinegear-format](https://www.npmjs.com/package/@ramstack/alpinegear-format)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/format))<br>
110
- Provides the `x-format` directive, which allows you to easily interpolate text using a template syntax similar to what's available in `Vue.js`.
111
-
112
- **[@ramstack/alpinegear-template](https://www.npmjs.com/package/@ramstack/alpinegear-template)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/template))<br>
113
- Provides the `x-template` directive, which allows you to define a template once anywhere in the DOM and reference it by its ID.
114
-
115
- **[@ramstack/alpinegear-fragment](https://www.npmjs.com/package/@ramstack/alpinegear-fragment)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/fragment))<br>
116
- Provides the `x-fragment` directive, which allows for fragment-like behavior similar to what's available in frameworks
117
- like `Vue.js` or `React`, where multiple root elements can be grouped together.
118
-
119
- **[@ramstack/alpinegear-match](https://www.npmjs.com/package/@ramstack/alpinegear-match)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/match))<br>
120
- Provides the `x-match` directive, which functions similarly to the `switch` statement in many programming languages,
121
- allowing you to conditionally render elements based on matching cases.
122
-
123
- **[@ramstack/alpinegear-destroy](https://www.npmjs.com/package/@ramstack/alpinegear-destroy)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/destroy))<br>
124
- Provides the `x-destroy` directive, which is the opposite of `x-init` and allows you to hook into the cleanup phase
125
- of any element, running a callback when the element is removed from the DOM.
126
-
127
- **[@ramstack/alpinegear-hotkey](https://www.npmjs.com/package/@ramstack/alpinegear-hotkey)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/hotkey))<br>
128
- Provides the `x-hotkey` directive, which allows you to easily handle keyboard shortcuts within your Alpine.js components or application.
129
-
130
- **[@ramstack/alpinegear-router](https://www.npmjs.com/package/@ramstack/alpinegear-router)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/router))<br>
131
- Provides the `x-router` and `x-route` directives, which enable client-side navigation and routing functionality within your Alpine.js application.
132
-
133
- **[@ramstack/alpinegear-dialog](https://www.npmjs.com/package/@ramstack/alpinegear-dialog)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/dialog))<br>
134
- Provides a headless dialog directive for Alpine.js based on the native HTML `<dialog>` element.
135
- It supports declarative composition, value-based close semantics, and both modal and non-modal dialogs,
136
- with optional Promise-based imperative control.
137
-
138
-
139
- ## Contributions
140
- Bug reports and contributions are welcome.
141
-
142
- ## License
143
- This package is released as open source under the **MIT License**.
144
- See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
1
+ # @ramstack/alpinegear-when
2
+ [![NPM](https://img.shields.io/npm/v/@ramstack/alpinegear-when)](https://www.npmjs.com/package/@ramstack/alpinegear-when)
3
+ [![MIT](https://img.shields.io/github/license/rameel/ramstack.alpinegear.js)](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE)
4
+
5
+ `@ramstack/alpinegear-when` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-when` directive.
6
+
7
+ This directive is used for conditionally rendering elements on the page, similar to `x-if`.
8
+
9
+ However, unlike `x-if`, it supports multiple root elements inside the `<template>` tag.
10
+ Essentially, `x-when` works like a combination of `x-if` and [x-fragment](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/fragment), but with cleaner syntax. There's no need to nest multiple `<template>` tags, which makes the markup simpler and easier to manage.
11
+
12
+ > [!Note]
13
+ > This package is part of the **[`@ramstack/alpinegear-main`](https://www.npmjs.com/package/@ramstack/alpinegear-main)** bundle.
14
+ > If you are using the main bundle, you don't need to install this package separately.
15
+
16
+ ## Installation
17
+
18
+ ### Using CDN
19
+ To include the CDN version of this plugin, add the following `<script>` tag before the core `alpine.js` file:
20
+
21
+ ```html
22
+ <!-- alpine.js plugin -->
23
+ <script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-when@1/alpinegear-when.min.js" defer></script>
24
+
25
+ <!-- alpine.js -->
26
+ <script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
27
+ ```
28
+
29
+ ### Using NPM
30
+ Alternatively, you can install the plugin via `npm`:
31
+
32
+ ```bash
33
+ npm install --save @ramstack/alpinegear-when
34
+ ```
35
+
36
+ Then initialize it in your bundle:
37
+
38
+ ```js
39
+ import Alpine from "alpinejs";
40
+ import when from "@ramstack/alpinegear-when";
41
+
42
+ Alpine.plugin(when);
43
+ Alpine.start();
44
+ ```
45
+
46
+ ## Usage
47
+ The `x-when` directive functions similarly to `x-if`, but allows multiple root elements in the `<template>` tag:
48
+
49
+ ```html
50
+ <div x-data="{ show: false }">
51
+ <button @click="show = !show">Show more</button>
52
+
53
+ <ul>
54
+ <li>Apple</li>
55
+ <li>Banana</li>
56
+
57
+ <template x-when="show">
58
+ <li>Orange</li>
59
+ <li>Grape</li>
60
+ <li>Mango</li>
61
+ </template>
62
+ </ul>
63
+ </div>
64
+ ```
65
+ 🚀 [Live demo | Alpine.js x-when: Multiple root elements](https://jsfiddle.net/rameel/91zhsLqp/)
66
+
67
+ ### Using with `x-for`
68
+ The `x-when` directive can also be used with the directive `x-for` to conditionally render multiple items:
69
+
70
+ ```html
71
+ <div x-data="{
72
+ items: [
73
+ { term: 'Star', description: 'Luminous plasma sphere.' },
74
+ { term: 'Planet', description: 'Body orbiting a star.' },
75
+ { term: 'Galaxy', description: 'Stars and dust system.' },
76
+ { term: 'Nebula', description: 'Cloud of gas in space.' }
77
+ ]}">
78
+ <button @click="items.reverse()">Reverse</button>
79
+
80
+ <dl>
81
+ <template x-for="item in items" :key="item.term">
82
+ <template x-when="true">
83
+ <dt x-text="item.term"></dt>
84
+ <dd x-text="item.description"></dd>
85
+ </template>
86
+ </template>
87
+ </dl>
88
+ </div>
89
+ ```
90
+ 🚀 [Live demo | Alpine.js x-when: Multiple root elements with x-for](https://jsfiddle.net/rameel/cuoh3297/)
91
+
92
+ ## Source code
93
+ You can find the source code for this plugin on GitHub:
94
+
95
+ https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/when
96
+
97
+
98
+ ## Related packages
99
+ This package is part of **[AlpineGear](https://github.com/rameel/ramstack.alpinegear.js)** —
100
+ a collection of utilities and directives for [Alpine.js](https://alpinejs.dev).
101
+
102
+ You can find the full list of related packages and their documentation here:
103
+ https://github.com/rameel/ramstack.alpinegear.js
104
+
105
+
106
+ ## Contributions
107
+ Bug reports and contributions are welcome.
108
+
109
+ ## License
110
+ This package is released as open source under the **MIT License**.
111
+ See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
@@ -1,60 +1,60 @@
1
- const warn = (...args) => console.warn("alpinegear.js:", ...args);
2
- const is_template = el => el.matches("template");
1
+ const warn = (...args) => console.warn("alpinegear.js:", ...args);
2
+ const is_template = el => el.matches("template");
3
3
  const is_element = el => el.nodeType === Node.ELEMENT_NODE;
4
4
 
5
- function anchor_block(el, template, { addScopeToNode, cleanup, initTree, mutateDom, scope = {} }) {
6
- if (el._r_block) {
7
- return;
8
- }
9
-
10
- initialize();
11
-
12
- let nodes = is_template(template)
13
- ? [...template.content.cloneNode(true).childNodes]
14
- : [template.cloneNode(true)];
15
-
16
- mutateDom(() => {
17
- for (let node of nodes) {
18
- is_element(node) && addScopeToNode(node, scope, el);
19
- el.parentElement.insertBefore(node, el);
20
- is_element(node) && initTree(node);
21
- }
22
- });
23
-
24
- el._r_block = {
25
- template,
26
- update() {
27
- mutateDom(() => {
28
- for (let node of nodes ?? []) {
29
- el.parentElement.insertBefore(node, el);
30
- }
31
- });
32
- },
33
- delete() {
34
- el._r_block = null;
35
- for (let node of nodes ?? []) {
36
- node.remove();
37
- }
38
- nodes = null;
39
- }
40
- };
41
-
42
- cleanup(() => el._r_block?.delete());
43
- }
44
-
45
- function initialize() {
46
- document.body._r_block ??= (() => {
47
- const observer = new MutationObserver(mutations => {
48
- for (let mutation of mutations) {
49
- for (let node of mutation.addedNodes) {
50
- node._r_block?.update();
51
- }
52
- }
53
- });
54
-
55
- observer.observe(document.body, { childList: true, subtree: true });
56
- return observer;
57
- })();
5
+ function anchor_block(el, template, { addScopeToNode, cleanup, initTree, mutateDom, scope = {} }) {
6
+ if (el._r_block) {
7
+ return;
8
+ }
9
+
10
+ initialize();
11
+
12
+ let nodes = is_template(template)
13
+ ? [...template.content.cloneNode(true).childNodes]
14
+ : [template.cloneNode(true)];
15
+
16
+ mutateDom(() => {
17
+ for (let node of nodes) {
18
+ is_element(node) && addScopeToNode(node, scope, el);
19
+ el.parentElement.insertBefore(node, el);
20
+ is_element(node) && initTree(node);
21
+ }
22
+ });
23
+
24
+ el._r_block = {
25
+ template,
26
+ update() {
27
+ mutateDom(() => {
28
+ for (let node of nodes ?? []) {
29
+ el.parentElement.insertBefore(node, el);
30
+ }
31
+ });
32
+ },
33
+ delete() {
34
+ el._r_block = null;
35
+ for (let node of nodes ?? []) {
36
+ node.remove();
37
+ }
38
+ nodes = null;
39
+ }
40
+ };
41
+
42
+ cleanup(() => el._r_block?.delete());
43
+ }
44
+
45
+ function initialize() {
46
+ document.body._r_block ??= (() => {
47
+ const observer = new MutationObserver(mutations => {
48
+ for (let mutation of mutations) {
49
+ for (let node of mutation.addedNodes) {
50
+ node._r_block?.update();
51
+ }
52
+ }
53
+ });
54
+
55
+ observer.observe(document.body, { childList: true, subtree: true });
56
+ return observer;
57
+ })();
58
58
  }
59
59
 
60
60
  function create_getter(evaluate_later, ...args) {
@@ -70,19 +70,19 @@ function has_getter(value) {
70
70
  return typeof value?.get === "function";
71
71
  }
72
72
 
73
- function plugin({ addScopeToNode, directive, initTree, mutateDom }) {
74
- directive("when", (el, { expression }, { cleanup, effect, evaluateLater }) => {
75
- if (!is_template(el)) {
76
- warn("x-when can only be used on a 'template' tag");
77
- return;
78
- }
79
-
80
- const activate = () => anchor_block(el, el, { addScopeToNode, cleanup, initTree, mutateDom });
81
- const clear = () => el._r_block?.delete();
82
-
83
- const get = create_getter(evaluateLater, expression);
84
- effect(() => get() ? activate() : clear());
85
- });
73
+ function plugin({ addScopeToNode, directive, initTree, mutateDom }) {
74
+ directive("when", (el, { expression }, { cleanup, effect, evaluateLater }) => {
75
+ if (!is_template(el)) {
76
+ warn("x-when can only be used on a 'template' tag");
77
+ return;
78
+ }
79
+
80
+ const activate = () => anchor_block(el, el, { addScopeToNode, cleanup, initTree, mutateDom });
81
+ const clear = () => el._r_block?.delete();
82
+
83
+ const get = create_getter(evaluateLater, expression);
84
+ effect(() => get() ? activate() : clear());
85
+ });
86
86
  }
87
87
 
88
- export { plugin as when };
88
+ export { plugin as default, plugin as when };
@@ -1 +1 @@
1
- const e=e=>e.matches("template"),o=e=>e.nodeType===Node.ELEMENT_NODE;function t({addScopeToNode:t,directive:n,initTree:r,mutateDom:l}){n("when",(n,{expression:c},{cleanup:d,effect:a,evaluateLater:u})=>{if(!e(n))return void console.warn("alpinegear.js:","x-when can only be used on a 'template' tag");const i=function(e,...o){const t=e(...o);return()=>{let e;return t(o=>e=o),o=e,"function"==typeof o?.get?e.get():e;var o}}(u,c);a(()=>i()?function(t,n,{addScopeToNode:r,cleanup:l,initTree:c,mutateDom:d,scope:a={}}){if(t._r_block)return;document.body._r_block??=(()=>{const e=new MutationObserver(e=>{for(let o of e)for(let e of o.addedNodes)e._r_block?.update()});return e.observe(document.body,{childList:!0,subtree:!0}),e})();let u=e(n)?[...n.content.cloneNode(!0).childNodes]:[n.cloneNode(!0)];d(()=>{for(let e of u)o(e)&&r(e,a,t),t.parentElement.insertBefore(e,t),o(e)&&c(e)}),t._r_block={template:n,update(){d(()=>{for(let e of u??[])t.parentElement.insertBefore(e,t)})},delete(){t._r_block=null;for(let e of u??[])e.remove();u=null}},l(()=>t._r_block?.delete())}(n,n,{addScopeToNode:t,cleanup:d,initTree:r,mutateDom:l}):n._r_block?.delete())})}export{t as when};
1
+ const e=e=>e.matches("template"),t=e=>e.nodeType===Node.ELEMENT_NODE;function o({addScopeToNode:o,directive:n,initTree:r,mutateDom:l}){n("when",(n,{expression:c},{cleanup:d,effect:a,evaluateLater:u})=>{if(!e(n))return void console.warn("alpinegear.js:","x-when can only be used on a 'template' tag");const i=function(e,...t){const o=e(...t);return()=>{let e;return o(t=>e=t),t=e,"function"==typeof t?.get?e.get():e;var t}}(u,c);a(()=>i()?function(o,n,{addScopeToNode:r,cleanup:l,initTree:c,mutateDom:d,scope:a={}}){if(o._r_block)return;document.body._r_block??=(()=>{const e=new MutationObserver(e=>{for(let t of e)for(let e of t.addedNodes)e._r_block?.update()});return e.observe(document.body,{childList:!0,subtree:!0}),e})();let u=e(n)?[...n.content.cloneNode(!0).childNodes]:[n.cloneNode(!0)];d(()=>{for(let e of u)t(e)&&r(e,a,o),o.parentElement.insertBefore(e,o),t(e)&&c(e)}),o._r_block={template:n,update(){d(()=>{for(let e of u??[])o.parentElement.insertBefore(e,o)})},delete(){o._r_block=null;for(let e of u??[])e.remove();u=null}},l(()=>o._r_block?.delete())}(n,n,{addScopeToNode:o,cleanup:d,initTree:r,mutateDom:l}):n._r_block?.delete())})}export{o as default,o as when};
@@ -1,63 +1,68 @@
1
1
  (function () {
2
2
  'use strict';
3
3
 
4
- const warn = (...args) => console.warn("alpinegear.js:", ...args);
5
- const is_template = el => el.matches("template");
4
+ const warn = (...args) => console.warn("alpinegear.js:", ...args);
5
+ const is_template = el => el.matches("template");
6
6
  const is_element = el => el.nodeType === Node.ELEMENT_NODE;
7
7
 
8
- function anchor_block(el, template, { addScopeToNode, cleanup, initTree, mutateDom, scope = {} }) {
9
- if (el._r_block) {
10
- return;
11
- }
12
-
13
- initialize();
14
-
15
- let nodes = is_template(template)
16
- ? [...template.content.cloneNode(true).childNodes]
17
- : [template.cloneNode(true)];
18
-
19
- mutateDom(() => {
20
- for (let node of nodes) {
21
- is_element(node) && addScopeToNode(node, scope, el);
22
- el.parentElement.insertBefore(node, el);
23
- is_element(node) && initTree(node);
24
- }
25
- });
26
-
27
- el._r_block = {
28
- template,
29
- update() {
30
- mutateDom(() => {
31
- for (let node of nodes ?? []) {
32
- el.parentElement.insertBefore(node, el);
33
- }
34
- });
35
- },
36
- delete() {
37
- el._r_block = null;
38
- for (let node of nodes ?? []) {
39
- node.remove();
40
- }
41
- nodes = null;
42
- }
43
- };
44
-
45
- cleanup(() => el._r_block?.delete());
46
- }
47
-
48
- function initialize() {
49
- document.body._r_block ??= (() => {
50
- const observer = new MutationObserver(mutations => {
51
- for (let mutation of mutations) {
52
- for (let node of mutation.addedNodes) {
53
- node._r_block?.update();
54
- }
55
- }
56
- });
57
-
58
- observer.observe(document.body, { childList: true, subtree: true });
59
- return observer;
60
- })();
8
+ const listen = (target, type, listener, options) => {
9
+ target.addEventListener(type, listener, options);
10
+ return () => target.removeEventListener(type, listener, options);
11
+ };
12
+
13
+ function anchor_block(el, template, { addScopeToNode, cleanup, initTree, mutateDom, scope = {} }) {
14
+ if (el._r_block) {
15
+ return;
16
+ }
17
+
18
+ initialize();
19
+
20
+ let nodes = is_template(template)
21
+ ? [...template.content.cloneNode(true).childNodes]
22
+ : [template.cloneNode(true)];
23
+
24
+ mutateDom(() => {
25
+ for (let node of nodes) {
26
+ is_element(node) && addScopeToNode(node, scope, el);
27
+ el.parentElement.insertBefore(node, el);
28
+ is_element(node) && initTree(node);
29
+ }
30
+ });
31
+
32
+ el._r_block = {
33
+ template,
34
+ update() {
35
+ mutateDom(() => {
36
+ for (let node of nodes ?? []) {
37
+ el.parentElement.insertBefore(node, el);
38
+ }
39
+ });
40
+ },
41
+ delete() {
42
+ el._r_block = null;
43
+ for (let node of nodes ?? []) {
44
+ node.remove();
45
+ }
46
+ nodes = null;
47
+ }
48
+ };
49
+
50
+ cleanup(() => el._r_block?.delete());
51
+ }
52
+
53
+ function initialize() {
54
+ document.body._r_block ??= (() => {
55
+ const observer = new MutationObserver(mutations => {
56
+ for (let mutation of mutations) {
57
+ for (let node of mutation.addedNodes) {
58
+ node._r_block?.update();
59
+ }
60
+ }
61
+ });
62
+
63
+ observer.observe(document.body, { childList: true, subtree: true });
64
+ return observer;
65
+ })();
61
66
  }
62
67
 
63
68
  function create_getter(evaluate_later, ...args) {
@@ -73,21 +78,21 @@
73
78
  return typeof value?.get === "function";
74
79
  }
75
80
 
76
- function plugin({ addScopeToNode, directive, initTree, mutateDom }) {
77
- directive("when", (el, { expression }, { cleanup, effect, evaluateLater }) => {
78
- if (!is_template(el)) {
79
- warn("x-when can only be used on a 'template' tag");
80
- return;
81
- }
82
-
83
- const activate = () => anchor_block(el, el, { addScopeToNode, cleanup, initTree, mutateDom });
84
- const clear = () => el._r_block?.delete();
85
-
86
- const get = create_getter(evaluateLater, expression);
87
- effect(() => get() ? activate() : clear());
88
- });
81
+ function plugin({ addScopeToNode, directive, initTree, mutateDom }) {
82
+ directive("when", (el, { expression }, { cleanup, effect, evaluateLater }) => {
83
+ if (!is_template(el)) {
84
+ warn("x-when can only be used on a 'template' tag");
85
+ return;
86
+ }
87
+
88
+ const activate = () => anchor_block(el, el, { addScopeToNode, cleanup, initTree, mutateDom });
89
+ const clear = () => el._r_block?.delete();
90
+
91
+ const get = create_getter(evaluateLater, expression);
92
+ effect(() => get() ? activate() : clear());
93
+ });
89
94
  }
90
95
 
91
- document.addEventListener("alpine:init", () => { Alpine.plugin(plugin); });
96
+ listen(document, "alpine:init", () => Alpine.plugin(plugin));
92
97
 
93
98
  })();
@@ -1 +1 @@
1
- !function(){"use strict";const e=e=>e.matches("template"),t=e=>e.nodeType===Node.ELEMENT_NODE;function o({addScopeToNode:o,directive:n,initTree:r,mutateDom:l}){n("when",(n,{expression:c},{cleanup:d,effect:a,evaluateLater:i})=>{if(!e(n))return void console.warn("alpinegear.js:","x-when can only be used on a 'template' tag");const u=function(e,...t){const o=e(...t);return()=>{let e;return o(t=>e=t),t=e,"function"==typeof t?.get?e.get():e;var t}}(i,c);a(()=>u()?function(o,n,{addScopeToNode:r,cleanup:l,initTree:c,mutateDom:d,scope:a={}}){if(o._r_block)return;document.body._r_block??=(()=>{const e=new MutationObserver(e=>{for(let t of e)for(let e of t.addedNodes)e._r_block?.update()});return e.observe(document.body,{childList:!0,subtree:!0}),e})();let i=e(n)?[...n.content.cloneNode(!0).childNodes]:[n.cloneNode(!0)];d(()=>{for(let e of i)t(e)&&r(e,a,o),o.parentElement.insertBefore(e,o),t(e)&&c(e)}),o._r_block={template:n,update(){d(()=>{for(let e of i??[])o.parentElement.insertBefore(e,o)})},delete(){o._r_block=null;for(let e of i??[])e.remove();i=null}},l(()=>o._r_block?.delete())}(n,n,{addScopeToNode:o,cleanup:d,initTree:r,mutateDom:l}):n._r_block?.delete())})}document.addEventListener("alpine:init",()=>{Alpine.plugin(o)})}();
1
+ !function(){"use strict";const e=e=>e.matches("template"),t=e=>e.nodeType===Node.ELEMENT_NODE;function o({addScopeToNode:o,directive:n,initTree:r,mutateDom:l}){n("when",(n,{expression:c},{cleanup:d,effect:i,evaluateLater:a})=>{if(!e(n))return void console.warn("alpinegear.js:","x-when can only be used on a 'template' tag");const u=function(e,...t){const o=e(...t);return()=>{let e;return o(t=>e=t),t=e,"function"==typeof t?.get?e.get():e;var t}}(a,c);i(()=>u()?function(o,n,{addScopeToNode:r,cleanup:l,initTree:c,mutateDom:d,scope:i={}}){if(o._r_block)return;document.body._r_block??=(()=>{const e=new MutationObserver(e=>{for(let t of e)for(let e of t.addedNodes)e._r_block?.update()});return e.observe(document.body,{childList:!0,subtree:!0}),e})();let a=e(n)?[...n.content.cloneNode(!0).childNodes]:[n.cloneNode(!0)];d(()=>{for(let e of a)t(e)&&r(e,i,o),o.parentElement.insertBefore(e,o),t(e)&&c(e)}),o._r_block={template:n,update(){d(()=>{for(let e of a??[])o.parentElement.insertBefore(e,o)})},delete(){o._r_block=null;for(let e of a??[])e.remove();a=null}},l(()=>o._r_block?.delete())}(n,n,{addScopeToNode:o,cleanup:d,initTree:r,mutateDom:l}):n._r_block?.delete())})}document.addEventListener("alpine:init",()=>Alpine.plugin(o),void 0)}();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ramstack/alpinegear-when",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "@ramstack/alpinegear-when provides additional directives, magic functions, and utilities for a more productive development experience",
5
5
  "author": {
6
6
  "name": "Rameel Burhan",
@@ -18,6 +18,12 @@
18
18
  "alpinejs-directive",
19
19
  "alpinejs-plugin"
20
20
  ],
21
- "main": "dist/alpinegear-when.js",
22
- "module": "dist/alpinegear-when.esm.js"
21
+ "exports": {
22
+ ".": {
23
+ "import": {
24
+ "production": "./alpinegear-when.esm.min.js",
25
+ "default": "./alpinegear-when.esm.js"
26
+ }
27
+ }
28
+ }
23
29
  }