useGetGradientSize.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { useCallback } from 'react';
  2. import { useGetBoneDimensions } from './useGetBoneDimensions';
  3. import type {
  4. ICustomViewStyle,
  5. ISkeletonProps,
  6. IComponentSize,
  7. } from '../constants';
  8. type UseGetGradientSizeProps = Pick<ISkeletonProps, 'animationDirection'> & {
  9. boneLayout: ICustomViewStyle;
  10. };
  11. export const useGetGradientSize = (componentSize: IComponentSize) => {
  12. const getBoneDimensions = useGetBoneDimensions(componentSize);
  13. return useCallback(
  14. ({ animationDirection, boneLayout }: UseGetGradientSizeProps) => {
  15. const { width, height } = getBoneDimensions(boneLayout);
  16. const gradientStyle: ICustomViewStyle = {};
  17. if (
  18. animationDirection === 'diagonalDownRight' ||
  19. animationDirection === 'diagonalDownLeft' ||
  20. animationDirection === 'diagonalTopRight' ||
  21. animationDirection === 'diagonalTopLeft'
  22. ) {
  23. gradientStyle.width = width as number;
  24. gradientStyle.height = height as number;
  25. if (height >= width) {
  26. gradientStyle.height *= 1.5;
  27. } else {
  28. gradientStyle.width *= 1.5;
  29. }
  30. }
  31. return gradientStyle;
  32. },
  33. [getBoneDimensions],
  34. );
  35. };