|
| 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 | + |
| 6 | +const Header = ({ name }) => html` |
| 7 | + <h1 class="text-3xl font-bold text-gray-800 mb-4">${name} List</h1> |
| 8 | +` |
| 9 | + |
| 10 | +const Footer = props => { |
| 11 | + const count = useSignal(0) |
| 12 | + const double = useComputed(() => count.value * 2) |
| 13 | + |
| 14 | + const handleClick = useCallback(() => { |
| 15 | + count.value++ |
| 16 | + }, [count]) |
| 17 | + |
| 18 | + return html` |
| 19 | + <footer class="mt-8 p-4 bg-gray-100 rounded shadow flex flex-col gap-2" ...${props}> |
| 20 | + <div class="text-gray-600">Count: ${count}</div> |
| 21 | + <div class="text-gray-600">Double: ${double}</div> |
| 22 | + <div>${props.children}</div> |
| 23 | + <button |
| 24 | + onClick=${handleClick} |
| 25 | + class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700 transition" |
| 26 | + > |
| 27 | + Click |
| 28 | + </button> |
| 29 | + </footer> |
| 30 | + ` |
| 31 | +} |
| 32 | + |
| 33 | +class App extends Component { |
| 34 | + addTodo () { |
| 35 | + const { todos = [] } = this.state |
| 36 | + this.setState({ todos: todos.concat(`Item ${todos.length}`) }) |
| 37 | + } |
| 38 | + |
| 39 | + render ({ page }, { todos = [] }) { |
| 40 | + return html` |
| 41 | + <div class="app max-w-xl mx-auto p-6 bg-white rounded shadow"> |
| 42 | + <${Header} name="ToDo's (${page})" /> |
| 43 | + <ul class="space-y-2 mb-4"> |
| 44 | + ${todos.map(todo => html` |
| 45 | + <li key=${todo} class="p-2 bg-gray-50 rounded shadow">${todo}</li> |
| 46 | + `)} |
| 47 | + </ul> |
| 48 | + <button |
| 49 | + onClick=${() => this.addTodo()} |
| 50 | + class="bg-green-600 text-white px-4 py-2 rounded hover:bg-green-700 transition" |
| 51 | + > |
| 52 | + Add Todo |
| 53 | + </button> |
| 54 | + <${Footer}>footer content here<//> |
| 55 | + </div> |
| 56 | + ` |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export const page = () => html` |
| 61 | + <div class="space-y-6"> |
| 62 | + <${App} page="Isomorphic"/> |
| 63 | + <${Footer}>footer content here<//> |
| 64 | + <${Footer}>footer content here<//> |
| 65 | + </div> |
| 66 | +` |
| 67 | + |
| 68 | +if (typeof window !== 'undefined') { |
| 69 | + const renderTarget = document.querySelector('.app-main') |
| 70 | + render(page(), renderTarget) |
| 71 | +} |
0 commit comments