All files / hooks useResponsive.ts

62.82% Statements 49/78
43.75% Branches 14/32
43.75% Functions 7/16
73.43% Lines 47/64

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330                                                                                                                                                                                                                                              7x   7x   7x 7x 7x 7x     7x 7x 7x     109x 7x 7x 7x 56x 56x 56x 56x 56x   7x 42x 42x   7x 7x     7x 7x 7x 7x 7x 7x         7x 7x 7x 7x 7x     7x 7x 7x 7x 7x       7x     7x           7x             7x                       7x                       7x                   7x                   7x                                                                                                                                                                                        
/**
 * 响应式设计 Hook
 *
 * 提供响应式布局所需的所有工具和值
 * 自动响应屏幕尺寸变化
 */
 
import { useMemo } from 'react';
import { useWindowDimensions, PixelRatio } from 'react-native';
 
import {
  BREAKPOINTS,
  type DeviceSize,
  scaleWidth,
  scaleHeight,
  scale,
  scaleFont,
  moderateScale,
  widthPercent,
  heightPercent,
  getGridColumns,
  getGridItemWidth,
} from '@/src/utils/responsive';
 
/**
 * 响应式 Hook 返回值类型
 */
export interface ResponsiveValues {
  // 屏幕尺寸
  width: number;
  height: number;
 
  // 设备类型
  deviceSize: DeviceSize;
  isExtraSmall: boolean;
  isSmall: boolean;
  isMedium: boolean;
  isLarge: boolean;
  isTablet: boolean;
 
  // 缩放函数
  sw: (size: number) => number;
  sh: (size: number) => number;
  s: (size: number) => number;
  sf: (size: number, factor?: number) => number;
  ms: (size: number, factor?: number) => number;
  wp: (percent: number) => number;
  hp: (percent: number) => number;
 
  // 布局工具
  horizontalPadding: number;
  maxContentWidth: number;
  gridColumns: (minItemWidth: number, gap?: number) => number;
  gridItemWidth: (columns: number, gap?: number) => number;
 
  // 预设尺寸
  spacing: {
    xxs: number;
    xs: number;
    sm: number;
    md: number;
    lg: number;
    xl: number;
    xxl: number;
    xxxl: number;
  };
  fontSize: {
    xs: number;
    sm: number;
    md: number;
    lg: number;
    xl: number;
    xxl: number;
    xxxl: number;
    display: number;
  };
  iconSize: {
    xs: number;
    sm: number;
    md: number;
    lg: number;
    xl: number;
    xxl: number;
  };
  borderRadius: {
    xs: number;
    sm: number;
    md: number;
    lg: number;
    xl: number;
    xxl: number;
    full: number;
  };
}
 
/**
 * 响应式设计 Hook
 *
 * @returns 响应式工具和值
 *
 * @example
 * ```tsx
 * function MyComponent() {
 *   const { sw, sf, spacing, isSmall, deviceSize } = useResponsive();
 *
 *   return (
 *     <View style={{
 *       padding: spacing.md,
 *       width: sw(300),
 *     }}>
 *       <Text style={{ fontSize: sf(16) }}>
 *         {isSmall ? '小屏模式' : '正常模式'}
 *       </Text>
 *     </View>
 *   );
 * }
 * ```
 */
