Add Partials (#1824)
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { Logger } from "./Logger.tsx";
|
||||
|
||||
export default function Counter(props: { id: string }) {
|
||||
const sig = useSignal(0);
|
||||
return (
|
||||
<Logger name={`Counter ${props.id}`}>
|
||||
<div class="island">
|
||||
<p class={`output-${props.id}`}>{sig.value}</p>
|
||||
<button onClick={() => sig.value += 1}>
|
||||
update
|
||||
</button>
|
||||
</div>
|
||||
</Logger>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { Logger } from "./Logger.tsx";
|
||||
|
||||
export default function CounterA() {
|
||||
const sig = useSignal(0);
|
||||
return (
|
||||
<Logger name="Counter A">
|
||||
<div class="island island-a">
|
||||
<p class="output-a">{sig.value}</p>
|
||||
<button onClick={() => sig.value += 1}>
|
||||
update
|
||||
</button>
|
||||
</div>
|
||||
</Logger>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { Logger } from "./Logger.tsx";
|
||||
|
||||
export default function CounterB() {
|
||||
const sig = useSignal(0);
|
||||
return (
|
||||
<Logger name="Counter B">
|
||||
<div class="island island-b">
|
||||
<p class="output-b">{sig.value}</p>
|
||||
<button onClick={() => sig.value += 1}>
|
||||
update
|
||||
</button>
|
||||
</div>
|
||||
</Logger>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { useEffect, useRef } from "preact/hooks";
|
||||
import { ComponentChildren } from "preact";
|
||||
|
||||
export function Fader(props: { children?: ComponentChildren }) {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
const mounted = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!mounted.current) {
|
||||
mounted.current = true;
|
||||
return;
|
||||
}
|
||||
if (ref.current) {
|
||||
ref.current.animate([
|
||||
{ backgroundColor: "white" },
|
||||
{ backgroundColor: "peachpuff" },
|
||||
], {
|
||||
fill: "backwards",
|
||||
easing: "ease-out",
|
||||
duration: 600,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return <div ref={ref}>{props.children}</div>;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Partial } from "$fresh/runtime.ts";
|
||||
|
||||
export default function InvalidSlot() {
|
||||
return (
|
||||
<div class="island">
|
||||
<Partial name="invalid">
|
||||
<h1>it doesn't work</h1>
|
||||
</Partial>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { ComponentChildren } from "preact";
|
||||
import { useEffect, useRef } from "preact/hooks";
|
||||
|
||||
export function Logger(props: { children?: ComponentChildren; name?: string }) {
|
||||
const mounted = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
mounted.current = true;
|
||||
const logs = document.querySelector("#logs");
|
||||
if (logs) {
|
||||
logs.textContent += `mount ${props.name}\n`;
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (logs) {
|
||||
logs.textContent += `unmount ${props.name}\n`;
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
if (mounted.current && typeof document !== "undefined") {
|
||||
const logs = document.querySelector("#logs");
|
||||
if (logs) {
|
||||
logs.textContent += `update ${props.name}\n`;
|
||||
}
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
return props.children as any;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { Logger } from "./Logger.tsx";
|
||||
|
||||
export default function Other() {
|
||||
const sig = useSignal(0);
|
||||
return (
|
||||
<Logger name="Other">
|
||||
<div class="island island-other">
|
||||
<p class="output-other">{sig.value}</p>
|
||||
<button onClick={() => sig.value += 1}>
|
||||
update
|
||||
</button>
|
||||
</div>
|
||||
</Logger>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Signal } from "@preact/signals";
|
||||
import { ComponentChildren } from "preact";
|
||||
|
||||
export default function PartialTrigger(
|
||||
props: {
|
||||
class: string;
|
||||
href: string;
|
||||
partial?: string;
|
||||
loading?: Signal<boolean>;
|
||||
children?: ComponentChildren;
|
||||
},
|
||||
) {
|
||||
return (
|
||||
<a
|
||||
class={props.class}
|
||||
href={props.href}
|
||||
fh-partial={props.partial}
|
||||
fh-loading={props.loading}
|
||||
>
|
||||
{props.children}
|
||||
</a>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Fader } from "$fresh/tests/fixture_partials/islands/Fader.tsx";
|
||||
import { Logger } from "./Logger.tsx";
|
||||
import { ComponentChildren } from "preact";
|
||||
|
||||
export default function PassThrough(props: { children?: ComponentChildren }) {
|
||||
return (
|
||||
<Fader>
|
||||
<Logger name="PassThrough">
|
||||
<div class="island">
|
||||
{props.children}
|
||||
</div>
|
||||
</Logger>
|
||||
</Fader>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
export default function PropIsland(
|
||||
props: {
|
||||
number: number;
|
||||
string: string;
|
||||
boolean: boolean;
|
||||
strArr: string[];
|
||||
obj: { foo: number };
|
||||
},
|
||||
) {
|
||||
return (
|
||||
<pre class="pre-props">
|
||||
{JSON.stringify({
|
||||
number: props.number,
|
||||
string: props.string,
|
||||
null: null,
|
||||
boolean: props.boolean,
|
||||
object: props.obj,
|
||||
strArr: props.strArr,
|
||||
},null,2)}
|
||||
</pre>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Signal } from "@preact/signals";
|
||||
|
||||
export default function SignalProp(props: { sig: Signal<number> }) {
|
||||
return (
|
||||
<div class="island">
|
||||
<p class="output">{props.sig.value}</p>
|
||||
<button onClick={() => props.sig.value += 1}>
|
||||
update
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Signal } from "@preact/signals";
|
||||
|
||||
export default function Spinner(props: { id: string; show: Signal<boolean> }) {
|
||||
return props.show.value
|
||||
? <p class={`spinner spinner-${props.id}`}>loading...</p>
|
||||
: null;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { useState } from "preact/hooks";
|
||||
|
||||
export default function Stateful(props: { id: string }) {
|
||||
const [v, set] = useState(0);
|
||||
return (
|
||||
<div class="island">
|
||||
<p class={`output-${props.id}`}>{v}</p>
|
||||
<button class={`btn-${props.id}`} onClick={() => set((v) => v + 1)}>
|
||||
update
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user