|
| 1 | +import type { ReactNode } from 'react'; |
| 2 | + |
| 3 | +export interface DividerProps { |
| 4 | + className?: string; |
| 5 | + type?: "horizontal" | "vertical"; |
| 6 | + orientation?: "left" | "center" | "right"; |
| 7 | + orientationMargin?: string | number; |
| 8 | + dashed?: boolean; |
| 9 | + children?: ReactNode; |
| 10 | + plain?: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +export const Divider = ({ |
| 14 | + className = "", |
| 15 | + type = "horizontal", |
| 16 | + orientation = "center", |
| 17 | + orientationMargin, |
| 18 | + dashed = false, |
| 19 | + children, |
| 20 | + plain = false, |
| 21 | +}: DividerProps) => { |
| 22 | + const isVertical = type === "vertical"; |
| 23 | + const hasChildren = !!children; |
| 24 | + |
| 25 | + const baseClasses = ` |
| 26 | + ${isVertical ? "inline-block h-4 w-px mx-2" : "w-full my-4"} |
| 27 | + ${dashed ? "border-dashed" : "border-solid"} |
| 28 | + ${isVertical |
| 29 | + ? "border-l border-gray-200 dark:border-gray-700" |
| 30 | + : "border-t border-gray-200 dark:border-gray-700" |
| 31 | + } |
| 32 | + `.trim(); |
| 33 | + |
| 34 | + if (isVertical) { |
| 35 | + return <div className={`${baseClasses} ${className}`} />; |
| 36 | + } |
| 37 | + |
| 38 | + if (!hasChildren) { |
| 39 | + return <div className={`${baseClasses} ${className}`} />; |
| 40 | + } |
| 41 | + |
| 42 | + const textClasses = ` |
| 43 | + px-4 text-sm |
| 44 | + ${plain |
| 45 | + ? "text-gray-500 dark:text-gray-400" |
| 46 | + : "text-gray-900 dark:text-white font-medium" |
| 47 | + } |
| 48 | + `.trim(); |
| 49 | + |
| 50 | + const marginStyle = orientationMargin |
| 51 | + ? (typeof orientationMargin === 'number' |
| 52 | + ? `${orientationMargin}px` |
| 53 | + : orientationMargin) |
| 54 | + : undefined; |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className={`flex items-center my-4 ${className}`}> |
| 58 | + <div |
| 59 | + className={` |
| 60 | + flex-1 |
| 61 | + ${dashed ? "border-dashed" : "border-solid"} |
| 62 | + border-t border-gray-200 dark:border-gray-700 |
| 63 | + ${orientation === "left" ? "flex-none w-6" : ""} |
| 64 | + `} |
| 65 | + style={orientation === "left" && marginStyle ? { width: marginStyle } : undefined} |
| 66 | + /> |
| 67 | + |
| 68 | + {children && ( |
| 69 | + <span className={textClasses}> |
| 70 | + {children} |
| 71 | + </span> |
| 72 | + )} |
| 73 | + |
| 74 | + <div |
| 75 | + className={` |
| 76 | + flex-1 |
| 77 | + ${dashed ? "border-dashed" : "border-solid"} |
| 78 | + border-t border-gray-200 dark:border-gray-700 |
| 79 | + ${orientation === "right" ? "flex-none w-6" : ""} |
| 80 | + `} |
| 81 | + style={orientation === "right" && marginStyle ? { width: marginStyle } : undefined} |
| 82 | + /> |
| 83 | + </div> |
| 84 | + ); |
| 85 | +}; |
0 commit comments