-
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.
Merge pull request #223 from Peersyst/rc/feat/circular-progress
[RC] Feat: add `CircularProgress` component
- Loading branch information
Showing
11 changed files
with
997 additions
and
2,005 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
94 changes: 94 additions & 0 deletions
94
packages/genesys/packages/react-components/src/CircularProgress/CircularProgress.styles.ts
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,94 @@ | ||
/* Greatly inspired by MUI's `CircularProgress` */ | ||
|
||
import styled, { css, keyframes } from "styled-components"; | ||
import { CircularProgressProps } from "./CircularProgress.types"; | ||
|
||
const circularRotateKeyframe = keyframes` | ||
0% { | ||
transform: rotate(0deg); | ||
} | ||
100% { | ||
transform: rotate(360deg); | ||
} | ||
`; | ||
|
||
const circularDashKeyframe = keyframes` | ||
0% { | ||
stroke-dasharray: 1px, 200px; | ||
stroke-dashoffset: 0; | ||
} | ||
50% { | ||
stroke-dasharray: 100px, 200px; | ||
stroke-dashoffset: -15px; | ||
} | ||
100% { | ||
stroke-dasharray: 100px, 200px; | ||
stroke-dashoffset: -125px; | ||
} | ||
`; | ||
|
||
function getCircularProgressRootStyles(value: CircularProgressProps["value"]) { | ||
if (!value) { | ||
return css` | ||
animation: ${circularRotateKeyframe} 1.4s linear infinite; | ||
`; | ||
} | ||
} | ||
|
||
function getCircularProgressCircleStyles(value: CircularProgressProps["value"]) { | ||
if (value) { | ||
return css` | ||
transition: stroke-dashoffset 0.3s cubic-bezier(0.4, 0, 0.2, 1); | ||
`; | ||
} else { | ||
return css` | ||
animation: ${circularDashKeyframe} 1.4s ease-in-out infinite; | ||
stroke-dasharray: 80px, 200px; | ||
stroke-dashoffset: 0px; | ||
`; | ||
} | ||
} | ||
|
||
export const CircularProgressRoot = styled.span<CircularProgressProps>( | ||
({ value }) => css` | ||
display: inline-block; | ||
${getCircularProgressRootStyles(value)}; | ||
`, | ||
); | ||
|
||
export const CircularProgressSvg = styled.svg( | ||
() => css` | ||
display: block; | ||
`, | ||
); | ||
|
||
export const CircularProgressCircle = styled.circle<CircularProgressProps>( | ||
({ value }) => css` | ||
stroke: currentColor; | ||
stroke-linecap: round; | ||
${getCircularProgressCircleStyles(value)}; | ||
`, | ||
); | ||
|
||
export const CircularProgressContent = styled.div( | ||
() => css` | ||
top: 0; | ||
left: 0; | ||
bottom: 0; | ||
right: 0; | ||
position: absolute; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
`, | ||
); | ||
|
||
export const CircularProgressWrapper = styled.div( | ||
() => css` | ||
position: relative; | ||
display: inline-flex; | ||
`, | ||
); |
71 changes: 71 additions & 0 deletions
71
packages/genesys/packages/react-components/src/CircularProgress/CircularProgress.tsx
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,71 @@ | ||
/* Greatly inspired by MUI's `CircularProgress` */ | ||
|
||
import { CircularProgressProps } from "./CircularProgress.types"; | ||
import { | ||
CircularProgressCircle, | ||
CircularProgressContent, | ||
CircularProgressRoot, | ||
CircularProgressSvg, | ||
CircularProgressWrapper, | ||
} from "./CircularProgress.styles"; | ||
import { cx } from "@peersyst/react-utils"; | ||
import { useColor, useMergeDefaultProps } from "@peersyst/react-components-core"; | ||
import { CSSProperties } from "react"; | ||
|
||
const SIZE = 44; | ||
|
||
export default function CircularProgress(props: CircularProgressProps): JSX.Element { | ||
const { | ||
value = 0, | ||
className, | ||
style, | ||
color: colorProp = "primary", | ||
size = 40, | ||
thickness = 4, | ||
children, | ||
} = useMergeDefaultProps("CircularProgress", props); | ||
|
||
const color = useColor(colorProp); | ||
|
||
const circleStyle: CSSProperties = {}; | ||
const rootStyle: CSSProperties = {}; | ||
|
||
if (value) { | ||
const circumference = 2 * Math.PI * ((SIZE - thickness) / 2); | ||
circleStyle.strokeDasharray = circumference.toFixed(3); | ||
circleStyle.strokeDashoffset = `${(((100 - value) / 100) * circumference).toFixed(3)}px`; | ||
rootStyle.transform = "rotate(-90deg)"; | ||
} | ||
|
||
return ( | ||
<CircularProgressWrapper> | ||
<CircularProgressRoot | ||
className={cx("CircularProgress", className)} | ||
style={{ width: size, height: size, color, ...rootStyle, ...style }} | ||
role="progressbar" | ||
value={value} | ||
> | ||
<CircularProgressSvg | ||
className="CircularProgressSvg" | ||
viewBox={`${SIZE / 2} ${SIZE / 2} ${SIZE} ${SIZE}`} | ||
> | ||
<CircularProgressCircle | ||
className="CircularProgressCircle" | ||
style={circleStyle} | ||
cx={SIZE} | ||
cy={SIZE} | ||
r={(SIZE - thickness) / 2} | ||
fill="none" | ||
strokeWidth={thickness} | ||
value={value} | ||
/> | ||
</CircularProgressSvg> | ||
</CircularProgressRoot> | ||
{children && ( | ||
<CircularProgressContent className="CircularProgressContent"> | ||
{children} | ||
</CircularProgressContent> | ||
)} | ||
</CircularProgressWrapper> | ||
); | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/genesys/packages/react-components/src/CircularProgress/CircularProgress.types.ts
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 { ThemeColor } from "@peersyst/react-components-core"; | ||
import { CSSProperties, ReactNode } from "react"; | ||
|
||
export interface CircularProgressProps { | ||
/** | ||
* Progress indicator. Must be between 0 and 100 | ||
*/ | ||
value?: number; | ||
/** | ||
* CircularProgress className | ||
*/ | ||
className?: string; | ||
/** | ||
* CircularProgress style | ||
*/ | ||
style?: CSSProperties; | ||
/** | ||
* CircularProgress color | ||
*/ | ||
color?: ThemeColor; | ||
/** | ||
* CircularProgress size | ||
*/ | ||
size?: number; | ||
/** | ||
* CircularProgress thickness | ||
*/ | ||
thickness?: number; | ||
/** | ||
* CircularProgress children | ||
*/ | ||
children?: ReactNode; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/genesys/packages/react-components/src/CircularProgress/index.ts
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,3 @@ | ||
export { default as CircularProgress } from "./CircularProgress"; | ||
export * from "./CircularProgress.styles"; | ||
export * from "./CircularProgress.types"; |
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
17 changes: 17 additions & 0 deletions
17
packages/genesys/packages/react-components/storybook/stories/CircularProgress.stories.tsx
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,17 @@ | ||
import { ComponentMeta, ComponentStory } from "@storybook/react"; | ||
import { CircularProgress } from "../../src"; | ||
|
||
export default { | ||
title: "CircularProgress", | ||
component: CircularProgress, | ||
} as ComponentMeta<typeof CircularProgress>; | ||
|
||
const Template: ComponentStory<typeof CircularProgress> = (args) => <CircularProgress {...args} />; | ||
|
||
export const Indeterminate = Template.bind({}); | ||
Indeterminate.args = {}; | ||
|
||
export const Determinate = Template.bind({}); | ||
Determinate.args = { | ||
value: 50, | ||
}; |
Oops, something went wrong.