Skip to content

Commit

Permalink
feat: add @fresh/examples package (#2555)
Browse files Browse the repository at this point in the history
This can be used to showcase remote islands, combining apps and other
things.
  • Loading branch information
marvinhagemeister authored Jun 28, 2024
1 parent 138d4c4 commit 9fe48d4
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ jobs:
- name: Publish @fresh/update
working-directory: ./update
run: deno publish

- name: Publish @fresh/examples
working-directory: ./examples
run: deno publish
20 changes: 20 additions & 0 deletions examples/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2021-2024 the Deno authors

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55 changes: 55 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Examples for Fresh

This is an package contains examples for using Fresh with
[JSR](https://jsr.io/).

Learn more about the Fresh framework here:
[https://fresh.deno.dev/](https://fresh.deno.dev/)

## Usage: Island example

```tsx
import { App } from "fresh";
// Import the island function
import { DemoIsland } from "jsr:@fresh/examples/island";

export const app = new App({ root: import.meta.url })
.use(staticFiles());

// Register the island
app.island(
// Module specifier for esbuild, could also be a file path
"jsr:@fresh/examples/island",
// Name of the island
"DemoIsland",
// Island component function
DemoIsland,
);

// Use the island somewhere in your components
app.get("/", (ctx) => ctx.render(<DemoIsland />));

await app.listen();
```

## Usage: App1 or App2 example

```tsx
import { App } from "fresh";
// Import the example apps
import { app1 } from "jsr:@fresh/examples/app1";
import { app2 } from "jsr:@fresh/examples/app2";

export const app = new App({ root: import.meta.url })
.use(staticFiles());

// Merge apps from JSR into this one
app.mountApp("/app1", app1);
app.mountApp("/app2", app1);

await app.listen();
```

## License

MIT, see the [LICENSE](./LICENSE) file.
18 changes: 18 additions & 0 deletions examples/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@fresh/examples",
"version": "1.0.0",
"exports": {
"./island": "./src/island.tsx",
"./app1": "./src/app1.tsx",
"./app2": "./src/app2.tsx"
},
"imports": {
"@preact/signals": "npm:@preact/signals@^1.2.3",
"preact": "npm:preact@^10.22.0",
"fresh": "jsr:@fresh/core@^2.0.0-alpha.18"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
96 changes: 96 additions & 0 deletions examples/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions examples/src/app1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { App } from "fresh";
import { Doc } from "./shared.tsx";

export const app1 = new App()
.get("/", (ctx) =>
ctx.render(
<Doc>
<h1>App1</h1>
<p>This app is loaded from JSR</p>
</Doc>,
));
11 changes: 11 additions & 0 deletions examples/src/app2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { App } from "fresh";
import { Doc } from "./shared.tsx";

export const app2 = new App()
.get("/", (ctx) =>
ctx.render(
<Doc>
<h1>App2</h1>
<p>This app is loaded from JSR</p>
</Doc>,
));
13 changes: 13 additions & 0 deletions examples/src/island.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useSignal } from "@preact/signals";

export default function DemoIsland() {
const count = useSignal(0);

return (
<div style="display: flex; gap: 1rem; padding: 2rem;">
<button onClick={() => (count.value -= 1)}>-1</button>
<p style="font-variant-numeric: tabular-nums;">{count}</p>
<button onClick={() => (count.value += 1)}>+1</button>
</div>
);
}
13 changes: 13 additions & 0 deletions examples/src/shared.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { ComponentChildren } from "preact";

export function Doc(props: { children?: ComponentChildren }) {
return (
<html>
<head>
<meta charset="utf-8" />
<title>Fresh Examples</title>
</head>
<body>{props.children}</body>
</html>
);
}

0 comments on commit 9fe48d4

Please sign in to comment.