@alpinejs/docs 3.7.1-revision.1 → 3.8.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.7.1-revision.1",
3
+ "version": "3.8.0-revision.1",
4
4
  "description": "The documentation for Alpine",
5
5
  "author": "Caleb Porzio",
6
6
  "license": "MIT"
@@ -17,4 +17,4 @@ If this definition is confusing for you, that's ok. It's better explained throug
17
17
 
18
18
  When this component is loaded, the `x-effect` expression will be run and "Hello" will be logged into the console.
19
19
 
20
- Because Alpine knows about any property references contained within `x-effect`, when the button is clicked and `label` is changed", the effect will be re-triggered and "Hello World!" will be logged to the console.
20
+ Because Alpine knows about any property references contained within `x-effect`, when the button is clicked and `label` is changed, the effect will be re-triggered and "Hello World!" will be logged to the console.
@@ -18,7 +18,7 @@ This is useful for things like modals (especially nesting them), where it's help
18
18
 
19
19
  By attaching `x-teleport` to a `<template>` element, you are telling Alpine to "append" that element to the provided selector.
20
20
 
21
- > The `x-template` selector can be any string you would normally pass into something like `document.querySelector`. It will find the first element that matches, be it a tag name (`body`), class name (`.my-class`), ID (`#my-id`), or any other valid CSS selector.
21
+ > The `x-teleport` selector can be any string you would normally pass into something like `document.querySelector`. It will find the first element that matches, be it a tag name (`body`), class name (`.my-class`), ID (`#my-id`), or any other valid CSS selector.
22
22
 
23
23
  [→ Read more about `document.querySelector`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector)
24
24
 
@@ -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.7.1/dist/cdn.min.js"></script>
36
+ <script defer src="https://unpkg.com/alpinejs@3.8.0/dist/cdn.min.js"></script>
37
37
  ```
38
38
 
39
39
  That's it! Alpine is now available for use inside your page.
@@ -303,7 +303,7 @@ Here is an example of a dynamically bound `class` attribute:
303
303
  As a shortcut, you can leave out the `x-bind` and use the shorthand `:` syntax directly:
304
304
 
305
305
  ```alpine
306
- <button ... :class="red ? 'bg-red' : ''>"
306
+ <button ... :class="red ? 'bg-red' : ''">
307
307
  ```
308
308
 
309
309
  Toggling classes on and off based on data inside Alpine is a common need. Here's an example of toggling a class using Alpine's `class` binding object syntax: (Note: this syntax is only available for `class` attributes)
@@ -53,7 +53,7 @@ When the `<button>` is pressed, `foo.bar` will be set to "bob", and "{bar: 'bob'
53
53
 
54
54
  ```alpine
55
55
  <!-- 🚫 Infinite loop -->
56
- <div x-data="{ foo: { bar: 'baz', bob: 'lob' }}" x-init="$watch('foo', value => foo.bob = foo.bar">
56
+ <div x-data="{ foo: { bar: 'baz', bob: 'lob' }}" x-init="$watch('foo', value => foo.bob = foo.bar)">
57
57
  <button @click="foo.bar = 'bob'">Update</button>
58
58
  </div>
59
59
  ```
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  order: 3
3
- title: Trap
4
- description: Easily trap page focus within an element (modals, dialogs, etc...)
5
- graph_image: https://alpinejs.dev/social_trap.jpg
3
+ title: Focus
4
+ description: Easily manage focus within the page
5
+ graph_image: https://alpinejs.dev/social_focus.jpg
6
6
  ---
7
7
 
8
- # Trap Plugin
8
+ # Focus Plugin
9
9
 
10
- Alpine's Trap plugin allows you to conditionally trap focus inside an element.
10
+ Alpine's Focus plugin allows you to manage focus on a page.
11
11
 
12
- This is useful for modals and other dialog elements.
12
+ > This plugin internally makes heavy use of the open source tool: [Tabbable](https://github.com/focus-trap/tabbable). Big thanks to that team for providing a much needed solution to this problem.
13
13
 
14
14
  <a name="installation"></a>
15
15
  ## Installation
@@ -22,7 +22,7 @@ You can include the CDN build of this plugin as a `<script>` tag, just make sure
22
22
 
23
23
  ```alpine
24
24
  <!-- Alpine Plugins -->
25
- <script defer src="https://unpkg.com/@alpinejs/trap@3.x.x/dist/cdn.min.js"></script>
25
+ <script defer src="https://unpkg.com/@alpinejs/focus@3.x.x/dist/cdn.min.js"></script>
26
26
 
27
27
  <!-- Alpine Core -->
28
28
  <script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
