refactor: replace useState() with useSignal() (#1234)
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
"$fresh/": "../../",
|
||||
"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";
|
||||
|
||||
interface CounterProps {
|
||||
start: number;
|
||||
count: Signal<number>;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export default function Counter(props: CounterProps) {
|
||||
const [count, setCount] = useState(props.start);
|
||||
return (
|
||||
<div id={props.id}>
|
||||
<p>{count}</p>
|
||||
<p>{props.count}</p>
|
||||
<button
|
||||
id={`b-${props.id}`}
|
||||
onClick={() => setCount(count + 1)}
|
||||
onClick={() => props.count.value += 1}
|
||||
disabled={!IS_BROWSER}
|
||||
>
|
||||
+1
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { useState } from "preact/hooks";
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
export default function RootFragment() {
|
||||
const [shown, setShown] = useState(false);
|
||||
const shown = useSignal(false);
|
||||
|
||||
return (
|
||||
<>
|
||||
Hello
|
||||
<div onClick={() => setShown(true)} id="root-fragment-click-me">
|
||||
<div onClick={() => shown.value = true} id="root-fragment-click-me">
|
||||
World
|
||||
</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() {
|
||||
const [shown, setShown] = useState(false);
|
||||
const shown = useSignal(false);
|
||||
|
||||
return (
|
||||
<>
|
||||
{shown && <div>I'm rendered on top</div>}
|
||||
{shown.value && <div>I'm rendered on top</div>}
|
||||
Hello
|
||||
<div
|
||||
onClick={() => setShown(true)}
|
||||
onClick={() => shown.value = true}
|
||||
id="root-fragment-conditional-first-click-me"
|
||||
>
|
||||
World
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
import { useState } from "preact/hooks";
|
||||
import type { Signal } from "@preact/signals";
|
||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||
|
||||
interface CounterProps {
|
||||
start: number;
|
||||
count: Signal<number>;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export default function Counter(props: CounterProps) {
|
||||
const [count, setCount] = useState(props.start);
|
||||
return (
|
||||
<div id={props.id}>
|
||||
<p>{count}</p>
|
||||
<p>{props.count}</p>
|
||||
<button
|
||||
id={`b-${props.id}`}
|
||||
onClick={() => setCount(count + 1)}
|
||||
onClick={() => props.count.value += 1}
|
||||
disabled={!IS_BROWSER}
|
||||
>
|
||||
+1
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import { useState } from "preact/hooks";
|
||||
import type { Signal } from "@preact/signals";
|
||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||
|
||||
interface KebabCaseFileNameTestProps {
|
||||
start: number;
|
||||
count: Signal<number>;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export default function KebabCaseFileNameTest(
|
||||
props: KebabCaseFileNameTestProps,
|
||||
) {
|
||||
const [count, setCount] = useState(props.start);
|
||||
return (
|
||||
<div id={props.id}>
|
||||
<p>{count}</p>
|
||||
<p>{props.count}</p>
|
||||
<button
|
||||
id={`b-${props.id}`}
|
||||
onClick={() => setCount(count + 1)}
|
||||
onClick={() => props.count.value += 1}
|
||||
disabled={!IS_BROWSER}
|
||||
>
|
||||
+1
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import Counter from "../../islands/Counter.tsx";
|
||||
import FolderCounter from "../../islands/folder/Counter.tsx";
|
||||
import KebabCaseFileNameTest from "../../islands/kebab-case-counter-test.tsx";
|
||||
@@ -6,10 +7,13 @@ import Test from "../../islands/Test.tsx";
|
||||
export default function Home() {
|
||||
return (
|
||||
<div>
|
||||
<Counter id="counter1" start={3} />
|
||||
<Counter id="counter2" start={10} />
|
||||
<FolderCounter id="folder-counter" start={3} />
|
||||
<KebabCaseFileNameTest id="kebab-case-file-counter" start={5} />
|
||||
<Counter id="counter1" count={useSignal(3)} />
|
||||
<Counter id="counter2" count={useSignal(10)} />
|
||||
<FolderCounter id="folder-counter" count={useSignal(3)} />
|
||||
<KebabCaseFileNameTest
|
||||
id="kebab-case-file-counter"
|
||||
count={useSignal(5)}
|
||||
/>
|
||||
<Test message="" />
|
||||
<Test message={`</script><script>alert('test')</script>`} />
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// 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 { useSignal } from "@preact/signals";
|
||||
|
||||
/**
|
||||
* Returns a cssrulelist of styleElement matching the selector.
|
||||
@@ -11,22 +12,18 @@ function getCssrules(selector: string) {
|
||||
}
|
||||
|
||||
export default function CheckDuplication() {
|
||||
const [cssRulesFRSHTWIND, setCssRulesFRSHTWIND] = useState<
|
||||
undefined | CSSRuleList
|
||||
>(undefined);
|
||||
const [cssRulesClaimed, setCssRulesClaimed] = useState<
|
||||
undefined | CSSRuleList
|
||||
>(undefined);
|
||||
const cssRulesFRSHTWIND = useSignal<undefined | CSSRuleList>(undefined);
|
||||
const cssRulesClaimed = useSignal<undefined | CSSRuleList>(undefined);
|
||||
|
||||
// Init
|
||||
useEffect(() => {
|
||||
// get <style id="__FRSH_TWIND">
|
||||
setCssRulesFRSHTWIND(getCssrules("#__FRSH_TWIND"));
|
||||
cssRulesFRSHTWIND.value = getCssrules("#__FRSH_TWIND");
|
||||
|
||||
// get <style data-twind="claimed">
|
||||
// see https://github.com/tw-in-js/twind/blob/main/packages/core/src/sheets.ts#L5-L16
|
||||
setCssRulesClaimed(
|
||||
getCssrules('[data-twind="claimed"]:not(#__FRSH_TWIND)'),
|
||||
cssRulesClaimed.value = getCssrules(
|
||||
'[data-twind="claimed"]:not(#__FRSH_TWIND)',
|
||||
);
|
||||
});
|
||||
|
||||
@@ -37,22 +34,24 @@ export default function CheckDuplication() {
|
||||
|
||||
{/* Status of duplicates */}
|
||||
{(() => {
|
||||
if (cssRulesFRSHTWIND != null && cssRulesClaimed != null) {
|
||||
if (cssRulesFRSHTWIND.value != null && cssRulesClaimed.value != null) {
|
||||
return (
|
||||
<div>
|
||||
<p>Error :</p>
|
||||
<p id="numDuplicates">
|
||||
{`${
|
||||
cmpCssRules(
|
||||
cssRulesFRSHTWIND,
|
||||
cssRulesClaimed,
|
||||
cssRulesFRSHTWIND.value,
|
||||
cssRulesClaimed.value,
|
||||
)
|
||||
}`}
|
||||
</p>
|
||||
<p>cssrules are duplicated</p>
|
||||
</div>
|
||||
);
|
||||
} else if (cssRulesFRSHTWIND != null && cssRulesClaimed == null) {
|
||||
} else if (
|
||||
cssRulesFRSHTWIND.value != null && cssRulesClaimed.value == null
|
||||
) {
|
||||
return <p id="okNoDuplicates">Ok : No duplicates</p>;
|
||||
} else {
|
||||
return <p id="errorNoExistsRules">Error : Cssrules does not exist</p>;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// 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.
|
||||
@@ -10,21 +11,19 @@ function getNumCssrules(): number | undefined {
|
||||
}
|
||||
|
||||
export default function InsertCssrules() {
|
||||
const [numDefCssRules, setNumDefCssRules] = useState<number | undefined>(
|
||||
undefined,
|
||||
);
|
||||
const [numCssRules, setNumCssRules] = useState<number | undefined>(undefined);
|
||||
const [insertedStyles, setInsertedStyles] = useState("");
|
||||
const numDefCssRules = useSignal<number | undefined>(undefined);
|
||||
const numCssRules = useSignal<number | undefined>(undefined);
|
||||
const insertedStyles = useSignal("");
|
||||
|
||||
// Init numDefCssRules
|
||||
useEffect(() => {
|
||||
setNumDefCssRules(getNumCssrules());
|
||||
numDefCssRules.value = getNumCssrules();
|
||||
}, []);
|
||||
|
||||
// Init and Update numCssRules
|
||||
useEffect(() => {
|
||||
setNumCssRules(getNumCssrules());
|
||||
}, [insertedStyles]);
|
||||
numCssRules.value = getNumCssrules();
|
||||
}, [insertedStyles.value]);
|
||||
|
||||
return (
|
||||
<div>
|
||||
@@ -33,22 +32,26 @@ export default function InsertCssrules() {
|
||||
<div>
|
||||
<p>Default Number of __FRSH_TWIND CssRules :</p>
|
||||
<p id="defaultNumCssRules" class={`text-xl`}>
|
||||
{numDefCssRules ? numDefCssRules : "Error : Cannot get cssrules"}
|
||||
{numDefCssRules.value
|
||||
? numDefCssRules.value
|
||||
: "Error : Cannot get cssrules"}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<p>Current Number of __FRSH_TWIND CssRules :</p>
|
||||
<p id="currentNumCssRules" class={`text-xl ${insertedStyles}`}>
|
||||
{numCssRules ? numCssRules : "Error : Cannot get cssrules"}
|
||||
<p id="currentNumCssRules" class={`text-xl ${insertedStyles.value}`}>
|
||||
{numCssRules.value
|
||||
? numCssRules.value
|
||||
: "Error : Cannot get cssrules"}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Status of insert css rules */}
|
||||
{(() => {
|
||||
if (insertedStyles === "") {
|
||||
if (insertedStyles.value === "") {
|
||||
return <p id="waitClickButton">Plese click button</p>;
|
||||
} else if (numDefCssRules === numCssRules) {
|
||||
} else if (numDefCssRules.value === numCssRules.value) {
|
||||
return (
|
||||
<p id="errorInsertCssrules">
|
||||
{'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
|
||||
id="insertCssRuleButton"
|
||||
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
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user