-
Notifications
You must be signed in to change notification settings - Fork 33
added the circular progress bar #6
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
Open
a-khushal
wants to merge
3
commits into
code100x:main
Choose a base branch
from
a-khushal:circular_progress_bar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,50 +1,140 @@ | ||
| import * as React from "react" | ||
| import { StyleProp, TextStyle, View, ViewStyle } from "react-native" | ||
| import { observer } from "mobx-react-lite" | ||
| import { spacing } from "app/theme" | ||
| import { Text } from "app/components/Text" | ||
|
|
||
| export interface ProgressProps { | ||
| /** a required prop to specify the progress. */ | ||
| progress: number | ||
| /** an optional style override useful for padding & margin. */ | ||
| style?: StyleProp<ViewStyle> | ||
| // Packages Imports | ||
|
||
| import { useState } from "react"; | ||
| import { StyleProp, StyleSheet, TextStyle, View, ViewStyle } from "react-native"; | ||
| import Animated, { | ||
| Extrapolate, | ||
| interpolate, | ||
| runOnJS, | ||
| useAnimatedProps, | ||
| useDerivedValue, | ||
| withTiming, | ||
| } from "react-native-reanimated"; | ||
| import { Svg, Circle } from "react-native-svg"; | ||
|
|
||
| // Create an Animated Component for the Circle | ||
| const AnimatedCircle = Animated.createAnimatedComponent(Circle); | ||
|
|
||
| // interface for the component | ||
| export interface CircularProgressProps { | ||
| progress: number; | ||
| size?: number; | ||
| strokeWidth?: number; | ||
| showLabel?: boolean; | ||
| outerCircleColor?: string; | ||
| progressCircleColor?: string; | ||
| labelColor?: string; | ||
| labelStyle?: StyleProp<TextStyle>; | ||
| labelSize?: number; | ||
| } | ||
|
|
||
| export const Progress = observer(function Progress(props: ProgressProps) { | ||
| const { progress, style } = props | ||
| const $styles = [$container, style] | ||
| // function component for CircularProgress | ||
| function CircularProgress(props: CircularProgressProps) { | ||
| // Destructuring props | ||
| const { | ||
| size = 80, | ||
| strokeWidth = (5 * size) / 100, | ||
| progress = 0, | ||
| showLabel = true, | ||
| labelSize = (20 * size) / 100, | ||
| ...otherProps | ||
| } = props; | ||
|
|
||
| // get other props | ||
| const { | ||
| labelColor = "white", | ||
| labelStyle, | ||
| outerCircleColor = "white", | ||
| progressCircleColor = "dodgerblue", | ||
| } = otherProps; | ||
|
|
||
| // Constants | ||
| const radius = (size - strokeWidth) / 2; | ||
| const circum = radius * 2 * Math.PI; | ||
|
|
||
| // Local States | ||
| const [LabelText, SetLabelText] = useState(0); | ||
|
|
||
| // Derive the progress value from props | ||
| const derivedProgressValue = useDerivedValue(() => { | ||
| if (showLabel) runOnJS(SetLabelText)(Math.min(progress, 100)); | ||
|
|
||
| return withTiming(progress); | ||
| }, [progress]); | ||
|
|
||
| // AnimatedProps for the circle | ||
| const circleAnimatedProps = useAnimatedProps(() => { | ||
| const SVG_Progress = interpolate( | ||
| derivedProgressValue.value, | ||
| [0, 100], | ||
| [100, 0], | ||
| Extrapolate.CLAMP | ||
| ); | ||
|
|
||
| // This dash offset is the inner circle progress | ||
| return { | ||
| strokeDashoffset: radius * Math.PI * 2 * (SVG_Progress / 100), | ||
| }; | ||
| }); | ||
|
|
||
| // Style for the Label View | ||
| const labelViewContainerStyle: StyleProp<ViewStyle> = [ | ||
| styles.labelView, | ||
| { | ||
| width: size, | ||
| height: size, | ||
| }, | ||
| ]; | ||
|
|
||
| // const style for the label text | ||
| const labelTextStyles: StyleProp<TextStyle> = [ | ||
| { color: labelColor, fontSize: labelSize }, | ||
| labelStyle, | ||
| ]; | ||
|
|
||
| // render | ||
| return ( | ||
| <View style={$styles}> | ||
| <View style={$bar}> | ||
| <View style={[$fill, { width: `${progress}%` }]} /> | ||
| </View> | ||
| <Text style={$text}>{progress}%</Text> | ||
| </View> | ||
| ) | ||
| }) | ||
|
|
||
| const $container: ViewStyle = { | ||
| flexDirection: "row", | ||
| alignItems: "center", | ||
| } | ||
| <Svg width={size} height={size}> | ||
| <Circle | ||
| stroke={outerCircleColor} | ||
| fill="none" | ||
| cx={size / 2} | ||
| cy={size / 2} | ||
| r={radius} | ||
| strokeWidth={strokeWidth} | ||
| /> | ||
|
|
||
| const $bar: ViewStyle = { | ||
| backgroundColor: "#0e1829", | ||
| borderRadius: spacing.xxs, | ||
| flex: 1, | ||
| height: spacing.xs, | ||
| } | ||
| <AnimatedCircle | ||
| stroke={progressCircleColor} | ||
| fill="none" | ||
| cx={size / 2} | ||
| cy={size / 2} | ||
| r={radius} | ||
| strokeDasharray={`${circum} ${circum}`} | ||
| strokeLinecap="round" | ||
| transform={`rotate(-90, ${size / 2}, ${size / 2})`} | ||
| strokeWidth={strokeWidth} | ||
| animatedProps={circleAnimatedProps} | ||
| /> | ||
|
|
||
| const $fill: ViewStyle = { | ||
| backgroundColor: "#1d3255", | ||
| borderRadius: spacing.xxxs, | ||
| height: "100%", | ||
| {showLabel ? ( | ||
| <View style={labelViewContainerStyle}> | ||
| <Animated.Text style={labelTextStyles}>{`${LabelText}%`}</Animated.Text> | ||
| </View> | ||
| ) : null} | ||
| </Svg> | ||
| ); | ||
| } | ||
|
|
||
| const $text: TextStyle = { | ||
| color: "#9CA3AF", | ||
| fontSize: spacing.md, | ||
| marginLeft: spacing.sm, | ||
| } | ||
| // exports | ||
| export default CircularProgress; | ||
|
|
||
| // styles | ||
| const styles = StyleSheet.create({ | ||
| labelView: { | ||
| position: "absolute", | ||
| top: 0, | ||
| left: 0, | ||
| justifyContent: "center", | ||
| alignItems: "center", | ||
| }, | ||
| }); | ||
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.
remove inline styling and use object-based styling as the other styles