Skip to content

Commit 58f9769

Browse files
authored
refactor: add components/ in starter project (#353)
This commit adds a `components/` folder to the starter project to indicate that one can also create components that are not themselves islands.
1 parent 1dc1f87 commit 58f9769

File tree

3 files changed

+50
-41
lines changed

3 files changed

+50
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ You can scaffold a new project by running the Fresh init script. To scaffold a
3030
project in the `my-project` folder, run the following:
3131

3232
```sh
33-
deno run -A -r https://fresh.deno.dev my-project
33+
deno run -A -r https://fresh.deno.dev deno-fresh-demo
3434
```
3535

3636
Then navigate to the newly created project folder:

init.ts

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const useVSCode = flags.vscode === null
8686
await Deno.mkdir(join(resolvedDirectory, "routes", "api"), { recursive: true });
8787
await Deno.mkdir(join(resolvedDirectory, "islands"), { recursive: true });
8888
await Deno.mkdir(join(resolvedDirectory, "static"), { recursive: true });
89+
await Deno.mkdir(join(resolvedDirectory, "components"), { recursive: true });
8990
if (useVSCode) {
9091
await Deno.mkdir(join(resolvedDirectory, ".vscode"), { recursive: true });
9192
}
@@ -140,57 +141,52 @@ await Deno.writeTextFile(
140141
ROUTES_INDEX_TSX,
141142
);
142143

143-
let ISLANDS_COUNTER_TSX = `/** @jsx h */
144+
const COMPONENTS_BUTTON_TSX = `/** @jsx h */
144145
import { h } from "preact";
145-
import { useState } from "preact/hooks";
146146
import { IS_BROWSER } from "$fresh/runtime.ts";
147147
${useTwind ? 'import { tw } from "@twind";\n' : ""}
148+
export function Button(props: h.JSX.HTMLAttributes<HTMLButtonElement>) {
149+
return (
150+
<button
151+
{...props}
152+
disabled={!IS_BROWSER || props.disabled}
153+
${
154+
useTwind
155+
? " class={tw\`px-2 py-1 border(gray-100 2) hover:bg-gray-200\`}\n "
156+
: ""
157+
}/>
158+
);
159+
}
160+
`;
161+
await Deno.writeTextFile(
162+
join(resolvedDirectory, "components", "Button.tsx"),
163+
COMPONENTS_BUTTON_TSX,
164+
);
165+
166+
const ISLANDS_COUNTER_TSX = `/** @jsx h */
167+
import { h } from "preact";
168+
import { useState } from "preact/hooks";
169+
${useTwind ? 'import { tw } from "@twind";\n' : ""}
170+
import { Button } from "../components/Button.tsx";
171+
148172
interface CounterProps {
149173
start: number;
150174
}
151175
152176
export default function Counter(props: CounterProps) {
153177
const [count, setCount] = useState(props.start);
154-
`;
155-
156-
if (useTwind) {
157-
ISLANDS_COUNTER_TSX +=
158-
` const btn = tw\`px-2 py-1 border(gray-100 1) hover:bg-gray-200\`;\n`;
159-
ISLANDS_COUNTER_TSX += ` return (
160-
<div class={tw\`flex gap-2 w-full\`}>
161-
<p class={tw\`flex-grow-1 font-bold text-xl\`}>{count}</p>
162-
<button
163-
class={btn}
164-
onClick={() => setCount(count - 1)}
165-
disabled={!IS_BROWSER}
166-
>
167-
-1
168-
</button>
169-
<button
170-
class={btn}
171-
onClick={() => setCount(count + 1)}
172-
disabled={!IS_BROWSER}
173-
>
174-
+1
175-
</button>
176-
</div>
177-
);
178-
`;
179-
} else {
180-
ISLANDS_COUNTER_TSX += ` return (
181-
<div>
182-
<p>{count}</p>
183-
<button onClick={() => setCount(count - 1)} disabled={!IS_BROWSER}>
184-
-1
185-
</button>
186-
<button onClick={() => setCount(count + 1)} disabled={!IS_BROWSER}>
187-
+1
188-
</button>
178+
return (
179+
<div${useTwind ? " class={tw\`flex gap-2 w-full\`}" : ""}>
180+
<p${
181+
useTwind ? " class={tw\`flex-grow-1 font-bold text-xl\`}" : ""
182+
}>{count}</p>
183+
<Button onClick={() => setCount(count - 1)}>-1</Button>
184+
<Button onClick={() => setCount(count + 1)}>+1</Button>
189185
</div>
190186
);
191-
`;
192187
}
193-
ISLANDS_COUNTER_TSX += `}\n`;
188+
`;
189+
194190
await Deno.writeTextFile(
195191
join(resolvedDirectory, "islands", "Counter.tsx"),
196192
ISLANDS_COUNTER_TSX,

tests/cli_test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ Deno.test({
6666
{ "type": "file", "name": "README.md" },
6767
{ "type": "file", "name": "import_map.json" },
6868
{ "type": "file", "name": "fresh.gen.ts" },
69+
{
70+
"type": "directory",
71+
"name": "components",
72+
"contents": [
73+
{ "type": "file", "name": "Button.tsx" },
74+
],
75+
},
6976
{
7077
"type": "directory",
7178
"name": "islands",
@@ -152,7 +159,6 @@ Deno.test({
152159
await delay(100);
153160

154161
counterValue = await counter?.evaluate((el) => el.textContent);
155-
console.log(counterValue);
156162
assert(counterValue === "4");
157163
await page.close();
158164
await browser.close();
@@ -200,6 +206,13 @@ Deno.test({
200206
{ "type": "file", "name": "README.md" },
201207
{ "type": "file", "name": "import_map.json" },
202208
{ "type": "file", "name": "fresh.gen.ts" },
209+
{
210+
"type": "directory",
211+
"name": "components",
212+
"contents": [
213+
{ "type": "file", "name": "Button.tsx" },
214+
],
215+
},
203216
{
204217
"type": "directory",
205218
"name": "islands",

0 commit comments

Comments
 (0)