@@ -30,19 +30,19 @@ You can include the CDN build of this plugin as a `<script>` tag, just make sure
30
30
 
31
31
  ### Via NPM
32
32
 
33
- You can install Trap from NPM for use inside your bundle like so:
33
+ You can install Focus from NPM for use inside your bundle like so:
34
34
 
35
35
  ```shell
36
- npm install @alpinejs/trap
36
+ npm install @alpinejs/focus
37
37
  ```
38
38
 
39
39
  Then initialize it from your bundle:
40
40
 
41
41
  ```js
42
42
  import Alpine from 'alpinejs'
43
- import trap from '@alpinejs/trap'
43
+ import focus from '@alpinejs/focus'
44
44
 
45
- Alpine.plugin(trap)
45
+ Alpine.plugin(focus)
46
46
 
47
47
  ...
48
48
  ```
@@ -50,7 +50,7 @@ Alpine.plugin(trap)
50
50
  <a name="x-trap"></a>
51
51
  ## x-trap
52
52
 
53
- The primary API for using this plugin is the `x-trap` directive.
53
+ Focus offers a dedicated API for trapping focus within an element: the `x-trap` directive.
54
54
 
55
55
  `x-trap` accepts a JS expression. If the result of that expression is true, then the focus will be trapped inside that element until the expression becomes false, then at that point, focus will be returned to where it was previously.
56
56
 
@@ -99,7 +99,7 @@ For example:
99
99
  <!-- END_VERBATIM -->
100
100
 
101
101
  <a name="nesting"></a>
102
- ## Nesting dialogs
102
+ ### Nesting dialogs
103
103
 
104
104
  Sometimes you may want to nest one dialog inside another. `x-trap` makes this trivial and handles it automatically.
105
105
 
@@ -180,10 +180,10 @@ Here is nesting in action:
180
180
  <!-- END_VERBATIM -->
181
181
 
182
182
  <a name="modifiers"></a>
183
- ## Modifiers
183
+ ### Modifiers
184
184
 
185
185
  <a name="inert"></a>
186
- ### .inert
186
+ #### .inert
187
187
 
188
188
  When building things like dialogs/modals, it's recommended to hide all the other elements on the page from screenreaders when trapping focus.
189
189
 
