@alpinejs/docs 3.7.0-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.0-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.
@@ -234,6 +234,14 @@ The above example is a great use case of throttling. Without `.throttle`, the `h
234
234
 
235
235
  > Fun Fact: This exact strategy is used on this very documentation site to update the currently highlighted section in the right sidebar.
236
236
 
237
+ Just like with `.debounce`, you can add a custom duration to your throttled event:
238
+
239
+ ```alpine
240
+ <div @scroll.window.throttle.750ms="handleScroll">...</div>
241
+ ```
242
+
243
+ Now, `handleScroll` will only be called every 750 milliseconds.
244
+
237
245
  <a name="self"></a>
238
246
  ### .self
239
247
 
@@ -5,18 +5,22 @@ description: Send Alpine templates to other parts of the DOM
5
5
  graph_image: https://alpinejs.dev/social_teleport.jpg
6
6
  ---
7
7
 
8
- # Teleport Plugin
8
+ # x-teleport
9
9
 
10
- Alpine's Teleport plugin allows you to transport part of your Alpine template to another part of the DOM on the page entirely.
10
+ The `x-teleport` directive allows you to transport part of your Alpine template to another part of the DOM on the page entirely.
11
11
 
12
12
  This is useful for things like modals (especially nesting them), where it's helpful to break out of the z-index of the current Alpine component.
13
13
 
14
+ > Warning: if you are a [Livewire](https://laravel-livewire.com) user, this functionality does not currently work inside Livewire components. Support for this is on the roadmap.
15
+
14
16
  <a name="x-teleport"></a>
15
17
  ## x-teleport
16
18
 
17
19
  By attaching `x-teleport` to a `<template>` element, you are telling Alpine to "append" that element to the provided selector.
18
20
 
19
- > The `x-template` selector can be any string you would normally pass into something like `document.querySelector`
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
+
23
+ [→ Read more about `document.querySelector`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector)
20
24
 
21
25
  Here's a contrived modal example:
22
26
 
@@ -61,11 +65,11 @@ Notice how when toggling the modal, the actual modal contents show up AFTER the
61
65
  <a name="forwarding-events"></a>
62
66
  ## Forwarding events
63
67
 
64
- Alpine tries it's best to make the experience of telporting seemless. Anything you would normally do in a template, you should be able to do inside an `x-teleport` template. Teleported content can access the normal Alpine scope of the component as well as other features like `$refs`, `$root`, etc...
68
+ Alpine tries its best to make the experience of teleporting seamless. Anything you would normally do in a template, you should be able to do inside an `x-teleport` template. Teleported content can access the normal Alpine scope of the component as well as other features like `$refs`, `$root`, etc...
65
69
 
66
70
  However, native DOM events have no concept of teleportation, so if, for example, you trigger a "click" event from inside a teleported element, that event will bubble up the DOM tree as it normally would.
67
71
 
68
- To make this experience more seemless, you can "forward" events by simply registering event listeners on the `<template x-teleport...>` element itself like so:
72
+ To make this experience more seamless, you can "forward" events by simply registering event listeners on the `<template x-teleport...>` element itself like so:
69
73
 
70
74
  ```alpine
71
75
  <div x-data="{ open: false }">
@@ -138,11 +138,11 @@ For direct control over exactly what goes into your transitions, you can apply C
138
138
  <div
139
139
  x-show="open"
140
140
  x-transition:enter="transition ease-out duration-300"
141
- x-transition:enter-start="opacity-0 transform scale-90"
142
- x-transition:enter-end="opacity-100 transform scale-100"
141
+ x-transition:enter-start="opacity-0 scale-90"
142
+ x-transition:enter-end="opacity-100 scale-100"
143
143
  x-transition:leave="transition ease-in duration-300"
144
- x-transition:leave-start="opacity-100 transform scale-100"
145
- x-transition:leave-end="opacity-0 transform scale-90"
144
+ x-transition:leave-start="opacity-100 scale-100"
145
+ x-transition:leave-end="opacity-0 scale-90"
146
146
  >Hello 👋</div>
147
147
  </div>
148
148
  ```
@@ -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.0/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)
@@ -35,3 +35,25 @@ When the `<button>` is pressed, `foo.bar` will be set to "bob", and "bob" will b
35
35
  <button @click="open = ! open">Toggle Open</button>
36
36
  </div>
37
37
  ```
38
+
39
+ <a name="deep-watching"></a>
40
+ ### Deep watching
41
+
42
+ `$watch` will automatically watches from changes at any level but you should keep in mind that, when a change is detected, the watcher will return the value of the observed property, not the value of the subproperty that has changed.
43
+
44
+ ```alpine
45
+ <div x-data="{ foo: { bar: 'baz' }}" x-init="$watch('foo', (value, oldValue) => console.log(value, oldValue))">
46
+ <button @click="foo.bar = 'bob'">Update</button>
47
+ </div>
48
+ ```
49
+
50
+ When the `<button>` is pressed, `foo.bar` will be set to "bob", and "{bar: 'bob'} {bar: 'baz'}" will be logged to the console (new and old value).
51
+
52
+ > ⚠️ Changing a property of a "watched" object as a side effect of the `$watch` callback will generate an infinite loop and eventually error.
53
+
54
+ ```alpine
55
+ <!-- 🚫 Infinite loop -->
56
+ <div x-data="{ foo: { bar: 'baz', bob: 'lob' }}" x-init="$watch('foo', value => foo.bob = foo.bar)">
57
+ <button @click="foo.bar = 'bob'">Update</button>
58
+ </div>
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>
@@ -88,7 +88,7 @@ You can listen for other events as you'd imagine. For example, listening for a `
88
88
 
89
89
  When a `click` event happens, Alpine will call the associated JavaScript expression, `count++` in our case. As you can see, we have direct access to data declared in the `x-data` expression.
90
90
 
91
- > You will often see `@` instead of `x-on`. This is a shorter, friendlier syntax that many prefer. From now on, this documentation will likely use `@` instead of `x-on`.
91
+ > You will often see `@` instead of `x-on:`. This is a shorter, friendlier syntax that many prefer. From now on, this documentation will likely use `@` instead of `x-on:`.
92
92
 
93
93
  [→ Read more about `x-on`](/directives/on)
94
94