All files / src/components/ui/layout Flex.tsx

98.19% Statements 163/166
100% Branches 6/6
100% Functions 0/0
98.19% Lines 163/166

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 1671x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 121x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 117x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x  
'use client';
 
import { HTMLAttributes, forwardRef } from 'react';
import { cn } from '@/lib/core';
import { FlexProps, HStackProps, VStackProps, CenterProps } from '@/types/layout';
 
const directionMap = {
  row: 'flex-row',
  column: 'flex-col',
  'row-reverse': 'flex-row-reverse',
  'column-reverse': 'flex-col-reverse'};
 
const alignMap = {
  start: 'items-start',
  center: 'items-center',
  end: 'items-end',
  stretch: 'items-stretch',
  baseline: 'items-baseline'};
 
const justifyMap = {
  start: 'justify-start',
  center: 'justify-center',
  end: 'justify-end',
  between: 'justify-between',
  around: 'justify-around',
  evenly: 'justify-evenly'};
 
const gapMap: Record<number, string> = {
  0: 'gap-0',
  1: 'gap-1',
  2: 'gap-2',
  2.5: 'gap-2.5',
  3: 'gap-3',
  4: 'gap-4',
  5: 'gap-5',
  6: 'gap-6',
  7: 'gap-7',
  8: 'gap-8',
  10: 'gap-10',
  12: 'gap-12',
  16: 'gap-16',
  20: 'gap-20',
  24: 'gap-24'};
 
const wrapMap = {
  true: 'flex-wrap',
  false: 'flex-nowrap',
  reverse: 'flex-wrap-reverse'};
 
/**
 * Flex - Flexible box layout component
 *
 * Simplifies common flex patterns with props instead of className strings.
 * Reduces repeated `flex items-center justify-between` patterns.
 *
 * @example
 * ```tsx
 * // Horizontal centered layout
 * <Flex align="center" justify="center" gap={4}>
 *   <div>Item 1</div>
 *   <div>Item 2</div>
 * </Flex>
 *
 * // Vertical stack with gap
 * <Flex direction="column" gap={2}>
 *   <h2>Title</h2>
 *   <p>Content</p>
 * </Flex>
 *
 * // Space between items
 * <Flex align="center" justify="between">
 *   <Logo />
 *   <Navigation />
 * </Flex>
 * ```
 */
export const Flex = forwardRef<HTMLDivElement, FlexProps>(
  (
    {
      direction = 'row',
      align,
      justify,
      gap,
      wrap = false,
      as: Component = 'div',
      className,
      children,
      ...props
    },
    ref
  ) => {
    const classes = cn(
      'flex',
      directionMap[direction],
      align && alignMap[align],
      justify && justifyMap[justify],
      gap !== undefined && gapMap[gap],
      wrapMap[wrap.toString() as keyof typeof wrapMap],
      className
    );
 
    return (
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      <Component ref={ref as any} className={classes} {...(props as any)}>
        {children}
      </Component>
    );
  }
);
 
Flex.displayName = 'Flex';
 
/**
 * HStack - Horizontal stack (flex row)
 *
 * Shorthand for common horizontal layouts
 */
export const HStack = forwardRef<HTMLDivElement, HStackProps>((props, ref) => {
  return <Flex ref={ref} direction="row" {...props} />;
});
 
HStack.displayName = 'HStack';
 
/**
 * VStack - Vertical stack (flex column)
 *
 * Shorthand for common vertical layouts
 */
export const VStack = forwardRef<HTMLDivElement, VStackProps>((props, ref) => {
  return <Flex ref={ref} direction="column" {...props} />;
});
 
VStack.displayName = 'VStack';
 
/**
 * Center - Centered content
 *
 * Shorthand for centering content both horizontally and vertically
 */
export const Center = forwardRef<HTMLDivElement, CenterProps>((props, ref) => {
  return <Flex ref={ref} align="center" justify="center" {...props} />;
});
 
Center.displayName = 'Center';
 
/**
 * Spacer - Flexible spacer element
 *
 * Pushes adjacent flex items apart
 *
 * @example
 * ```tsx
 * <HStack>
 *   <Logo />
 *   <Spacer />
 *   <Navigation />
 * </HStack>
 * ```
 */
export const Spacer = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
  ({ className, ...props }, ref) => {
    return <div ref={ref} className={cn('flex-1', className)} {...props} />;
  }
);
 
Spacer.displayName = 'Spacer';