Skip to content

Commit

Permalink
docs: Add loading bar pure component
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Jun 7, 2024
1 parent a7a83a9 commit cb36e8d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/nextjs/components/Formatted.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import clsx from "clsx";
import React from "react";

Expand Down
18 changes: 18 additions & 0 deletions examples/nextjs/components/LoadingBar.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.container {
position: fixed;
top: 0;
width: 100%;
height: 3px;
z-index: 200;
}

.inner {
position: absolute;
top: 0;
left: -10px;
height: 100%;
width: 100%;
background-color: red;
transform-origin: left center;
box-shadow: 0px -3px 10px 5px rgba(255, 0, 0, 0.6);
}
66 changes: 66 additions & 0 deletions examples/nextjs/components/LoadingBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use client';
import { useRef, useEffect } from 'react';
import styles from './LoadingBar.module.css';

const LoadingBar = ({
duration,
loading,
}: {
duration?: number;
loading?: boolean;
}) => {
const barRef = useRef<HTMLDivElement>(null);
const delay = 50;

useEffect(() => {
if (!loading) return;
const node = barRef.current;

let animation: Animation;
const timeoutId = setTimeout(() => {
if (!node) return;
animation = node.animate(
[
{
transform: `translateX(${-100}%)`,
},
{
transform: `translateX(${-50}%)`,
offset: 0.2,
},
{
transform: `translateX(${0}%)`,
easing: 'ease-out',
},
],
{
duration: duration,
iterations: 1,
easing: 'linear',
},
);
}, delay);
return () => {
clearTimeout(timeoutId);
if (animation) animation.cancel();
};
}, [duration, loading]);

return (
<div className={styles.container}>
<div
className={styles.inner}
style={{
transform: `translateX(${-100}%)`,
}}
ref={barRef}
></div>
</div>
);
};
LoadingBar.defaults = {
duration: 200,
loading: false,
};

export default LoadingBar;

0 comments on commit cb36e8d

Please sign in to comment.