Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move idiomorph-htmx extension over from Idiomorph repo #146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/idiomorph/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BSD Zero Clause License

Copyright (c) 2023, Alexander Petros

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
55 changes: 55 additions & 0 deletions src/idiomorph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
This extension allows you to use [Idiomorph](https://github.com/bigskysoftware/idiomorph) as the swapping mechanism in htmx.

## Install

```html
<script src="https://unpkg.com/[email protected]/idiomorph.js"></script>
```

## Usage

```html
<header>
<script src="https://unpkg.com/htmx.org@latest"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]/idiomorph.js"></script>
</header>

<body>
<div hx-ext="morph">
<button hx-get="/example" hx-swap="morph:innerHTML">
Morph My Inner HTML
</button>

<button hx-get="/example" hx-swap="morph:outerHTML">
Morph My Outer HTML
</button>

<button hx-get="/example" hx-swap="morph">
Morph My Outer HTML
</button>
</div>
</body>
```

## Configuring

The Idiomorph extension for htmx supports three different syntaxes for specifying behavior:

* `hx-swap='morph'` - This will perform a morph on the outerHTML of the target
* `hx-swap='morph:outerHTML'` - This will perform a morph on the outerHTML of the target (explicit)
* `hx-swap='morph:innerHTML'` - This will perform a morph on the innerHTML of the target (i.e. the children)
* `hx-swap='morph:<expr>'` - In this form, `<expr>` can be any valid JavaScript expression. The results of the expression
will be passed into the `Idiomorph.morph()` method as the configuration.

The last form gives you access to all the configuration options of Idiomorph. So, for example, if you wanted to ignore
the input value in a given morph, you could use the following swap specification:

```html
<button hx-get="/example"
hx-swap="morph:{ignoreActiveValue:true}"
hx-target="closest form">
Morph The Closest Form But Ignore The Active Input Value
</button>
```

24 changes: 24 additions & 0 deletions src/idiomorph/idiomorph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(function() {
function createMorphConfig(swapStyle) {
if (swapStyle === "morph" || swapStyle === "morph:outerHTML") {
return { morphStyle: "outerHTML" };
} else if (swapStyle === "morph:innerHTML") {
return { morphStyle: "innerHTML" };
} else if (swapStyle.startsWith("morph:")) {
return Function("return (" + swapStyle.slice(6) + ")")();
}
}

htmx.defineExtension("idiomorph", {
isInlineSwap: function (swapStyle) {
let config = createMorphConfig(swapStyle);
return config?.morphStyle === "outerHTML" || config?.morphStyle == null;
},
handleSwap: function (swapStyle, target, fragment) {
let config = createMorphConfig(swapStyle);
if (config) {
return Idiomorph.morph(target, fragment.children, config);
}
},
});
})()
Loading