-
Notifications
You must be signed in to change notification settings - Fork 283
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(elements): Add temporary useIsLoading hook #2751
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
58caf39
feat(elements): Add loading tags
LekoArts 96b0304
feat(elements): Add unstable useIsLoading hook
LekoArts 1652b46
chore(elements): Use new hook in example sign-in
LekoArts 5efe6f3
chore(elements): Bump package version
LekoArts d2e3e5b
chore(repo): Add empty changeset
LekoArts 935f396
chore(elements): Revert some stuff to main
LekoArts fdb31e6
Merge branch 'main' into lekoarts/sdk-1294-loading-hook-as-a-test
LekoArts a9e7639
chore(elements): Adjust logger to be more concise
LekoArts c4da5bd
fix(elements): Adjust loader and hook
LekoArts fc379f5
chore(elements): Adjust example page
LekoArts 910981a
chore(elements): Revert unnecessary change
LekoArts 4b0d0c0
fix(elements): Review comments
LekoArts 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,2 @@ | ||
--- | ||
--- |
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,64 @@ | ||
'use client'; | ||
import { unstable_useIsLoading } from '@clerk/elements/sign-in'; | ||
import { motion } from 'framer-motion'; | ||
|
||
const colors = ['#22238f', '#6b45fa', '#ca3286', '#fe2b49', '#fe652d']; | ||
|
||
const containerVariants = { | ||
initial: {}, | ||
animate: { | ||
transition: { | ||
when: 'beforeChildren', | ||
staggerChildren: 0.1, | ||
}, | ||
}, | ||
}; | ||
|
||
const dotVariants = { | ||
initial: {}, | ||
animate: { | ||
height: [20, 40, 20], | ||
transition: { | ||
repeat: Infinity, | ||
}, | ||
}, | ||
}; | ||
|
||
const Loader = ({ count = 5 }) => { | ||
return ( | ||
<motion.div | ||
variants={containerVariants} | ||
initial='initial' | ||
animate='animate' | ||
style={{ | ||
display: 'flex', | ||
gap: 10, | ||
height: 40, | ||
alignItems: 'center', | ||
}} | ||
> | ||
{Array(count) | ||
.fill(null) | ||
.map((_, index) => { | ||
return ( | ||
<motion.div | ||
key={index} | ||
variants={dotVariants} | ||
style={{ | ||
height: 20, | ||
width: 20, | ||
backgroundColor: colors[index % colors.length], | ||
borderRadius: 20, | ||
}} | ||
/> | ||
); | ||
})} | ||
</motion.div> | ||
); | ||
}; | ||
|
||
export function Loading({ children }: { children: React.ReactNode }) { | ||
const [isLoading] = unstable_useIsLoading(); | ||
|
||
return isLoading ? <Loader /> : children; | ||
} |
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,33 @@ | ||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
import { useActiveTags } from '~/react/hooks/use-active-tags.hook'; | ||
import { SignInStartCtx } from '~/react/sign-in/start'; | ||
import { SignInFirstFactorCtx, SignInSecondFactorCtx } from '~/react/sign-in/verifications'; | ||
|
||
/** | ||
* Caution: This hook is unstable and may disappear in the future. | ||
* This is a temporary hook until the actual loading API is explored and implemented. | ||
*/ | ||
export const unstable_useIsLoading = () => { | ||
let startLoading = false; | ||
let firstFactorLoading = false; | ||
let secondFactorLoading = false; | ||
|
||
const startRef = SignInStartCtx.useActorRef(true); | ||
if (startRef) { | ||
startLoading = useActiveTags(startRef, 'state:loading'); | ||
} | ||
|
||
const firstFactorRef = SignInFirstFactorCtx.useActorRef(true); | ||
if (firstFactorRef) { | ||
firstFactorLoading = useActiveTags(firstFactorRef, 'state:loading'); | ||
} | ||
|
||
const secondFactorRef = SignInSecondFactorCtx.useActorRef(true); | ||
if (secondFactorRef) { | ||
secondFactorLoading = useActiveTags(secondFactorRef, 'state:loading'); | ||
} | ||
|
||
const isGlobalLoading = startLoading || firstFactorLoading || secondFactorLoading; | ||
|
||
return [isGlobalLoading, { start: startLoading, firstFactor: firstFactorLoading, secondFactor: secondFactorLoading }]; | ||
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. Likely a cleaner/clearer way to handle this in the long-run, but cool for now. On concern though is that it's specific to signIn, and lives in the root React hooks. Mind moving it over? |
||
}; |
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
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.
To keep with React hook naming conventions: