@ramstack/alpinegear-match 1.2.2 → 1.2.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 +66 -24
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
This directive functions similarly to the `switch` statement in many programming languages,
|
|
8
8
|
allowing you to conditionally render elements based on matching cases.
|
|
9
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
|
+
|
|
10
14
|
## Installation
|
|
11
15
|
|
|
12
16
|
### Using CDN
|
|
@@ -17,7 +21,7 @@ To include the CDN version of this plugin, add the following `<script>` tag befo
|
|
|
17
21
|
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-match@1/alpinegear-match.min.js" defer></script>
|
|
18
22
|
|
|
19
23
|
<!-- alpine.js -->
|
|
20
|
-
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3
|
|
24
|
+
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
|
|
21
25
|
```
|
|
22
26
|
|
|
23
27
|
### Using NPM
|
|
@@ -47,16 +51,17 @@ The corresponding block will be displayed if its condition evaluates to true.
|
|
|
47
51
|
Here's a simple example solving the **FizzBuzz** game:
|
|
48
52
|
```html
|
|
49
53
|
<div x-data="{ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] }">
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
</template>
|
|
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>
|
|
57
60
|
</template>
|
|
61
|
+
</template>
|
|
58
62
|
</div>
|
|
59
63
|
```
|
|
64
|
+
🚀 [Live demo | Alpine.js x-match: FizzBuzz game](https://jsfiddle.net/rameel/5reaopmb/)
|
|
60
65
|
|
|
61
66
|
While it's possible to wrap the loop's content in an additional `<div>` (since `x-for` only allows a single root element),
|
|
62
67
|
to achieve similar results, the `x-match` directive provides a much cleaner and more readable approach.
|
|
@@ -76,24 +81,25 @@ Additionally, it avoids introducing extra elements that are only needed to bypas
|
|
|
76
81
|
Here's an example demonstrating the use of `<template>` with `x-case` to render multiple root elements:
|
|
77
82
|
```html
|
|
78
83
|
<template x-match>
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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>
|
|
95
100
|
</template>
|
|
96
101
|
```
|
|
102
|
+
🚀 [Live demo | Alpine.js x-match: Multiple root elements](https://jsfiddle.net/rameel/0vLksypo/)
|
|
97
103
|
|
|
98
104
|
In this example, the `x-match` directive with `<template>` tags allows rendering multiple root elements
|
|
99
105
|
(e.g., `<h2>`, `<p>`, `<a>`, and `<button>`) without needing an extra wrapper like a `<div>`.
|
|
@@ -103,6 +109,42 @@ You can find the source code for this plugin on GitHub:
|
|
|
103
109
|
|
|
104
110
|
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/match
|
|
105
111
|
|
|
112
|
+
## Related projects
|
|
113
|
+
|
|
114
|
+
**[@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>
|
|
115
|
+
Provides a combined plugin that includes several useful directives.
|
|
116
|
+
This package aggregates multiple individual plugins, offering a convenient all-in-one bundle.
|
|
117
|
+
Included directives: `x-bound`, `x-format`, `x-fragment`, `x-match`, `x-template`, and `x-when`.
|
|
118
|
+
|
|
119
|
+
**[@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>
|
|
120
|
+
Provides the `x-bound` directive, which allows for two-way binding of input elements and their associated data properties.
|
|
121
|
+
It works similarly to the binding provided by [Svelte](https://svelte.dev/docs/element-directives#bind-property)
|
|
122
|
+
and also supports synchronizing values between two `Alpine.js` data properties.
|
|
123
|
+
|
|
124
|
+
**[@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>
|
|
125
|
+
Provides the `x-format` directive, which allows you to easily interpolate text using a template syntax similar to what's available in `Vue.js`.
|
|
126
|
+
|
|
127
|
+
**[@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>
|
|
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
|
+
|
|
106
148
|
## Contributions
|
|
107
149
|
Bug reports and contributions are welcome.
|
|
108
150
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ramstack/alpinegear-match",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.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",
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
13
13
|
"alpine.js",
|
|
14
|
-
"alpinejs"
|
|
14
|
+
"alpinejs",
|
|
15
|
+
"alpinejs-directive",
|
|
16
|
+
"alpinejs-plugin"
|
|
15
17
|
],
|
|
16
18
|
"main": "alpinegear-match.js",
|
|
17
19
|
"module": "alpinegear-match.esm.js"
|