-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathclient-only.ts
More file actions
54 lines (51 loc) · 1.3 KB
/
client-only.ts
File metadata and controls
54 lines (51 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import {
createMemo,
createSignal,
onMount,
sharedConfig,
splitProps,
untrack,
} from 'solid-js'
import { isServer } from 'solid-js/web'
import type { Component, ComponentProps, JSX, Setter } from 'solid-js'
/**
*
* Read more: https://docs.solidjs.com/solid-start/reference/client/client-only
*/
// not using Suspense
export default function clientOnly<T extends Component<any>>(
fn: () => Promise<{
default: T
}>,
options: { lazy?: boolean } = {},
) {
if (isServer)
return (props: ComponentProps<T> & { fallback?: JSX.Element }) =>
props.fallback
const [comp, setComp] = createSignal<T>()
!options.lazy && load(fn, setComp)
return (props: ComponentProps<T>) => {
let Comp: T | undefined
let m: boolean
const [, rest] = splitProps(props, ['fallback'])
options.lazy && load(fn, setComp)
if ((Comp = comp()) && !sharedConfig.context) return Comp(rest)
const [mounted, setMounted] = createSignal(!sharedConfig.context)
onMount(() => setMounted(true))
return createMemo(
() => (
(Comp = comp()),
(m = mounted()),
untrack(() => (Comp && m ? Comp(rest) : props.fallback))
),
)
}
}
function load<T>(
fn: () => Promise<{
default: T
}>,
setComp: Setter<T>,
) {
fn().then((m) => setComp(() => m.default))
}