-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(skeleton): Skeleton 컴포넌트 추가 (#114)
- Loading branch information
Showing
10 changed files
with
197 additions
and
3 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
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,33 @@ | ||
import React from 'react'; | ||
import { CombineElementProps } from 'src/types/utils'; | ||
|
||
type Props = Omit< | ||
CombineElementProps< | ||
'div', | ||
{ | ||
width: number; | ||
height: number; | ||
backgroundStyle: string; | ||
} | ||
>, | ||
'role' | ||
>; | ||
|
||
const Circle = ({ width, height, backgroundStyle, style, ...rest }: Props) => { | ||
return ( | ||
<div | ||
style={{ | ||
display: 'inline-block', | ||
width: `${width}px`, | ||
height: `${height}px`, | ||
background: backgroundStyle, | ||
borderRadius: '50%', | ||
...style, | ||
}} | ||
role="img" | ||
{...rest} | ||
/> | ||
); | ||
}; | ||
|
||
export default Circle; |
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,32 @@ | ||
import React from 'react'; | ||
import { CombineElementProps } from 'src/types/utils'; | ||
|
||
type Props = Omit< | ||
CombineElementProps< | ||
'div', | ||
{ | ||
width: number; | ||
height: number; | ||
backgroundStyle: string; | ||
} | ||
>, | ||
'role' | ||
>; | ||
|
||
const Rect = ({ width, height, backgroundStyle, style, ...rest }: Props) => { | ||
return ( | ||
<div | ||
style={{ | ||
display: 'inline-block', | ||
width: `${width}px`, | ||
height: `${height}px`, | ||
background: backgroundStyle, | ||
...style, | ||
}} | ||
role="img" | ||
{...rest} | ||
/> | ||
); | ||
}; | ||
|
||
export default Rect; |
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,45 @@ | ||
import React from 'react'; | ||
import { CombineElementProps } from 'src/types/utils'; | ||
import { colors } from 'src/constants/colors'; | ||
import { useAnimateGradient } from './useAnimateGradient'; | ||
import Rect from './Rect'; | ||
import Circle from './Circle'; | ||
|
||
type Props = Omit< | ||
CombineElementProps< | ||
'div', | ||
{ | ||
type?: 'rect' | 'circle'; | ||
width: number; | ||
height: number; | ||
backgroundColor?: string; | ||
foregroundColor?: string; | ||
} | ||
>, | ||
'role' | ||
>; | ||
|
||
const Skeleton = ({ | ||
type = 'rect', | ||
width, | ||
height, | ||
backgroundColor = colors.gray30, | ||
foregroundColor = colors.gray20, | ||
...rest | ||
}: Props) => { | ||
const gradientStyle = useAnimateGradient({ | ||
foregroundColor, | ||
backgroundColor, | ||
}); | ||
|
||
if (type === 'rect') { | ||
return <Rect width={width} height={height} backgroundStyle={gradientStyle} {...rest} />; | ||
} else { | ||
return <Circle width={width} height={height} backgroundStyle={gradientStyle} {...rest} />; | ||
} | ||
}; | ||
|
||
export default Skeleton; | ||
|
||
Skeleton.Circle = Circle; | ||
Skeleton.Rect = Rect; |
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,44 @@ | ||
import { useCallback } from 'react'; | ||
import { useState } from 'react'; | ||
import { useAnimationFrame } from 'src/hooks/useAnimationFrame'; | ||
|
||
const gradientDegree = 90; | ||
|
||
export interface GradientOptions { | ||
foregroundColor: string; | ||
backgroundColor: string; | ||
progress: number; | ||
width: number; | ||
} | ||
export function calcGradient({ | ||
foregroundColor, | ||
backgroundColor, | ||
progress, | ||
width, | ||
}: GradientOptions) { | ||
const startPosition = Math.max(progress - width, 0); | ||
const endPosition = Math.min(progress + width, 100); | ||
|
||
return `linear-gradient(${gradientDegree}deg, ${backgroundColor} ${startPosition}%, ${foregroundColor} ${progress}%, ${backgroundColor} ${endPosition}%)`; | ||
} | ||
|
||
interface Options { | ||
foregroundColor: string; | ||
backgroundColor: string; | ||
} | ||
export function useAnimateGradient({ foregroundColor, backgroundColor }: Options) { | ||
const [progress, setProgress] = useState(-200); | ||
|
||
const updateProgress = useCallback(() => { | ||
setProgress((prev) => (prev >= 200 ? -200 : prev + 3)); | ||
}, []); | ||
|
||
useAnimationFrame(updateProgress); | ||
|
||
return calcGradient({ | ||
foregroundColor, | ||
backgroundColor, | ||
progress, | ||
width: 20, | ||
}); | ||
} |
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,21 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export function useAnimationFrame(callback: () => void) { | ||
const animateRequestRef = useRef<number>(); | ||
|
||
useEffect(() => { | ||
const animate = () => { | ||
callback(); | ||
console.log('callback'); | ||
animateRequestRef.current = requestAnimationFrame(animate); | ||
}; | ||
|
||
animate(); | ||
|
||
return () => { | ||
if (animateRequestRef.current != null) { | ||
cancelAnimationFrame(animateRequestRef.current); | ||
} | ||
}; | ||
}, [callback]); | ||
} |
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,18 @@ | ||
import { Skeleton, Spacing } from 'src'; | ||
import { Meta, Story, Canvas } from '@storybook/addon-docs/blocks'; | ||
|
||
<Meta title="components/Skeleton" components={Skeleton} /> | ||
|
||
# Skeleton | ||
|
||
Skeleton 컴포넌트는 | ||
|
||
<Canvas> | ||
<Story name="Skeleton"> | ||
<div style={{ display: 'flex', flexDirection: 'column' }}> | ||
<Skeleton width={50} height={50} type="circle" /> | ||
<Spacing size={10} /> | ||
<Skeleton width={100} height={20} /> | ||
</div> | ||
</Story> | ||
</Canvas> |