Skip to content
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

add epic progress bar #465

Merged
merged 6 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
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
58 changes: 58 additions & 0 deletions app/components/progess-bar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useNavigation } from '@remix-run/react'
import React, { useEffect, useRef, useState } from 'react'
import { useSpinDelay } from 'spin-delay'
import { cn } from '#app/utils/misc.tsx'
import { Spinner } from './spinner.tsx'

function EpicProgress() {
const transition = useNavigation()
const busy = transition.state !== 'idle'
const delayedPending = useSpinDelay(busy, {
delay: 400,
minDuration: 300,
})
const ref = useRef<HTMLDivElement>(null)
const [animationComplete, setAnimationComplete] = useState(true)

useEffect(() => {
if (!ref.current) return
if (delayedPending) setAnimationComplete(false)

const animationPromises = ref.current
.getAnimations()
.map(({ finished }) => finished)

Promise.allSettled(animationPromises).then(() => {
if (!delayedPending) setAnimationComplete(true)
})
}, [delayedPending])

return (
<div
role="progressbar"
aria-hidden={!delayedPending}
aria-valuetext={delayedPending ? 'Loading' : undefined}
className="fixed inset-x-0 left-0 top-0 z-50 h-[0.20rem] animate-pulse"
>
<div
ref={ref}
className={cn(
'h-full bg-blue-600 transition-all duration-300 ease-in-out',
transition.state === 'idle' &&
animationComplete &&
'w-0 opacity-0 transition-none',
transition.state === 'submitting' && 'w-1/12',
transition.state === 'loading' && 'w-2/12',
transition.state === 'idle' && !animationComplete && 'w-full',
)}
/>
{delayedPending && (
<div className="absolute inset-0 flex items-center justify-center">
<Spinner showSpinner={true} className="text-blue-600" />
</div>
)}
</div>
)
}

export { EpicProgress }
12 changes: 10 additions & 2 deletions app/components/spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
export function Spinner({ showSpinner }: { showSpinner: boolean }) {
import { cn } from '#app/utils/misc.tsx'

export function Spinner({
showSpinner,
className = '',
}: {
showSpinner: boolean
className?: string
}) {
return (
<div
className={`absolute right-0 top-[6px] transition-opacity ${
showSpinner ? 'opacity-100' : 'opacity-0'
}`}
>
<svg
className="-ml-1 mr-3 h-5 w-5 animate-spin"
className={cn('-ml-1 mr-3 h-5 w-5 animate-spin', className)}
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
Expand Down
8 changes: 3 additions & 5 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { z } from 'zod'
import { Confetti } from './components/confetti.tsx'
import { GeneralErrorBoundary } from './components/error-boundary.tsx'
import { ErrorList } from './components/forms.tsx'
import { EpicProgress } from './components/progess-bar.tsx'
import { SearchBar } from './components/search-bar.tsx'
import { EpicToaster } from './components/toaster.tsx'
import { Button } from './components/ui/button.tsx'
Expand All @@ -47,11 +48,7 @@ import { ClientHintCheck, getHints, useHints } from './utils/client-hints.tsx'
import { getConfetti } from './utils/confetti.server.ts'
import { prisma } from './utils/db.server.ts'
import { getEnv } from './utils/env.server.ts'
import {
combineHeaders,
getDomainUrl,
getUserImgSrc,
} from './utils/misc.tsx'
import { combineHeaders, getDomainUrl, getUserImgSrc } from './utils/misc.tsx'
import { useNonce } from './utils/nonce-provider.ts'
import { useRequestInfo } from './utils/request-info.ts'
import { type Theme, setTheme, getTheme } from './utils/theme.server.ts'
Expand Down Expand Up @@ -273,6 +270,7 @@ function App() {
</div>
<Confetti id={data.confettiId} />
<EpicToaster toast={data.toast} />
<EpicProgress/>
</Document>
)
}
Expand Down