Skip to content

Commit

Permalink
feat: ProgressIndicator clone
Browse files Browse the repository at this point in the history
  • Loading branch information
semnil5202 committed Jul 27, 2024
1 parent 3e8bb9c commit be2cdeb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { HTMLAttributes, PropsWithChildren, forwardRef } from 'react';

import { progressIndicatorCss, stepCss } from './styles';

export interface ProgressIndicatorProps extends Omit<HTMLAttributes<HTMLDivElement>, 'style'> {
mode?: 'horizontal' | 'vertical';
totalStep: number;
currentStep: number;
}

export const ProgressIndicator = forwardRef<HTMLDivElement, PropsWithChildren<ProgressIndicatorProps>>(
({ mode = 'horizontal', totalStep, currentStep, children, ...rest }, ref) => {
const flexDirection = mode === 'horizontal' ? 'row' : 'column';
const currentStepIndex = Math.min(totalStep, currentStep) - 1;
const basis = 100 / totalStep;

return (
<div ref={ref} {...rest} css={progressIndicatorCss({ flexDirection })}>
{Array.from({ length: totalStep }).map((_, index) => (
<ProgressIndicator.Step key={index} isCurrent={index === currentStepIndex} basis={basis} />
))}
{children}
</div>
);
},
) as React.ForwardRefExoticComponent<ProgressIndicatorProps> & { Step: typeof Step };

export interface StepProps extends HTMLAttributes<HTMLSpanElement> {
basis: number;
isCurrent: boolean;
}

const Step = ({ isCurrent, basis, ...rest }: StepProps) => {
let color = '#E1E1E1';

if (isCurrent) {
color = '#FF7664';
}

return <span {...rest} css={stepCss({ basis, color })} />;
};

ProgressIndicator.displayName = 'ProgressIndicator';
ProgressIndicator.Step = Step;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { css } from '@emotion/react';

import { ProgressIndicatorCssArgs, StepCssArgs } from './type';

export const progressIndicatorCss = ({ flexDirection }: ProgressIndicatorCssArgs) =>
css({ width: '100%', display: 'flex', flexWrap: 'nowrap', flexDirection });

export const stepCss = ({ basis, color }: StepCssArgs) =>
css({
boxSizing: 'border-box',
flex: `${basis}% 1 1`,
height: '4px',
backgroundColor: color,
borderRadius: '8px',
margin: '2px',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type ProgressIndicatorCssArgs = {
flexDirection: 'row' | 'column';
};

export type StepCssArgs = {
color: string;
basis: number;
};

0 comments on commit be2cdeb

Please sign in to comment.