Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoroth committed Jul 19, 2022
0 parents commit 85fabc9
Show file tree
Hide file tree
Showing 9 changed files with 2,076 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*.tgz
*.tsbuildinfo
*.log
*~

/dist
node_modules/

yarn-error.log

.DS_Store
.rollup.cache
21 changes: 21 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.babelrc
.babelrc.js
.DS_Store
.gitignore
.yarn.lock

*.log
*.tsbuildinfo

README.md
rollup.config.js
tsconfig.json
webpack.config.js
yarn-error.log

/.git
/.github

/node_modules
/playground
/src
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Marco Roth

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Turbo Morph

`turbo-morph` is a [morphdom](https://github.com/patrick-steele-idem/morphdom) integration for [Turbo Streams](https://turbo.hotwired.dev/reference/streams). It provides a new Turbo Stream `morph` action.

Note: Requires Turbo **7.2+**

## Getting Started

```bash
yarn add turbo-morph
```

```diff
// application.js
import * as Turbo from '@hotwired/turbo'

+import { registerMorph } from 'turbo-morph'
+registerMorph()
```

## Example

```html
<turbo-stream action="morph" target="body">
<template>
<h1>This is the new body</h1>
</template>
</turbo-stream>
```

### `children-only` option

[`morphdom` exposes a `childrenOnly` option](https://github.com/patrick-steele-idem/morphdom#morphdomfromnode-tonode-options--node) that can be passed to a morph call.

With Turbo Streams you can apply this option by adding the `[children-only]` attribute to your `<turbo-stream>` element.

```html
<turbo-stream action="morph" target="body" children-only>
...
</turbo-stream>
```

## Usage with Rails

TBD


## Acknowledgments

tubro-morph is [MIT-licensed](LICENSE) open-source software from [Marco Roth](https://github.com/marcoroth).

Turbo is [MIT-licensed](https://github.com/hotwired/turbo/blob/main/MIT-LICENSE) open-source software from [Basecamp](https://basecamp.com/).

morphdom is [MIT-licensed](https://github.com/patrick-steele-idem/morphdom/blob/master/LICENSE) open-source software from [Patrick Steele-Idem](https://github.com/patrick-steele-idem)
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "turbo-morph",
"version": "0.1.0-beta1",
"description": "Morphdom integration for Turbo Streams",
"main": "dist/index.js",
"module": "dist/index.js",
"unpkg": "dist/index.umd.js",
"types": "dist/types/index.d.ts",
"author": "Marco Roth",
"license": "MIT",
"repository": "https://github.com/marcoroth/turbo-morph",
"sideEffects": false,
"scripts": {
"prebuild": "yarn clean",
"build": "tsc --noEmit false --declaration true --emitDeclarationOnly true --outDir dist/types --jsx react && rollup -c",
"watch": "rollup -wc",
"dev": "concurrently 'yarn run watch' 'yarn run start'",
"clean": "rimraf dist",
"prerelease": "yarn build"
},
"devDependencies": {
"@hotwired/turbo": "^7.2.0-beta.1",
"@rollup/plugin-node-resolve": "^13.0.5",
"@rollup/plugin-typescript": "^8.2.5",
"concurrently": "^7.1.0",
"rimraf": "^3.0.2",
"rollup": "^2.57.0",
"rollup-plugin-filesize": "^9.1.1",
"rollup-plugin-terser": "^7.0.2",
"sourcemap": "^0.1.0",
"tslib": "^2.3.1",
"typescript": "^3.9.7",
"morphdom": "^2.6.1"
},
"peerDependencies": {
"@hotwired/turbo": ">= 7.1",
"morphdom": ">= 2.6"
}
}
49 changes: 49 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import resolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import filesize from 'rollup-plugin-filesize'
import { terser } from 'rollup-plugin-terser'

import { version } from './package.json'
const year = new Date().getFullYear()
const banner = `/*\TurboMorph ${version}\n*/`

const minify = () => {
return terser({
mangle: true,
compress: true
})
}

export default [
{
input: 'src/index.ts',
external: [
'@hotwired/turbo'
],
output: [
{
name: 'TurboMorph',
file: 'dist/index.umd.js',
format: 'umd',
banner,
globals: {
'@hotwired/turbo': 'Turbo'
}
},
{
file: 'dist/index.js',
format: 'es',
banner
}
],
plugins: [
resolve(),
typescript(),
filesize(),
minify()
],
watch: {
include: 'src/**'
}
}
]
27 changes: 27 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import morphdom from "morphdom"

import { StreamActions } from "@hotwired/turbo"

function morph() {
const options = {
// @ts-ignore
childrenOnly: this.hasAttribute("children-only"),
onBeforeElUpdated: () => { console.log("before"); return true },
onElUpdated: () => { console.log("after") }
}

// @ts-ignore
this.targetElements.forEach(element => {
// @ts-ignore
morphdom(element, this.templateContent, options)
})
}

const registerMorph = () => {
StreamActions.morph = morph
}

export {
morph,
registerMorph
}
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"lib": ["dom", "es2015", "scripthost"],
"module": "es2015",
"moduleResolution": "node",
"noUnusedLocals": true,
"rootDir": "src",
"strict": true,
"target": "es2017",
"removeComments": true,
"outDir": "dist",
"baseUrl": ".",
"noEmit": false,
"declaration": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
}
}
Loading

0 comments on commit 85fabc9

Please sign in to comment.