@ramstack/alpinegear-match 1.1.0 → 1.2.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 CHANGED
@@ -4,7 +4,8 @@
4
4
 
5
5
  `@ramstack/alpinegear-match` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-match` directive.
6
6
 
7
- This directive functions similarly to the `switch` statement in many programming languages, allowing you to conditionally render elements based on matching cases.
7
+ This directive functions similarly to the `switch` statement in many programming languages,
8
+ allowing you to conditionally render elements based on matching cases.
8
9
 
9
10
  ## Installation
10
11
 
@@ -13,7 +14,7 @@ To include the CDN version of this plugin, add the following `<script>` tag befo
13
14
 
14
15
  ```html
15
16
  <!-- alpine.js plugin -->
16
- <script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-when@1/alpinegear-match.min.js" defer></script>
17
+ <script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-match@1/alpinegear-match.min.js" defer></script>
17
18
 
18
19
  <!-- alpine.js -->
19
20
  <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
@@ -37,11 +38,13 @@ Alpine.start();
37
38
  ```
38
39
 
39
40
  ## Usage
40
- 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.
41
+ The `x-match` directive is similar to using multiple consecutive `x-if` or `x-when` directives.
42
+ However, using multiple `x-if` or `x-when` can make your markup harder to read and lead to code bloat.
41
43
 
42
- 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.
44
+ The `x-match` directive provides a cleaner solution by allowing you to define multiple blocks with conditions.
45
+ The corresponding block will be displayed if its condition evaluates to true.
43
46
 
44
- Here's a simple example solving the classic **FizzBuzz** game:
47
+ Here's a simple example solving the **FizzBuzz** game:
45
48
  ```html
46
49
  <div x-data="{ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] }">
47
50
  <template x-for="n in numbers">
@@ -55,40 +58,45 @@ Here's a simple example solving the classic **FizzBuzz** game:
55
58
  </div>
56
59
  ```
57
60
 
58
- 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.
59
-
60
- For comparison, here's how the same **FizzBuzz** game might look using `x-fragment`:
61
-
62
- ```html
63
- <div x-data="{ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] }">
64
- <template x-for="n in numbers">
65
- <div>
66
- <template x-if="n % 3 == 0 && n % 5 == 0">
67
- <div>Fizz Buzz</div>
68
- </template>
69
- <template x-if="n % 3 == 0">
70
- <div>Fizz</div>
71
- </template>
72
- <template x-if="n % 5 == 0">
73
- <div>Buzz</div>
74
- </template>
75
- <template x-if="n % 3 != 0 && n % 5 != 0">
76
- <div x-text="n"></div>
77
- </template>
78
- </div>
79
- </template>
80
- </div>
81
- ```
61
+ While it's possible to wrap the loop's content in an additional `<div>` (since `x-for` only allows a single root element),
62
+ to achieve similar results, the `x-match` directive provides a much cleaner and more readable approach.
63
+ Additionally, it avoids introducing extra elements that are only needed to bypass these limitations.
82
64
 
83
65
  > [!IMPORTANT]
84
- > 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.
66
+ > Ensure that `x-case` conditions are ordered from most specific to least specific.
67
+ > Otherwise, a more general case might intercept the condition, causing subsequent cases not to execute.
85
68
 
86
69
  > [!NOTE]
87
70
  > The `x-default` branch is optional and only renders if none of the `x-case` conditions evaluate to `true`.
88
71
 
89
72
  > [!TIP]
90
- > 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.
73
+ > The `x-case` directive can be applied to regular HTML tags or `<template>` tags. When used with `<template>`,
74
+ > you can define multiple root elements, and all will be rendered.
91
75
 
76
+ Here's an example demonstrating the use of `<template>` with `x-case` to render multiple root elements:
77
+ ```html
78
+ <template x-match>
79
+ <template x-case="status === 'active'">
80
+ <h2>Welcome!</h2>
81
+ <p>You have full access to all features.</p>
82
+ <a href="/dashboard">Dashboard</a>
83
+ </template>
84
+
85
+ <template x-case="status === 'inactive'">
86
+ <h2>Inactive Account</h2>
87
+ <p>Please activate your account to continue.</p>
88
+ </template>
89
+
90
+ <template x-default>
91
+ <h2>Guest Mode</h2>
92
+ <p>Sign up to unlock more features!</p>
93
+ <button>Sign Up</button>
94
+ </template>
95
+ </template>
96
+ ```
97
+
98
+ In this example, the `x-match` directive with `<template>` tags allows rendering multiple root elements
99
+ (e.g., `<h2>`, `<p>`, `<a>`, and `<button>`) without needing an extra wrapper like a `<div>`.
92
100
 
93
101
  ## Source code
94
102
  You can find the source code for this plugin on GitHub:
@@ -101,3 +109,4 @@ Bug reports and contributions are welcome.
101
109
  ## License
102
110
  This package is released as open source under the **MIT License**.
103
111
  See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
112
+
@@ -1,4 +1,4 @@
1
- const warn = (...args) => console.warn("alpine-gear.js:", ...args);
1
+ const warn = (...args) => console.warn("alpinegear.js:", ...args);
2
2
  const is_template = el => el instanceof HTMLTemplateElement;
3
3
  const is_element = el => el.nodeType === Node.ELEMENT_NODE;
4
4
 
@@ -1 +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};
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("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: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};
@@ -1,7 +1,7 @@
1
1
  (function () {
2
2
  'use strict';
3
3
 
4
- const warn = (...args) => console.warn("alpine-gear.js:", ...args);
4
+ const warn = (...args) => console.warn("alpinegear.js:", ...args);
5
5
  const is_template = el => el instanceof HTMLTemplateElement;
6
6
  const is_element = el => el.nodeType === Node.ELEMENT_NODE;
7
7
 
@@ -1 +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)}))}();
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("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(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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ramstack/alpinegear-match",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
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",