TwoOptionToggle.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { cn } from 'ui'
  2. interface TwoOptionToggleProps {
  3. options: string[]
  4. width?: number
  5. activeOption: string
  6. onClickOption: (value: string) => void
  7. borderOverride: string
  8. }
  9. export const TwoOptionToggle = ({
  10. options,
  11. width = 50,
  12. activeOption,
  13. onClickOption,
  14. borderOverride = 'border-stronger',
  15. }: TwoOptionToggleProps) => {
  16. const buttonStyle = (
  17. isActive: boolean
  18. ) => `absolute top-0 z-1 text-xs inline-flex h-full items-center justify-center font-medium
  19. ${
  20. isActive ? 'hover:text-foreground-light hover:text-foreground' : 'hover:text-foreground'
  21. } hover:text-foreground focus:z-10 focus:outline-hidden focus:border-blue-300 focus:ring-blue
  22. transition ease-in-out duration-150`
  23. return (
  24. <div
  25. className={`relative border ${borderOverride} rounded-md h-7`}
  26. style={{ padding: 1, width: (width + 1) * 2 }}
  27. >
  28. <span
  29. style={{ width, translate: activeOption === options[1] ? '0px' : `${width - 2}px` }}
  30. aria-hidden="true"
  31. className={cn(
  32. 'z-0 inline-block rounded-sm h-full bg-overlay-hover shadow-sm transform',
  33. 'transition-all ease-in-out border border-strong'
  34. )}
  35. />
  36. {options.map((option, index: number) => (
  37. <span
  38. key={`toggle_${index}`}
  39. style={{ width: width + 1 }}
  40. className={`
  41. ${activeOption === option ? 'text-foreground' : 'text-foreground-light'}
  42. ${index === 0 ? 'right-0' : 'left-0'}
  43. ${buttonStyle(activeOption === option)}
  44. cursor-pointer
  45. `}
  46. onClick={() => onClickOption(option)}
  47. >
  48. <span
  49. className={cn(
  50. 'capitalize hover:text-foreground',
  51. activeOption === option ? 'text-foreground' : 'text-foreground-light'
  52. )}
  53. >
  54. {option}
  55. </span>
  56. </span>
  57. ))}
  58. </div>
  59. )
  60. }