-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elements): Add temporary useIsLoading hook (#2751)
* feat(elements): Add loading tags * feat(elements): Add unstable useIsLoading hook * chore(elements): Use new hook in example sign-in * chore(elements): Bump package version * chore(repo): Add empty changeset * chore(elements): Revert some stuff to main * chore(elements): Adjust logger to be more concise * fix(elements): Adjust loader and hook * chore(elements): Adjust example page * chore(elements): Revert unnecessary change * fix(elements): Review comments Co-authored-by: Tom Milewski <[email protected]> --------- Co-authored-by: Tom Milewski <[email protected]>
- Loading branch information
Showing
6 changed files
with
149 additions
and
43 deletions.
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 { useIsLoading_unstable } 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] = useIsLoading_unstable(); | ||
|
||
return isLoading ? <Loader /> : children; | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/elements/src/react/sign-in/hooks/use-loading.hook.ts
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 useIsLoading_unstable = () => { | ||
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 }]; | ||
}; |
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