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
Merged

Conversation

Collection50
Copy link
Contributor

@Collection50 Collection50 commented Jun 24, 2024

related #528

Overview

Implemented the ClientOnly component.

PR Checklist

  • I did below actions if need
  1. I read the Contributing Guide
  2. I added documents and tests.

Copy link

changeset-bot bot commented Jun 24, 2024

🦋 Changeset detected

Latest commit: 4b8d354

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@suspensive/react Patch
@suspensive/react-query Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jun 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
suspensive.org ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 24, 2024 9:17am
v1.suspensive.org ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 24, 2024 9:17am
visualization.suspensive.org ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 24, 2024 9:17am

Comment on lines 1 to 15
import { useSyncExternalStore } from 'react'
import { noop } from '../utils'

function getSnapshot() {
return false
}

function getServerSnapshot() {
return true
}

// hook interface instead of useIsClient in hooks
export function useIsClient() {
return useSyncExternalStore(() => noop, getSnapshot, getServerSnapshot)
}
Copy link
Member

Choose a reason for hiding this comment

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

This change is for next stage. Use as-is useIsClient please first for current user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about this?

import React, { useState } from 'react'
import { noop } from '../utils'
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'

export const useIsClient = () => {
  const [isClient, setIsClient] = useState(false)

  useIsomorphicLayoutEffect(() => {
    setIsClient(true)
  }, [])

  if (typeof React['useSyncExternalStore'] === 'function') {
    return React['useSyncExternalStore'](
      () => noop,
      () => true,
      () => false
    )
  }

  return isClient
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a variant of the existing useIsClient and does not change the interface of the current Suspensive user; rather, the migration to the next stage is much more acceptable.

Copy link
Member

@manudeli manudeli Jun 24, 2024

Choose a reason for hiding this comment

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

This is a variant of the existing useIsClient and does not change the interface of the current Suspensive user; rather, the migration to the next stage is much more acceptable.

I agree your opinion and good Idea. but we should change our feature more gradually. I want to check this feature change as beta version. use original useIsClient please. In my opinion, This pull request should be just only for adding ClientOnly component. not improving Suspense clientOnly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your opinion. I'll reflect it.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for accepting my opinion. I also accept your opinion if we can too. but we should be more careful for this library users. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just grateful to be able to contribute to a great library, with a developer I admire. If there's anything else I can contribute to, I'd love to.

Comment on lines 4 to 8
export const ClientOnly = ({ children, fallback }: SuspenseProps) => {
const isClient = useIsClient()

return isClient ? <>{children}</> : <>{fallback}</>
}
Copy link
Member

Choose a reason for hiding this comment

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

  1. Shortly if we can please
  2. This is ClientOnly. so make type ClientOnlyProps please
Suggested change
export const ClientOnly = ({ children, fallback }: SuspenseProps) => {
const isClient = useIsClient()
return isClient ? <>{children}</> : <>{fallback}</>
}
export const ClientOnly = ({ children, fallback }: ClientOnlyProps) => <>{useIsClient() ? children : fallback}</>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think most of the time <ClientOnly/> be used as the HOC, so it need a children.
What do you think about using it as?

interface ClienOnlyProp {
  children: ReactNode:
  fallback?: ReactNode
}

Copy link
Member

Choose a reason for hiding this comment

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

Cool! That's good for me

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

Choose a reason for hiding this comment

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

👍

@manudeli manudeli changed the title feat(react): add ClienOnly component fix(react): add ClientOnly component internally Jun 24, 2024
@manudeli manudeli changed the title fix(react): add ClientOnly component internally fix(react): add <ClientOnly/> internally Jun 24, 2024
Comment on lines 1 to 39
import { render, screen } from '@testing-library/react'
import { useIsClient } from '../hooks/useIsClient'
import { ClientOnly } from './ClientOnly'

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

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

describe('<ClientOnly/>', () => {
it('renders children when isClient is true', () => {
useIsClient.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.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()
})
})
Copy link
Member

Choose a reason for hiding this comment

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

Cool test code!

Copy link
Member

@manudeli manudeli left a comment

Choose a reason for hiding this comment

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

Thanks! @Collection50

@manudeli
Copy link
Member

@Collection50 resolve ci:type error please.

Copy link

codspeed-hq bot commented Jun 24, 2024

CodSpeed Performance Report

Merging #964 will create unknown performance changes

Comparing Collection50:ClientOnly (4b8d354) with main (aa9008b)

Summary

⚠️ No benchmarks were detected in both the base of the PR and the PR.

Copy link
Member

@manudeli manudeli left a comment

Choose a reason for hiding this comment

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

@Collection50 Let's improve clientOnly gradually after this pull request merging

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.71%. Comparing base (aa9008b) to head (4b8d354).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #964      +/-   ##
==========================================
+ Coverage   80.67%   80.71%   +0.04%     
==========================================
  Files          36       37       +1     
  Lines         445      446       +1     
  Branches       98       98              
==========================================
+ Hits          359      360       +1     
  Misses         77       77              
  Partials        9        9              
Components Coverage Δ
@suspensive/react 96.99% <100.00%> (+0.01%) ⬆️
@suspensive/react-query ∅ <ø> (∅)
@suspensive/react-query-4 0.00% <ø> (ø)
@suspensive/react-query-5 0.00% <ø> (ø)
@suspensive/react-await 100.00% <ø> (ø)
@suspensive/react-image 23.52% <ø> (ø)

@manudeli manudeli merged commit d87a482 into toss:main Jun 24, 2024
14 checks passed
@github-actions github-actions bot mentioned this pull request Jun 24, 2024
manudeli pushed a commit that referenced this pull request Jun 24, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @suspensive/react@2.2.1

### Patch Changes

- [#964](#964)
[`d87a482`](d87a482)
Thanks [@Collection50](https://github.com/Collection50)! - fix(react):
add `<ClienOnly/>` for clientOnly prop of `<Suspense/>`

## @suspensive/react-query@2.2.1

### Patch Changes

- Updated dependencies
\[[`d87a482`](d87a482)]:
    -   @suspensive/react@2.2.1
    -   @suspensive/react-query-4@0.0.1
    -   @suspensive/react-query-5@0.0.1

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
manudeli added a commit that referenced this pull request Aug 3, 2024
related #528 

# Overview

Implemented the `ClientOnly` component.

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md)
2. I added documents and tests.

---------

Co-authored-by: Jonghyeon Ko <jonghyeon@toss.im>
manudeli added a commit that referenced this pull request Aug 3, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @suspensive/react@2.2.1

### Patch Changes

- [#964](#964)
[`d87a482`](d87a482)
Thanks [@Collection50](https://github.com/Collection50)! - fix(react):
add `<ClienOnly/>` for clientOnly prop of `<Suspense/>`

## @suspensive/react-query@2.2.1

### Patch Changes

- Updated dependencies
\[[`d87a482`](d87a482)]:
    -   @suspensive/react@2.2.1
    -   @suspensive/react-query-4@0.0.1
    -   @suspensive/react-query-5@0.0.1

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
manudeli pushed a commit that referenced this pull request Aug 3, 2024
related #528 

# Overview

Implemented the `ClientOnly` component.

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md)
2. I added documents and tests.

---------
manudeli added a commit that referenced this pull request Aug 3, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @suspensive/react@2.2.1

### Patch Changes

- [#964](#964)
[`d87a482`](d87a482)
Thanks [@Collection50](https://github.com/Collection50)! - fix(react):
add `<ClienOnly/>` for clientOnly prop of `<Suspense/>`

## @suspensive/react-query@2.2.1

### Patch Changes

- Updated dependencies
\[[`d87a482`](d87a482)]:
    -   @suspensive/react@2.2.1
    -   @suspensive/react-query-4@0.0.1
    -   @suspensive/react-query-5@0.0.1
manudeli added a commit that referenced this pull request Aug 3, 2024
related #528 

# Overview

Implemented the `ClientOnly` component.

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md)
2. I added documents and tests.

---------

Co-authored-by: Jonghyeon Ko <jonghyeon@toss.im>
manudeli added a commit that referenced this pull request Aug 3, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @suspensive/react@2.2.1

### Patch Changes

- [#964](#964)
[`d87a482`](d87a482)
Thanks [@Collection50](https://github.com/Collection50)! - fix(react):
add `<ClienOnly/>` for clientOnly prop of `<Suspense/>`

## @suspensive/react-query@2.2.1

### Patch Changes

- Updated dependencies
\[[`d87a482`](d87a482)]:
    -   @suspensive/react@2.2.1
    -   @suspensive/react-query-4@0.0.1
    -   @suspensive/react-query-5@0.0.1
manudeli added a commit that referenced this pull request Aug 3, 2024
related #528 

# Overview

Implemented the `ClientOnly` component.

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md)
2. I added documents and tests.

---------

Co-authored-by: Jonghyeon Ko <jonghyeon@toss.im>
manudeli added a commit that referenced this pull request Aug 3, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @suspensive/react@2.2.1

### Patch Changes

- [#964](#964)
[`d87a482`](d87a482)
Thanks [@Collection50](https://github.com/Collection50)! - fix(react):
add `<ClienOnly/>` for clientOnly prop of `<Suspense/>`

## @suspensive/react-query@2.2.1

### Patch Changes

- Updated dependencies
\[[`d87a482`](d87a482)]:
    -   @suspensive/react@2.2.1
    -   @suspensive/react-query-4@0.0.1
    -   @suspensive/react-query-5@0.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants