-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9022d4a
commit 0df78b9
Showing
7 changed files
with
168 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@preact/signals-react-runtime", | ||
"description": "Sub package for @preact/signals-react that contains some useful utilities", | ||
"private": true, | ||
"amdName": "reactSignalsutils", | ||
"main": "dist/utils.js", | ||
"module": "dist/utils.module.js", | ||
"unpkg": "dist/utils.min.js", | ||
"types": "dist/index.d.ts", | ||
"source": "src/index.ts", | ||
"mangle": "../../../mangle.json", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"browser": "./dist/utils.module.js", | ||
"import": "./dist/utils.mjs", | ||
"require": "./dist/utils.js" | ||
} | ||
}, | ||
"dependencies": { | ||
"@preact/signals-core": "workspace:^1.3.0" | ||
}, | ||
"peerDependencies": { | ||
"react": "^16.14.0 || 17.x || 18.x || 19.x" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ReadonlySignal, Signal } from "@preact/signals-core"; | ||
import { useSignal } from "@preact/signals-react"; | ||
import { useSignals } from "@preact/signals-react/runtime"; | ||
import { Fragment, createElement, useMemo } from "react"; | ||
|
||
interface ShowProps<T = boolean> { | ||
when: Signal<T> | ReadonlySignal<T>; | ||
fallback?: JSX.Element; | ||
children: JSX.Element | ((value: T) => JSX.Element); | ||
} | ||
|
||
export function Show<T = boolean>(props: ShowProps<T>): JSX.Element | null { | ||
useSignals(); | ||
const value = props.when.value; | ||
if (!value) return props.fallback || null; | ||
return typeof props.children === "function" | ||
? props.children(value) | ||
: props.children; | ||
} | ||
|
||
interface ForProps<T> { | ||
each: Signal<Array<T>> | ReadonlySignal<Array<T>>; | ||
fallback?: JSX.Element; | ||
children: (value: T, index: number) => JSX.Element; | ||
} | ||
|
||
export function For<T>(props: ForProps<T>): JSX.Element | null { | ||
useSignals(); | ||
const cache = useMemo(() => new Map(), []); | ||
const list = props.each.value; | ||
if (!list.length) return props.fallback || null; | ||
const items = list.map((value, key) => { | ||
if (!cache.has(value)) { | ||
cache.set(value, props.children(value, key)); | ||
} | ||
return cache.get(value); | ||
}); | ||
return createElement(Fragment, { children: items }); | ||
} | ||
|
||
export function useLiveSignal<T>(value: Signal<T> | ReadonlySignal<T>) { | ||
const s = useSignal(value); | ||
if (s.peek() !== value) s.value = value; | ||
return s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { For, Show } from "../../src"; | ||
import { | ||
act, | ||
checkHangingAct, | ||
createRoot, | ||
Root, | ||
} from "../../../test/shared/utils"; | ||
import { signal } from "@preact/signals-react"; | ||
import { createElement } from "react"; | ||
|
||
describe("@preact/signals-react-utils", () => { | ||
let scratch: HTMLDivElement; | ||
let root: Root; | ||
async function render(element: Parameters<Root["render"]>[0]) { | ||
await act(() => root.render(element)); | ||
} | ||
|
||
beforeEach(async () => { | ||
scratch = document.createElement("div"); | ||
document.body.appendChild(scratch); | ||
root = await createRoot(scratch); | ||
}); | ||
|
||
afterEach(async () => { | ||
checkHangingAct(); | ||
await act(() => root.unmount()); | ||
scratch.remove(); | ||
}); | ||
|
||
describe("<Show />", () => { | ||
it("Should reactively show an element", () => { | ||
const toggle = signal(false)!; | ||
const Paragraph = (p: any) => <p>{p.children}</p>; | ||
act(() => { | ||
render( | ||
<Show when={toggle} fallback={<Paragraph>Hiding</Paragraph>}> | ||
<Paragraph>Showing</Paragraph> | ||
</Show> | ||
); | ||
}); | ||
expect(scratch.innerHTML).to.eq("<p>Hiding</p>"); | ||
|
||
act(() => { | ||
toggle.value = true; | ||
}); | ||
expect(scratch.innerHTML).to.eq("<p>Showing</p>"); | ||
}); | ||
}); | ||
|
||
describe("<For />", () => { | ||
it("Should iterate over a list of signals", () => { | ||
const list = signal<Array<string>>([])!; | ||
const Paragraph = (p: any) => <p>{p.children}</p>; | ||
act(() => { | ||
render( | ||
<For each={list} fallback={<Paragraph>No items</Paragraph>}> | ||
{item => <Paragraph key={item}>{item}</Paragraph>} | ||
</For> | ||
); | ||
}); | ||
expect(scratch.innerHTML).to.eq("<p>No items</p>"); | ||
|
||
act(() => { | ||
list.value = ["foo", "bar"]; | ||
}); | ||
expect(scratch.innerHTML).to.eq("<p>foo</p><p>bar</p>"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters