refactor: replace useState() with useSignal() (#1234)
This commit is contained in:
@@ -14,15 +14,14 @@ island. The file must have a default export that is a regular Preact component.
|
|||||||
```tsx
|
```tsx
|
||||||
// islands/MyIsland.tsx
|
// islands/MyIsland.tsx
|
||||||
|
|
||||||
import { useState } from "preact/hooks";
|
import type { Signal } from "@preact/signals";
|
||||||
|
|
||||||
export default function MyIsland() {
|
export default function MyIsland() {
|
||||||
const [count, setCount] = useState(0);
|
const count = useSignal(0);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
Counter is at {count}.{" "}
|
Counter is at {count}. <button onClick={() => count.value += 1}>+</button>
|
||||||
<button onClick={() => setCount(count + 1)}>+</button>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ Here is an example of an island component that counts down to a specific time.
|
|||||||
```tsx
|
```tsx
|
||||||
// islands/Countdown.tsx
|
// islands/Countdown.tsx
|
||||||
|
|
||||||
import { useEffect, useState } from "preact/hooks";
|
import { useSignal } from "@preact/signals";
|
||||||
|
import { useEffect } from "preact/hooks";
|
||||||
|
|
||||||
const timeFmt = new Intl.RelativeTimeFormat("en-US");
|
const timeFmt = new Intl.RelativeTimeFormat("en-US");
|
||||||
|
|
||||||
@@ -39,18 +40,16 @@ const timeFmt = new Intl.RelativeTimeFormat("en-US");
|
|||||||
// props to island components need to be JSON (de)serializable.
|
// props to island components need to be JSON (de)serializable.
|
||||||
export default function Countdown(props: { target: string }) {
|
export default function Countdown(props: { target: string }) {
|
||||||
const target = new Date(props.target);
|
const target = new Date(props.target);
|
||||||
const [now, setNow] = useState(new Date());
|
const now = useSignal(new Date());
|
||||||
|
|
||||||
// Set up an interval to update the `now` date every second with the current
|
// Set up an interval to update the `now` date every second with the current
|
||||||
// date as long as the component is mounted.
|
// date as long as the component is mounted.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timer = setInterval(() => {
|
const timer = setInterval(() => {
|
||||||
setNow((now) => {
|
if (now.value > target) {
|
||||||
if (now > target) {
|
clearInterval(timer);
|
||||||
clearInterval(timer);
|
}
|
||||||
}
|
now.value = new Date();
|
||||||
return new Date();
|
|
||||||
});
|
|
||||||
}, 1000);
|
}, 1000);
|
||||||
return () => clearInterval(timer);
|
return () => clearInterval(timer);
|
||||||
}, [props.target]);
|
}, [props.target]);
|
||||||
|
|||||||
@@ -112,9 +112,11 @@ await Deno.writeTextFile(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const ROUTES_INDEX_TSX = `import { Head } from "$fresh/runtime.ts";
|
const ROUTES_INDEX_TSX = `import { Head } from "$fresh/runtime.ts";
|
||||||
|
import { useSignal } from "@preact/signals";
|
||||||
import Counter from "../islands/Counter.tsx";
|
import Counter from "../islands/Counter.tsx";
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
|
const count = useSignal(3);
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Head>
|
<Head>
|
||||||
@@ -132,7 +134,7 @@ export default function Home() {
|
|||||||
Welcome to \`fresh\`. Try updating this message in the
|
Welcome to \`fresh\`. Try updating this message in the
|
||||||
./routes/index.tsx file, and refresh.
|
./routes/index.tsx file, and refresh.
|
||||||
</p>
|
</p>
|
||||||
<Counter start={3} />
|
<Counter count={count} />
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
@@ -164,20 +166,21 @@ await Deno.writeTextFile(
|
|||||||
COMPONENTS_BUTTON_TSX,
|
COMPONENTS_BUTTON_TSX,
|
||||||
);
|
);
|
||||||
|
|
||||||
const ISLANDS_COUNTER_TSX = `import { useState } from "preact/hooks";
|
const ISLANDS_COUNTER_TSX = `import type { Signal } from "@preact/signals";
|
||||||
import { Button } from "../components/Button.tsx";
|
import { Button } from "../components/Button.tsx";
|
||||||
|
|
||||||
interface CounterProps {
|
interface CounterProps {
|
||||||
start: number;
|
count: Signal<number>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Counter(props: CounterProps) {
|
export default function Counter(props: CounterProps) {
|
||||||
const [count, setCount] = useState(props.start);
|
|
||||||
return (
|
return (
|
||||||
<div${useTwind ? ' class="flex gap-2 w-full"' : ""}>
|
<div${useTwind ? ' class="flex gap-2 w-full"' : ""}>
|
||||||
<p${useTwind ? ' class="flex-grow-1 font-bold text-xl"' : ""}>{count}</p>
|
<p${
|
||||||
<Button onClick={() => setCount(count - 1)}>-1</Button>
|
useTwind ? ' class="flex-grow-1 font-bold text-xl"' : ""
|
||||||
<Button onClick={() => setCount(count + 1)}>+1</Button>
|
}>{props.count}</p>
|
||||||
|
<Button onClick={() => props.count.value -= 1}>-1</Button>
|
||||||
|
<Button onClick={() => props.count.value += 1}>+1</Button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
"$fresh/": "../../",
|
"$fresh/": "../../",
|
||||||
"preact": "https://esm.sh/preact@10.13.1",
|
"preact": "https://esm.sh/preact@10.13.1",
|
||||||
"preact/": "https://esm.sh/preact@10.13.1/",
|
"preact/": "https://esm.sh/preact@10.13.1/",
|
||||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.6"
|
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.6",
|
||||||
|
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
|
||||||
|
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,18 @@
|
|||||||
import { useState } from "preact/hooks";
|
import type { Signal } from "@preact/signals";
|
||||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||||
|
|
||||||
interface CounterProps {
|
interface CounterProps {
|
||||||
start: number;
|
count: Signal<number>;
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Counter(props: CounterProps) {
|
export default function Counter(props: CounterProps) {
|
||||||
const [count, setCount] = useState(props.start);
|
|
||||||
return (
|
return (
|
||||||
<div id={props.id}>
|
<div id={props.id}>
|
||||||
<p>{count}</p>
|
<p>{props.count}</p>
|
||||||
<button
|
<button
|
||||||
id={`b-${props.id}`}
|
id={`b-${props.id}`}
|
||||||
onClick={() => setCount(count + 1)}
|
onClick={() => props.count.value += 1}
|
||||||
disabled={!IS_BROWSER}
|
disabled={!IS_BROWSER}
|
||||||
>
|
>
|
||||||
+1
|
+1
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import { useState } from "preact/hooks";
|
import { useSignal } from "@preact/signals";
|
||||||
|
|
||||||
export default function RootFragment() {
|
export default function RootFragment() {
|
||||||
const [shown, setShown] = useState(false);
|
const shown = useSignal(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
Hello
|
Hello
|
||||||
<div onClick={() => setShown(true)} id="root-fragment-click-me">
|
<div onClick={() => shown.value = true} id="root-fragment-click-me">
|
||||||
World
|
World
|
||||||
</div>
|
</div>
|
||||||
{shown && <div>I'm rendered now</div>}
|
{shown.value && <div>I'm rendered now</div>}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import { useState } from "preact/hooks";
|
import { useSignal } from "@preact/signals";
|
||||||
|
|
||||||
export default function RootFragmentWithConditionalFirst() {
|
export default function RootFragmentWithConditionalFirst() {
|
||||||
const [shown, setShown] = useState(false);
|
const shown = useSignal(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{shown && <div>I'm rendered on top</div>}
|
{shown.value && <div>I'm rendered on top</div>}
|
||||||
Hello
|
Hello
|
||||||
<div
|
<div
|
||||||
onClick={() => setShown(true)}
|
onClick={() => shown.value = true}
|
||||||
id="root-fragment-conditional-first-click-me"
|
id="root-fragment-conditional-first-click-me"
|
||||||
>
|
>
|
||||||
World
|
World
|
||||||
|
|||||||
@@ -1,19 +1,18 @@
|
|||||||
import { useState } from "preact/hooks";
|
import type { Signal } from "@preact/signals";
|
||||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||||
|
|
||||||
interface CounterProps {
|
interface CounterProps {
|
||||||
start: number;
|
count: Signal<number>;
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Counter(props: CounterProps) {
|
export default function Counter(props: CounterProps) {
|
||||||
const [count, setCount] = useState(props.start);
|
|
||||||
return (
|
return (
|
||||||
<div id={props.id}>
|
<div id={props.id}>
|
||||||
<p>{count}</p>
|
<p>{props.count}</p>
|
||||||
<button
|
<button
|
||||||
id={`b-${props.id}`}
|
id={`b-${props.id}`}
|
||||||
onClick={() => setCount(count + 1)}
|
onClick={() => props.count.value += 1}
|
||||||
disabled={!IS_BROWSER}
|
disabled={!IS_BROWSER}
|
||||||
>
|
>
|
||||||
+1
|
+1
|
||||||
|
|||||||
@@ -1,21 +1,20 @@
|
|||||||
import { useState } from "preact/hooks";
|
import type { Signal } from "@preact/signals";
|
||||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||||
|
|
||||||
interface KebabCaseFileNameTestProps {
|
interface KebabCaseFileNameTestProps {
|
||||||
start: number;
|
count: Signal<number>;
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function KebabCaseFileNameTest(
|
export default function KebabCaseFileNameTest(
|
||||||
props: KebabCaseFileNameTestProps,
|
props: KebabCaseFileNameTestProps,
|
||||||
) {
|
) {
|
||||||
const [count, setCount] = useState(props.start);
|
|
||||||
return (
|
return (
|
||||||
<div id={props.id}>
|
<div id={props.id}>
|
||||||
<p>{count}</p>
|
<p>{props.count}</p>
|
||||||
<button
|
<button
|
||||||
id={`b-${props.id}`}
|
id={`b-${props.id}`}
|
||||||
onClick={() => setCount(count + 1)}
|
onClick={() => props.count.value += 1}
|
||||||
disabled={!IS_BROWSER}
|
disabled={!IS_BROWSER}
|
||||||
>
|
>
|
||||||
+1
|
+1
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { useSignal } from "@preact/signals";
|
||||||
import Counter from "../../islands/Counter.tsx";
|
import Counter from "../../islands/Counter.tsx";
|
||||||
import FolderCounter from "../../islands/folder/Counter.tsx";
|
import FolderCounter from "../../islands/folder/Counter.tsx";
|
||||||
import KebabCaseFileNameTest from "../../islands/kebab-case-counter-test.tsx";
|
import KebabCaseFileNameTest from "../../islands/kebab-case-counter-test.tsx";
|
||||||
@@ -6,10 +7,13 @@ import Test from "../../islands/Test.tsx";
|
|||||||
export default function Home() {
|
export default function Home() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Counter id="counter1" start={3} />
|
<Counter id="counter1" count={useSignal(3)} />
|
||||||
<Counter id="counter2" start={10} />
|
<Counter id="counter2" count={useSignal(10)} />
|
||||||
<FolderCounter id="folder-counter" start={3} />
|
<FolderCounter id="folder-counter" count={useSignal(3)} />
|
||||||
<KebabCaseFileNameTest id="kebab-case-file-counter" start={5} />
|
<KebabCaseFileNameTest
|
||||||
|
id="kebab-case-file-counter"
|
||||||
|
count={useSignal(5)}
|
||||||
|
/>
|
||||||
<Test message="" />
|
<Test message="" />
|
||||||
<Test message={`</script><script>alert('test')</script>`} />
|
<Test message={`</script><script>alert('test')</script>`} />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// https://github.com/denoland/fresh/pull/1050
|
// https://github.com/denoland/fresh/pull/1050
|
||||||
import { useEffect, useState } from "preact/hooks";
|
import { useEffect } from "preact/hooks";
|
||||||
import { cmpCssRules } from "../utils/utils.ts";
|
import { cmpCssRules } from "../utils/utils.ts";
|
||||||
|
import { useSignal } from "@preact/signals";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a cssrulelist of styleElement matching the selector.
|
* Returns a cssrulelist of styleElement matching the selector.
|
||||||
@@ -11,22 +12,18 @@ function getCssrules(selector: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function CheckDuplication() {
|
export default function CheckDuplication() {
|
||||||
const [cssRulesFRSHTWIND, setCssRulesFRSHTWIND] = useState<
|
const cssRulesFRSHTWIND = useSignal<undefined | CSSRuleList>(undefined);
|
||||||
undefined | CSSRuleList
|
const cssRulesClaimed = useSignal<undefined | CSSRuleList>(undefined);
|
||||||
>(undefined);
|
|
||||||
const [cssRulesClaimed, setCssRulesClaimed] = useState<
|
|
||||||
undefined | CSSRuleList
|
|
||||||
>(undefined);
|
|
||||||
|
|
||||||
// Init
|
// Init
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// get <style id="__FRSH_TWIND">
|
// get <style id="__FRSH_TWIND">
|
||||||
setCssRulesFRSHTWIND(getCssrules("#__FRSH_TWIND"));
|
cssRulesFRSHTWIND.value = getCssrules("#__FRSH_TWIND");
|
||||||
|
|
||||||
// get <style data-twind="claimed">
|
// get <style data-twind="claimed">
|
||||||
// see https://github.com/tw-in-js/twind/blob/main/packages/core/src/sheets.ts#L5-L16
|
// see https://github.com/tw-in-js/twind/blob/main/packages/core/src/sheets.ts#L5-L16
|
||||||
setCssRulesClaimed(
|
cssRulesClaimed.value = getCssrules(
|
||||||
getCssrules('[data-twind="claimed"]:not(#__FRSH_TWIND)'),
|
'[data-twind="claimed"]:not(#__FRSH_TWIND)',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -37,22 +34,24 @@ export default function CheckDuplication() {
|
|||||||
|
|
||||||
{/* Status of duplicates */}
|
{/* Status of duplicates */}
|
||||||
{(() => {
|
{(() => {
|
||||||
if (cssRulesFRSHTWIND != null && cssRulesClaimed != null) {
|
if (cssRulesFRSHTWIND.value != null && cssRulesClaimed.value != null) {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<p>Error :</p>
|
<p>Error :</p>
|
||||||
<p id="numDuplicates">
|
<p id="numDuplicates">
|
||||||
{`${
|
{`${
|
||||||
cmpCssRules(
|
cmpCssRules(
|
||||||
cssRulesFRSHTWIND,
|
cssRulesFRSHTWIND.value,
|
||||||
cssRulesClaimed,
|
cssRulesClaimed.value,
|
||||||
)
|
)
|
||||||
}`}
|
}`}
|
||||||
</p>
|
</p>
|
||||||
<p>cssrules are duplicated</p>
|
<p>cssrules are duplicated</p>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
} else if (cssRulesFRSHTWIND != null && cssRulesClaimed == null) {
|
} else if (
|
||||||
|
cssRulesFRSHTWIND.value != null && cssRulesClaimed.value == null
|
||||||
|
) {
|
||||||
return <p id="okNoDuplicates">Ok : No duplicates</p>;
|
return <p id="okNoDuplicates">Ok : No duplicates</p>;
|
||||||
} else {
|
} else {
|
||||||
return <p id="errorNoExistsRules">Error : Cssrules does not exist</p>;
|
return <p id="errorNoExistsRules">Error : Cssrules does not exist</p>;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// https://github.com/denoland/fresh/pull/1050
|
// https://github.com/denoland/fresh/pull/1050
|
||||||
import { useEffect, useState } from "preact/hooks";
|
import { useEffect } from "preact/hooks";
|
||||||
|
import { useSignal } from "@preact/signals";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a number of cssrules set by twind.
|
* Returns a number of cssrules set by twind.
|
||||||
@@ -10,21 +11,19 @@ function getNumCssrules(): number | undefined {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function InsertCssrules() {
|
export default function InsertCssrules() {
|
||||||
const [numDefCssRules, setNumDefCssRules] = useState<number | undefined>(
|
const numDefCssRules = useSignal<number | undefined>(undefined);
|
||||||
undefined,
|
const numCssRules = useSignal<number | undefined>(undefined);
|
||||||
);
|
const insertedStyles = useSignal("");
|
||||||
const [numCssRules, setNumCssRules] = useState<number | undefined>(undefined);
|
|
||||||
const [insertedStyles, setInsertedStyles] = useState("");
|
|
||||||
|
|
||||||
// Init numDefCssRules
|
// Init numDefCssRules
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setNumDefCssRules(getNumCssrules());
|
numDefCssRules.value = getNumCssrules();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Init and Update numCssRules
|
// Init and Update numCssRules
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setNumCssRules(getNumCssrules());
|
numCssRules.value = getNumCssrules();
|
||||||
}, [insertedStyles]);
|
}, [insertedStyles.value]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
@@ -33,22 +32,26 @@ export default function InsertCssrules() {
|
|||||||
<div>
|
<div>
|
||||||
<p>Default Number of __FRSH_TWIND CssRules :</p>
|
<p>Default Number of __FRSH_TWIND CssRules :</p>
|
||||||
<p id="defaultNumCssRules" class={`text-xl`}>
|
<p id="defaultNumCssRules" class={`text-xl`}>
|
||||||
{numDefCssRules ? numDefCssRules : "Error : Cannot get cssrules"}
|
{numDefCssRules.value
|
||||||
|
? numDefCssRules.value
|
||||||
|
: "Error : Cannot get cssrules"}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<p>Current Number of __FRSH_TWIND CssRules :</p>
|
<p>Current Number of __FRSH_TWIND CssRules :</p>
|
||||||
<p id="currentNumCssRules" class={`text-xl ${insertedStyles}`}>
|
<p id="currentNumCssRules" class={`text-xl ${insertedStyles.value}`}>
|
||||||
{numCssRules ? numCssRules : "Error : Cannot get cssrules"}
|
{numCssRules.value
|
||||||
|
? numCssRules.value
|
||||||
|
: "Error : Cannot get cssrules"}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Status of insert css rules */}
|
{/* Status of insert css rules */}
|
||||||
{(() => {
|
{(() => {
|
||||||
if (insertedStyles === "") {
|
if (insertedStyles.value === "") {
|
||||||
return <p id="waitClickButton">Plese click button</p>;
|
return <p id="waitClickButton">Plese click button</p>;
|
||||||
} else if (numDefCssRules === numCssRules) {
|
} else if (numDefCssRules.value === numCssRules.value) {
|
||||||
return (
|
return (
|
||||||
<p id="errorInsertCssrules">
|
<p id="errorInsertCssrules">
|
||||||
{'Error: A cssrule has been inserted into a style sheet other than <style id="__FRSH_TWIND">'}
|
{'Error: A cssrule has been inserted into a style sheet other than <style id="__FRSH_TWIND">'}
|
||||||
@@ -62,9 +65,9 @@ export default function InsertCssrules() {
|
|||||||
<button
|
<button
|
||||||
id="insertCssRuleButton"
|
id="insertCssRuleButton"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setInsertedStyles("text-green-600");
|
insertedStyles.value = "text-green-600";
|
||||||
}}
|
}}
|
||||||
disabled={insertedStyles === "" ? false : true}
|
disabled={insertedStyles.value === "" ? false : true}
|
||||||
>
|
>
|
||||||
Add `text-green-600` to Cureent Number Class
|
Add `text-green-600` to Cureent Number Class
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState } from "preact/hooks";
|
import { useSignal } from "@preact/signals";
|
||||||
|
|
||||||
import Prism from "https://esm.sh/prismjs@1.27.0";
|
import Prism from "https://esm.sh/prismjs@1.27.0";
|
||||||
import "https://esm.sh/prismjs@1.27.0/components/prism-typescript?no-check";
|
import "https://esm.sh/prismjs@1.27.0/components/prism-typescript?no-check";
|
||||||
@@ -15,10 +15,10 @@ export default function CodeBox(props: CodeBoxProps) {
|
|||||||
);
|
);
|
||||||
const onCopy = () => {
|
const onCopy = () => {
|
||||||
navigator.clipboard.writeText(props.code);
|
navigator.clipboard.writeText(props.code);
|
||||||
setCopied(true);
|
copied.value = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const [copied, setCopied] = useState(false);
|
const copied = useSignal(false);
|
||||||
return (
|
return (
|
||||||
<div class="">
|
<div class="">
|
||||||
<details>
|
<details>
|
||||||
@@ -35,7 +35,7 @@ export default function CodeBox(props: CodeBoxProps) {
|
|||||||
onClick={onCopy}
|
onClick={onCopy}
|
||||||
class="absolute top-2 right-2 px-3 py-2 border border-gray-400 rounded text-white bg-gray-800"
|
class="absolute top-2 right-2 px-3 py-2 border border-gray-400 rounded text-white bg-gray-800"
|
||||||
>
|
>
|
||||||
{copied ? "Copied!" : "Copy"}
|
{copied.value ? "Copied!" : "Copy"}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</details>
|
</details>
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { ComponentChildren } from "preact";
|
import { ComponentChildren } from "preact";
|
||||||
import { useEffect, useState } from "preact/hooks";
|
import { useEffect } from "preact/hooks";
|
||||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||||
import * as Icons from "../components/Icons.tsx";
|
import * as Icons from "../components/Icons.tsx";
|
||||||
|
import { useSignal } from "@preact/signals";
|
||||||
|
|
||||||
export default function CopyArea(props: { children: ComponentChildren }) {
|
export default function CopyArea(props: { children: ComponentChildren }) {
|
||||||
const [copied, setCopied] = useState(false);
|
const copied = useSignal(false);
|
||||||
|
|
||||||
async function handleClick() {
|
async function handleClick() {
|
||||||
if (props.children === undefined || props.children === null) {
|
if (props.children === undefined || props.children === null) {
|
||||||
@@ -12,22 +13,22 @@ export default function CopyArea(props: { children: ComponentChildren }) {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await navigator.clipboard.writeText(props.children.toString());
|
await navigator.clipboard.writeText(props.children.toString());
|
||||||
setCopied(true);
|
copied.value = true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setCopied(false);
|
copied.value = false;
|
||||||
console.error((error && error.message) || "Copy failed");
|
console.error((error && error.message) || "Copy failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!copied) {
|
if (!copied.value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
setCopied(false);
|
copied.value = false;
|
||||||
}, 2000);
|
}, 2000);
|
||||||
return () => clearTimeout(timer);
|
return () => clearTimeout(timer);
|
||||||
}, [copied]);
|
}, [copied.value]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class="bg(gray-800) rounded text-white flex items-center">
|
<div class="bg(gray-800) rounded text-white flex items-center">
|
||||||
@@ -47,7 +48,7 @@ export default function CopyArea(props: { children: ComponentChildren }) {
|
|||||||
aria-label="Copy to Clipboard"
|
aria-label="Copy to Clipboard"
|
||||||
disabled={!IS_BROWSER}
|
disabled={!IS_BROWSER}
|
||||||
class={`rounded p-1.5 border border-gray-300 hover:bg-gray-700 ${
|
class={`rounded p-1.5 border border-gray-300 hover:bg-gray-700 ${
|
||||||
copied ? "text-green-500" : ""
|
copied.value ? "text-green-500" : ""
|
||||||
} relative`}
|
} relative`}
|
||||||
onClick={handleClick}
|
onClick={handleClick}
|
||||||
>
|
>
|
||||||
|
|||||||
+24
-23
@@ -1,5 +1,6 @@
|
|||||||
import { useEffect, useRef, useState } from "preact/hooks";
|
import { useEffect, useRef } from "preact/hooks";
|
||||||
import { Spring, WaveTank } from "../components/WaveTank.ts";
|
import { useSignal } from "@preact/signals";
|
||||||
|
import { WaveTank } from "../components/WaveTank.ts";
|
||||||
|
|
||||||
function easeInCirc(x: number) {
|
function easeInCirc(x: number) {
|
||||||
return 1 - Math.sqrt(1 - Math.pow(x, 2));
|
return 1 - Math.sqrt(1 - Math.pow(x, 2));
|
||||||
@@ -9,44 +10,44 @@ const waveTank = new WaveTank();
|
|||||||
|
|
||||||
function LemonDrop() {
|
function LemonDrop() {
|
||||||
const SVG_WIDTH = 100;
|
const SVG_WIDTH = 100;
|
||||||
const [counter, setCounter] = useState(0);
|
const counter = useSignal(0);
|
||||||
const [dropy, setDropy] = useState(60);
|
const dropy = useSignal(60);
|
||||||
const [width, setWidth] = useState(SVG_WIDTH);
|
const width = useSignal(SVG_WIDTH);
|
||||||
const widthRef = useRef(width);
|
const widthRef = useRef(width.value);
|
||||||
const [springs, setSprings] = useState<Spring[]>(waveTank.springs);
|
const springs = useSignal(waveTank.springs);
|
||||||
const requestIdRef = useRef<number>();
|
const requestIdRef = useRef<number>();
|
||||||
const grid = SVG_WIDTH / waveTank.waveLength;
|
const grid = SVG_WIDTH / waveTank.waveLength;
|
||||||
const points = [
|
const points = [
|
||||||
[0, 100],
|
[0, 100],
|
||||||
[0, 0],
|
[0, 0],
|
||||||
...springs.map((x, i) => [i * grid, x.p]),
|
...springs.value.map((x, i) => [i * grid, x.p]),
|
||||||
[width, 0],
|
[width.value, 0],
|
||||||
[width, 100],
|
[width.value, 100],
|
||||||
];
|
];
|
||||||
const springsPath = `${points.map((x) => x.join(",")).join(" ")}`;
|
const springsPath = `${points.map((x) => x.join(",")).join(" ")}`;
|
||||||
const juice = `M18 ${63 + counter} C15 ${63 + counter} 16 ${
|
const juice = `M18 ${63 + counter.value} C15 ${63 + counter.value} 16 ${
|
||||||
63 + counter
|
63 + counter.value
|
||||||
} 12 61L9 56C2 33 62 -3 80 12C103 27 44 56 29 58C27 58 25 59 24 61C20 ${
|
} 12 61L9 56C2 33 62 -3 80 12C103 27 44 56 29 58C27 58 25 59 24 61C20 ${
|
||||||
63 + counter
|
63 + counter.value
|
||||||
} 21 ${63 + counter} 18 ${63 + counter}Z`;
|
} 21 ${63 + counter.value} 18 ${63 + counter.value}Z`;
|
||||||
|
|
||||||
function updateJuice(timestamp: number) {
|
function updateJuice(timestamp: number) {
|
||||||
const amp = 40;
|
const amp = 40;
|
||||||
const x = timestamp / 2000;
|
const x = timestamp / 2000;
|
||||||
const saw = x - Math.floor(x);
|
const saw = x - Math.floor(x);
|
||||||
if (saw < 0.6) {
|
if (saw < 0.6) {
|
||||||
setCounter(easeInCirc(saw) * amp);
|
counter.value = easeInCirc(saw) * amp;
|
||||||
setDropy(-100);
|
dropy.value = -100;
|
||||||
} else {
|
} else {
|
||||||
setCounter(easeInCirc(1 - saw) * amp * 0.1);
|
counter.value = easeInCirc(1 - saw) * amp * 0.1;
|
||||||
setDropy(70 + Math.pow(saw - 0.6, 2) * 10000);
|
dropy.value = 70 + Math.pow(saw - 0.6, 2) * 10000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function update(timestamp: number) {
|
function update(timestamp: number) {
|
||||||
updateJuice(timestamp);
|
updateJuice(timestamp);
|
||||||
waveTank.update(waveTank.springs);
|
waveTank.update(waveTank.springs);
|
||||||
setSprings([...waveTank.springs]);
|
springs.value = [...waveTank.springs];
|
||||||
|
|
||||||
const offset = 500;
|
const offset = 500;
|
||||||
const saw = (timestamp + offset) / 2000 -
|
const saw = (timestamp + offset) / 2000 -
|
||||||
@@ -58,7 +59,7 @@ function LemonDrop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function resize() {
|
function resize() {
|
||||||
setWidth(document.body.clientWidth);
|
width.value = document.body.clientWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
function drop() {
|
function drop() {
|
||||||
@@ -69,8 +70,8 @@ function LemonDrop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
widthRef.current = width;
|
widthRef.current = width.value;
|
||||||
}, [width]);
|
}, [width.value]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
|
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
|
||||||
@@ -102,7 +103,7 @@ function LemonDrop() {
|
|||||||
role="img"
|
role="img"
|
||||||
aria-label="Fresh logo"
|
aria-label="Fresh logo"
|
||||||
>
|
>
|
||||||
<circle cx="18" cy={dropy} r="4" fill="white"></circle>
|
<circle cx="18" cy={dropy.value} r="4" fill="white"></circle>
|
||||||
<path
|
<path
|
||||||
d="M11.9 96.3v24H7v-24h4.9Zm7.5 10.2v4h-8.8v-4h8.8Zm1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