-
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 #111 from emiljohansson/feature/more-svelte
Feature/more svelte
- Loading branch information
Showing
29 changed files
with
990 additions
and
113 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,42 @@ | ||
import { Signal, computed, effect } from '@preact/signals-core' | ||
import { useEffect, useMemo, useRef, useState } from 'react' | ||
|
||
export function useSignalValue<T>(signal: Signal<T>): T { | ||
const [state, setState] = useState<T>(signal.value) | ||
useEffect(() => { | ||
return effect(() => setState(signal.value)) | ||
}, [signal]) | ||
return state | ||
} | ||
|
||
export function useSignalAndValue<T>(initialValue: T): [Signal<T>, T] { | ||
const signal = useSignal(initialValue) | ||
const value = useSignalValue<T>(signal) | ||
return [signal, value] | ||
} | ||
|
||
export function useComputedValue<T>(compute: () => T): T { | ||
const signal = useComputed(compute) | ||
const value = useSignalValue(signal) | ||
return value | ||
} | ||
|
||
// Following hooks taken from https://github.com/preactjs/signals/blob/main/packages/react/runtime/src/index.ts | ||
export function useSignal<T>(initialValue: T): Signal<T> { | ||
return useMemo(() => new Signal<T>(initialValue), []) | ||
} | ||
|
||
export function useComputed<T>(compute: () => T): Signal<T> { | ||
const $compute = useRef(compute) | ||
$compute.current = compute | ||
return useMemo(() => computed<T>(() => $compute.current()), []) | ||
} | ||
|
||
export function useSignalEffect(cb: () => void | (() => void)): void { | ||
const callback = useRef(cb) | ||
callback.current = cb | ||
|
||
useEffect(() => { | ||
return effect(() => callback.current()) | ||
}, []) | ||
} |
This file was deleted.
Oops, something went wrong.
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,46 @@ | ||
// temporary solution from https://github.com/JonAbrams/signals-react-safe/tree/main | ||
|
||
import { | ||
Signal, | ||
signal, | ||
computed, | ||
effect, | ||
batch, | ||
type ReadonlySignal, | ||
untracked, | ||
} from '@preact/signals-core' | ||
import { useSignalValue } from './hooks' | ||
import { ReactElement } from 'react' | ||
|
||
export { | ||
signal, | ||
computed, | ||
effect, | ||
batch, | ||
Signal, | ||
type ReadonlySignal, | ||
untracked, | ||
} | ||
export * from './hooks' | ||
|
||
const ReactElemType = Symbol.for('react.element') // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15 | ||
|
||
function SignalValue({ data }: { data: Signal }) { | ||
return useSignalValue(data) | ||
} | ||
|
||
Object.defineProperties(Signal.prototype, { | ||
$$typeof: { configurable: true, value: ReactElemType }, | ||
type: { configurable: true, value: SignalValue }, | ||
props: { | ||
configurable: true, | ||
get() { | ||
return { data: this } | ||
}, | ||
}, | ||
ref: { configurable: true, value: null }, | ||
}) | ||
|
||
declare module '@preact/signals-core' { | ||
interface Signal extends ReactElement {} | ||
} |
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
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,8 @@ | ||
import type { PageServerLoad } from './$types' | ||
import randomString from '@emiljohansson/random-string' | ||
|
||
export const load: PageServerLoad<{ randomValue: string }> = () => { | ||
return { | ||
randomValue: randomString(), | ||
} | ||
} |
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,25 @@ | ||
<script lang="ts"> | ||
import type { PageData } from './$types' | ||
import ClickOutsideSection from './ClickOutsideSection.svelte' | ||
import CounterSection from './CounterSection.svelte' | ||
import DebounceSection from './DebounceSection.svelte' | ||
import RandomStringSection from './RandomStringSection.svelte' | ||
export let data: PageData | ||
</script> | ||
|
||
<svelte:head> | ||
<title>Hooks | emiljohansson.dev</title> | ||
<meta name="description" content="Genrates a random string" /> | ||
</svelte:head> | ||
|
||
<header class="bg-white border-b border-slate-200 px-6 py-12"> | ||
<h1 class="text-4xl font-medium mb-0">Current Time</h1> | ||
</header> | ||
|
||
<main class="px-6 py-4 grid gap-4"> | ||
<RandomStringSection initValue={data.randomValue} /> | ||
<DebounceSection /> | ||
<CounterSection /> | ||
<ClickOutsideSection /> | ||
</main> |
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,16 @@ | ||
<script lang="ts"> | ||
let isWithin = false | ||
function onGlobalClick() { | ||
isWithin = false; | ||
} | ||
</script> | ||
|
||
<svelte:document on:click={onGlobalClick} /> | ||
|
||
<article> | ||
<h2>Click Outside</h2> | ||
<button class="btn-primary" on:click|stopPropagation={() => isWithin = true}> | ||
{isWithin ? 'Within' : 'Outside'} | ||
</button> | ||
</article> |
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,24 @@ | ||
<script lang="ts"> | ||
import { createCounter } from "./counter" | ||
let {count, increment, decrement, reset, set} = createCounter() | ||
</script> | ||
|
||
<article> | ||
<h2>Counter</h2> | ||
<p>Count: {$count}</p> | ||
<div class="flex"> | ||
<button class="btn-secondary" on:click={increment}> | ||
Increment | ||
</button> | ||
<button class="btn-secondary" on:click={decrement}> | ||
Decrement | ||
</button> | ||
<button class="btn-secondary" on:click={reset}> | ||
Reset | ||
</button> | ||
<button class="btn-secondary" on:click={() => set(5)}> | ||
Set 5 | ||
</button> | ||
</div> | ||
</article> |
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,22 @@ | ||
<script lang="ts"> | ||
let value = '' | ||
let debounced = '' | ||
let timer: ReturnType<typeof setTimeout> | ||
const debounce = (func: () => void, delay = 300) => { | ||
clearTimeout(timer); | ||
timer = setTimeout(func, delay) | ||
} | ||
</script> | ||
|
||
<article> | ||
<h2>Debounce</h2> | ||
<input | ||
class="input" | ||
type="text" | ||
bind:value={value} | ||
on:keyup={() => debounce(() => debounced = value)} | ||
/> | ||
<p>Value: {value}</p> | ||
<p>Debounced: {debounced}</p> | ||
</article> |
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,12 @@ | ||
<script lang="ts"> | ||
import { randomString } from "./randomString" | ||
export let initValue: string | ||
let {value: random, generate} = randomString(initValue) | ||
</script> | ||
|
||
<article> | ||
<h2>Random String</h2> | ||
<button on:click={generate}>{$random}</button> | ||
</article> |
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,20 @@ | ||
import { clamp } from 'lib/utils/number' | ||
import { writable } from 'svelte/store' | ||
|
||
export function createCounter( | ||
initialValue = 0, | ||
options?: Partial<{ | ||
min: number | ||
max: number | ||
}>, | ||
) { | ||
const { min, max } = { min: -Infinity, max: Infinity, ...options } | ||
const count = writable(clamp(initialValue, min, max)) | ||
|
||
const increment = () => count.update((v) => clamp(v + 1, min, max)) | ||
const decrement = () => count.update((v) => clamp(v - 1, min, max)) | ||
const reset = () => count.set(clamp(initialValue, min, max)) | ||
const set = (newValue: number) => count.set(clamp(newValue, min, max)) | ||
|
||
return { count, increment, decrement, reset, set } as const | ||
} |
Oops, something went wrong.
7250e4d
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.emiljohansson.dev
games-git-main-emiljohansson.vercel.app
7250e4d
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-emiljohansson.vercel.app
pw.emiljohansson.dev
password-manager-kappa-blush.vercel.app
password-manager-git-main-emiljohansson.vercel.app