-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: GLTFLoader의 프로그래스를 보여 줄 로딩 오버레이 추가
- Loading branch information
Showing
6 changed files
with
171 additions
and
15 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
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 |
---|---|---|
@@ -1,7 +1,24 @@ | ||
import { useState } from 'react'; | ||
import { ModelViewer } from './components/ModelViewer'; | ||
import { LoadingOverlay } from './components/LoadingOverlay'; | ||
|
||
function App() { | ||
return <ModelViewer />; | ||
const [progress, setProgress] = useState(0); | ||
const [isLoading, setLoading] = useState(true); | ||
|
||
const handleLoadComplete = () => { | ||
setLoading(false); | ||
}; | ||
|
||
return ( | ||
<> | ||
<ModelViewer | ||
onProgress={setProgress} | ||
onLoadComplete={handleLoadComplete} | ||
/> | ||
<LoadingOverlay progress={progress} isLoading={isLoading} /> | ||
</> | ||
); | ||
} | ||
|
||
export default App; |
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,57 @@ | ||
import { act, render, screen } from '@testing-library/react'; | ||
import { expect, describe, it, vi } from 'vitest'; | ||
import { LoadingOverlay } from './LoadingOverlay'; | ||
|
||
describe('LoadingOverlay', () => { | ||
it('renders correctly with given progress', () => { | ||
render(<LoadingOverlay progress={50} isLoading={true} />); | ||
|
||
expect(screen.getByRole('progressbar')).toBeInTheDocument(); | ||
expect(screen.getByText('Loading...')).toBeInTheDocument(); | ||
}); | ||
|
||
it('updates progress correctly', () => { | ||
const { rerender } = render( | ||
<LoadingOverlay progress={25} isLoading={true} /> | ||
); | ||
|
||
let progressBar = screen.getByRole('progressbar').firstChild; | ||
expect(progressBar).toHaveStyle('transform: translateX(-75%)'); | ||
|
||
rerender(<LoadingOverlay progress={75} isLoading={true} />); | ||
progressBar = screen.getByRole('progressbar').firstChild; | ||
expect(progressBar).toHaveStyle('transform: translateX(-25%)'); | ||
}); | ||
|
||
it('fades out and removes from DOM when not loading', async () => { | ||
vi.useFakeTimers(); | ||
const { rerender } = render( | ||
<LoadingOverlay progress={100} isLoading={true} /> | ||
); | ||
|
||
expect(screen.getByRole('progressbar')).toBeInTheDocument(); | ||
|
||
rerender(<LoadingOverlay progress={100} isLoading={false} />); | ||
|
||
expect( | ||
screen.getByRole('progressbar').parentElement?.parentElement | ||
).toHaveClass('opacity-0'); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(350); | ||
}); | ||
|
||
expect(screen.queryByRole('progressbar')).not.toBeInTheDocument(); | ||
|
||
vi.useRealTimers(); | ||
}); | ||
|
||
it('has correct accessibility attributes', () => { | ||
render(<LoadingOverlay progress={50} isLoading={true} />); | ||
|
||
const progressBar = screen.getByRole('progressbar') | ||
.firstChild as HTMLElement; | ||
expect(progressBar).toHaveAttribute('aria-valuenow', '50'); | ||
expect(progressBar).toHaveAttribute('aria-labelledby'); | ||
}); | ||
}); |
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,52 @@ | ||
import React, { useEffect, useId, useState, useTransition } from 'react'; | ||
|
||
interface LoadingOverlayProps { | ||
progress: number; | ||
isLoading: boolean; | ||
} | ||
|
||
export const LoadingOverlay: React.FC<LoadingOverlayProps> = ({ | ||
progress, | ||
isLoading, | ||
}) => { | ||
const id = useId(); | ||
const [_, startTransition] = useTransition(); | ||
const [show, setShow] = useState(isLoading); | ||
|
||
useEffect(() => { | ||
if (!isLoading) { | ||
startTransition(() => { | ||
setTimeout(() => setShow(false), 350); | ||
}); | ||
} else { | ||
setShow(true); | ||
} | ||
}, [isLoading]); | ||
|
||
if (!show) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div | ||
className={`fixed top-0 w-screen h-screen flex items-center justify-center bg-gradient-to-b from-slate-950 to-yellow-950 transition-opacity duration-300 ${isLoading ? 'opacity-100' : 'opacity-0'}`} | ||
> | ||
<div className="w-1/3 flex flex-col items-center gap-4"> | ||
<div | ||
className="w-full rounded-full h-2.5 bg-gray-700 overflow-hidden" | ||
role="progressbar" | ||
> | ||
<div | ||
className="w-full h-2.5 rounded-full bg-gradient-to-r from-yellow-700 to-yellow-200 transition-transform" | ||
style={{ transform: `translateX(${progress - 100}%)` }} | ||
aria-valuenow={progress} | ||
aria-labelledby={id} | ||
/> | ||
</div> | ||
<div id={id} className="font-bold text-white"> | ||
Loading... | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
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