VisibleAnimationHandle.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import React from 'react';
  2. import { dataType, OriginRectType, ShowAnimateEnum } from '../types';
  3. interface VisibleHandleProps {
  4. visible: boolean;
  5. currentImage?: dataType;
  6. children: ({
  7. photoVisible,
  8. showAnimateType,
  9. originRect,
  10. onShowAnimateEnd,
  11. }: {
  12. photoVisible: boolean;
  13. showAnimateType: ShowAnimateEnum;
  14. originRect: OriginRectType;
  15. onShowAnimateEnd: () => void;
  16. }) => JSX.Element | null;
  17. }
  18. export default function VisibleAnimationHandle({
  19. visible,
  20. currentImage,
  21. children,
  22. }: VisibleHandleProps) {
  23. const [photoVisible, updatePhotoVisible] = React.useState(visible);
  24. const [showAnimateType, updateAnimateStatus] = React.useState<
  25. ShowAnimateEnum
  26. >(ShowAnimateEnum.None);
  27. const [originRect, updateOriginRect] = React.useState<OriginRectType>();
  28. function onShowAnimateEnd() {
  29. updateAnimateStatus(ShowAnimateEnum.None);
  30. // Close
  31. if (showAnimateType === ShowAnimateEnum.Out) {
  32. updatePhotoVisible(false);
  33. }
  34. }
  35. React.useEffect(() => {
  36. if (!currentImage) {
  37. return;
  38. }
  39. const originRef = currentImage.originRef;
  40. if (originRef && originRef.nodeType === 1) {
  41. // 获取触发时节点位置
  42. const { top, left, width, height } = originRef.getBoundingClientRect();
  43. updateOriginRect({
  44. clientX: left + width / 2,
  45. clientY: top + height / 2,
  46. });
  47. }
  48. if (visible) {
  49. updateAnimateStatus(ShowAnimateEnum.In);
  50. updatePhotoVisible(true);
  51. } else {
  52. updateAnimateStatus(ShowAnimateEnum.Out);
  53. }
  54. }, [visible]);
  55. return children({
  56. photoVisible,
  57. showAnimateType,
  58. originRect,
  59. onShowAnimateEnd,
  60. });
  61. }