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

Add a simple example of counter with ESM #47

Merged
merged 1 commit into from
Nov 18, 2021
Merged
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
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>