Skip to content

Commit

Permalink
Add a simple example of counter with ESM (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Nov 18, 2021
1 parent a173b32 commit af3519c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
60 changes: 60 additions & 0 deletions examples/counter-with-esm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Counter with ESM

Yes, you can use Teaful without compilers like Webpack, only a plain `html` file.

To test it:

```bh
touch example.html
```

Edit `example.html` file to:

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" />
<script type="module">
import createStore from 'https://cdn.skypack.dev/teaful'
import React from 'https://cdn.skypack.dev/react'
import ReactDOM from 'https://cdn.skypack.dev/react-dom'
import htm from 'https://cdn.skypack.dev/htm'
const html = htm.bind(React.createElement)
const initialStore = { count: 0 }
const { useStore } = createStore(initialStore)
function Counter() {
const [count, setCount] = useStore.count()
return html`
<div>
<h1>${count}</h1>
<button onClick=${() => setCount(count + 1)}>+</button>
<button onClick=${() => setCount(count - 1)}>-</button>
<button onClick=${() => setCount(initialStore.count)}>Reset</button>
</div>
`
}
function App() {
return html`
<h1>Teaful Counter</h1>
<${Counter} />
`
}
ReactDOM.render(html`<${App} />`, document.getElementById('root'))
</script>
<title>Teaful Counter</title>
</head>
<body id="root" />
</html>
```

And finally:

```bh
open example.html
```
40 changes: 40 additions & 0 deletions examples/counter-with-esm/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" />
<script type="module">
import createStore from 'https://cdn.skypack.dev/teaful'
import React from 'https://cdn.skypack.dev/react'
import ReactDOM from 'https://cdn.skypack.dev/react-dom'
import htm from 'https://cdn.skypack.dev/htm'

const html = htm.bind(React.createElement)
const initialStore = { count: 0 }
const { useStore } = createStore(initialStore)

function Counter() {
const [count, setCount] = useStore.count()
return html`
<div>
<h1>${count}</h1>
<button onClick=${() => setCount(count + 1)}>+</button>
<button onClick=${() => setCount(count - 1)}>-</button>
<button onClick=${() => setCount(initialStore.count)}>Reset</button>
</div>
`
}

function App() {
return html`
<h1>Teaful Counter</h1>
<${Counter} />
`
}

ReactDOM.render(html`<${App} />`, document.getElementById('root'))
</script>
<title>Teaful Counter</title>
</head>
<body id="root" />
</html>

0 comments on commit af3519c

Please sign in to comment.