useGetBoneStyles.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { useCallback } from 'react';
  2. import { useGetBoneDimensions } from '../hooks';
  3. import {
  4. ICustomViewStyle,
  5. ISkeletonProps,
  6. IComponentSize,
  7. DEFAULT_CONFIG,
  8. } from '../constants';
  9. type UseGetBoneStylesProps = Pick<
  10. ISkeletonProps,
  11. 'animationType' | 'animationDirection' | 'boneColor'
  12. > & {
  13. boneLayout: ICustomViewStyle;
  14. };
  15. export const useGetBoneStyles = (componentSize: IComponentSize) => {
  16. const getBoneDimensions = useGetBoneDimensions(componentSize);
  17. return useCallback(
  18. ({
  19. animationDirection,
  20. animationType,
  21. boneLayout,
  22. boneColor,
  23. }: UseGetBoneStylesProps) => {
  24. const { backgroundColor, borderRadius } = boneLayout;
  25. const { width, height } = getBoneDimensions(boneLayout);
  26. const boneStyle: ICustomViewStyle = {
  27. width,
  28. height,
  29. borderRadius: borderRadius || DEFAULT_CONFIG.BORDER_RADIUS,
  30. ...boneLayout,
  31. };
  32. if (animationType !== 'pulse') {
  33. boneStyle.overflow = 'hidden';
  34. boneStyle.backgroundColor = backgroundColor || boneColor;
  35. }
  36. if (
  37. animationDirection === 'diagonalDownRight' ||
  38. animationDirection === 'diagonalDownLeft' ||
  39. animationDirection === 'diagonalTopRight' ||
  40. animationDirection === 'diagonalTopLeft'
  41. ) {
  42. boneStyle.justifyContent = 'center';
  43. boneStyle.alignItems = 'center';
  44. }
  45. return boneStyle;
  46. },
  47. [getBoneDimensions],
  48. );
  49. };