Skip to content

The open alternative to Salesforce, designed for AI.

4.1 KB
Raw Blame
import { styled } from '@linaria/react';
import { motion } from 'framer-motion';

import { AnimatedCheckmark } from '@/ui/navigation/step-bar/components/internal/AnimatedCheckmark/AnimatedCheckmark';
import { useIsMobile } from 'twenty-ui/utilities';
import { MOBILE_VIEWPORT, useTheme, themeCssVariables } from 'twenty-ui/theme';

const StyledContainer = styled.div<{ isLast: boolean }>`
  align-items: center;
  display: flex;
  flex-grow: ${({ isLast }) => (isLast ? '0' : '1')};
  @media (max-width: ${MOBILE_VIEWPORT}px) {
    flex-grow: 0;
  }
`;

const StyledStepCircleBase = styled.div<{ isInNextSteps: boolean }>`
  align-items: center;
  border-color: ${({ isInNextSteps }) =>
    isInNextSteps
      ? themeCssVariables.border.color.medium
      : themeCssVariables.border.color.inverted} !important;
  border-radius: 50%;
  border-style: solid;
  border-width: 1px;
  corner-shape: round;
  display: flex;
  flex-basis: auto;
  flex-shrink: 0;
  height: 20px;
  justify-content: center;
  overflow: hidden;
  position: relative;
  width: 20px;
`;

const StyledStepCircle = motion.create(StyledStepCircleBase);

const StyledStepIndex = styled.span<{ isCurrentStep: boolean }>`
  color: ${({ isCurrentStep }) =>
    isCurrentStep
      ? themeCssVariables.font.color.inverted
      : themeCssVariables.font.color.tertiary};
  font-size: ${themeCssVariables.font.size.md};
  font-weight: ${themeCssVariables.font.weight.medium};
`;

const StyledStepLabel = styled.span<{ isInNextSteps: boolean }>`
  color: ${({ isInNextSteps }) =>
    isInNextSteps
      ? themeCssVariables.font.color.tertiary
      : themeCssVariables.font.color.primary};
  font-size: ${themeCssVariables.font.size.md};
  font-weight: ${themeCssVariables.font.weight.semiBold};
  margin-left: ${themeCssVariables.spacing[2]};
  white-space: nowrap;
`;

const StyledStepLineBase = styled.div`
  height: 2px;
  margin-left: ${themeCssVariables.spacing[2]};
  margin-right: ${themeCssVariables.spacing[2]};
  overflow: hidden;
  width: 100%;
`;

const StyledStepLine = motion.create(StyledStepLineBase);

export type StepProps = React.PropsWithChildren &
  React.ComponentProps<'div'> & {
    isLast?: boolean;
    index?: number;
    label: string;
    activeStep?: number;
  };

export const Step = ({
  isLast = false,
  index = 0,
  label,
  children,
  activeStep = 0,
}: StepProps) => {
  const isMobile = useIsMobile();
  const theme = useTheme();

  const variantsLine = {
    previous: {
      backgroundColor: theme.font.color.primary,
      transition: { duration: 0.5 },
    },
    next: {
      backgroundColor: theme.border.color.medium,
      transition: { duration: 0.5 },
    },
  };

  const variantsCircle = {
    current: {
      backgroundColor: theme.background.invertedPrimary,
      transition: { duration: 0.5 },
    },
    previous: {
      backgroundColor: theme.background.secondary,
      transition: { duration: 0.5 },
    },
    next: {
      backgroundColor: theme.background.tertiary,
      transition: { duration: 0.5 },
    },
  };

  const isInPreviousSteps = activeStep > index;
  const isCurrentStep = activeStep === index;
  const isInNextSteps = activeStep < index;

  return (
    <StyledContainer isLast={isLast}>
      <StyledStepCircle
        variants={variantsCircle}
        animate={
          isCurrentStep ? 'current' : isInPreviousSteps ? 'previous' : 'next'
        }
        isInNextSteps={isInNextSteps}
      >
        {isInPreviousSteps && (
          <AnimatedCheckmark
            isAnimating={isInPreviousSteps}
            color={theme.grayScale.gray12}
          />
        )}
        {!isInPreviousSteps && (
          <StyledStepIndex isCurrentStep={isCurrentStep}>
            {index + 1}
          </StyledStepIndex>
        )}
      </StyledStepCircle>
      <StyledStepLabel isInNextSteps={isInNextSteps}>{label}</StyledStepLabel>
      {!isLast && !isMobile && (
        <StyledStepLine
          variants={variantsLine}
          animate={isInPreviousSteps ? 'previous' : 'next'}
        />
      )}
      {(isInPreviousSteps || isCurrentStep) && children}
    </StyledContainer>
  );
};

Step.displayName = 'StepBar';