Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use client";
import React from 'react'

import {
useMotionTemplate,
useSpring,
m,
LazyMotion,
domAnimation,
AnimatePresence,
} from "framer-motion";
import {
ReactNode,
Expand Down Expand Up @@ -79,28 +80,29 @@ export function useProgressInternal() {

useInterval(
() => {
// If we start progress but the bar is currently complete, reset it first.
if (spring.get() === 100) {
spring.jump(0);
}

const current = spring.get();
spring.set(Math.min(current + getDiff(current), 99));
},
loading ? 750 : null
);

useEffect(() => {
if (!loading) {
spring.jump(0);
if (loading) {
// Simluate first "tick", to avoid having to wait 750 ms for feedback
Copy link

Choose a reason for hiding this comment

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

Simulate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. I'll fix it

spring.set(getDiff(0));
} else {
// Jump to 100 when complete
spring.jump(100);
}
}, [spring, loading]);

/**
* Start the progress.
*/
function start() {
setLoading(true)
// Reset when starting
spring.jump(0);
setLoading(true);
}

return { loading, spring, start };
Expand Down Expand Up @@ -161,13 +163,15 @@ export function ProgressBar({

return (
<LazyMotion features={domAnimation}>
{progress.loading && (
<m.div
style={{ width }}
exit={{ opacity: 0 }}
className={className}
/>
)}
<AnimatePresence>
{progress.loading && (
<m.div
style={{ width }}
exit={{ opacity: 0 }}
className={className}
/>
)}
</AnimatePresence>
</LazyMotion>
);
}
Expand All @@ -185,5 +189,4 @@ export function useProgress(): StartProgress {
progress.start();
}
return startProgress
}

}