Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): add <ClientOnly/> internally #964

Merged
merged 11 commits into from
Jun 24, 2024
5 changes: 5 additions & 0 deletions .changeset/thirty-panthers-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suspensive/react": patch
---

fix(react): add `<ClienOnly/>` for clientOnly prop of `<Suspense/>`
10 changes: 7 additions & 3 deletions packages/react/src/Suspense.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Suspense as ReactSuspense, type SuspenseProps as ReactSuspenseProps, useContext } from 'react'
import { ClientOnly } from './components/ClientOnly'
import { SuspenseDefaultPropsContext, syncDevMode } from './contexts'
import { useIsClient } from './hooks'
import type { PropsWithDevMode } from './utility-types'

const SuspenseClientOnly = (props: ReactSuspenseProps) =>
useIsClient() ? <ReactSuspense {...props} /> : <>{props.fallback}</>
const SuspenseClientOnly = (props: ReactSuspenseProps) => (
<ClientOnly fallback={props.fallback}>
<ReactSuspense {...props} />
</ClientOnly>
)
Comment on lines +6 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


export interface SuspenseProps extends PropsWithDevMode<ReactSuspenseProps, SuspenseDevModeProp> {
/**
Expand All @@ -21,6 +24,7 @@ export interface SuspenseProps extends PropsWithDevMode<ReactSuspenseProps, Susp
export const Suspense = ({ clientOnly, devMode, children, fallback }: SuspenseProps) => {
const defaultProps = useContext(SuspenseDefaultPropsContext)
const DefinedSuspense = defaultProps.clientOnly ?? clientOnly ? SuspenseClientOnly : ReactSuspense

return (
<DefinedSuspense fallback={typeof fallback === 'undefined' ? defaultProps.fallback : fallback}>
{children}
Expand Down
40 changes: 40 additions & 0 deletions packages/react/src/components/ClientOnly.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { render, screen } from '@testing-library/react'
import { type Mock } from 'vitest'
import { useIsClient } from '../hooks/useIsClient'
import { ClientOnly } from './ClientOnly'

vi.mock('../hooks/useIsClient', () => ({
useIsClient: vi.fn(),
}))

afterEach(() => {
vi.clearAllMocks()
})

describe('<ClientOnly/>', () => {
it('renders children when isClient is true', () => {
;(useIsClient as Mock).mockReturnValue(true)

render(
<ClientOnly fallback={<div>Loading...</div>}>
<div>Client Content</div>
</ClientOnly>
)

expect(screen.getByText('Client Content')).toBeInTheDocument()
expect(screen.queryByText('Loading...')).not.toBeInTheDocument()
})

it('renders fallback when isClient is false', () => {
;(useIsClient as Mock).mockReturnValue(false)

render(
<ClientOnly fallback={<div>Loading...</div>}>
<div>Client Content</div>
</ClientOnly>
)

expect(screen.getByText('Loading...')).toBeInTheDocument()
expect(screen.queryByText('Client Content')).not.toBeInTheDocument()
})
})
9 changes: 9 additions & 0 deletions packages/react/src/components/ClientOnly.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { ReactNode } from 'react'
import { useIsClient } from '../hooks/useIsClient'

interface ClientOnlyProps {
children: ReactNode
fallback?: ReactNode
}

export const ClientOnly = ({ children, fallback }: ClientOnlyProps) => <>{useIsClient() ? children : fallback}</>
Loading