@alpinejs/docs 3.5.2-revision.1 → 3.6.0-revision.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpinejs/docs",
3
- "version": "3.5.2-revision.1",
3
+ "version": "3.6.0-revision.1",
4
4
  "description": "The documentation for Alpine",
5
5
  "author": "Caleb Porzio",
6
6
  "license": "MIT"
@@ -0,0 +1,33 @@
1
+ ---
2
+ order: 17
3
+ title: id
4
+ ---
5
+
6
+ # x-id
7
+ `x-id` allows you to declare a new "scope" for any new IDs generated using `$id()`. It accepts an array of strings (ID names) and adds a suffix to each `$id('...')` generated within it that is unique to other IDs on the page.
8
+
9
+ `x-id` is meant to be used in conjunction with the `$id(...)` magic.
10
+
11
+ [Visit the $id documentation](/magics/id) for a better understanding of this feature.
12
+
13
+ Here's a brief example of this directive in use:
14
+
15
+ ```alpine
16
+ <div x-id="['text-input']">
17
+ <label :for="$id('text-input')">Username</label>
18
+ <!-- for="text-input-1" -->
19
+
20
+ <input type="text" :id="$id('text-input')">
21
+ <!-- id="text-input-1" -->
22
+ </div>
23
+
24
+ <div x-id="['text-input']">
25
+ <label :for="$id('text-input')">Username</label>
26
+ <!-- for="text-input-2" -->
27
+
28
+ <input type="text" :id="$id('text-input')">
29
+ <!-- id="text-input-2" -->
30
+ </div>
31
+ ```
32
+
33
+
@@ -33,7 +33,7 @@ This is by far the simplest way to get started with Alpine. Include the followin
33
33
  Notice the `@3.x.x` in the provided CDN link. This will pull the latest version of Alpine version 3. For stability in production, it's recommended that you hardcode the latest version in the CDN link.
34
34
 
35
35
  ```alpine
36
- <script defer src="https://unpkg.com/alpinejs@3.5.2/dist/cdn.min.js"></script>
36
+ <script defer src="https://unpkg.com/alpinejs@3.6.0/dist/cdn.min.js"></script>
37
37
  ```
38
38
 
39
39
  That's it! Alpine is now available for use inside your page.
@@ -0,0 +1,106 @@
1
+ ---
2
+ order: 9
3
+ prefix: $
4
+ title: id
5
+ ---
6
+
7
+ # $id
8
+
9
+ `$id` is a magic property that can be used to generate an element's ID and ensure that it is within on the page and won't conflict with other IDs of the same name.
10
+
11
+ This utility is extremely helpful when building re-usable components (presumably in a back-end template) that might occur multiple times on a page, and make use of ID attributes.
12
+
13
+ Things like input components, modals, listboxes, etc. will all benefit from this utility.
14
+
15
+ <a name="basic-usage"></a>
16
+ ## Basic usage
17
+
18
+ Suppose you have two input elements on a page, and you want them to have a unique ID from each other, you can do the following:
19
+
20
+ ```alpine
21
+ <input type="text" :id="$id('text-input')">
22
+ <!-- id="text-input-1" -->
23
+
24
+ <input type="text" :id="$id('text-input')">
25
+ <!-- id="text-input-2" -->
26
+ ```
27
+
28
+ As you can see, `$id` takes in a string and spits out an appended suffix that is unique on the page.
29
+
30
+ <a name="groups-with-x-id"></a>
31
+ ## Grouping with x-id
32
+
33
+ Now let's say you want to have those same two input elements, but this time you want `<label>` elements for each of them.
34
+
35
+ This presents a problem, you now need to be able to reference the same ID twice. One for the `<label>`'s `for` attribute, and the other for the `id` on the input.
36
+
37
+ Here's is a way that you might think to accomplish this and is totally valid:
38
+
39
+ ```alpine
40
+ <div x-data="{ id: $id('text-input') }">
41
+ <label :for="id"> <!-- "text-input-1" -->
42
+ <input type="text" :id="id"> <!-- "text-input-1" -->
43
+ </div>
44
+
45
+ <div x-data="{ id: $id('text-input') }">
46
+ <label :for="id"> <!-- "text-input-2" -->
47
+ <input type="text" :id="id"> <!-- "text-input-2" -->
48
+ </div>
49
+ ```
50
+
51
+ This approach is fine, however, having to name and store the ID in your component scope feels cumbersome.
52
+
53
+ To accomplish this same task in a more flexible way, you can use Alpine's `x-id` directive to declare an "id scope" for a set of IDs:
54
+
55
+ ```alpine
56
+ <div x-id="['text-input']">
57
+ <label :for="$id('text-input')"> <!-- "text-input-1" -->
58
+ <input type="text" :id="$id('text-input')"> <!-- "text-input-1" -->
59
+ </div>
60
+
61
+ <div x-id="['text-input']">
62
+ <label :for="$id('text-input')"> <!-- "text-input-2" -->
63
+ <input type="text" :id="$id('text-input')"> <!-- "text-input-2" -->
64
+ </div>
65
+ ```
66
+
67
+ As you can see, `x-id` accepts an array of ID names. Now any usages of `$id()` within that scope, will all use the same ID. Think of them as "id groups".
68
+
69
+ <a name="nesting"></a>
70
+ ## Nesting
71
+
72
+ As you might have intuited, you can freely nest these `x-id` groups, like so:
73
+
74
+ ```alpine
75
+ <div x-id="['text-input']">
76
+ <label :for="$id('text-input')"> <!-- "text-input-1" -->
77
+ <input type="text" :id="$id('text-input')"> <!-- "text-input-1" -->
78
+
79
+ <div x-id="['text-input']">
80
+ <label :for="$id('text-input')"> <!-- "text-input-2" -->
81
+ <input type="text" :id="$id('text-input')"> <!-- "text-input-2" -->
82
+ </div>
83
+ </div>
84
+ ```
85
+
86
+ <a name="keyed-ids"></a>
87
+ ## Keyed IDs (For Looping)
88
+
89
+ Sometimes, it is helpful to specify an additional suffix on the end of an ID for the purpose of identifying it within a loop.
90
+
91
+ For this, `$id()` accepts an optional second parameter that will be added as a suffix on the end of the generated ID.
92
+
93
+ A common example of this need is something like a listbox component that uses the `aria-activedescendant` attribute to tell assistive technologies which element is "active" in the list:
94
+
95
+ ```alpine
96
+ <ul
97
+ x-id="['list-item']"
98
+ :aria-activedescendant="$id('list-item', activeItem.id)"
99
+ >
100
+ <template x-for="item in items" :key="item.id">
101
+ <li :id="$id('list-item', item.id)">...</li>
102
+ </template>
103
+ </ul>
104
+ ```
105
+
106
+ This is an incomplete example of a listbox, but it should still be helpful to demonstrate a scenario where you might need each ID in a group to still be unique to the page, but also be keyed within a loop so that you can reference individual IDs within that group.
@@ -61,6 +61,7 @@ Then initialize it from your bundle:
61
61
  import Alpine from 'alpinejs'
62
62
  import morph from '@alpinejs/morph'
63
63
 
64
+ window.Alpine = Alpine
64
65
  Alpine.plugin(morph)
65
66
 
66
67
  ...