From 50d507c41fe20ed420f270a711383182b5f1e7ae Mon Sep 17 00:00:00 2001 From: ed Date: Tue, 10 Sep 2024 23:34:55 +0200 Subject: [PATCH] vault backup: 2024-09-10 23:34:55 Affected files: Alpine.js DOCS.md --- Alpine.js DOCS.md | 1811 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 1792 insertions(+), 19 deletions(-) diff --git a/Alpine.js DOCS.md b/Alpine.js DOCS.md index e38b372..c4d2130 100644 --- a/Alpine.js DOCS.md +++ b/Alpine.js DOCS.md @@ -748,8 +748,11 @@ Because `x-data` is evaluated as a normal JavaScript object, in addition to st For example, let's extract the "Toggle Content" behavior into a method on `x-data`. -``` -
 
Content...
+```html +
+   +
Content...
+
``` Notice the added `toggle() { this.open = ! this.open }` method on `x-data`. This method can now be called from anywhere inside the component. @@ -758,11 +761,14 @@ You'll also notice the usage of `this.` to access state on the object itself. If you prefer, you can leave the calling parenthesis off of the `toggle` method completely. For example: -``` -  +```html + +  + + ``` -## [Getters](https://alpinejs.dev/directives/data#getters) +## Getters JavaScript [getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) are handy when the sole purpose of a method is to return data based on other state. @@ -770,52 +776,1819 @@ Think of them like "computed properties" (although, they are not cached like Vue Let's refactor our component to use a getter called `isOpen` instead of accessing `open` directly. -``` -
 
Content...
+```html +
+ + +
Content...
+
``` Notice the "Content" now depends on the `isOpen` getter instead of the `open` property directly. In this case there is no tangible benefit. But in some cases, getters are helpful for providing a more expressive syntax in your components. -## [Data-less components](https://alpinejs.dev/directives/data#data-less-components) +## Data-less components Occasionally, you want to create an Alpine component, but you don't need any data. In these cases, you can always pass in an empty object. -``` +```html
``` However, if you wish, you can also eliminate the attribute value entirely if it looks better to you. -``` +```html
``` -## [Single-element components](https://alpinejs.dev/directives/data#single-element-components) +## Single-element components Sometimes you may only have a single element inside your Alpine component, like the following: -``` -
+```html +
+ +
``` In these cases, you can declare `x-data` directly on that single element: -``` - +```html + ``` -## [Re-usable Data](https://alpinejs.dev/directives/data#re-usable-data) +## Re-usable Data If you find yourself duplicating the contents of `x-data`, or you find the inline syntax verbose, you can extract the `x-data` object out to a dedicated component using `Alpine.data`. Here's a quick example: -``` -
 
Content...
  +```html +
+   +
Content...
+
  + + ``` -[→ Read more about `Alpine.data(...)`](https://alpinejs.dev/globals/alpine-data) \ No newline at end of file +# x-init + +The `x-init` directive allows you to hook into the initialization phase of any element in Alpine. + +```html +
+``` + +In the above example, "I'm being initialized!" will be output in the console before it makes further DOM updates. + +Consider another example where `x-init` is used to fetch some JSON and store it in `x-data` before the component is processed. + +```html +
...
+``` + +## $nextTick + +Sometimes, you want to wait until after Alpine has completely finished rendering to execute some code. + +This would be something like `useEffect(..., [])` in react, or `mount` in Vue. + +By using Alpine's internal `$nextTick` magic, you can make this happen. + +```html +
+``` + +## Standalone `x-init` + +You can add `x-init` to any elements inside or outside an `x-data` HTML block. For example: + +```html +
+ +
  + +``` + +## Auto-evaluate init() method + +If the `x-data` object of a component contains an `init()` method, it will be called automatically. For example: + +```html +
...
+``` + +This is also the case for components that were registered using the `Alpine.data()` syntax. + +```js +Alpine.data('dropdown', () => ({ + init() { + console.log('I will get evaluated when initializing each "dropdown" component.') + }, +})) +``` + +If you have both an `x-data` object containing an `init()` method and an `x-init` directive, the `x-data` method will be called before the directive. + +```html +
... +
+``` + +# x-show + +`x-show` is one of the most useful and powerful directives in Alpine. It provides an expressive way to show and hide DOM elements. + +Here's an example of a simple dropdown component using `x-show`. + +```html +
+   +
Dropdown Contents...
+
+``` + +When the "Toggle Dropdown" button is clicked, the dropdown will show and hide accordingly. + +> If the "default" state of an `x-show` on page load is "false", you may want to use `x-cloak` on the page to avoid "page flicker" (The effect that happens when the browser renders your content before Alpine is finished initializing and hiding it.) You can learn more about `x-cloak` in its documentation. + +## With transitions + +If you want to apply smooth transitions to the `x-show` behavior, you can use it in conjunction with `x-transition`. You can learn more about that directive [here](https://alpinejs.dev/directives/transition), but here's a quick example of the same component as above, just with transitions applied. + +```html +
+   +
Dropdown Contents...
+
+``` + +## Using the important modifier + +Sometimes you need to apply a little more force to actually hide an element. In cases where a CSS selector applies the `display` property with the `!important` flag, it will take precedence over the inline style set by Alpine. + +In these cases you may use the `.important` modifier to set the inline style to `display: none !important`. + +```html +
+   +
Dropdown Contents...
+
+``` + +# x-bind + +`x-bind` allows you to set HTML attributes on elements based on the result of JavaScript expressions. + +For example, here's a component where we will use `x-bind` to set the placeholder value of an input. + +```html +
+ +
+``` + +## Shorthand syntax + +If `x-bind:` is too verbose for your liking, you can use the shorthand: `:`. For example, here is the same input element as above, but refactored to use the shorthand syntax. + +```html + +``` + +## Binding classes + +`x-bind` is most often useful for setting specific classes on an element based on your Alpine state. + +Here's a simple example of a simple dropdown toggle, but instead of using `x-show`, we'll use a "hidden" class to toggle an element. + +```html +
+   +
Dropdown Contents...
+
+``` + +Now, when `open` is `false`, the "hidden" class will be added to the dropdown. + +### Shorthand conditionals + +In cases like these, if you prefer a less verbose syntax you can use JavaScript's short-circuit evaluation instead of standard conditionals: + +```html +
+ +
+``` + +The inverse is also available to you. Suppose instead of `open`, we use a variable with the opposite value: `closed`. + +```html +
+ +
+``` + +### Class object syntax + +Alpine offers an additional syntax for toggling classes if you prefer. By passing a JavaScript object where the classes are the keys and booleans are the values, Alpine will know which classes to apply and which to remove. For example: + +```html +
+``` + +This technique offers a unique advantage to other methods. When using object-syntax, Alpine will NOT preserve original classes applied to an element's `class` attribute. + +For example, if you wanted to apply the "hidden" class to an element before Alpine loads, AND use Alpine to toggle its existence you can only achieve that behavior using object-syntax: + +```html +