|
| 1 | +import { forwardRef } from "react"; |
| 2 | +import { cn } from "~/lib/utils"; |
| 3 | + |
| 4 | +type DivProps = React.HTMLAttributes<HTMLDivElement> & { |
| 5 | + xSpacing?: "left" | "center" | "right" | "baseline" | "stretch"; |
| 6 | + ySpacing?: "top" | "middle" | "bottom" | "space-between" | "space-around"; |
| 7 | + gap?: number; |
| 8 | + centered?: boolean; |
| 9 | +}; |
| 10 | + |
| 11 | +export const VStack = forwardRef<HTMLDivElement, DivProps>( |
| 12 | + ( |
| 13 | + { |
| 14 | + children, |
| 15 | + className, |
| 16 | + gap = 4, |
| 17 | + centered = false, |
| 18 | + xSpacing, |
| 19 | + ySpacing, |
| 20 | + ...props |
| 21 | + }, |
| 22 | + ref |
| 23 | + ) => { |
| 24 | + VStack.displayName = "VStack"; |
| 25 | + const _xSpacing = centered ? "center" : xSpacing ?? "left"; |
| 26 | + const _ySpacing = centered ? "middle" : ySpacing ?? "top"; |
| 27 | + return ( |
| 28 | + <div |
| 29 | + ref={ref} |
| 30 | + className={cn("flex flex-col", className, { |
| 31 | + "items-center": _xSpacing === "center", |
| 32 | + "items-left": _xSpacing === "left", |
| 33 | + "items-right": _xSpacing === "right", |
| 34 | + "justify-center": _ySpacing === "middle", |
| 35 | + "justify-start": _ySpacing === "top", |
| 36 | + "justify-end": _ySpacing === "bottom", |
| 37 | + "justify-between": _ySpacing === "space-between", |
| 38 | + "justify-around": _ySpacing === "space-around", |
| 39 | + })} |
| 40 | + style={{ gap: `${gap / 4}rem` }} |
| 41 | + {...props} |
| 42 | + > |
| 43 | + {children} |
| 44 | + </div> |
| 45 | + ); |
| 46 | + } |
| 47 | +); |
| 48 | + |
| 49 | +export const HStack = forwardRef<HTMLDivElement, DivProps>( |
| 50 | + ( |
| 51 | + { |
| 52 | + children, |
| 53 | + className, |
| 54 | + gap = 4, |
| 55 | + centered = false, |
| 56 | + xSpacing, |
| 57 | + ySpacing, |
| 58 | + ...props |
| 59 | + }, |
| 60 | + ref |
| 61 | + ) => { |
| 62 | + HStack.displayName = "HStack"; |
| 63 | + const _xSpacing = centered ? "center" : xSpacing ?? "left"; |
| 64 | + const _ySpacing = centered ? "middle" : ySpacing ?? "top"; |
| 65 | + return ( |
| 66 | + <div |
| 67 | + ref={ref} |
| 68 | + className={cn("flex flex-row", className, { |
| 69 | + "items-center": _xSpacing === "center", |
| 70 | + "items-left": _xSpacing === "left", |
| 71 | + "items-right": _xSpacing === "right", |
| 72 | + "justify-start": _ySpacing === "top", |
| 73 | + "justify-center": _ySpacing === "middle", |
| 74 | + "justify-end": _ySpacing === "bottom", |
| 75 | + "justify-between": _ySpacing === "space-between", |
| 76 | + "justify-around": _ySpacing === "space-around", |
| 77 | + })} |
| 78 | + style={{ gap: `${gap / 4}rem` }} |
| 79 | + {...props} |
| 80 | + > |
| 81 | + {children} |
| 82 | + </div> |
| 83 | + ); |
| 84 | + } |
| 85 | +); |
| 86 | + |
| 87 | +export const Wrap = forwardRef<HTMLDivElement, DivProps & { maxCols?: number }>( |
| 88 | + ( |
| 89 | + { |
| 90 | + children, |
| 91 | + className, |
| 92 | + gap = 4, |
| 93 | + centered = false, |
| 94 | + xSpacing, |
| 95 | + ySpacing, |
| 96 | + maxCols, |
| 97 | + ...props |
| 98 | + }, |
| 99 | + ref |
| 100 | + ) => { |
| 101 | + Wrap.displayName = "Wrap"; |
| 102 | + const _xSpacing = centered ? "center" : xSpacing ?? "left"; |
| 103 | + const _ySpacing = centered ? "middle" : ySpacing ?? "top"; |
| 104 | + return ( |
| 105 | + <div |
| 106 | + ref={ref} |
| 107 | + className={cn("flex flex-row flex-wrap", className, { |
| 108 | + "flex-wrap": maxCols !== -1, |
| 109 | + "items-center": _xSpacing === "center", |
| 110 | + "items-left": _xSpacing === "left", |
| 111 | + "items-right": _xSpacing === "right", |
| 112 | + "justify-center": _ySpacing === "middle", |
| 113 | + "justify-start": _ySpacing === "top", |
| 114 | + "justify-end": _ySpacing === "bottom", |
| 115 | + "justify-between": _ySpacing === "space-between", |
| 116 | + "justify-around": _ySpacing === "space-around", |
| 117 | + })} |
| 118 | + style={{ gap: `${gap / 4}rem` }} |
| 119 | + {...props} |
| 120 | + > |
| 121 | + {children} |
| 122 | + </div> |
| 123 | + ); |
| 124 | + } |
| 125 | +); |
| 126 | + |
| 127 | +type GridProps = React.HTMLAttributes<HTMLDivElement> & { |
| 128 | + maxCols?: number; |
| 129 | + xGap?: number; |
| 130 | + yGap?: number; |
| 131 | + direction?: "row" | "column"; |
| 132 | + xSpacing?: |
| 133 | + | "center" |
| 134 | + | "left" |
| 135 | + | "right" |
| 136 | + | "space-between" |
| 137 | + | "space-around" |
| 138 | + | "space-evenly"; |
| 139 | + ySpacing?: |
| 140 | + | "middle" |
| 141 | + | "top" |
| 142 | + | "bottom" |
| 143 | + | "space-between" |
| 144 | + | "space-around" |
| 145 | + | "space-evenly"; |
| 146 | +}; |
| 147 | +export const Grid = forwardRef<HTMLDivElement, GridProps>( |
| 148 | + ( |
| 149 | + { |
| 150 | + children, |
| 151 | + className, |
| 152 | + maxCols = -1, |
| 153 | + xGap = 4, |
| 154 | + yGap = 4, |
| 155 | + direction = "row", |
| 156 | + xSpacing = "left", |
| 157 | + ySpacing = "top", |
| 158 | + ...props |
| 159 | + }, |
| 160 | + ref |
| 161 | + ) => { |
| 162 | + Grid.displayName = "Grid"; |
| 163 | + return ( |
| 164 | + <div |
| 165 | + ref={ref} |
| 166 | + className={cn("grid", className, { |
| 167 | + "grid-cols-1": direction === "column", |
| 168 | + "grid-cols-2": direction === "row", |
| 169 | + "items-center": xSpacing === "center", |
| 170 | + "items-left": xSpacing === "left", |
| 171 | + "items-right": xSpacing === "right", |
| 172 | + "justify-center": ySpacing === "middle", |
| 173 | + "justify-start": ySpacing === "top", |
| 174 | + "justify-end": ySpacing === "bottom", |
| 175 | + "justify-between": ySpacing === "space-between", |
| 176 | + "justify-around": ySpacing === "space-around", |
| 177 | + })} |
| 178 | + style={{ |
| 179 | + gap: `${yGap / 4}rem ${xGap / 4}rem`, |
| 180 | + gridTemplateColumns: `repeat(${maxCols}, 1fr)`, |
| 181 | + }} |
| 182 | + {...props} |
| 183 | + > |
| 184 | + {children} |
| 185 | + </div> |
| 186 | + ); |
| 187 | + } |
| 188 | +); |
0 commit comments