export function useResponsive(): ResponsiveValues {
  const { width, height } = useWindowDimensions();
 
  return useMemo(() => {
    // 设计稿基准宽度
    const DESIGN_WIDTH = 375;
    const DESIGN_HEIGHT = 812;
    const MIN_SCALE = 0.8;
    const MAX_SCALE = 1.4;
 
    // 计算缩放比例
    const widthScale = Math.min(Math.max(width / DESIGN_WIDTH, MIN_SCALE), MAX_SCALE);
    const heightScale = Math.min(Math.max(height / DESIGN_HEIGHT, MIN_SCALE), MAX_SCALE);
    const minScale = Math.min(widthScale, heightScale);
 
    // 缩放函数
    const sw = (size: number) => Math.round(size * widthScale);
    const sh = (size: number) => Math.round(size * heightScale);
    const s = (size: number) => Math.round(size * minScale);
    const sf = (size: number, factor: number = 1) => {
      const scaledSize = size * widthScale * factor;
      const minSize = size * 0.85;
      const maxSize = size * 1.3;
      const clampedSize = Math.min(Math.max(scaledSize, minSize), maxSize);
      return Math.round(PixelRatio.roundToNearestPixel(clampedSize));
    };
    const ms = (size: number, factor: number = 0.5) => {
      const scaleDiff = widthScale - 1;
      return Math.round(size + scaleDiff * size * factor);
    };
    const wp = (percent: number) => Math.round((width * percent) / 100);
    const hp = (percent: number) => Math.round((height * percent) / 100);
 
    // 设备类型判断
    const deviceSize: DeviceSize = (() => {
      Iif (width < BREAKPOINTS.xs) return 'xs';
      Iif (width < BREAKPOINTS.sm) return 'xs';
      Iif (width < BREAKPOINTS.md) return 'sm';
      Iif (width < BREAKPOINTS.lg) return 'md';
      Eif (width < BREAKPOINTS.xl) return 'lg';
      if (width < BREAKPOINTS.xxl) return 'xl';
      return 'xxl';
    })();
 
    const isExtraSmall = width < BREAKPOINTS.sm;
    const isSmall = width < BREAKPOINTS.md;
    const isMedium = width >= BREAKPOINTS.md && width < BREAKPOINTS.lg;
    const isLarge = width >= BREAKPOINTS.lg && width < BREAKPOINTS.xl;
    const isTablet = width >= BREAKPOINTS.xl;
 
    // 水平内边距
    const horizontalPadding = (() => {
      Iif (isExtraSmall) return 12;
      Iif (isSmall) return 16;
      Iif (isTablet) return 24;
      return 16;
    })();
 
    // 最大内容宽度
    const maxContentWidth = isTablet ? Math.min(width - 64, 768) : width;
 
    // 网格计算
    const gridColumns = (minItemWidth: number, gap: number = 16) => {
      const availableWidth = width - horizontalPadding * 2;
      const columns = Math.floor((availableWidth + gap) / (minItemWidth + gap));
      return Math.max(1, columns);
    };
 
    const gridItemWidth = (columns: number, gap: number = 16) => {
      const availableWidth = width - horizontalPadding * 2;
      const totalGap = gap * (columns - 1);
      return Math.floor((availableWidth - totalGap) / columns);
    };
 
    // 预设间距
    const spacing = {
      xxs: sw(2),
      xs: sw(4),
      sm: sw(8),
      md: sw(12),
      lg: sw(16),
      xl: sw(20),
      xxl: sw(24),
      xxxl: sw(32),
    };
 
    // 预设字体大小
    const fontSize = {
      xs: sf(10),
      sm: sf(12),
      md: sf(14),
      lg: sf(16),
      xl: sf(18),
      xxl: sf(20),
      xxxl: sf(24),
      display: sf(28),
    };
 
    // 预设图标大小
    const iconSize = {
      xs: ms(12, 0.3),
      sm: ms(16, 0.3),
      md: ms(20, 0.3),
      lg: ms(24, 0.3),
      xl: ms(28, 0.3),
      xxl: ms(32, 0.3),
    };
 
    // 预设圆角
    const borderRadius = {
      xs: sw(4),
      sm: sw(8),
      md: sw(12),
      lg: sw(16),
      xl: sw(20),
      xxl: sw(24),
      full: 9999,
    };
 
    return {
      width,
      height,
      deviceSize,
      isExtraSmall,
      isSmall,
      isMedium,
      isLarge,
      isTablet,
      sw,
      sh,
      s,
      sf,
      ms,
      wp,
      hp,
      horizontalPadding,
      maxContentWidth,
      gridColumns,
      gridItemWidth,
      spacing,
      fontSize,
      iconSize,
      borderRadius,
    };
  }, [width, height]);
}
 
/**
 * 响应式值选择器 Hook
 *
 * @param values 不同设备尺寸的值
 * @returns 当前设备对应的值
 *
 * @example
 * ```tsx
 * function MyComponent() {
 *   const columns = useResponsiveValue({
 *     xs: 1,
 *     sm: 2,
 *     md: 3,
 *     lg: 4,
 *     default: 2,
 *   });
 *
 *   return <Grid columns={columns} />;
 * }
 * ```
 */
export function useResponsiveValue<T>(values: Partial<Record<DeviceSize, T>> & { default: T }): T {
  const { deviceSize } = useResponsive();
  return values[deviceSize] ?? values.default;
}
 
/**
 * 响应式样式 Hook
 *
 * 根据设备尺寸返回不同的样式对象
 *
 * @example
 * ```tsx
 * function MyComponent() {
 *   const styles = useResponsiveStyles({
 *     container: {
 *       xs: { padding: 8, flexDirection: 'column' },
 *       md: { padding: 16, flexDirection: 'row' },
 *       default: { padding: 12 },
 *     },
 *   });
 *
 *   return <View style={styles.container} />;
 * }
 * ```
 */
export function useResponsiveStyles<
  T extends Record<string, Record<string, unknown>>,
>(styleDefinitions: {
  [K in keyof T]: Partial<Record<DeviceSize, T[K]>> & { default: T[K] };
}): T {
  const { deviceSize } = useResponsive();
 
  return useMemo(() => {
    const result = {} as T;
    for (const key in styleDefinitions) {
      const definition = styleDefinitions[key];
      result[key] = (definition[deviceSize] ?? definition.default) as T[typeof key];
    }
    return result;
  }, [deviceSize, styleDefinitions]);
}
 
export default useResponsive;