-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6b270c
commit fab1f49
Showing
13 changed files
with
135 additions
and
22 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 |
---|---|---|
|
@@ -33,4 +33,5 @@ yarn-error.log* | |
.DS_Store | ||
*.pem | ||
|
||
data.db* | ||
data.db* | ||
static |
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
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
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,27 @@ | ||
import { Hono } from "hono"; | ||
import { serveStatic } from "hono/bun"; | ||
import { build } from "plainstack/bun"; | ||
import { render } from "plainstack/client"; | ||
import { Counter } from "./counter"; | ||
|
||
const app = new Hono(); | ||
|
||
app.use("/static/*", serveStatic({ root: "./example/2-client-component" })); | ||
|
||
await build({ | ||
entrypoints: ["example/2-client-component/counter.tsx"], | ||
outdir: "example/2-client-component/static", | ||
}); | ||
|
||
app.get("/", async (c) => { | ||
return c.html( | ||
<html lang="en"> | ||
<body> | ||
<div id="counter" /> | ||
{render(Counter, { path: "/static" })} | ||
</body> | ||
</html>, | ||
); | ||
}); | ||
|
||
export default app; |
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,15 @@ | ||
import { useState } from "hono/jsx"; | ||
import { mount } from "plainstack/client"; | ||
|
||
export function Counter() { | ||
const [count, setCount] = useState(0); | ||
return ( | ||
<div> | ||
<p>Count: {count}</p> | ||
{/* biome-ignore lint/a11y/useButtonType: <explanation> */} | ||
<button onClick={() => setCount(count + 1)}>Increment</button> | ||
</div> | ||
); | ||
} | ||
|
||
mount(Counter); |
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
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
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 @@ | ||
import { kebabCase } from "change-case"; | ||
import { type Child, render as renderHono } from "hono/jsx/dom"; | ||
import type { JSX } from "hono/jsx/jsx-runtime"; | ||
|
||
export function render<T>( | ||
Component: (props: T) => Child, | ||
options: { path: string }, | ||
props?: T, | ||
) { | ||
const name = kebabCase(Component.name); | ||
return ( | ||
<> | ||
<div id={`${name}-data`} data-props={JSON.stringify(props)} /> | ||
<script type="module" defer src={`${options.path}/${name}.js`} /> | ||
</> | ||
); | ||
} | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
export function mount(Component: (props?: any) => JSX.Element) { | ||
if (typeof document !== "undefined") { | ||
const name = kebabCase(Component.name); | ||
const dataElement = document.getElementById(`${name}-data`); | ||
if (!dataElement) { | ||
throw new Error( | ||
`unable to mount client component ${name}, data element not found. make sure you have a <div id="${name}-data"></div> in your html`, | ||
); | ||
} | ||
const dataJson = dataElement.dataset.props; | ||
const data = JSON.parse(dataJson ?? "{}"); | ||
console.info(`found data for client component ${name} in DOM`, data); | ||
const targetElement = document.getElementById(name); | ||
if (!targetElement) { | ||
throw new Error( | ||
`unable to render client component ${name}, target element not found. make sure you have a <div id="${name}"></div> in your html`, | ||
); | ||
} | ||
renderHono(<Component {...data} />, targetElement); | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
export { store } from "./entity"; | ||
export { migrate, rollback } from "./database"; | ||
export { test, dev, prod } from "./env"; | ||
export { session } from "./middleware/session"; | ||
export { job, queue, perform, work } from "./job"; | ||
export { schedule, job, queue, perform, work } from "./job"; | ||
export { form } from "./form"; | ||
export { type DB } from "./db"; |
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
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