refactor: replace useState() with useSignal() (#1234)
This commit is contained in:
@@ -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 "https://esm.sh/prismjs@1.27.0/components/prism-typescript?no-check";
|
||||
@@ -15,10 +15,10 @@ export default function CodeBox(props: CodeBoxProps) {
|
||||
);
|
||||
const onCopy = () => {
|
||||
navigator.clipboard.writeText(props.code);
|
||||
setCopied(true);
|
||||
copied.value = true;
|
||||
};
|
||||
|
||||
const [copied, setCopied] = useState(false);
|
||||
const copied = useSignal(false);
|
||||
return (
|
||||
<div class="">
|
||||
<details>
|
||||
@@ -35,7 +35,7 @@ export default function CodeBox(props: CodeBoxProps) {
|
||||
onClick={onCopy}
|
||||
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>
|
||||
</div>
|
||||
</details>
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { ComponentChildren } from "preact";
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { useEffect } from "preact/hooks";
|
||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||
import * as Icons from "../components/Icons.tsx";
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
export default function CopyArea(props: { children: ComponentChildren }) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const copied = useSignal(false);
|
||||
|
||||
async function handleClick() {
|
||||
if (props.children === undefined || props.children === null) {
|
||||
@@ -12,22 +13,22 @@ export default function CopyArea(props: { children: ComponentChildren }) {
|
||||
}
|
||||
try {
|
||||
await navigator.clipboard.writeText(props.children.toString());
|
||||
setCopied(true);
|
||||
copied.value = true;
|
||||
} catch (error) {
|
||||
setCopied(false);
|
||||
copied.value = false;
|
||||
console.error((error && error.message) || "Copy failed");
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!copied) {
|
||||
if (!copied.value) {
|
||||
return;
|
||||
}
|
||||
const timer = setTimeout(() => {
|
||||
setCopied(false);
|
||||
copied.value = false;
|
||||
}, 2000);
|
||||
return () => clearTimeout(timer);
|
||||
}, [copied]);
|
||||
}, [copied.value]);
|
||||
|
||||
return (
|
||||
<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"
|
||||
disabled={!IS_BROWSER}
|
||||
class={`rounded p-1.5 border border-gray-300 hover:bg-gray-700 ${
|
||||
copied ? "text-green-500" : ""
|
||||
copied.value ? "text-green-500" : ""
|
||||
} relative`}
|
||||
onClick={handleClick}
|
||||
>
|
||||
|
||||
+24
-23
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useRef, useState } from "preact/hooks";
|
||||
import { Spring, WaveTank } from "../components/WaveTank.ts";
|
||||
import { useEffect, useRef } from "preact/hooks";
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { WaveTank } from "../components/WaveTank.ts";
|
||||
|
||||
function easeInCirc(x: number) {
|
||||
return 1 - Math.sqrt(1 - Math.pow(x, 2));
|
||||
@@ -9,44 +10,44 @@ const waveTank = new WaveTank();
|
||||
|
||||
function LemonDrop() {
|
||||
const SVG_WIDTH = 100;
|
||||
const [counter, setCounter] = useState(0);
|
||||
const [dropy, setDropy] = useState(60);
|
||||
const [width, setWidth] = useState(SVG_WIDTH);
|
||||
const widthRef = useRef(width);
|
||||
const [springs, setSprings] = useState<Spring[]>(waveTank.springs);
|
||||
const counter = useSignal(0);
|
||||
const dropy = useSignal(60);
|
||||
const width = useSignal(SVG_WIDTH);
|
||||
const widthRef = useRef(width.value);
|
||||
const springs = useSignal(waveTank.springs);
|
||||
const requestIdRef = useRef<number>();
|
||||
const grid = SVG_WIDTH / waveTank.waveLength;
|
||||
const points = [
|
||||
[0, 100],
|
||||
[0, 0],
|
||||
...springs.map((x, i) => [i * grid, x.p]),
|
||||
[width, 0],
|
||||
[width, 100],
|
||||
...springs.value.map((x, i) => [i * grid, x.p]),
|
||||
[width.value, 0],
|
||||
[width.value, 100],
|
||||
];
|
||||
const springsPath = `${points.map((x) => x.join(",")).join(" ")}`;
|
||||
const juice = `M18 ${63 + counter} C15 ${63 + counter} 16 ${
|
||||
63 + counter
|
||||
const juice = `M18 ${63 + counter.value} C15 ${63 + counter.value} 16 ${
|
||||
63 + counter.value
|
||||
} 12 61L9 56C2 33 62 -3 80 12C103 27 44 56 29 58C27 58 25 59 24 61C20 ${
|
||||
63 + counter
|
||||
} 21 ${63 + counter} 18 ${63 + counter}Z`;
|
||||
63 + counter.value
|
||||
} 21 ${63 + counter.value} 18 ${63 + counter.value}Z`;
|
||||
|
||||
function updateJuice(timestamp: number) {
|
||||
const amp = 40;
|
||||
const x = timestamp / 2000;
|
||||
const saw = x - Math.floor(x);
|
||||
if (saw < 0.6) {
|
||||
setCounter(easeInCirc(saw) * amp);
|
||||
setDropy(-100);
|
||||
counter.value = easeInCirc(saw) * amp;
|
||||
dropy.value = -100;
|
||||
} else {
|
||||
setCounter(easeInCirc(1 - saw) * amp * 0.1);
|
||||
setDropy(70 + Math.pow(saw - 0.6, 2) * 10000);
|
||||
counter.value = easeInCirc(1 - saw) * amp * 0.1;
|
||||
dropy.value = 70 + Math.pow(saw - 0.6, 2) * 10000;
|
||||
}
|
||||
}
|
||||
|
||||
function update(timestamp: number) {
|
||||
updateJuice(timestamp);
|
||||
waveTank.update(waveTank.springs);
|
||||
setSprings([...waveTank.springs]);
|
||||
springs.value = [...waveTank.springs];
|
||||
|
||||
const offset = 500;
|
||||
const saw = (timestamp + offset) / 2000 -
|
||||
@@ -58,7 +59,7 @@ function LemonDrop() {
|
||||
}
|
||||
|
||||
function resize() {
|
||||
setWidth(document.body.clientWidth);
|
||||
width.value = document.body.clientWidth;
|
||||
}
|
||||
|
||||
function drop() {
|
||||
@@ -69,8 +70,8 @@ function LemonDrop() {
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
widthRef.current = width;
|
||||
}, [width]);
|
||||
widthRef.current = width.value;
|
||||
}, [width.value]);
|
||||
|
||||
useEffect(() => {
|
||||
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
|
||||
@@ -102,7 +103,7 @@ function LemonDrop() {
|
||||
role="img"
|
||||
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
|
||||
d="M11.9 96.3v24H7v-24h4.9Zm7.5 10.2v4h-8.8v-4h8.8Zm1-10.2v4h-9.8v-4h9.8ZM23.2 96.3H31c1.6 0 3 .3 4.1.9 1.1.5 2 1.3 2.6 2.4a8 8 0 0 1 1 4c0 1.3-.2 2.4-.6 3.3-.3 1-.8 1.7-1.5 2.3-.7.6-1.4 1-2.3 1.5l-1.5.8h-6.3v-4h4.3a3 3 0 0 0 1.7-.4c.4-.3.7-.7 1-1.2a5 5 0 0 0 .3-2c0-.7-.1-1.3-.3-1.9-.2-.5-.5-1-1-1.2-.3-.3-.9-.5-1.5-.5h-3v20h-4.8v-24Zm11 24-4.4-10.7h5l4.6 10.5v.2h-5.2ZM55.9 116.3v4H45.4v-4h10.5Zm-9-20v24H42v-24H47Zm7.6 9.8v3.8h-9.1v-3.8h9Zm1.4-9.8v4H45.4v-4h10.5ZM69 114c0-.4 0-.8-.2-1.1 0-.4-.2-.7-.5-1l-1-1-1.9-.8-2.6-1.2-2.2-1.5a6.5 6.5 0 0 1-1.7-2 6 6 0 0 1-.5-2.7c0-1 .1-2 .5-2.7a6 6 0 0 1 1.6-2.2c.7-.5 1.5-1 2.4-1.3a9.5 9.5 0 0 1 7.1.5c1.2.6 2 1.5 2.7 2.6.6 1 1 2.4 1 3.8h-5a5 5 0 0 0-.2-1.8c-.2-.5-.5-1-1-1.2-.4-.3-1-.5-1.6-.5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