-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from emiljohansson/feature/signals
install preact signals and create a example page
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use client' | ||
|
||
import { signal, computed } from '@preact/signals-core' | ||
import './initSignals' | ||
|
||
const count = signal(0) | ||
const double = computed(() => count.value * 2) | ||
|
||
export default function Content() { | ||
return ( | ||
<> | ||
<h2>Counter</h2> | ||
<p>Count: {count}</p> | ||
<p>Double: {double}</p> | ||
<div className="flex"> | ||
<button className="btn-secondary" onClick={() => count.value++}> | ||
Increment | ||
</button> | ||
<button className="btn-secondary" onClick={() => count.value--}> | ||
Decrement | ||
</button> | ||
<button | ||
className="btn-secondary" | ||
onClick={() => { | ||
count.value = 0 | ||
}} | ||
> | ||
Reset | ||
</button> | ||
<button | ||
className="btn-secondary" | ||
onClick={() => { | ||
count.value = 5 | ||
}} | ||
> | ||
Set 5 | ||
</button> | ||
</div> | ||
</> | ||
) | ||
} |
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,32 @@ | ||
import type { Signal } from '@preact/signals-react' | ||
|
||
import { Signal as SignalBase, effect } from '@preact/signals-core' | ||
import { useEffect, useState } from 'react' | ||
|
||
export function useSignalState<T>(signal: Signal<T>) { | ||
const [state, setState] = useState<T>(signal.value) | ||
|
||
useEffect(() => { | ||
return effect(() => setState(signal.value)) | ||
}, [signal]) | ||
|
||
return state | ||
} | ||
|
||
const ReactElemType = Symbol.for('react.element') // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15 | ||
|
||
function SignalValue({ data }: { data: Signal }) { | ||
return useSignalState(data) | ||
} | ||
|
||
Object.defineProperties(SignalBase.prototype, { | ||
$$typeof: { configurable: true, value: ReactElemType }, | ||
type: { configurable: true, value: SignalValue }, | ||
props: { | ||
configurable: true, | ||
get() { | ||
return { data: this } | ||
}, | ||
}, | ||
ref: { configurable: true, value: null }, | ||
}) |
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,5 @@ | ||
import Content from './Content' | ||
|
||
export default function Page() { | ||
return <Content /> | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
639e3bd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
password-manager – ./apps/password-manager
password-manager-kappa-blush.vercel.app
password-manager-git-main-emiljohansson.vercel.app
password-manager-emiljohansson.vercel.app
pw.emiljohansson.dev
639e3bd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
games – ./apps/games
games-emiljohansson.vercel.app
games-ivory.vercel.app
games-git-main-emiljohansson.vercel.app
games.emiljohansson.dev