All files / atoms/Separator/mobile Separator.native.tsx

100% Statements 30/30
72.6% Branches 53/73
100% Functions 7/7
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179              3x 68x           3x 104x   3x                 2x 2x 2x   2x       2x 37x                                                 3x                           65x 65x 65x 65x   65x 65x   65x 65x   65x 66x       66x                                 65x           65x 2x                                       63x 2x                             5x                                   61x                    
import React from 'react';
import { View, ViewStyle, DimensionValue, Platform } from 'react-native';
import { SeparatorProps } from '../Separator.types';
import { cn } from '@sb/libs/utils';
import { useTheme } from '@sb/ui/components/Themes/ThemeProvider';
import { colors } from '@sb/styles/colors';
 
export const normalizeSize = (value: number | string): DimensionValue =>
  typeof value === 'string' && value.includes('%')
    ? `${parseFloat(value)}%`
    : typeof value === 'number'
      ? value
      : parseFloat(value) || 0;
 
export const strToNumber = (value: string | number): number =>
  typeof value === 'number' ? value : parseFloat(value.toString()) || 0;
 
const DashedAndDottedIOS = (
  id: string | undefined,
  isVertical: boolean,
  length: number | string,
  color: string,
  thickness: string | number,
  variant: 'dashed' | 'dotted',
  className?: string
): JSX.Element[] => {
  const segmentSize = variant === 'dotted' ? 2 : 4;
  const spacing = variant === 'dotted' ? 2 : 4;
  const thicknessVal = Math.max(strToNumber(thickness), 1);
  const segmentCount =
    typeof length === 'string' && length.includes('%')
      ? Math.floor((parseFloat(length) / 100) * (isVertical ? 100 : 200)) / (segmentSize + spacing)
      : Math.floor(strToNumber(length) / (segmentSize + spacing));
 
  return Array.from({ length: segmentCount }).map((_, index) => {
    return (
      <View
        key={`${id}-${index}`}
        testID={`${id}-${index}`}
        className={cn(className)}
        style={{
          backgroundColor: color,
          ...(isVertical
            ? {
                width: thicknessVal,
                height: segmentSize,
                marginBottom: index < segmentCount - 1 ? strToNumber(spacing) : 0,
              }
            : {
                height: thicknessVal,
                width: segmentSize,
                marginRight: index < segmentCount - 1 ? strToNumber(spacing) : 0,
              }),
          borderStyle: 'solid',
        }}
      />
    );
  });
};
 
export const Separator: React.FC<SeparatorProps> = ({
  id,
  style,
  orientation = 'horizontal',
  thickness = 1,
  color,
  spacing = 0,
  length = 100,
  variant = 'solid',
  lineCount = 1,
  lineSpacing = 0,
  ariaLabel = 'Separator divider',
  ...props
}) => {
  const { theme } = useTheme();
  const defaultColor = colors[theme].bgBlack;
  const resolvedColor = color || defaultColor;
  const isVertical = orientation === 'vertical';
 
  const isIos = Platform.OS === 'ios';
  const FallBackVarient = isIos && (variant === 'dashed' || variant === 'dotted');
 
  const normalizedLength = normalizeSize(length);
  const lineThickness = strToNumber(thickness);
 
  const getLineStyle = (index: number): ViewStyle => {
    const marginStyle: ViewStyle = isVertical
      ? { marginTop: index > 0 ? normalizeSize(lineSpacing) : 0 }
      : { marginLeft: index > 0 ? normalizeSize(lineSpacing) : 0 };
 
    return {
      ...(isVertical
        ? {
            height: normalizedLength,
            borderLeftWidth: lineThickness,
            borderLeftColor: resolvedColor,
          }
        : {
            width: normalizedLength,
            borderTopWidth: lineThickness,
            borderTopColor: resolvedColor,
          }),
      borderStyle: variant === 'dashed' ? 'dashed' : variant === 'dotted' ? 'dotted' : 'solid',
      ...marginStyle,
    };
  };
 
  const containerStyle: ViewStyle = {
    margin: typeof spacing === 'number' ? spacing : normalizeSize(spacing),
    flexDirection: isVertical ? 'column' : 'row',
    justifyContent: 'flex-start',
  };
 
  if (FallBackVarient) {
    return (
      <View
        style={[containerStyle, style as ViewStyle]}
        id={id}
        testID={id}
        className={cn(props.className)}
      >
        {DashedAndDottedIOS(
          id,
          isVertical,
          (normalizedLength as string | number) ?? 0,
          resolvedColor,
          thickness,
          variant,
          props.className
        )}
      </View>
    );
  }
 
  if (variant === 'multiple' && lineCount > 1) {
    return (
      <View
        style={[
          containerStyle,
          {
            flexDirection: variant === 'multiple' && isVertical ? 'row' : 'column',
            alignItems: 'center',
          },
          style as ViewStyle,
        ]}
        id={id}
        testID={id}
        className={cn(props.className)}
      >
        {Array.from({ length: lineCount }).map((_, index) => (
          <View
            key={index}
            accessibilityLabel={`${index}-${ariaLabel}`}
            testID={`${id}-line-${index}`}
            style={[
              getLineStyle(0),
              index > 0 &&
                (isVertical
                  ? { marginLeft: normalizeSize(lineSpacing) }
                  : { marginTop: normalizeSize(lineSpacing) }),
            ]}
          />
        ))}
      </View>
    );
  }
 
  // Single line fallback
  return (
    <View
      id={id}
      testID={id}
      style={[getLineStyle(0), containerStyle, style as ViewStyle]}
      className={cn(props.className)}
      accessibilityLabel={ariaLabel}
    />
  );
};