@ramstack/alpinegear-when 1.2.3 → 1.3.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.
- package/README.md +75 -20
- package/alpinegear-when.esm.js +2 -2
- package/alpinegear-when.esm.min.js +1 -1
- package/alpinegear-when.js +2 -2
- package/alpinegear-when.min.js +1 -1
- package/package.json +7 -2
package/README.md
CHANGED
|
@@ -9,6 +9,10 @@ This directive is used for conditionally rendering elements on the page, similar
|
|
|
9
9
|
However, unlike `x-if`, it supports multiple root elements inside the `<template>` tag.
|
|
10
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
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
|
+
|
|
12
16
|
## Installation
|
|
13
17
|
|
|
14
18
|
### Using CDN
|
|
@@ -19,7 +23,7 @@ To include the CDN version of this plugin, add the following `<script>` tag befo
|
|
|
19
23
|
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-when@1/alpinegear-when.min.js" defer></script>
|
|
20
24
|
|
|
21
25
|
<!-- alpine.js -->
|
|
22
|
-
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3
|
|
26
|
+
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
|
|
23
27
|
```
|
|
24
28
|
|
|
25
29
|
### Using NPM
|
|
@@ -43,44 +47,95 @@ Alpine.start();
|
|
|
43
47
|
The `x-when` directive functions similarly to `x-if`, but allows multiple root elements in the `<template>` tag:
|
|
44
48
|
|
|
45
49
|
```html
|
|
46
|
-
<div x-data="{ show:
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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>
|
|
50
56
|
|
|
51
57
|
<template x-when="show">
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
<li>Orange</li>
|
|
59
|
+
<li>Grape</li>
|
|
60
|
+
<li>Mango</li>
|
|
54
61
|
</template>
|
|
62
|
+
</ul>
|
|
55
63
|
</div>
|
|
56
64
|
```
|
|
65
|
+
🚀 [Live demo | Alpine.js x-when: Multiple root elements](https://jsfiddle.net/rameel/91zhsLqp/)
|
|
57
66
|
|
|
58
67
|
### Using with `x-for`
|
|
59
68
|
The `x-when` directive can also be used with the directive `x-for` to conditionally render multiple items:
|
|
60
69
|
|
|
61
70
|
```html
|
|
62
71
|
<div x-data="{
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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>
|
|
76
88
|
</div>
|
|
77
89
|
```
|
|
90
|
+
🚀 [Live demo | Alpine.js x-when: Multiple root elements with x-for](https://jsfiddle.net/rameel/cuoh3297/)
|
|
78
91
|
|
|
79
92
|
## Source code
|
|
80
93
|
You can find the source code for this plugin on GitHub:
|
|
81
94
|
|
|
82
95
|
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/when
|
|
83
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
|
+
|
|
84
139
|
## Contributions
|
|
85
140
|
Bug reports and contributions are welcome.
|
|
86
141
|
|
package/alpinegear-when.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const warn = (...args) => console.warn("alpinegear.js:", ...args);
|
|
2
|
-
const is_template = el => el
|
|
2
|
+
const is_template = el => el.matches("template");
|
|
3
3
|
const is_element = el => el.nodeType === Node.ELEMENT_NODE;
|
|
4
4
|
|
|
5
5
|
function anchor_block(el, template, { addScopeToNode, cleanup, initTree, mutateDom, scope = {} }) {
|
|
@@ -85,4 +85,4 @@ function plugin({ addScopeToNode, directive, initTree, mutateDom }) {
|
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
export { plugin as when };
|
|
88
|
+
export { plugin as when };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=e=>e
|
|
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};
|
package/alpinegear-when.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
const warn = (...args) => console.warn("alpinegear.js:", ...args);
|
|
5
|
-
const is_template = el => el
|
|
5
|
+
const is_template = el => el.matches("template");
|
|
6
6
|
const is_element = el => el.nodeType === Node.ELEMENT_NODE;
|
|
7
7
|
|
|
8
8
|
function anchor_block(el, template, { addScopeToNode, cleanup, initTree, mutateDom, scope = {} }) {
|
|
@@ -90,4 +90,4 @@
|
|
|
90
90
|
|
|
91
91
|
document.addEventListener("alpine:init", () => { Alpine.plugin(plugin); });
|
|
92
92
|
|
|
93
|
-
})();
|
|
93
|
+
})();
|
package/alpinegear-when.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){"use strict";const e=e=>e
|
|
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)})}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ramstack/alpinegear-when",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
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",
|
|
@@ -12,6 +12,11 @@
|
|
|
12
12
|
"url": "git+https://github.com/rameel/ramstack.alpinegear.js.git",
|
|
13
13
|
"directory": "src/plugins/when"
|
|
14
14
|
},
|
|
15
|
-
"keywords": [
|
|
15
|
+
"keywords": [
|
|
16
|
+
"alpine.js",
|
|
17
|
+
"alpinejs",
|
|
18
|
+
"alpinejs-directive",
|
|
19
|
+
"alpinejs-plugin"
|
|
20
|
+
],
|
|
16
21
|
"main": "dist/alpinegear-when.js"
|
|
17
22
|
}
|