@ramstack/alpinegear-match 1.0.0-preview.1
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 +101 -0
- package/alpinegear-match.esm.js +126 -0
- package/alpinegear-match.esm.min.js +1 -0
- package/alpinegear-match.js +131 -0
- package/alpinegear-match.min.js +1 -0
- package/package.json +18 -0
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# @ramstack/alpinegear-match
|
|
2
|
+
|
|
3
|
+
`@ramstack/alpinegear-match` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-match` directive.
|
|
4
|
+
|
|
5
|
+
This directive functions similarly to the `switch` statement in many programming languages, allowing you to conditionally render elements based on matching cases.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
### Using CDN
|
|
10
|
+
To include the CDN version of this plugin, add the following `<script>` tag before the core `alpine.js` file:
|
|
11
|
+
|
|
12
|
+
```html
|
|
13
|
+
<!-- alpine.js plugin -->
|
|
14
|
+
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-when@1/alpinegear-match.min.js" defer></script>
|
|
15
|
+
|
|
16
|
+
<!-- alpine.js -->
|
|
17
|
+
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Using NPM
|
|
21
|
+
Alternatively, you can install the plugin via `npm`:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install --save @ramstack/alpinegear-match
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Then initialize it in your bundle:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import Alpine from "alpinejs";
|
|
31
|
+
import match from "@ramstack/alpinegear-match";
|
|
32
|
+
|
|
33
|
+
Alpine.plugin(match);
|
|
34
|
+
Alpine.start();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
The `x-match` directive is similar to using multiple consecutive `x-if` or `x-when` directives. However, using multiple `x-if` or `x-when` can make your markup harder to read and lead to code bloat.
|
|
39
|
+
|
|
40
|
+
The `x-match` directive provides a cleaner solution by allowing you to define multiple blocks with conditions. The corresponding block will be displayed if its condition evaluates to true.
|
|
41
|
+
|
|
42
|
+
Here's a simple example solving the classic **FizzBuzz** game:
|
|
43
|
+
```html
|
|
44
|
+
<div x-data="{ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] }">
|
|
45
|
+
<template x-for="n in numbers">
|
|
46
|
+
<template x-match>
|
|
47
|
+
<div x-case="n % 3 == 0 && n % 5 == 0">Fizz Buzz</div>
|
|
48
|
+
<div x-case="n % 3 == 0">Fizz</div>
|
|
49
|
+
<div x-case="n % 5 == 0">Buzz</div>
|
|
50
|
+
<div x-default x-text="n"></div>
|
|
51
|
+
</template>
|
|
52
|
+
</template>
|
|
53
|
+
</div>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
While it's possible to wrap the loop's content in an additional `<div>` (since `x-for` only allows a single root element), or use a plugin like [@ramstack/alpinegear-fragment](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/fragment), to achieve similar results, the `x-match` directive provides a much cleaner and more readable approach. Additionally, it avoids introducing extra elements that are only needed to bypass these limitations.
|
|
57
|
+
|
|
58
|
+
For comparison, here's how the same **FizzBuzz** game might look using `x-fragment`:
|
|
59
|
+
|
|
60
|
+
```html
|
|
61
|
+
<div x-data="{ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] }">
|
|
62
|
+
<template x-for="n in numbers">
|
|
63
|
+
<div>
|
|
64
|
+
<template x-if="n % 3 == 0 && n % 5 == 0">
|
|
65
|
+
<div>Fizz Buzz</div>
|
|
66
|
+
</template>
|
|
67
|
+
<template x-if="n % 3 == 0">
|
|
68
|
+
<div>Fizz</div>
|
|
69
|
+
</template>
|
|
70
|
+
<template x-if="n % 5 == 0">
|
|
71
|
+
<div>Buzz</div>
|
|
72
|
+
</template>
|
|
73
|
+
<template x-if="n % 3 != 0 && n % 5 != 0">
|
|
74
|
+
<div x-text="n"></div>
|
|
75
|
+
</template>
|
|
76
|
+
</div>
|
|
77
|
+
</template>
|
|
78
|
+
</div>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
> [!IMPORTANT]
|
|
82
|
+
> Ensure that `x-case` conditions are ordered from most specific to least specific. Otherwise, a more general case might intercept the condition, causing subsequent cases not to execute.
|
|
83
|
+
|
|
84
|
+
> [!NOTE]
|
|
85
|
+
> The `x-default` branch is optional and only renders if none of the `x-case` conditions evaluate to `true`.
|
|
86
|
+
|
|
87
|
+
> [!TIP]
|
|
88
|
+
> The `x-case` directive can be applied to regular HTML tags or `<template>` tags. When used with `<template>`, you can define multiple root elements, and all will be rendered.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
## Source code
|
|
92
|
+
You can find the source code for this plugin on GitHub:
|
|
93
|
+
|
|
94
|
+
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/match
|
|
95
|
+
|
|
96
|
+
## Contributions
|
|
97
|
+
Bug reports and contributions are welcome.
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
This package is released as open source under the **MIT License**.
|
|
101
|
+
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
const warn = (...args) => console.warn("alpine-gear.js:", ...args);
|
|
2
|
+
const is_template = el => el instanceof HTMLTemplateElement;
|
|
3
|
+
const is_element = el => el.nodeType === Node.ELEMENT_NODE;
|
|
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
|
+
})();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function create_getter(evaluate_later, ...args) {
|
|
61
|
+
const evaluate = evaluate_later(...args);
|
|
62
|
+
return () => {
|
|
63
|
+
let result;
|
|
64
|
+
evaluate(v => result = v);
|
|
65
|
+
return has_getter(result) ? result.get() : result;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function has_getter(value) {
|
|
70
|
+
return typeof value?.get === "function";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function plugin({ addScopeToNode, directive, initTree, mutateDom }) {
|
|
74
|
+
directive("match", (el, { }, { cleanup, effect, evaluateLater }) => {
|
|
75
|
+
if (!is_template(el)) {
|
|
76
|
+
warn("x-match can only be used on a 'template' tag.");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const branches = [];
|
|
81
|
+
const has_default_case = () => branches.some(b => b.default);
|
|
82
|
+
|
|
83
|
+
for (let node of el.content.children) {
|
|
84
|
+
const expr = node.getAttribute("x-case");
|
|
85
|
+
if (expr !== null) {
|
|
86
|
+
has_default_case() && warn("The x-case directive cannot be appear after x-default.");
|
|
87
|
+
branches.push({ el: node, get_value: create_getter(evaluateLater, expr) });
|
|
88
|
+
}
|
|
89
|
+
else if (node.hasAttribute("x-default")) {
|
|
90
|
+
has_default_case() && warn("Only one x-default directive is allowed.");
|
|
91
|
+
branches.push({ el: node, get_value: () => true, default: true });
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
warn("Element has no x-case or x-default directive and will be ignored.", node);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const activate = branch => {
|
|
99
|
+
if (el._r_block?.template !== branch.el) {
|
|
100
|
+
clear();
|
|
101
|
+
anchor_block(el, branch.el, {
|
|
102
|
+
addScopeToNode,
|
|
103
|
+
cleanup,
|
|
104
|
+
initTree,
|
|
105
|
+
mutateDom
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const clear = () => el._r_block?.delete();
|
|
111
|
+
|
|
112
|
+
effect(() => {
|
|
113
|
+
let active;
|
|
114
|
+
|
|
115
|
+
for (let branch of branches) {
|
|
116
|
+
if (branch.get_value() && !active) {
|
|
117
|
+
active = branch;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
active ? activate(active) : clear();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export { plugin as match };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=e=>e instanceof HTMLTemplateElement,t=e=>e.nodeType===Node.ELEMENT_NODE;function o(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}}function l({addScopeToNode:l,directive:n,initTree:r,mutateDom:c}){n("match",((n,{},{cleanup:a,effect:d,evaluateLater:u})=>{if(!e(n))return void console.warn("alpine-gear.js:","x-match can only be used on a 'template' tag.");const f=[];for(let e of n.content.children){const t=e.getAttribute("x-case");null!==t?f.push({el:e,get_value:o(u,t)}):e.hasAttribute("x-default")&&f.push({el:e,get_value:()=>!0,default:!0})}const i=()=>n._r_block?.delete();d((()=>{let o;for(let e of f)e.get_value()&&!o&&(o=e);var d;o?(d=o,n._r_block?.template!==d.el&&(i(),function(o,l,{addScopeToNode:n,cleanup:r,initTree:c,mutateDom:a,scope:d={}}){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(l)?[...l.content.cloneNode(!0).childNodes]:[l.cloneNode(!0)];a((()=>{for(let e of u)t(e)&&n(e,d,o),o.parentElement.insertBefore(e,o),t(e)&&c(e)})),o._r_block={template:l,update(){a((()=>{for(let e of u??[])o.parentElement.insertBefore(e,o)}))},delete(){o._r_block=null;for(let e of u??[])e.remove();u=null}},r((()=>o._r_block?.delete()))}(n,d.el,{addScopeToNode:l,cleanup:a,initTree:r,mutateDom:c}))):i()}))}))}export{l as match};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const warn = (...args) => console.warn("alpine-gear.js:", ...args);
|
|
5
|
+
const is_template = el => el instanceof HTMLTemplateElement;
|
|
6
|
+
const is_element = el => el.nodeType === Node.ELEMENT_NODE;
|
|
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
|
+
})();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function create_getter(evaluate_later, ...args) {
|
|
64
|
+
const evaluate = evaluate_later(...args);
|
|
65
|
+
return () => {
|
|
66
|
+
let result;
|
|
67
|
+
evaluate(v => result = v);
|
|
68
|
+
return has_getter(result) ? result.get() : result;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function has_getter(value) {
|
|
73
|
+
return typeof value?.get === "function";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function plugin({ addScopeToNode, directive, initTree, mutateDom }) {
|
|
77
|
+
directive("match", (el, { }, { cleanup, effect, evaluateLater }) => {
|
|
78
|
+
if (!is_template(el)) {
|
|
79
|
+
warn("x-match can only be used on a 'template' tag.");
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const branches = [];
|
|
84
|
+
const has_default_case = () => branches.some(b => b.default);
|
|
85
|
+
|
|
86
|
+
for (let node of el.content.children) {
|
|
87
|
+
const expr = node.getAttribute("x-case");
|
|
88
|
+
if (expr !== null) {
|
|
89
|
+
has_default_case() && warn("The x-case directive cannot be appear after x-default.");
|
|
90
|
+
branches.push({ el: node, get_value: create_getter(evaluateLater, expr) });
|
|
91
|
+
}
|
|
92
|
+
else if (node.hasAttribute("x-default")) {
|
|
93
|
+
has_default_case() && warn("Only one x-default directive is allowed.");
|
|
94
|
+
branches.push({ el: node, get_value: () => true, default: true });
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
warn("Element has no x-case or x-default directive and will be ignored.", node);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const activate = branch => {
|
|
102
|
+
if (el._r_block?.template !== branch.el) {
|
|
103
|
+
clear();
|
|
104
|
+
anchor_block(el, branch.el, {
|
|
105
|
+
addScopeToNode,
|
|
106
|
+
cleanup,
|
|
107
|
+
initTree,
|
|
108
|
+
mutateDom
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const clear = () => el._r_block?.delete();
|
|
114
|
+
|
|
115
|
+
effect(() => {
|
|
116
|
+
let active;
|
|
117
|
+
|
|
118
|
+
for (let branch of branches) {
|
|
119
|
+
if (branch.get_value() && !active) {
|
|
120
|
+
active = branch;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
active ? activate(active) : clear();
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
document.addEventListener("alpine:init", () => { Alpine.plugin(plugin); });
|
|
130
|
+
|
|
131
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(){"use strict";const e=e=>e instanceof HTMLTemplateElement,t=e=>e.nodeType===Node.ELEMENT_NODE;function o(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}}function n({addScopeToNode:n,directive:l,initTree:r,mutateDom:c}){l("match",((l,{},{cleanup:a,effect:u,evaluateLater:d})=>{if(!e(l))return void console.warn("alpine-gear.js:","x-match can only be used on a 'template' tag.");const i=[];for(let e of l.content.children){const t=e.getAttribute("x-case");null!==t?i.push({el:e,get_value:o(d,t)}):e.hasAttribute("x-default")&&i.push({el:e,get_value:()=>!0,default:!0})}const f=()=>l._r_block?.delete();u((()=>{let o;for(let e of i)e.get_value()&&!o&&(o=e);var u;o?(u=o,l._r_block?.template!==u.el&&(f(),function(o,n,{addScopeToNode:l,cleanup:r,initTree:c,mutateDom:a,scope:u={}}){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 d=e(n)?[...n.content.cloneNode(!0).childNodes]:[n.cloneNode(!0)];a((()=>{for(let e of d)t(e)&&l(e,u,o),o.parentElement.insertBefore(e,o),t(e)&&c(e)})),o._r_block={template:n,update(){a((()=>{for(let e of d??[])o.parentElement.insertBefore(e,o)}))},delete(){o._r_block=null;for(let e of d??[])e.remove();d=null}},r((()=>o._r_block?.delete()))}(l,u.el,{addScopeToNode:n,cleanup:a,initTree:r,mutateDom:c}))):f()}))}))}document.addEventListener("alpine:init",(()=>{Alpine.plugin(n)}))}();
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ramstack/alpinegear-match",
|
|
3
|
+
"version": "1.0.0-preview.1",
|
|
4
|
+
"description": "@ramstack/alpinegear-match provides the 'x-match' Alpine.js directive, which functions similarly to the 'switch' statement in many programming languages, allowing to conditionally render elements based on matching cases.",
|
|
5
|
+
"author": "Rameel Burhan",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/rameel/ramstack.alpinegear.js.git",
|
|
10
|
+
"directory": "src/plugins/match"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"alpine.js",
|
|
14
|
+
"alpinejs"
|
|
15
|
+
],
|
|
16
|
+
"main": "alpinegear-match.js",
|
|
17
|
+
"module": "alpinegear-match.esm.js"
|
|
18
|
+
}
|