carousel.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {useEffect, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import {IconArrow} from 'sentry/icons';
  5. import {ArrowProps} from 'sentry/icons/iconArrow';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. type Props = {
  9. children?: React.ReactNode;
  10. };
  11. function Carousel({children}: Props) {
  12. const ref = useRef<HTMLDivElement | null>(null);
  13. const [anchorRefs, setAnchorRefs] = useState<HTMLElement[]>([]);
  14. const [childrenRefs, setChildrenRefs] = useState<HTMLElement[]>([]);
  15. const [isAtStart, setIsAtStart] = useState(true);
  16. const [isAtEnd, setIsAtEnd] = useState(false);
  17. useEffect(() => {
  18. if (!ref.current) {
  19. return () => {};
  20. }
  21. const observer = new IntersectionObserver(
  22. entries => {
  23. entries.forEach(e => {
  24. if (e.target.id === anchorRefs[0].id) {
  25. setIsAtStart(e.isIntersecting);
  26. } else if (e.target.id === anchorRefs[1].id) {
  27. setIsAtEnd(e.isIntersecting);
  28. }
  29. });
  30. },
  31. {root: ref.current, threshold: [1]}
  32. );
  33. if (anchorRefs) {
  34. anchorRefs.map(anchor => observer.observe(anchor));
  35. }
  36. return () => observer.disconnect();
  37. }, [anchorRefs]);
  38. useEffect(() => {
  39. if (!ref.current) {
  40. return;
  41. }
  42. setChildrenRefs(Array.from(ref.current.children) as HTMLElement[]);
  43. const anchors = [
  44. ref.current.children[0],
  45. ref.current.children[ref.current.children.length - 1],
  46. ] as HTMLElement[];
  47. setAnchorRefs(anchors);
  48. }, [children]);
  49. const handleScroll = (direction: string) => {
  50. if (!ref.current) {
  51. return;
  52. }
  53. const scrollLeft = ref.current.scrollLeft;
  54. if (direction === 'left') {
  55. // scroll to the last child to the left of the left side of the container
  56. const elements = childrenRefs.filter(child => child.offsetLeft < scrollLeft);
  57. ref.current.scrollTo(elements[elements.length - 1].offsetLeft, 0);
  58. } else if (direction === 'right') {
  59. // scroll to the first child to the right of the left side of the container
  60. const elements = childrenRefs.filter(child => child.offsetLeft > scrollLeft);
  61. ref.current.scrollTo(elements[0].offsetLeft, 0);
  62. }
  63. };
  64. return (
  65. <CarouselContainer>
  66. <CarouselItems ref={ref}>
  67. <Anchor id="left-anchor" />
  68. {children}
  69. <Anchor id="right-anchor" />
  70. </CarouselItems>
  71. {!isAtStart && (
  72. <ScrollButton onClick={() => handleScroll('left')} direction="left" />
  73. )}
  74. {!isAtEnd && (
  75. <ScrollButton onClick={() => handleScroll('right')} direction="right" />
  76. )}
  77. </CarouselContainer>
  78. );
  79. }
  80. const CarouselContainer = styled('div')`
  81. position: relative;
  82. padding-bottom: ${space(0.5)};
  83. `;
  84. const CarouselItems = styled('div')`
  85. display: flex;
  86. overflow-x: scroll;
  87. scroll-behavior: smooth;
  88. &::-webkit-scrollbar {
  89. background-color: transparent;
  90. height: 8px;
  91. }
  92. &::-webkit-scrollbar-thumb {
  93. background: ${p => p.theme.gray300};
  94. border-radius: 8px;
  95. }
  96. `;
  97. const Anchor = styled('div')``;
  98. type ScrollButtonProps = {
  99. direction: ArrowProps['direction'];
  100. onClick: () => void;
  101. };
  102. function ScrollButton({onClick, direction = 'left'}: ScrollButtonProps) {
  103. return (
  104. <StyledArrowButton
  105. onClick={onClick}
  106. direction={direction}
  107. aria-label={t('Scroll %s', direction)}
  108. icon={<IconArrow size="sm" direction={direction} />}
  109. />
  110. );
  111. }
  112. const StyledArrowButton = styled(Button)<{direction: string}>`
  113. position: absolute;
  114. ${p => (p.direction === 'left' ? `left: 0;` : `right: 0;`)}
  115. top: 0;
  116. bottom: 0;
  117. height: 36px;
  118. width: 36px;
  119. border-radius: 50%;
  120. border: 1px solid ${p => p.theme.gray200};
  121. padding: 0;
  122. margin: auto;
  123. background-color: ${p => p.theme.background};
  124. `;
  125. export default Carousel;