-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat(skeleton): Skeleton 컴포넌트 추가 #114
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기 콘솔로그 지워지면 좋을 것 같슴다...!