rail-tab.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import OLTooltip from '@/shared/components/ol/ol-tooltip'
  2. import MaterialIcon from '@/shared/components/material-icon'
  3. import classNames from 'classnames'
  4. import { ComponentProps, forwardRef, ReactElement } from 'react'
  5. import { NavLink } from 'react-bootstrap'
  6. import { RailElement } from '@/features/ide-react/util/rail-types'
  7. const RailTab = forwardRef<
  8. HTMLButtonElement,
  9. {
  10. icon: RailElement['icon']
  11. eventKey?: string
  12. open: boolean
  13. indicator?: ReactElement
  14. title: string
  15. } & ComponentProps<'button'>
  16. >(({ icon, className, eventKey, open, indicator, title, ...props }, ref) => {
  17. return (
  18. <OLTooltip
  19. id={`rail-tab-tooltip-${eventKey}`}
  20. description={title}
  21. overlayProps={{ delay: 0, placement: 'right' }}
  22. >
  23. <NavLink
  24. {...props}
  25. ref={ref}
  26. eventKey={eventKey}
  27. className={classNames('ide-rail-tab-link', className, {
  28. 'open-rail': open,
  29. })}
  30. as="button"
  31. >
  32. <RailTabIcon icon={icon} title={title} open={open} />
  33. {indicator}
  34. </NavLink>
  35. </OLTooltip>
  36. )
  37. })
  38. const RailTabIcon = ({
  39. icon,
  40. title,
  41. open,
  42. }: {
  43. icon: RailElement['icon']
  44. title: string
  45. open: boolean
  46. }) => {
  47. if (typeof icon === 'string') {
  48. return open ? (
  49. <MaterialIcon
  50. type={icon}
  51. className="ide-rail-tab-link-icon"
  52. accessibilityLabel={title}
  53. />
  54. ) : (
  55. <MaterialIcon
  56. type={icon}
  57. className="ide-rail-tab-link-icon"
  58. unfilled
  59. accessibilityLabel={title}
  60. />
  61. )
  62. } else {
  63. const Component = icon
  64. return <Component open={open} title={title} />
  65. }
  66. }
  67. RailTab.displayName = 'RailTab'
  68. export default RailTab