@ramstack/alpinegear-match 1.4.3 → 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 +127 -159
- package/alpinegear-match.esm.js +107 -107
- package/alpinegear-match.esm.min.js +1 -1
- package/alpinegear-match.js +112 -107
- package/alpinegear-match.min.js +1 -1
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -1,159 +1,127 @@
|
|
|
1
|
-
# @ramstack/alpinegear-match
|
|
2
|
-
[](https://www.npmjs.com/package/@ramstack/alpinegear-match)
|
|
3
|
-
[](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE)
|
|
4
|
-
|
|
5
|
-
`@ramstack/alpinegear-match` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-match` directive.
|
|
6
|
-
|
|
7
|
-
This directive functions similarly to the `switch` statement in many programming languages,
|
|
8
|
-
allowing you to conditionally render elements based on matching cases.
|
|
9
|
-
|
|
10
|
-
> [!Note]
|
|
11
|
-
> This package is part of the **[`@ramstack/alpinegear-main`](https://www.npmjs.com/package/@ramstack/alpinegear-main)** bundle.
|
|
12
|
-
> If you are using the main bundle, you don't need to install this package separately.
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
### Using CDN
|
|
17
|
-
To include the CDN version of this plugin, add the following `<script>` tag before the core `alpine.js` file:
|
|
18
|
-
|
|
19
|
-
```html
|
|
20
|
-
<!-- alpine.js plugin -->
|
|
21
|
-
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-match@1/alpinegear-match.min.js" defer></script>
|
|
22
|
-
|
|
23
|
-
<!-- alpine.js -->
|
|
24
|
-
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
### Using NPM
|
|
28
|
-
Alternatively, you can install the plugin via `npm`:
|
|
29
|
-
|
|
30
|
-
```bash
|
|
31
|
-
npm install --save @ramstack/alpinegear-match
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
Then initialize it in your bundle:
|
|
35
|
-
|
|
36
|
-
```js
|
|
37
|
-
import Alpine from "alpinejs";
|
|
38
|
-
import match from "@ramstack/alpinegear-match";
|
|
39
|
-
|
|
40
|
-
Alpine.plugin(match);
|
|
41
|
-
Alpine.start();
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
## Usage
|
|
45
|
-
The `x-match` directive is similar to using multiple consecutive `x-if` or `x-when` directives.
|
|
46
|
-
However, using multiple `x-if` or `x-when` can make your markup harder to read and lead to code bloat.
|
|
47
|
-
|
|
48
|
-
The `x-match` directive provides a cleaner solution by allowing you to define multiple blocks with conditions.
|
|
49
|
-
The corresponding block will be displayed if its condition evaluates to true.
|
|
50
|
-
|
|
51
|
-
Here's a simple example solving the **FizzBuzz** game:
|
|
52
|
-
```html
|
|
53
|
-
<div x-data="{ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] }">
|
|
54
|
-
<template x-for="n in numbers">
|
|
55
|
-
<template x-match>
|
|
56
|
-
<div x-case="n % 3 == 0 && n % 5 == 0">Fizz Buzz</div>
|
|
57
|
-
<div x-case="n % 3 == 0">Fizz</div>
|
|
58
|
-
<div x-case="n % 5 == 0">Buzz</div>
|
|
59
|
-
<div x-default x-text="n"></div>
|
|
60
|
-
</template>
|
|
61
|
-
</template>
|
|
62
|
-
</div>
|
|
63
|
-
```
|
|
64
|
-
🚀 [Live demo | Alpine.js x-match: FizzBuzz game](https://jsfiddle.net/rameel/5reaopmb/)
|
|
65
|
-
|
|
66
|
-
While it's possible to wrap the loop's content in an additional `<div>` (since `x-for` only allows a single root element),
|
|
67
|
-
to achieve similar results, the `x-match` directive provides a much cleaner and more readable approach.
|
|
68
|
-
Additionally, it avoids introducing extra elements that are only needed to bypass these limitations.
|
|
69
|
-
|
|
70
|
-
> [!IMPORTANT]
|
|
71
|
-
> Ensure that `x-case` conditions are ordered from most specific to least specific.
|
|
72
|
-
> Otherwise, a more general case might intercept the condition, causing subsequent cases not to execute.
|
|
73
|
-
|
|
74
|
-
> [!NOTE]
|
|
75
|
-
> The `x-default` branch is optional and only renders if none of the `x-case` conditions evaluate to `true`.
|
|
76
|
-
|
|
77
|
-
> [!TIP]
|
|
78
|
-
> The `x-case` directive can be applied to regular HTML tags or `<template>` tags. When used with `<template>`,
|
|
79
|
-
> you can define multiple root elements, and all will be rendered.
|
|
80
|
-
|
|
81
|
-
Here's an example demonstrating the use of `<template>` with `x-case` to render multiple root elements:
|
|
82
|
-
```html
|
|
83
|
-
<template x-match>
|
|
84
|
-
<template x-case="status === 'active'">
|
|
85
|
-
<h2>Welcome!</h2>
|
|
86
|
-
<p>You have full access to all features.</p>
|
|
87
|
-
<a href="#dashboard">Dashboard</a>
|
|
88
|
-
</template>
|
|
89
|
-
|
|
90
|
-
<template x-case="status === 'disabled'">
|
|
91
|
-
<h2>Inactive Account</h2>
|
|
92
|
-
<p>Please activate your account to continue.</p>
|
|
93
|
-
</template>
|
|
94
|
-
|
|
95
|
-
<template x-default>
|
|
96
|
-
<h2>Guest Mode</h2>
|
|
97
|
-
<p>Sign up to unlock more features!</p>
|
|
98
|
-
<button>Sign Up</button>
|
|
99
|
-
</template>
|
|
100
|
-
</template>
|
|
101
|
-
```
|
|
102
|
-
🚀 [Live demo | Alpine.js x-match: Multiple root elements](https://jsfiddle.net/rameel/0vLksypo/)
|
|
103
|
-
|
|
104
|
-
In this example, the `x-match` directive with `<template>` tags allows rendering multiple root elements
|
|
105
|
-
(e.g., `<h2>`, `<p>`, `<a>`, and `<button>`) without needing an extra wrapper like a `<div>`.
|
|
106
|
-
|
|
107
|
-
## Source code
|
|
108
|
-
You can find the source code for this plugin on GitHub:
|
|
109
|
-
|
|
110
|
-
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/match
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
Provides the `x-template` directive, which allows you to define a template once anywhere in the DOM and reference it by its ID.
|
|
129
|
-
|
|
130
|
-
**[@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>
|
|
131
|
-
Provides the `x-fragment` directive, which allows for fragment-like behavior similar to what's available in frameworks
|
|
132
|
-
like `Vue.js` or `React`, where multiple root elements can be grouped together.
|
|
133
|
-
|
|
134
|
-
**[@ramstack/alpinegear-when](https://www.npmjs.com/package/@ramstack/alpinegear-when)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/when))<br>
|
|
135
|
-
Provides the `x-when` directive, which allows for conditional rendering of elements similar to `x-if`, but supports multiple root elements.
|
|
136
|
-
|
|
137
|
-
**[@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>
|
|
138
|
-
Provides the `x-destroy` directive, which is the opposite of `x-init` and allows you to hook into the cleanup phase
|
|
139
|
-
of any element, running a callback when the element is removed from the DOM.
|
|
140
|
-
|
|
141
|
-
**[@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>
|
|
142
|
-
Provides the `x-hotkey` directive, which allows you to easily handle keyboard shortcuts within your Alpine.js components or application.
|
|
143
|
-
|
|
144
|
-
**[@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>
|
|
145
|
-
Provides the `x-router` and `x-route` directives, which enable client-side navigation and routing functionality within your Alpine.js application.
|
|
146
|
-
|
|
147
|
-
**[@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>
|
|
148
|
-
Provides a headless dialog directive for Alpine.js based on the native HTML `<dialog>` element.
|
|
149
|
-
It supports declarative composition, value-based close semantics, and both modal and non-modal dialogs,
|
|
150
|
-
with optional Promise-based imperative control.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
## Contributions
|
|
154
|
-
Bug reports and contributions are welcome.
|
|
155
|
-
|
|
156
|
-
## License
|
|
157
|
-
This package is released as open source under the **MIT License**.
|
|
158
|
-
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|
|
159
|
-
|
|
1
|
+
# @ramstack/alpinegear-match
|
|
2
|
+
[](https://www.npmjs.com/package/@ramstack/alpinegear-match)
|
|
3
|
+
[](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE)
|
|
4
|
+
|
|
5
|
+
`@ramstack/alpinegear-match` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-match` directive.
|
|
6
|
+
|
|
7
|
+
This directive functions similarly to the `switch` statement in many programming languages,
|
|
8
|
+
allowing you to conditionally render elements based on matching cases.
|
|
9
|
+
|
|
10
|
+
> [!Note]
|
|
11
|
+
> This package is part of the **[`@ramstack/alpinegear-main`](https://www.npmjs.com/package/@ramstack/alpinegear-main)** bundle.
|
|
12
|
+
> If you are using the main bundle, you don't need to install this package separately.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
### Using CDN
|
|
17
|
+
To include the CDN version of this plugin, add the following `<script>` tag before the core `alpine.js` file:
|
|
18
|
+
|
|
19
|
+
```html
|
|
20
|
+
<!-- alpine.js plugin -->
|
|
21
|
+
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-match@1/alpinegear-match.min.js" defer></script>
|
|
22
|
+
|
|
23
|
+
<!-- alpine.js -->
|
|
24
|
+
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Using NPM
|
|
28
|
+
Alternatively, you can install the plugin via `npm`:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install --save @ramstack/alpinegear-match
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Then initialize it in your bundle:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import Alpine from "alpinejs";
|
|
38
|
+
import match from "@ramstack/alpinegear-match";
|
|
39
|
+
|
|
40
|
+
Alpine.plugin(match);
|
|
41
|
+
Alpine.start();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
The `x-match` directive is similar to using multiple consecutive `x-if` or `x-when` directives.
|
|
46
|
+
However, using multiple `x-if` or `x-when` can make your markup harder to read and lead to code bloat.
|
|
47
|
+
|
|
48
|
+
The `x-match` directive provides a cleaner solution by allowing you to define multiple blocks with conditions.
|
|
49
|
+
The corresponding block will be displayed if its condition evaluates to true.
|
|
50
|
+
|
|
51
|
+
Here's a simple example solving the **FizzBuzz** game:
|
|
52
|
+
```html
|
|
53
|
+
<div x-data="{ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] }">
|
|
54
|
+
<template x-for="n in numbers">
|
|
55
|
+
<template x-match>
|
|
56
|
+
<div x-case="n % 3 == 0 && n % 5 == 0">Fizz Buzz</div>
|
|
57
|
+
<div x-case="n % 3 == 0">Fizz</div>
|
|
58
|
+
<div x-case="n % 5 == 0">Buzz</div>
|
|
59
|
+
<div x-default x-text="n"></div>
|
|
60
|
+
</template>
|
|
61
|
+
</template>
|
|
62
|
+
</div>
|
|
63
|
+
```
|
|
64
|
+
🚀 [Live demo | Alpine.js x-match: FizzBuzz game](https://jsfiddle.net/rameel/5reaopmb/)
|
|
65
|
+
|
|
66
|
+
While it's possible to wrap the loop's content in an additional `<div>` (since `x-for` only allows a single root element),
|
|
67
|
+
to achieve similar results, the `x-match` directive provides a much cleaner and more readable approach.
|
|
68
|
+
Additionally, it avoids introducing extra elements that are only needed to bypass these limitations.
|
|
69
|
+
|
|
70
|
+
> [!IMPORTANT]
|
|
71
|
+
> Ensure that `x-case` conditions are ordered from most specific to least specific.
|
|
72
|
+
> Otherwise, a more general case might intercept the condition, causing subsequent cases not to execute.
|
|
73
|
+
|
|
74
|
+
> [!NOTE]
|
|
75
|
+
> The `x-default` branch is optional and only renders if none of the `x-case` conditions evaluate to `true`.
|
|
76
|
+
|
|
77
|
+
> [!TIP]
|
|
78
|
+
> The `x-case` directive can be applied to regular HTML tags or `<template>` tags. When used with `<template>`,
|
|
79
|
+
> you can define multiple root elements, and all will be rendered.
|
|
80
|
+
|
|
81
|
+
Here's an example demonstrating the use of `<template>` with `x-case` to render multiple root elements:
|
|
82
|
+
```html
|
|
83
|
+
<template x-match>
|
|
84
|
+
<template x-case="status === 'active'">
|
|
85
|
+
<h2>Welcome!</h2>
|
|
86
|
+
<p>You have full access to all features.</p>
|
|
87
|
+
<a href="#dashboard">Dashboard</a>
|
|
88
|
+
</template>
|
|
89
|
+
|
|
90
|
+
<template x-case="status === 'disabled'">
|
|
91
|
+
<h2>Inactive Account</h2>
|
|
92
|
+
<p>Please activate your account to continue.</p>
|
|
93
|
+
</template>
|
|
94
|
+
|
|
95
|
+
<template x-default>
|
|
96
|
+
<h2>Guest Mode</h2>
|
|
97
|
+
<p>Sign up to unlock more features!</p>
|
|
98
|
+
<button>Sign Up</button>
|
|
99
|
+
</template>
|
|
100
|
+
</template>
|
|
101
|
+
```
|
|
102
|
+
🚀 [Live demo | Alpine.js x-match: Multiple root elements](https://jsfiddle.net/rameel/0vLksypo/)
|
|
103
|
+
|
|
104
|
+
In this example, the `x-match` directive with `<template>` tags allows rendering multiple root elements
|
|
105
|
+
(e.g., `<h2>`, `<p>`, `<a>`, and `<button>`) without needing an extra wrapper like a `<div>`.
|
|
106
|
+
|
|
107
|
+
## Source code
|
|
108
|
+
You can find the source code for this plugin on GitHub:
|
|
109
|
+
|
|
110
|
+
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/match
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
## Related packages
|
|
114
|
+
This package is part of **[AlpineGear](https://github.com/rameel/ramstack.alpinegear.js)** —
|
|
115
|
+
a collection of utilities and directives for [Alpine.js](https://alpinejs.dev).
|
|
116
|
+
|
|
117
|
+
You can find the full list of related packages and their documentation here:
|
|
118
|
+
https://github.com/rameel/ramstack.alpinegear.js
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
## Contributions
|
|
122
|
+
Bug reports and contributions are welcome.
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
This package is released as open source under the **MIT License**.
|
|
126
|
+
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|
|
127
|
+
|
package/alpinegear-match.esm.js
CHANGED
|
@@ -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,57 +70,57 @@ function has_getter(value) {
|
|
|
70
70
|
return typeof value?.get === "function";
|
|
71
71
|
}
|
|
72
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
|
-
});
|
|
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
124
|
}
|
|
125
125
|
|
|
126
|
-
export { plugin as match };
|
|
126
|
+
export { plugin as default, plugin as match };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=e=>e.matches("template"),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:
|
|
1
|
+
const e=e=>e.matches("template"),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:a}){n("match",(n,{},{cleanup:c,effect:d,evaluateLater:u})=>{if(!e(n))return void console.warn("alpinegear.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:a,mutateDom:c,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)];c(()=>{for(let e of u)t(e)&&n(e,d,o),o.parentElement.insertBefore(e,o),t(e)&&a(e)}),o._r_block={template:l,update(){c(()=>{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:c,initTree:r,mutateDom:a}))):i()})})}export{l as default,l as match};
|
package/alpinegear-match.js
CHANGED
|
@@ -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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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,59 +78,59 @@
|
|
|
73
78
|
return typeof value?.get === "function";
|
|
74
79
|
}
|
|
75
80
|
|
|
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
|
-
});
|
|
81
|
+
function plugin({ addScopeToNode, directive, initTree, mutateDom }) {
|
|
82
|
+
directive("match", (el, { }, { cleanup, effect, evaluateLater }) => {
|
|
83
|
+
if (!is_template(el)) {
|
|
84
|
+
warn("x-match can only be used on a 'template' tag");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const branches = [];
|
|
89
|
+
const has_default_case = () => branches.some(b => b.default);
|
|
90
|
+
|
|
91
|
+
for (let node of el.content.children) {
|
|
92
|
+
const expr = node.getAttribute("x-case");
|
|
93
|
+
if (expr !== null) {
|
|
94
|
+
has_default_case() && warn("The x-case directive cannot be appear after x-default");
|
|
95
|
+
branches.push({ el: node, get_value: create_getter(evaluateLater, expr) });
|
|
96
|
+
}
|
|
97
|
+
else if (node.hasAttribute("x-default")) {
|
|
98
|
+
has_default_case() && warn("Only one x-default directive is allowed");
|
|
99
|
+
branches.push({ el: node, get_value: () => true, default: true });
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
warn("Element has no x-case or x-default directive and will be ignored", node);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const activate = branch => {
|
|
107
|
+
if (el._r_block?.template !== branch.el) {
|
|
108
|
+
clear();
|
|
109
|
+
anchor_block(el, branch.el, {
|
|
110
|
+
addScopeToNode,
|
|
111
|
+
cleanup,
|
|
112
|
+
initTree,
|
|
113
|
+
mutateDom
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const clear = () => el._r_block?.delete();
|
|
119
|
+
|
|
120
|
+
effect(() => {
|
|
121
|
+
let active;
|
|
122
|
+
|
|
123
|
+
for (let branch of branches) {
|
|
124
|
+
if (branch.get_value() && !active) {
|
|
125
|
+
active = branch;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
active ? activate(active) : clear();
|
|
130
|
+
});
|
|
131
|
+
});
|
|
127
132
|
}
|
|
128
133
|
|
|
129
|
-
document
|
|
134
|
+
listen(document, "alpine:init", () => Alpine.plugin(plugin));
|
|
130
135
|
|
|
131
136
|
})();
|
package/alpinegear-match.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){"use strict";const e=e=>e.matches("template"),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:
|
|
1
|
+
!function(){"use strict";const e=e=>e.matches("template"),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:d,evaluateLater:u})=>{if(!e(l))return void console.warn("alpinegear.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(u,t)}):e.hasAttribute("x-default")&&i.push({el:e,get_value:()=>!0,default:!0})}const f=()=>l._r_block?.delete();d(()=>{let o;for(let e of i)e.get_value()&&!o&&(o=e);var d;o?(d=o,l._r_block?.template!==d.el&&(f(),function(o,n,{addScopeToNode:l,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(n)?[...n.content.cloneNode(!0).childNodes]:[n.cloneNode(!0)];a(()=>{for(let e of u)t(e)&&l(e,d,o),o.parentElement.insertBefore(e,o),t(e)&&c(e)}),o._r_block={template:n,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())}(l,d.el,{addScopeToNode:n,cleanup:a,initTree:r,mutateDom:c}))):f()})})}document.addEventListener("alpine:init",()=>Alpine.plugin(n),void 0)}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ramstack/alpinegear-match",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
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
5
|
"author": "Rameel Burhan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,6 +15,12 @@
|
|
|
15
15
|
"alpinejs-directive",
|
|
16
16
|
"alpinejs-plugin"
|
|
17
17
|
],
|
|
18
|
-
"
|
|
19
|
-
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": {
|
|
21
|
+
"production": "./alpinegear-match.esm.min.js",
|
|
22
|
+
"default": "./alpinegear-match.esm.js"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
20
26
|
}
|