-
Notifications
You must be signed in to change notification settings - Fork 1
ClientReady #56
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
Merged
Merged
ClientReady #56
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,104 @@ | ||
| /** @import { JSX } from 'retend/jsx-runtime' */ | ||
|
|
||
| import { Await, Cell, If, onSetup } from 'retend'; | ||
|
|
||
| /** | ||
| * @typedef {Object} ClientOnlyProps | ||
| * @property {JSX.Children} children | ||
| * The content to render only on the client side. | ||
| * @property {JSX.Template} [fallback] | ||
| * Optional content to render during server-side rendering | ||
| * and before the client has mounted. | ||
| */ | ||
|
|
||
| /** | ||
| * A component that only renders its children on the client side. | ||
| * | ||
| * During server-side rendering, the `fallback` is rendered instead | ||
| * (or nothing if no fallback is provided). Once the component is | ||
| * mounted on the client, it swaps to the real `children`. | ||
| * | ||
| * This is useful for components that depend on browser-only APIs | ||
| * (e.g., `window.innerWidth`) and would produce a server/client | ||
| * mismatch during hydration. | ||
| * | ||
| * @param {ClientOnlyProps} props | ||
| * @returns {JSX.Template} | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * import { ClientOnly } from 'retend-server'; | ||
| * | ||
| * const Page = () => ( | ||
| * <ClientOnly fallback={<p>Loading...</p>}> | ||
| * <WindowSizeDependentComponent /> | ||
| * </ClientOnly> | ||
| * ); | ||
| * ``` | ||
| */ | ||
| export function ClientOnly(props) { | ||
| const { children, fallback } = props; | ||
| const mounted = Cell.source(false); | ||
|
|
||
| onSetup(() => { | ||
| mounted.set(true); | ||
| }); | ||
|
|
||
| return If(mounted, { | ||
| true: () => children, | ||
| false: () => fallback, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @typedef {Object} ClientReadyProps | ||
| * @property {JSX.Children} children | ||
| * The content to render after the app is running on the client and its | ||
| * initial async work has completed. | ||
| * @property {JSX.Template} [fallback] | ||
| * Optional content to render during server-side rendering, hydration, | ||
| * and the client subtree's initial async work. | ||
| */ | ||
|
|
||
| /** | ||
| * A component that renders fallback content until a client-only subtree is | ||
| * mounted and its initial `Await` boundary has finished resolving. | ||
| * | ||
| * Unlike `ClientOnly`, this keeps the fallback visible while async cells inside | ||
| * the client subtree are still pending on the first client render. | ||
| * | ||
| * @param {ClientReadyProps} props | ||
| * @returns {JSX.Template} | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * import { ClientReady } from 'retend-server'; | ||
| * | ||
| * const App = () => ( | ||
| * <ClientReady fallback={<p>Starting...</p>}> | ||
| * <Dashboard /> | ||
| * </ClientReady> | ||
| * ); | ||
| * ``` | ||
| */ | ||
| export function ClientReady(props) { | ||
| const { children, fallback } = props; | ||
| const ready = Cell.source(false); | ||
|
|
||
| const ClientReadyContent = () => { | ||
| onSetup(() => ready.set(true)); | ||
| return children; | ||
| }; | ||
|
|
||
| return [ | ||
| ClientOnly({ | ||
| children: () => | ||
| Await({ | ||
| children: ClientReadyContent, | ||
| }), | ||
| }), | ||
| If(ready, { | ||
| false: () => fallback, | ||
| }), | ||
| ]; | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
1. Fallback/content overlap
🐞 Bug≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools