-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple example of counter with ESM (#47)
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |