timelineCursor.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import {Fragment, useCallback, useEffect, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {AnimatePresence, motion} from 'framer-motion';
  4. import {Overlay} from 'sentry/components/overlay';
  5. import {Sticky} from 'sentry/components/sticky';
  6. import {space} from 'sentry/styles/space';
  7. import testableTransition from 'sentry/utils/testableTransition';
  8. const TOOLTIP_OFFSET = 10;
  9. interface Options {
  10. /**
  11. * Function used to compute the text of the cursor tooltip. Recieves the %
  12. * value the cursor is within the container.
  13. */
  14. labelText: (percentPosition: number) => string;
  15. /**
  16. * May be set to false to disable rendering the timeline cursor
  17. */
  18. enabled?: boolean;
  19. /**
  20. * Should the label stick to teh top of the screen?
  21. */
  22. sticky?: boolean;
  23. }
  24. function useTimelineCursor<E extends HTMLElement>({
  25. enabled = true,
  26. sticky,
  27. labelText,
  28. }: Options) {
  29. const rafIdRef = useRef<number | null>(null);
  30. const containerRef = useRef<E>(null);
  31. const labelRef = useRef<HTMLDivElement>(null);
  32. const [isVisible, setIsVisible] = useState(false);
  33. const handleMouseMove = useCallback(
  34. (e: MouseEvent) => {
  35. if (rafIdRef.current !== null) {
  36. window.cancelAnimationFrame(rafIdRef.current);
  37. }
  38. if (containerRef.current === null) {
  39. return;
  40. }
  41. const containerRect = containerRef.current.getBoundingClientRect();
  42. // Instead of using onMouseEnter / onMouseLeave we check if the mouse is
  43. // within the containerRect. This proves to be less glitchy as some
  44. // elements within the container may trigger an onMouseLeave even when
  45. // the mouse is still "inside" of the container
  46. const isInsideContainer =
  47. e.clientX > containerRect.left &&
  48. e.clientX < containerRect.right &&
  49. e.clientY > containerRect.top &&
  50. e.clientY < containerRect.bottom;
  51. if (isInsideContainer !== isVisible) {
  52. setIsVisible(isInsideContainer);
  53. }
  54. rafIdRef.current = window.requestAnimationFrame(() => {
  55. if (containerRef.current === null || labelRef.current === null) {
  56. return;
  57. }
  58. const offset = e.clientX - containerRect.left;
  59. const tooltipWidth = labelRef.current.offsetWidth;
  60. labelRef.current.innerText = labelText(offset / containerRect.width);
  61. containerRef.current.style.setProperty('--cursorOffset', `${offset}px`);
  62. containerRef.current.style.setProperty('--cursorMax', `${containerRect.width}px`);
  63. containerRef.current.style.setProperty('--cursorLabelWidth', `${tooltipWidth}px`);
  64. });
  65. },
  66. [isVisible, labelText]
  67. );
  68. useEffect(() => {
  69. if (enabled) {
  70. window.addEventListener('mousemove', handleMouseMove);
  71. } else {
  72. setIsVisible(false);
  73. }
  74. return () => window.removeEventListener('mousemove', handleMouseMove);
  75. }, [enabled, handleMouseMove]);
  76. const cursorLabel = sticky ? (
  77. <StickyLabel>
  78. <CursorLabel ref={labelRef} animated placement="right" />
  79. </StickyLabel>
  80. ) : (
  81. <CursorLabel ref={labelRef} animated placement="right" />
  82. );
  83. const timelineCursor = (
  84. <AnimatePresence>
  85. {isVisible && (
  86. <Fragment>
  87. <Cursor role="presentation" />
  88. {cursorLabel}
  89. </Fragment>
  90. )}
  91. </AnimatePresence>
  92. );
  93. return {cursorContainerRef: containerRef, timelineCursor};
  94. }
  95. const Cursor = styled(motion.div)`
  96. pointer-events: none;
  97. background: ${p => p.theme.translucentBorder};
  98. width: 2px;
  99. height: 100%;
  100. position: absolute;
  101. top: 0;
  102. left: clamp(0px, var(--cursorOffset), var(--cursorMax));
  103. transform: translateX(-2px);
  104. z-index: 3;
  105. `;
  106. Cursor.defaultProps = {
  107. initial: 'initial',
  108. animate: 'animate',
  109. exit: 'exit',
  110. transition: testableTransition({duration: 0.1}),
  111. variants: {
  112. initial: {opacity: 0},
  113. animate: {opacity: 1},
  114. exit: {opacity: 0},
  115. },
  116. };
  117. const CursorLabel = styled(Overlay)`
  118. font-variant-numeric: tabular-nums;
  119. width: max-content;
  120. padding: ${space(0.75)} ${space(1)};
  121. color: ${p => p.theme.textColor};
  122. font-size: ${p => p.theme.fontSizeSmall};
  123. line-height: 1.2;
  124. position: absolute;
  125. top: 12px;
  126. left: clamp(
  127. 0px,
  128. calc(var(--cursorOffset) + ${TOOLTIP_OFFSET}px),
  129. calc(var(--cursorMax) - var(--cursorLabelWidth) - ${TOOLTIP_OFFSET}px)
  130. );
  131. `;
  132. const StickyLabel = styled(Sticky)`
  133. z-index: 2;
  134. `;
  135. export {useTimelineCursor};