|
| 1 | +import { html, Component } from 'htm/preact' |
| 2 | +import { render } from 'preact' |
| 3 | +import { useCallback } from 'preact/hooks' |
| 4 | +import { useSignal, useComputed } from '@preact/signals' |
| 5 | +import { outerShell } from './app.module.css' |
| 6 | + |
| 7 | +console.log({ outerShell }) |
| 8 | + |
| 9 | +const Header = ({ name }) => html`<h1>${name} List</h1>` |
| 10 | + |
| 11 | +const Footer = props => { |
| 12 | + const count = useSignal(0) |
| 13 | + const double = useComputed(() => count.value * 2) |
| 14 | + |
| 15 | + const handleClick = useCallback(() => { |
| 16 | + count.value++ |
| 17 | + }, [count]) |
| 18 | + |
| 19 | + return html`<footer ...${props}> |
| 20 | + ${count} |
| 21 | + ${double} |
| 22 | + ${props.children} |
| 23 | + <button onClick=${handleClick}>Click</button> |
| 24 | + </footer>` |
| 25 | +} |
| 26 | + |
| 27 | +class App extends Component { |
| 28 | + addTodo () { |
| 29 | + const { todos = [] } = this.state |
| 30 | + this.setState({ todos: todos.concat(`Item ${todos.length}`) }) |
| 31 | + } |
| 32 | + |
| 33 | + render ({ page }, { todos = [] }) { |
| 34 | + return html` |
| 35 | + <div class="${`app ${outerShell}`}"> |
| 36 | + <${Header} name="ToDo's (${page})" /> |
| 37 | + <ul> |
| 38 | + ${todos.map(todo => html` |
| 39 | + <li key=${todo}>${todo}</li> |
| 40 | + `)} |
| 41 | + </ul> |
| 42 | + <button onClick=${() => this.addTodo()}>Add Todo</button> |
| 43 | + <${Footer}>footer content here<//> |
| 44 | + </div> |
| 45 | + ` |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +export const page = () => html` |
| 50 | + <${App} page="Isomorphic"/> |
| 51 | + <${Footer}>footer content here<//> |
| 52 | + <${Footer}>footer content here<//> |
| 53 | + ` |
| 54 | + |
| 55 | +if (typeof window !== 'undefined') { |
| 56 | + const renderTarget = document.querySelector('.app-main') |
| 57 | + render(page(), renderTarget) |
| 58 | +} |
0 commit comments