-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomponents.js
30 lines (27 loc) · 1.05 KB
/
components.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { h } from 'https://esm.sh/preact@10.4.7'
import { useReducer } from 'https://esm.sh/preact@10.4.7/hooks'
import htm from 'https://esm.sh/htm@3.0.4'
const html = htm.bind(h)
export default function() {
return {
Counter: (props) => {
const heading = props.heading || 'A simple counter component'
const [count, dispatch] = useReducer((state, action) => {
switch (action) {
case 'increment': return state + 1
case 'decrement': return state - 1
case 'reset': return 0
}
}, 0)
return html`<div>
<h2>${heading}</h2>
<p>Current value: ${count}</p>
<div class="flex form-group">
<button class="btn btn-primary" onClick="${() => dispatch('increment')}" dangerouslySetInnerHTML=${{ __html: '+' }} />
<button class="btn btn-primary" onClick="${() => dispatch('decrement')}" dangerouslySetInnerHTML=${{ __html: '−' }} />
<button class="btn btn" onClick="${() => dispatch('reset')}">Reset</button>
</div>
</div>`
}
}
}