www: new fenced code block design (#1629)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
---
|
||||
description: |
|
||||
Add JavaScript based interactivity to your project without sacrificing user
|
||||
experience, by using Fresh's powerful islands system.
|
||||
experience, by using Fresh's powerful islands system.
|
||||
---
|
||||
|
||||
Up to now none of the pages in the demo project have contained any client side
|
||||
@@ -28,9 +28,7 @@ component. For example a counter component would be defined in the file
|
||||
|
||||
Here is an example of an island component that counts down to a specific time.
|
||||
|
||||
```tsx
|
||||
// islands/Countdown.tsx
|
||||
|
||||
```tsx islands/Countdown.tsx
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { useEffect } from "preact/hooks";
|
||||
|
||||
@@ -55,7 +53,7 @@ export default function Countdown(props: { target: string }) {
|
||||
}, [props.target]);
|
||||
|
||||
const secondsLeft = Math.floor(
|
||||
(target.getTime() - now.value.getTime()) / 1000,
|
||||
(target.getTime() - now.value.getTime()) / 1000
|
||||
);
|
||||
|
||||
// If the target date has passed, we stop counting down.
|
||||
@@ -73,9 +71,7 @@ To include this in a page component, one can just use the component normally.
|
||||
Fresh will take care of automatically mounting the island component on the
|
||||
client with the correct props:
|
||||
|
||||
```tsx
|
||||
// routes/countdown.tsx
|
||||
|
||||
```tsx routes/countdown.tsx
|
||||
import Countdown from "../islands/Countdown.tsx";
|
||||
|
||||
export default function Page() {
|
||||
|
||||
@@ -9,7 +9,7 @@ will scaffold out a new project with some example files to get you started.
|
||||
|
||||
To create a new project, run:
|
||||
|
||||
```
|
||||
```sh Terminal
|
||||
deno run -A -r https://fresh.deno.dev
|
||||
cd fresh-project
|
||||
deno task start
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
description: |
|
||||
Create a new route to a Fresh project by creating a new file in the `routes/`
|
||||
folder.
|
||||
folder.
|
||||
---
|
||||
|
||||
After getting the project running locally, the next step is to add a new route
|
||||
@@ -34,9 +34,7 @@ This is done with JSX.
|
||||
> [Preact][preact], a lighter weight virtual dom library that works similar to
|
||||
> React.
|
||||
|
||||
```tsx
|
||||
// routes/about.tsx
|
||||
|
||||
```tsx routes/about.tsx
|
||||
export default function AboutPage() {
|
||||
return (
|
||||
<main>
|
||||
|
||||
@@ -27,9 +27,7 @@ the HTTP method it handles.
|
||||
Here is an example of a custom `GET` handler that renders the page component and
|
||||
then adds a custom header to the response before returning it:
|
||||
|
||||
```tsx
|
||||
// routes/about.tsx
|
||||
|
||||
```tsx routes/about.tsx
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
@@ -54,9 +52,7 @@ Note that handlers do not need to call `ctx.render()`. This feature can be used
|
||||
to create API routes. Here is an API route that returns a random UUID as a JSON
|
||||
response:
|
||||
|
||||
```ts
|
||||
// routes/api/random-uuid.ts
|
||||
|
||||
```ts routes/api/random-uuid.ts
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
|
||||
@@ -24,9 +24,7 @@ render a page. The module must once again expose a component as a default
|
||||
export. This time the component will receive the matched path segment properties
|
||||
as arguments in its `props` object though.
|
||||
|
||||
```tsx
|
||||
// routes/greet/[name].tsx
|
||||
|
||||
```tsx routes/greet/[name].tsx
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function GreetPage(props: PageProps) {
|
||||
|
||||
@@ -17,9 +17,7 @@ The second `ctx` parameter is used to get the route parameters.
|
||||
Here is an example of a route that fetches user data from the GitHub API and
|
||||
renders it in a page component.
|
||||
|
||||
```tsx
|
||||
// routes/github/[username].tsx
|
||||
|
||||
```tsx routes/github/[username].tsx
|
||||
import { RouteContext } from "$fresh/server.ts";
|
||||
|
||||
interface GitHubResponse {
|
||||
@@ -30,14 +28,14 @@ interface GitHubResponse {
|
||||
|
||||
export default async function Page(_req: Request, ctx: RouteContext) {
|
||||
const resp = await fetch(
|
||||
`https://api.github.com/users/${ctx.params.username}`,
|
||||
`https://api.github.com/users/${ctx.params.username}`
|
||||
);
|
||||
|
||||
if (!resp.ok) {
|
||||
return <h1>An Error occurred</h1>;
|
||||
}
|
||||
|
||||
const { login, name, avatar_url } = await resp.json() as GitHubResponse;
|
||||
const { login, name, avatar_url } = (await resp.json()) as GitHubResponse;
|
||||
|
||||
return (
|
||||
<div>
|
||||
|
||||
@@ -29,9 +29,7 @@ any necessary processing on the form data, and then pass data to the
|
||||
Here is an example implementing a search form that filters an array of names
|
||||
server side:
|
||||
|
||||
```tsx
|
||||
// routes/search.tsx
|
||||
|
||||
```tsx routes/search.tsx
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
|
||||
const NAMES = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Frank"];
|
||||
@@ -59,7 +57,9 @@ export default function Page({ data }: PageProps<Data>) {
|
||||
<button type="submit">Search</button>
|
||||
</form>
|
||||
<ul>
|
||||
{results.map((name) => <li key={name}>{name}</li>)}
|
||||
{results.map((name) => (
|
||||
<li key={name}>{name}</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -8,7 +8,7 @@ The next step after scaffolding out a new project, is to actually start it. To
|
||||
do this you can just `deno task start`. Environment variables will be
|
||||
automatically read from `.env`.
|
||||
|
||||
```
|
||||
```sh Terminal
|
||||
$ deno task start
|
||||
Watcher Process started.
|
||||
🍋 Fresh ready
|
||||
@@ -38,19 +38,19 @@ you add a new route.
|
||||
If you want to change the port or host, modify the options bag of the `start()`
|
||||
call in `main.ts` to include an explicit port number:
|
||||
|
||||
```js
|
||||
```js main.ts
|
||||
await start(manifest, { port: 3000 });
|
||||
```
|
||||
|
||||
You can also change the port by setting the `PORT` environment variable:
|
||||
|
||||
```
|
||||
```sh Terminal
|
||||
$ PORT=3000 deno task start
|
||||
```
|
||||
|
||||
Combining all of this we get the following `deno run` command:
|
||||
|
||||
```
|
||||
```sh Terminal
|
||||
$ deno run --allow-net --allow-read --allow-env --allow-run --watch=static/,routes/ main.ts
|
||||
Watcher Process started.
|
||||
🍋 Fresh ready
|
||||
|
||||
Reference in New Issue
Block a user