|
2 | 2 | title = "htmx Idiomorph Extension" |
3 | 3 | +++ |
4 | 4 |
|
5 | | -[Idiomorph](https://github.com/bigskysoftware/idiomorph) is a DOM morphing algorithm created by the htmx creator. DOM |
| 5 | +[Idiomorph](https://github.com/bigskysoftware/idiomorph) is a DOM morphing algorithm created by the htmx creator. DOM |
6 | 6 | morphing is a process where an existing DOM tree is "morphed" into the shape of another in a way that resuses as much of |
7 | | -the existing DOM's nodes as possible. By preserving nodes when changing from one tree to another you can present a |
| 7 | +the existing DOM's nodes as possible. By preserving nodes when changing from one tree to another you can present a |
8 | 8 | much smoother transition between the two states. |
9 | 9 |
|
10 | | -You can use the idiomorph morphing algorithm as a [swapping](@attributes/hx-swap) strategy by including the idiomorph |
| 10 | +You can use the idiomorph morphing algorithm as a [swapping](@attributes/hx-swap) strategy by including the idiomorph |
11 | 11 | extension. |
12 | 12 |
|
13 | 13 | ## Installing |
14 | 14 |
|
15 | 15 | The fastest way to install `idiomorph` is to load it via a CDN. Remember to always include the core htmx library before the extension and [enable the extension](#usage). |
| 16 | + |
16 | 17 | ```HTML |
17 | 18 | <head> |
18 | 19 | < script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/[email protected]" integrity= "sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin= "anonymous"></ script> |
19 | | - < script src= "https://unpkg.com/[email protected]" integrity= "sha384-JcorokHTL/m+D6ZHe2+yFVQopVwZ+91GxAPDyEZ6/A/OEPGEx1+MeNSe2OGvoRS9" crossorigin= "anonymous"></ script> |
20 | 20 | < script src= "https://unpkg.com/[email protected]/dist/idiomorph-ext.min.js" integrity= "sha384-szktAZju9fwY15dZ6D2FKFN4eZoltuXiHStNDJWK9+FARrxJtquql828JzikODob" crossorigin= "anonymous"></ script> |
21 | 21 | </head> |
22 | 22 | <body hx-ext="morph"> |
23 | 23 | ``` |
24 | | -Unminified versions are also available at: |
25 | | -https://unpkg.com/idiomorph/dist/idiomorph.js |
26 | | -https://unpkg.com/idiomorph/dist/idiomorph-ext.js |
27 | 24 |
|
28 | | -While the CDN approach is simple, you may want to consider [not using CDNs in production](https://blog.wesleyac.com/posts/why-not-javascript-cdn). The next easiest way to install `idiomorph` is to simply copy it into your project. Download idiomorph and its htmx extension from `https://unpkg.com/idiomorph` and `https://unpkg.com/idiomorph/dist/idiomorph-ext.min.js`, add them to the appropriate directory in your project and include them where necessary with `<script>` tags. |
| 25 | +Unminified versions are also available at: |
| 26 | +<https://unpkg.com/idiomorph/dist/idiomorph-ext.js> |
| 27 | + |
| 28 | +While the CDN approach is simple, you may want to consider [not using CDNs in production](https://blog.wesleyac.com/posts/why-not-javascript-cdn). The next easiest way to install `idiomorph` is to simply copy it into your project. Download idiomorph with htmx extension from `https://unpkg.com/idiomorph/dist/idiomorph-ext.min.js`, add them to the appropriate directory in your project and include them where necessary with `<script>` tags. |
29 | 29 |
|
30 | 30 | For npm-style build systems, you can install `idiomorph` via [npm](https://www.npmjs.com/): |
| 31 | + |
31 | 32 | ```shell |
32 | 33 | npm install idiomorph |
33 | 34 | ``` |
34 | | -After installing, you'll need to use appropriate tooling to bundle `node_modules/idiomorph/dist/idiomorph.js` (or `.min.js`) and `node_modules/idiomorph/dist/idiomorph-ext.js`. For example, you might bundle the extension with htmx core from `node_modules/htmx.org/dist/htmx.js` and project-specific code. |
| 35 | + |
| 36 | +After installing, you'll need to use appropriate tooling to bundle `node_modules/idiomorph/dist/idiomorph-ext.js` (or `node_modules/idiomorph/dist/idiomorph-ext.min.js`). For example, you might bundle the extension with htmx core from `node_modules/htmx.org/dist/htmx.js` and project-specific code. |
35 | 37 |
|
36 | 38 | If you are using a bundler to manage your javascript (e.g. Webpack, Rollup): |
| 39 | + |
37 | 40 | - Install `htmx.org` and `idiomorph` via npm |
38 | 41 | - Import both packages to your `index.js` |
| 42 | + |
39 | 43 | ```JS |
40 | 44 | import `htmx.org`; |
41 | | -import `idiomorph`; |
| 45 | +import `idiomorph/htmx`; |
42 | 46 | ``` |
43 | 47 |
|
44 | 48 | ## Usage |
45 | 49 |
|
46 | 50 | Once you have referenced the idiomorph extension, you can register it with the name `morph` on the body and then being |
47 | 51 | using `morph`, `morph:outerHTML` or `morph:innerHTML` as swap strategies. |
48 | 52 |
|
49 | | -* `morph` & `morph:outerHTML` will morph the target element as well as it's children |
50 | | -* `morph:innerHTML` will morph only the inner children of an element, leaving the target untouched |
| 53 | +- `morph` & `morph:outerHTML` will morph the target element as well as it's children |
| 54 | +- `morph:innerHTML` will morph only the inner children of an element, leaving the target untouched |
51 | 55 |
|
52 | 56 | ```html |
53 | 57 | <body hx-ext="morph"> |
| 58 | + <button hx-get="/example" hx-swap="morph"> |
| 59 | + Morph My Outer HTML |
| 60 | + </button> |
54 | 61 |
|
55 | | - <button hx-get="/example" hx-swap="morph"> |
56 | | - Morph My Outer HTML |
57 | | - </button> |
58 | | - |
59 | | - <button hx-get="/example" hx-swap="morph:outerHTML"> |
60 | | - Morph My Outer HTML |
61 | | - </button> |
62 | | - |
63 | | - <button hx-get="/example" hx-swap="morph:innerHTML"> |
64 | | - Morph My Inner HTML |
65 | | - </button> |
| 62 | + <button hx-get="/example" hx-swap="morph:outerHTML"> |
| 63 | + Morph My Outer HTML |
| 64 | + </button> |
66 | 65 |
|
| 66 | + <button hx-get="/example" hx-swap="morph:innerHTML"> |
| 67 | + Morph My Inner HTML |
| 68 | + </button> |
67 | 69 | </body> |
68 | 70 | ``` |
69 | | - |
|
0 commit comments