@ramstack/alpinegear-fragment 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 +115 -147
- package/alpinegear-fragment.esm.js +65 -65
- package/alpinegear-fragment.esm.min.js +1 -1
- package/alpinegear-fragment.js +70 -65
- package/alpinegear-fragment.min.js +1 -1
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -1,147 +1,115 @@
|
|
|
1
|
-
# @ramstack/alpinegear-fragment
|
|
2
|
-
[](https://www.npmjs.com/package/@ramstack/alpinegear-fragment)
|
|
3
|
-
[](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE)
|
|
4
|
-
|
|
5
|
-
`@ramstack/alpinegear-fragment` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-fragment` directive.
|
|
6
|
-
|
|
7
|
-
This directive allows you to use multiple root elements in your templates, similar to the `Fragment` feature found in frameworks
|
|
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
|
-
import
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Alpine.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
<li>
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
<li>
|
|
58
|
-
<li>
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
</
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
{ term: '
|
|
78
|
-
{ term: '
|
|
79
|
-
{ term: '
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
<
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
</
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
**[@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>
|
|
117
|
-
Provides the `x-template` directive, which allows you to define a template once anywhere in the DOM and reference it by its ID.
|
|
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-when](https://www.npmjs.com/package/@ramstack/alpinegear-when)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/when))<br>
|
|
124
|
-
Provides the `x-when` directive, which allows for conditional rendering of elements similar to `x-if`, but supports multiple root elements.
|
|
125
|
-
|
|
126
|
-
**[@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>
|
|
127
|
-
Provides the `x-destroy` directive, which is the opposite of `x-init` and allows you to hook into the cleanup phase
|
|
128
|
-
of any element, running a callback when the element is removed from the DOM.
|
|
129
|
-
|
|
130
|
-
**[@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>
|
|
131
|
-
Provides the `x-hotkey` directive, which allows you to easily handle keyboard shortcuts within your Alpine.js components or application.
|
|
132
|
-
|
|
133
|
-
**[@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>
|
|
134
|
-
Provides the `x-router` and `x-route` directives, which enable client-side navigation and routing functionality within your Alpine.js application.
|
|
135
|
-
|
|
136
|
-
**[@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>
|
|
137
|
-
Provides a headless dialog directive for Alpine.js based on the native HTML `<dialog>` element.
|
|
138
|
-
It supports declarative composition, value-based close semantics, and both modal and non-modal dialogs,
|
|
139
|
-
with optional Promise-based imperative control.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
## Contributions
|
|
143
|
-
Bug reports and contributions are welcome.
|
|
144
|
-
|
|
145
|
-
## License
|
|
146
|
-
This package is released as open source under the **MIT License**.
|
|
147
|
-
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|
|
1
|
+
# @ramstack/alpinegear-fragment
|
|
2
|
+
[](https://www.npmjs.com/package/@ramstack/alpinegear-fragment)
|
|
3
|
+
[](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE)
|
|
4
|
+
|
|
5
|
+
`@ramstack/alpinegear-fragment` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-fragment` directive.
|
|
6
|
+
|
|
7
|
+
This directive allows you to use multiple root elements in your templates, similar to the `Fragment` feature found in frameworks
|
|
8
|
+
like `Vue.js` and `React`. It is particularly useful when you want to avoid wrapping elements in unnecessary container tags.
|
|
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-fragment@1/alpinegear-fragment.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-fragment
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Then initialize it in your bundle:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import Alpine from "alpinejs";
|
|
38
|
+
import fragment from "@ramstack/alpinegear-fragment";
|
|
39
|
+
|
|
40
|
+
Alpine.plugin(fragment);
|
|
41
|
+
Alpine.start();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
With the `x-fragment` directive, you can use multiple root elements in your components without needing a wrapper container:
|
|
46
|
+
|
|
47
|
+
```html
|
|
48
|
+
<div x-data="{ show: false }">
|
|
49
|
+
<button @click="show = !show">Show more</button>
|
|
50
|
+
|
|
51
|
+
<ul>
|
|
52
|
+
<li>Apple</li>
|
|
53
|
+
<li>Banana</li>
|
|
54
|
+
|
|
55
|
+
<template x-if="show">
|
|
56
|
+
<template x-fragment>
|
|
57
|
+
<li>Orange</li>
|
|
58
|
+
<li>Grape</li>
|
|
59
|
+
<li>Mango</li>
|
|
60
|
+
</template>
|
|
61
|
+
</template>
|
|
62
|
+
</ul>
|
|
63
|
+
</div>
|
|
64
|
+
```
|
|
65
|
+
🚀 [Live demo | Alpine.js x-fragment: Multiple root elements](https://jsfiddle.net/rameel/jdwuoatf/)
|
|
66
|
+
|
|
67
|
+
In this example, the `x-fragment` directive allows the `<li>` elements (Orange, Grape, and Mango) to be added
|
|
68
|
+
to the `<ul>` without a parent container, enabling multiple root elements in the `x-if` template.
|
|
69
|
+
|
|
70
|
+
### Using with `x-for`
|
|
71
|
+
The `x-fragment` directive can also be used with the directive `x-for`, giving you the flexibility to render
|
|
72
|
+
multiple sibling elements for each iteration without wrapping them:
|
|
73
|
+
|
|
74
|
+
```html
|
|
75
|
+
<div x-data="{
|
|
76
|
+
items: [
|
|
77
|
+
{ term: 'Star', description: 'Luminous plasma sphere.' },
|
|
78
|
+
{ term: 'Planet', description: 'Body orbiting a star.' },
|
|
79
|
+
{ term: 'Galaxy', description: 'Stars and dust system.' },
|
|
80
|
+
{ term: 'Nebula', description: 'Cloud of gas in space.' }]
|
|
81
|
+
}">
|
|
82
|
+
<button @click="items.reverse()">Reverse</button>
|
|
83
|
+
|
|
84
|
+
<dl>
|
|
85
|
+
<template x-for="item in items" :key="item.term">
|
|
86
|
+
<template x-fragment>
|
|
87
|
+
<dt x-text="item.term"></dt>
|
|
88
|
+
<dd x-text="item.description"></dd>
|
|
89
|
+
</template>
|
|
90
|
+
</template>
|
|
91
|
+
</dl>
|
|
92
|
+
</div>
|
|
93
|
+
```
|
|
94
|
+
🚀 [Live demo | Alpine.js x-fragment: Multiple root elements with x-for](https://jsfiddle.net/rameel/201rmntc/)
|
|
95
|
+
|
|
96
|
+
## Source code
|
|
97
|
+
You can find the source code for this plugin on GitHub:
|
|
98
|
+
|
|
99
|
+
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/fragment
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
## Related packages
|
|
103
|
+
This package is part of **[AlpineGear](https://github.com/rameel/ramstack.alpinegear.js)** —
|
|
104
|
+
a collection of utilities and directives for [Alpine.js](https://alpinejs.dev).
|
|
105
|
+
|
|
106
|
+
You can find the full list of related packages and their documentation here:
|
|
107
|
+
https://github.com/rameel/ramstack.alpinegear.js
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
## Contributions
|
|
111
|
+
Bug reports and contributions are welcome.
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
This package is released as open source under the **MIT License**.
|
|
115
|
+
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|
|
@@ -1,71 +1,71 @@
|
|
|
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());
|
|
58
43
|
}
|
|
59
44
|
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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 plugin({ addScopeToNode, directive, initTree, mutateDom }) {
|
|
61
|
+
directive("fragment", (el, {}, { cleanup }) => {
|
|
62
|
+
if (!is_template(el)) {
|
|
63
|
+
warn("x-fragment can only be used on a 'template' tag");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
anchor_block(el, el, { addScopeToNode, cleanup, initTree, mutateDom });
|
|
68
|
+
});
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
export { plugin as fragment };
|
|
71
|
+
export { plugin as default, plugin as fragment };
|
|
@@ -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("fragment",(n,{},{cleanup:
|
|
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("fragment",(n,{},{cleanup:a})=>{e(n)?function(t,n,{addScopeToNode:r,cleanup:l,initTree:a,mutateDom:d,scope:c={}}){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,c,t),t.parentElement.insertBefore(e,t),o(e)&&a(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:a,initTree:r,mutateDom:l}):console.warn("alpinegear.js:","x-fragment can only be used on a 'template' tag")})}export{t as default,t as fragment};
|
package/alpinegear-fragment.js
CHANGED
|
@@ -1,76 +1,81 @@
|
|
|
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
|
-
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());
|
|
61
51
|
}
|
|
62
52
|
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
+
})();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function plugin({ addScopeToNode, directive, initTree, mutateDom }) {
|
|
69
|
+
directive("fragment", (el, {}, { cleanup }) => {
|
|
70
|
+
if (!is_template(el)) {
|
|
71
|
+
warn("x-fragment can only be used on a 'template' tag");
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
anchor_block(el, el, { addScopeToNode, cleanup, initTree, mutateDom });
|
|
76
|
+
});
|
|
72
77
|
}
|
|
73
78
|
|
|
74
|
-
document
|
|
79
|
+
listen(document, "alpine:init", () => Alpine.plugin(plugin));
|
|
75
80
|
|
|
76
81
|
})();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){"use strict";const e=e=>e.matches("template"),o=e=>e.nodeType===Node.ELEMENT_NODE;function t({addScopeToNode:t,directive:n,initTree:l,mutateDom:r}){n("fragment",(n,{},{cleanup:d})=>{e(n)?function(t,n,{addScopeToNode:l,cleanup:r,initTree:d,mutateDom:c,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 i=e(n)?[...n.content.cloneNode(!0).childNodes]:[n.cloneNode(!0)];c(()=>{for(let e of i)o(e)&&l(e,a,t),t.parentElement.insertBefore(e,t),o(e)&&d(e)}),t._r_block={template:n,update(){c(()=>{for(let e of i??[])t.parentElement.insertBefore(e,t)})},delete(){t._r_block=null;for(let e of i??[])e.remove();i=null}},r(()=>t._r_block?.delete())}(n,n,{addScopeToNode:t,cleanup:d,initTree:l,mutateDom:r}):console.warn("alpinegear.js:","x-fragment can only be used on a 'template' tag")})}document.addEventListener("alpine:init",()=>
|
|
1
|
+
!function(){"use strict";const e=e=>e.matches("template"),o=e=>e.nodeType===Node.ELEMENT_NODE;function t({addScopeToNode:t,directive:n,initTree:l,mutateDom:r}){n("fragment",(n,{},{cleanup:d})=>{e(n)?function(t,n,{addScopeToNode:l,cleanup:r,initTree:d,mutateDom:c,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 i=e(n)?[...n.content.cloneNode(!0).childNodes]:[n.cloneNode(!0)];c(()=>{for(let e of i)o(e)&&l(e,a,t),t.parentElement.insertBefore(e,t),o(e)&&d(e)}),t._r_block={template:n,update(){c(()=>{for(let e of i??[])t.parentElement.insertBefore(e,t)})},delete(){t._r_block=null;for(let e of i??[])e.remove();i=null}},r(()=>t._r_block?.delete())}(n,n,{addScopeToNode:t,cleanup:d,initTree:l,mutateDom:r}):console.warn("alpinegear.js:","x-fragment can only be used on a 'template' tag")})}document.addEventListener("alpine:init",()=>Alpine.plugin(t),void 0)}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ramstack/alpinegear-fragment",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "@ramstack/alpinegear-fragment provides 'x-format' Alpine.js directive, allowing for fragment-like behavior similar to what's available in frameworks like 'Vue.js' or 'React', where multiple root elements can be grouped together.",
|
|
5
5
|
"author": "Rameel Burhan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,6 +18,12 @@
|
|
|
18
18
|
"alpinejs-plugin",
|
|
19
19
|
"template"
|
|
20
20
|
],
|
|
21
|
-
"
|
|
22
|
-
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": {
|
|
24
|
+
"production": "./alpinegear-fragment.esm.min.js",
|
|
25
|
+
"default": "./alpinegear-fragment.esm.js"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
23
29
|
}
|