@@ -214,7 +214,7 @@ By adding `.inert` to `x-trap`, when focus is trapped, all other elements on the
214
214
  ```
215
215
 
216
216
  <a name="noscroll"></a>
217
- ### .noscroll
217
+ #### .noscroll
218
218
 
219
219
  When building dialogs/modals with Alpine, it's recommended that you disable scrollling for the surrounding content when the dialog is open.
220
220
 
@@ -251,3 +251,121 @@ For example:
251
251
  </div>
252
252
  </div>
253
253
  <!-- END_VERBATIM -->
254
+
255
+ <a name="focus-magic"></a>
256
+ ## $focus
257
+
258
+ This plugin offers many smaller utilities for managing focus within a page. These utilities are exposed via the `$focus` magic.
259
+
260
+ | Property | Description |
261
+ | --- | --- |
262
+ | `focus(el)` | Focus the passed element (handling annoyances internally: using nextTick, etc.) |
263
+ | `focusable(el)` | Detect weather or not an element is focusable |
264
+ | `focusables()` | Get all "focusable" elements within the current element |
265
+ | `focused()` | Get the currently focused element on the page |
266
+ | `lastFocused()` | Get the last focused element on the page |
267
+ | `within(el)` | Specify an element to scope the `$focus` magic to (the current element by default) |
268
+ | `first()` | Focus the first focusable element |
269
+ | `last()` | Focus the last focusable element |
270
+ | `next()` | Focus the next focusable element |
271
+ | `previous()` | Focus the previous focusable element |
272
+ | `noscroll()` | Prevent scrolling to the element about to be focused |
273
+ | `wrap()` | When retrieving "next" or "previous" use "wrap around" (ex. returning the first element if getting the "next" element of the last element) |
274
+ | `getFirst()` | Retrieve the first focusable element |
275
+ | `getLast()` | Retrieve the last focusable element |
276
+ | `getNext()` | Retrieve the next focusable element |
277
+ | `getPrevious()` | Retrieve the previous focusable element |
278
+
279
+ Let's walk through a few examples of these utilities in use. The example below allows the user to control focus within the group of buttons using the arrow keys. You can test this by clicking on a button, then using the arrow keys to move focus around:
280
+
281
+ ```alpine
282
+ <div
283
+ @keydown.right="$focus.next()"
284
+ @keydown.left="$focus.previous()"
285
+ >
286
+ <button>First</button>
287
+ <button>Second</button>
288
+ <button>Third</button>
289
+ </div>
290
+ ```
291
+
292
+ <!-- START_VERBATIM -->
293
+ <div class="demo">
294
+ <div
295
+ x-data
296
+ @keydown.right="$focus.next()"
297
+ @keydown.left="$focus.previous()"
298
+ >
299
+ <button class="focus:outline-none focus:ring-2 focus:ring-aqua-400">First</button>
300
+ <button class="focus:outline-none focus:ring-2 focus:ring-aqua-400">Second</button>
301
+ <button class="focus:outline-none focus:ring-2 focus:ring-aqua-400">Third</button>
302
+ </div>
303
+ (Click a button, then use the arrow keys to move left and right)
304
+ </div>
305
+ <!-- END_VERBATIM -->
306
+
307
+ Notice how if the last button is focused, pressing "right arrow" won't do anything. Let's add the `.wrap()` method so that focus "wraps around":
308
+
309
+ ```alpine
310
+ <div
311
+ @keydown.right="$focus.wrap().next()"
312
+ @keydown.left="$focus.wrap().previous()"
313
+ >
314
+ <button>First</button>
315
+ <button>Second</button>
316
+ <button>Third</button>
317
+ </div>
318
+ ```
319
+
320
+ <!-- START_VERBATIM -->
321
+ <div class="demo">
322
+ <div
323
+ x-data
324
+ @keydown.right="$focus.wrap().next()"
325
+ @keydown.left="$focus.wrap().previous()"
326
+ >
327
+ <button class="focus:outline-none focus:ring-2 focus:ring-aqua-400">First</button>
328
+ <button class="focus:outline-none focus:ring-2 focus:ring-aqua-400">Second</button>
329
+ <button class="focus:outline-none focus:ring-2 focus:ring-aqua-400">Third</button>
330
+ </div>
331
+ (Click a button, then use the arrow keys to move left and right)
332
+ </div>
333
+ <!-- END_VERBATIM -->
334
+
335
+ Now, let's add two buttons, one to focus the first element in the button group, and another focus the last element:
336
+
337
+ ```alpine
338
+ <button @click="$focus.within($refs.buttons).first()">Focus "First"</button>
339
+ <button @click="$focus.within($refs.buttons).last()">Focus "Last"</button>
340
+
341
+ <div
342
+ x-ref="buttons"
343
+ @keydown.right="$focus.wrap().next()"
344
+ @keydown.left="$focus.wrap().previous()"
345
+ >
346
+ <button>First</button>
347
+ <button>Second</button>
348
+ <button>Third</button>
349
+ </div>
350
+ ```
351
+
352
+ <!-- START_VERBATIM -->
353
+ <div class="demo" x-data>
354
+ <button @click="$focus.within($refs.buttons).first()">Focus "First"</button>
355
+ <button @click="$focus.within($refs.buttons).last()">Focus "Last"</button>
356
+
357
+ <hr class="mt-2 mb-2"/>
358
+
359
+ <div
360
+ x-ref="buttons"
361
+ @keydown.right="$focus.wrap().next()"
362
+ @keydown.left="$focus.wrap().previous()"
363
+ >
364
+ <button class="focus:outline-none focus:ring-2 focus:ring-aqua-400">First</button>
365
+ <button class="focus:outline-none focus:ring-2 focus:ring-aqua-400">Second</button>
366
+ <button class="focus:outline-none focus:ring-2 focus:ring-aqua-400">Third</button>
367
+ </div>
368
+ </div>
369
+ <!-- END_VERBATIM -->
370
+
371
+ Notice that we needed to add a `.within()` method for each button so that `$focus` knows to scope itself to a different element (the `div` wrapping the buttons).
@@ -21,10 +21,10 @@ The best way to understand its purpose is with the following interactive visuali
21
21
 
22
22
  <div class="flex w-full justify-between" style="padding-bottom: 1rem">
23
23
  <div class="w-1/2 px-4">
24
- <button @click="slide = (slide === 1) ? 13 : slide - 1" class="w-full bg-brand rounded-full text-center py-3 font-bold text-white">Previous</button>
24
+ <button @click="slide = (slide === 1) ? 13 : slide - 1" class="w-full bg-aqua-400 rounded-full text-center py-3 font-bold text-white">Previous</button>
25
25
  </div>
26
26
  <div class="w-1/2 px-4">
27
- <button @click="slide = (slide % 13) + 1" class="w-full bg-brand rounded-full text-center py-3 font-bold text-white">Next</button>
27
+ <button @click="slide = (slide % 13) + 1" class="w-full bg-aqua-400 rounded-full text-center py-3 font-bold text-white">Next</button>
28
28
  </div>
29
29
  </div>
30
30
  </div>