import React from 'react' import useTheme from '../use-theme' import { useProportions } from '../utils/calculations' import { GeistUIThemesPalette } from '../themes/presets' import { NormalTypes } from '../utils/prop-types' import useScaleable, { withScaleable } from '../use-scaleable' export type ProgressColors = { [key: number]: string } export type ProgressTypes = NormalTypes interface Props { value?: number max?: number fixedTop?: boolean fixedBottom?: boolean colors?: ProgressColors type?: ProgressTypes className?: string } const defaultProps = { value: 0, max: 100, type: 'default' as ProgressTypes, fixedTop: false, fixedBottom: false, className: '', } type NativeAttrs = Omit, keyof Props> export type ProgressProps = Props & NativeAttrs const getCurrentColor = ( ratio: number, palette: GeistUIThemesPalette, type: ProgressTypes, colors: ProgressColors = {}, ): string => { const defaultColors: { [key in ProgressTypes]: string } = { default: palette.foreground, success: palette.success, secondary: palette.secondary, warning: palette.warning, error: palette.error, } const colorKeys = Object.keys(colors) if (colorKeys.length === 0) return defaultColors[type] const customColorKey = colorKeys.find(key => ratio <= +key) if (!customColorKey || Number.isNaN(+customColorKey)) return defaultColors[type] return colors[+customColorKey] } const ProgressComponent: React.FC = ({ value, max, className, type, colors, fixedTop, fixedBottom, ...props }: ProgressProps & typeof defaultProps) => { const theme = useTheme() const { SCALES } = useScaleable() const percentValue = useProportions(value, max) const currentColor = getCurrentColor(percentValue, theme.palette, type, colors) const fixed = fixedTop || fixedBottom return (
) } ProgressComponent.defaultProps = defaultProps ProgressComponent.displayName = 'GeistProgress' const Progress = withScaleable(ProgressComponent) export default Progress