All files / src/components/common LoadingSkeleton.tsx

100% Statements 20/20
87.5% Branches 7/8
100% Functions 13/13
100% Lines 20/20

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                                                                                                          7x           485x 485x     485x         473x   2348x                           4x         12x                   2x         8x                   2x   10x                             2x   8x                         2x     8x                             485x     485x               485x   485x                            
import React from 'react';
import type { LoadingSkeletonProps, SkeletonVariant } from '../../types/componentPropExports';
 
/**
 * Configuration structure for skeleton variants
 * 
 * Defines the rendering configuration for each skeleton variant including
 * accessibility attributes and the render function that produces the skeleton DOM.
 * 
 * @property ariaLabel - ARIA label for screen readers describing what's loading
 * @property srText - Screen reader text announced when skeleton is rendered
 * @property render - Function that generates the skeleton DOM elements
 */
interface SkeletonVariantConfig {
  ariaLabel: string;
  srText: string;
  render: (skeletonClasses: string) => React.ReactNode;
}
 
/**
 * Loading skeleton component for better perceived performance
 * 
 * ## Business Perspective
 * 
 * Improves perceived performance by showing content placeholders while
 * data loads, making the application feel more responsive and reducing
 * user anxiety during loading operations. 📊
 * 
 * ## Technical Perspective
 * 
 * Provides animated placeholder content that mimics the structure of
 * the actual content being loaded. Uses CSS animations for smooth
 * skeleton shimmer effect. Supports multiple variants for different
 * widget layouts.
 * 
 * @example
 * ```tsx
 * // Default 3-line skeleton
 * <LoadingSkeleton />
 * 
 * // Custom number of lines
 * <LoadingSkeleton lines={5} />
 * 
 * // Summary widget skeleton
 * <LoadingSkeleton variant="summary" />
 * 
 * // Chart widget skeleton
 * <LoadingSkeleton variant="chart" />
 * 
 * // Metrics widget skeleton
 * <LoadingSkeleton variant="metrics" />
 * ```
 */
export const LoadingSkeleton: React.FC<LoadingSkeletonProps> = ({ 
  lines = 3,
  variant = 'default',
  testId = 'loading-skeleton',
  className = ''
}) => {
  const baseClasses = 'animate-pulse';
  const skeletonClasses = 'bg-gray-200 dark:bg-gray-700 rounded';
 
  // Configuration map for each variant
  const variantConfigs: Record<SkeletonVariant, SkeletonVariantConfig> = {
    default: {
      ariaLabel: 'Loading content',
      srText: 'Loading...',
      render: (classes) => (
        <>
          {Array.from({ length: lines }).map((_, i) => (
            <div 
              key={`skeleton-line-${i}`}
              className={`h-4 ${classes}`}
              data-testid={`${testId}-line-${i}`}
              aria-hidden="true"
            />
          ))}
        </>
      )
    },
    summary: {
      ariaLabel: 'Loading summary',
      srText: 'Loading summary...',
      render: (classes) => (
        <>
          <div className={`h-8 ${classes} w-3/4`} aria-hidden="true" />
          <div className={`h-24 ${classes}`} aria-hidden="true" />
          <div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
            {['summary-metric-1', 'summary-metric-2', 'summary-metric-3'].map((key) => (
              <div key={key} className={`h-20 ${classes}`} aria-hidden="true" />
            ))}
          </div>
        </>
      )
    },
    chart: {
      ariaLabel: 'Loading chart',
      srText: 'Loading chart...',
      render: (classes) => (
        <>
          <div className={`h-6 ${classes} w-1/2`} aria-hidden="true" />
          <div className={`h-64 ${classes}`} aria-hidden="true" />
          <div className="flex justify-between gap-4">
            {['legend-1', 'legend-2', 'legend-3', 'legend-4'].map((key) => (
              <div key={key} className={`h-4 w-16 ${classes}`} aria-hidden="true" />
            ))}
          </div>
        </>
      )
    },
    list: {
      ariaLabel: 'Loading list',
      srText: 'Loading list...',
      render: (classes) => (
        <>
          {['list-item-1', 'list-item-2', 'list-item-3', 'list-item-4', 'list-item-5'].map((key) => (
            <div key={key} className="flex items-center space-x-4">
              <div className={`h-10 w-10 ${classes} rounded-full`} aria-hidden="true" />
              <div className="flex-1 space-y-2">
                <div className={`h-4 ${classes} w-3/4`} aria-hidden="true" />
                <div className={`h-3 ${classes} w-1/2`} aria-hidden="true" />
              </div>
            </div>
          ))}
        </>
      )
    },
    metrics: {
      ariaLabel: 'Loading metrics',
      srText: 'Loading metrics...',
      render: (classes) => (
        <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
          {['metric-1', 'metric-2', 'metric-3', 'metric-4'].map((key) => (
            <div key={key} className="space-y-3">
              <div className={`h-4 ${classes} w-2/3`} aria-hidden="true" />
              <div className={`h-8 ${classes}`} aria-hidden="true" />
              <div className={`h-3 ${classes} w-1/2`} aria-hidden="true" />
            </div>
          ))}
        </div>
      )
    },
    tabs: {
      ariaLabel: 'Loading tabs',
      srText: 'Loading tabs...',
      render: (classes) => (
        <>
          <div className="flex space-x-2 border-b border-gray-200 dark:border-gray-700">
            {['tab-1', 'tab-2', 'tab-3', 'tab-4'].map((key) => (
              <div key={key} className={`h-10 w-24 ${classes} mb-[-1px]`} aria-hidden="true" />
            ))}
          </div>
          <div className="space-y-3">
            <div className={`h-6 ${classes} w-3/4`} aria-hidden="true" />
            <div className={`h-32 ${classes}`} aria-hidden="true" />
            <div className={`h-4 ${classes} w-5/6`} aria-hidden="true" />
            <div className={`h-4 ${classes} w-4/6`} aria-hidden="true" />
          </div>
        </>
      )
    }
  };
 
  // Get the configuration for the current variant, fallback to default
  const config = variantConfigs[variant] || variantConfigs.default;
  
  // Explicit spacing configuration per variant for maintainability
  const spaceClassMap: Record<SkeletonVariant, string> = {
    default: 'space-y-4',
    summary: 'space-y-4',
    chart: 'space-y-4',
    tabs: 'space-y-4',
    list: 'space-y-3',
    metrics: ''
  };
  const spaceClass = spaceClassMap[variant] || spaceClassMap.default;
 
  return (
    <div 
      className={`${baseClasses} ${spaceClass} ${className}`}
      data-testid={testId}
      role="status"
      aria-label={config.ariaLabel}
    >
      {config.render(skeletonClasses)}
      <span className="sr-only">{config.srText}</span>
    </div>
  );
};
 
export default LoadingSkeleton;