From d1187eda43f98cc8263267715101f236f297344f Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 22 Dec 2022 20:14:09 +0100 Subject: [PATCH] refactor(web/progress): circle progress to Mantine --- .../features/progress/CircleProgressbar.tsx | 95 ++++++++++--------- web/src/index.css | 10 +- web/src/main.tsx | 6 +- 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/web/src/features/progress/CircleProgressbar.tsx b/web/src/features/progress/CircleProgressbar.tsx index ca3ce23..b504619 100644 --- a/web/src/features/progress/CircleProgressbar.tsx +++ b/web/src/features/progress/CircleProgressbar.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { CircularProgress, CircularProgressLabel, Flex, Text } from '@chakra-ui/react'; +import { RingProgress, Text, useMantineTheme, keyframes, Stack, createStyles } from '@mantine/core'; import { useNuiEvent } from '../../hooks/useNuiEvent'; import { fetchNui } from '../../utils/fetchNui'; @@ -10,13 +10,49 @@ export interface CircleProgressbarProps { percent?: boolean; } +// 33.5 is the r of the circle +const progressCircle = keyframes({ + '0%': { strokeDasharray: `0, ${33.5 * 2 * Math.PI}` }, + '100%': { strokeDasharray: `${33.5 * 2 * Math.PI}, 0` }, +}); + +const useStyles = createStyles((theme, params: { position: 'middle' | 'bottom'; duration: number }) => ({ + container: { + position: 'absolute', + top: params.position === 'middle' ? '50%' : undefined, + bottom: params.position === 'bottom' ? '20%' : undefined, + left: '50%', + transform: 'translate(-50%, -50%)', + justifyContent: 'center', + alignItems: 'center', + }, + progress: { + // Scuffed way of grabbing the first section and animating it + '> svg > circle:nth-child(2)': { + transition: 'none', + animation: `${progressCircle} linear forwards`, + animationDuration: `${params.duration}ms`, + }, + }, + value: { + textAlign: 'center', + fontFamily: 'roboto-mono', + textShadow: theme.shadows.sm, + }, + label: { + textAlign: 'center', + textShadow: theme.shadows.sm, + }, +})); + const CircleProgressbar: React.FC = () => { const [visible, setVisible] = React.useState(false); const [progressDuration, setProgressDuration] = React.useState(0); const [position, setPosition] = React.useState<'middle' | 'bottom'>('middle'); const [value, setValue] = React.useState(0); const [label, setLabel] = React.useState(''); - const [cancelled, setCancelled] = React.useState(false); + const theme = useMantineTheme(); + const { classes } = useStyles({ position, duration: progressDuration }); const progressComplete = () => { setVisible(false); @@ -24,7 +60,6 @@ const CircleProgressbar: React.FC = () => { }; const progressCancel = () => { - setCancelled(true); setValue(99); // Sets the final value to 100% kek setVisible(false); }; @@ -33,7 +68,6 @@ const CircleProgressbar: React.FC = () => { useNuiEvent('circleProgress', (data) => { if (visible) return; - setCancelled(false); setVisible(true); setValue(0); setLabel(data.label || ''); @@ -50,50 +84,21 @@ const CircleProgressbar: React.FC = () => { }); return ( - + <> {visible && ( - - + - {value}% - - - {label} - - + className={classes.progress} + label={{value}%} + /> + {label && {label}} + )} - + ); }; diff --git a/web/src/index.css b/web/src/index.css index 2522b4a..88fc75d 100644 --- a/web/src/index.css +++ b/web/src/index.css @@ -4,6 +4,7 @@ @import url('https://fonts.googleapis.com/css2?family=DM+Mono:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;500;600;700&display=swap'); @import url("https://use.typekit.net/wxh5ury.css"); +@import url("https://use.typekit.net/qgr5ebd.css"); html { color-scheme: normal !important; @@ -29,15 +30,6 @@ code { font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace; } -@keyframes progress { - 0% { - stroke-dasharray: 0, 264; - } - 100% { - stroke-dasharray: 264, 0; - } -} - @keyframes progress-bar { from { width: 0%; diff --git a/web/src/main.tsx b/web/src/main.tsx index 5a89415..f945ee5 100644 --- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -28,7 +28,11 @@ const root = document.getElementById('root'); ReactDOM.createRoot(root!).render( - +