Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

ClientOnly.tsx

38 lines972 Bytes

1import * as Solid from 'solid-js'
2import { isServer } from 'solid-js/web'
3​
4export interface ClientOnlyProps {
5 /**
6 * The children to render if the JS is loaded.
7 */
8 children: Solid.JSX.Element
9 /**
10 * The fallback component to render if the JS is not yet loaded.
11 */
12 fallback?: Solid.JSX.Element
14​
15/**
16 * Render the children only after the JS has loaded client-side. Use an optional
17 * fallback component if the JS is not yet loaded.
18 *
19 * @example
20 * Render a Chart component if JS loads, renders a simple FakeChart
21 * component server-side or if there is no JS. The FakeChart can have only the
22 * UI without the behavior or be a loading spinner or skeleton.
23 *
24 * ```tsx
25 * return (
26 * <ClientOnly fallback={<FakeChart />}>
27 * <Chart />
28 * </ClientOnly>
29 * )
30 * ```
31 */
32export function ClientOnly(props: ClientOnlyProps) {
33 return (
34 <Solid.Show when={!isServer} fallback={props.fallback}>
35 <>{props.children}</>
36 </Solid.Show>
37 )