-
Notifications
You must be signed in to change notification settings - Fork 280
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
feat(clerk-react): Support for fallback prop #4723
Draft
BRKalow
wants to merge
6
commits into
main
Choose a base branch
from
brk.feat/fallback-prop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+520
−216
Draft
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8441ba5
Support for fallback prop
BRKalow d78aa6b
Refactor implementation to live outside of ClerkHostRenderer
BRKalow ec8fa36
Adds fallback prop to remaining components
BRKalow c2f8a49
Adds changeset
BRKalow e10c5e4
Adds simple tests for sign in fallback, and cleans up prop types of c…
BRKalow 67e8e2e
render UserProfile fallback
alexcarpenter 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 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,10 @@ | ||
--- | ||
'@clerk/clerk-react': minor | ||
--- | ||
|
||
Adds support for a `fallback` prop on Clerk's components. This allows rendering of a placeholder element while Clerk's components are mounting. Use this to help mitigate mitigate layout shift when using Clerk's components. Example usage: | ||
|
||
|
||
```tsx | ||
<SignIn fallback={<LoadingSkeleton />} /> | ||
``` |
This file contains 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 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 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 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 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,118 @@ | ||
import { without } from '@clerk/shared/object'; | ||
import { isDeeplyEqual } from '@clerk/shared/react'; | ||
import type { PropsWithChildren } from 'react'; | ||
import React from 'react'; | ||
|
||
import type { MountProps, OpenProps } from '../types'; | ||
|
||
const isMountProps = (props: any): props is MountProps => { | ||
return 'mount' in props; | ||
}; | ||
|
||
const isOpenProps = (props: any): props is OpenProps => { | ||
return 'open' in props; | ||
}; | ||
// README: <ClerkHostRenderer/> should be a class pure component in order for mount and unmount | ||
// lifecycle props to be invoked correctly. Replacing the class component with a | ||
// functional component wrapped with a React.memo is not identical to the original | ||
// class implementation due to React intricacies such as the useEffect’s cleanup | ||
// seems to run AFTER unmount, while componentWillUnmount runs BEFORE. | ||
|
||
// More information can be found at https://clerk.slack.com/archives/C015S0BGH8R/p1624891993016300 | ||
|
||
// The function Portal implementation is commented out for future reference. | ||
|
||
// const Portal = React.memo(({ props, mount, unmount }: MountProps) => { | ||
// const portalRef = React.createRef<HTMLDivElement>(); | ||
|
||
// useEffect(() => { | ||
// if (portalRef.current) { | ||
// mount(portalRef.current, props); | ||
// } | ||
// return () => { | ||
// if (portalRef.current) { | ||
// unmount(portalRef.current); | ||
// } | ||
// }; | ||
// }, []); | ||
|
||
// return <div ref={portalRef} />; | ||
// }); | ||
|
||
// Portal.displayName = 'ClerkPortal'; | ||
|
||
/** | ||
* Used to orchestrate mounting of Clerk components in a host React application. | ||
* Components are rendered into a specific DOM node using mount/unmount methods provided by the Clerk class. | ||
*/ | ||
export class ClerkHostRenderer extends React.PureComponent< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is largely unchanged from the previous |
||
PropsWithChildren< | ||
(MountProps | OpenProps) & { | ||
component?: string; | ||
hideRootHtmlElement?: boolean; | ||
rootProps?: JSX.IntrinsicElements['div']; | ||
} | ||
> | ||
> { | ||
private rootRef = React.createRef<HTMLDivElement>(); | ||
|
||
componentDidUpdate(_prevProps: Readonly<MountProps | OpenProps>) { | ||
if (!isMountProps(_prevProps) || !isMountProps(this.props)) { | ||
return; | ||
} | ||
|
||
// Remove children and customPages from props before comparing | ||
// children might hold circular references which deepEqual can't handle | ||
// and the implementation of customPages or customMenuItems relies on props getting new references | ||
const prevProps = without(_prevProps.props, 'customPages', 'customMenuItems', 'children'); | ||
const newProps = without(this.props.props, 'customPages', 'customMenuItems', 'children'); | ||
// instead, we simply use the length of customPages to determine if it changed or not | ||
const customPagesChanged = prevProps.customPages?.length !== newProps.customPages?.length; | ||
const customMenuItemsChanged = prevProps.customMenuItems?.length !== newProps.customMenuItems?.length; | ||
|
||
if (!isDeeplyEqual(prevProps, newProps) || customPagesChanged || customMenuItemsChanged) { | ||
if (this.rootRef.current) { | ||
this.props.updateProps({ node: this.rootRef.current, props: this.props.props }); | ||
} | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
if (this.rootRef.current) { | ||
if (isMountProps(this.props)) { | ||
this.props.mount(this.rootRef.current, this.props.props); | ||
} | ||
|
||
if (isOpenProps(this.props)) { | ||
this.props.open(this.props.props); | ||
} | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.rootRef.current) { | ||
if (isMountProps(this.props)) { | ||
this.props.unmount(this.rootRef.current); | ||
} | ||
if (isOpenProps(this.props)) { | ||
this.props.close(); | ||
} | ||
} | ||
} | ||
|
||
render() { | ||
const { hideRootHtmlElement = false } = this.props; | ||
const rootAttributes = { | ||
ref: this.rootRef, | ||
...this.props.rootProps, | ||
...(this.props.component && { 'data-clerk-component': this.props.component }), | ||
}; | ||
|
||
return ( | ||
<> | ||
{!hideRootHtmlElement && <div {...rootAttributes} />} | ||
{this.props.children} | ||
</> | ||
); | ||
} | ||
} |
Oops, something went wrong.
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.
🥇