import React, { useMemo } from "react"; // ============================================================================ // TYPES & INTERFACES // ============================================================================ export type DividerShape = | "wave" | "wave-smooth" | "wave-multi" | "oblique" | "oblique-double" | "arrow" | "arrow-center" | "triangle" | "triangle-asymmetric" | "curve" | "curve-asymmetric" | "zigzag" | "steps" | "drops" | "mountains" | "clouds"; export type DividerPosition = "top" | "bottom"; export interface ShapeDividerProps { /** The shape type to render */ shape?: DividerShape; /** Position of the divider - affects flip direction */ position?: DividerPosition; /** Height of the divider in pixels */ height?: number; /** Background color of the shape */ color?: string; /** Background color behind the shape (transparent by default) */ backgroundColor?: string; /** Flip the shape horizontally */ flipX?: boolean; /** Flip the shape vertically */ flipY?: boolean; /** Animate the shape (for applicable shapes like waves) */ animate?: boolean; /** Animation duration in seconds */ animationDuration?: number; /** Custom className for the container */ className?: string; /** Custom inline styles */ style?: React.CSSProperties; /** Opacity of the shape (0-1) */ opacity?: number; /** Width multiplier for repeating patterns */ repeatWidth?: number; /** Custom SVG path (overrides shape) */ customPath?: string; } // Preset configurations for common use cases export interface DividerPreset extends Partial { name: string; description: string; } // ============================================================================ // SVG PATH GENERATORS // ============================================================================ const generateWavePath = (): string => { return "M0,64 C160,128 320,0 480,64 C640,128 800,0 960,64 L960,160 L0,160 Z"; }; const generateSmoothWavePath = (): string => { return "M0,96 Q240,160 480,96 T960,96 L960,160 L0,160 Z"; }; const generateMultiWavePath = (): string => { return ` M0,80 C120,120 240,40 360,80 C480,120 600,40 720,80 C840,120 960,40 960,80 L960,160 L0,160 Z `; }; const generateObliquePath = (): string => { return "M0,160 L960,0 L960,160 Z"; }; const generateObliqueDoublePath = (): string => { return "M0,160 L480,0 L960,160 L960,160 L0,160 Z"; }; const generateArrowPath = (): string => { return "M0,160 L480,80 L960,160 L960,160 L0,160 Z"; }; const generateArrowCenterPath = (): string => { return "M0,160 L480,0 L960,160 L960,160 L0,160 Z"; }; const generateTrianglePath = (): string => { return "M480,0 L960,160 L0,160 Z"; }; const generateTriangleAsymmetricPath = (): string => { return "M0,160 L720,0 L960,160 Z"; }; const generateCurvePath = (): string => { return "M0,160 Q480,0 960,160 L960,160 L0,160 Z"; }; const generateCurveAsymmetricPath = (): string => { return "M0,160 Q720,-40 960,160 L960,160 L0,160 Z"; }; const generateZigzagPath = (): string => { const segments = 8; const segmentWidth = 960 / segments; let path = "M0,160 "; for (let i = 0; i <= segments; i++) { const x = i * segmentWidth; const y = i % 2 === 0 ? 80 : 160; path += `L${x},${y} `; } path += "L960,160 L0,160 Z"; return path; }; const generateStepsPath = (): string => { const steps = 5; const stepWidth = 960 / steps; const stepHeight = 160 / steps; let path = "M0,160 "; for (let i = 0; i < steps; i++) { const x = i * stepWidth; const y = 160 - (i + 1) * stepHeight; path += `L${x},${y} L${x + stepWidth},${y} `; } path += "L960,0 L960,160 Z"; return path; }; const generateDropsPath = (): string => { return ` M0,160 C60,160 60,100 120,100 C180,100 180,140 240,140 C300,140 300,80 360,80 C420,80 420,120 480,120 C540,120 540,60 600,60 C660,60 660,100 720,100 C780,100 780,140 840,140 C900,140 900,80 960,80 L960,160 Z `; }; const generateMountainsPath = (): string => { return ` M0,160 L120,80 L200,120 L320,40 L440,100 L560,20 L680,80 L800,60 L880,100 L960,40 L960,160 Z `; }; const generateCloudsPath = (): string => { return ` M0,160 C0,160 40,80 120,80 C160,80 180,100 200,100 C240,100 260,60 320,60 C380,60 400,90 440,90 C500,90 520,50 600,50 C660,50 680,80 720,80 C780,80 800,100 840,100 C900,100 920,70 960,70 L960,160 Z `; }; const getShapePath = (shape: DividerShape): string => { const pathGenerators: Record string> = { wave: generateWavePath, "wave-smooth": generateSmoothWavePath, "wave-multi": generateMultiWavePath, oblique: generateObliquePath, "oblique-double": generateObliqueDoublePath, arrow: generateArrowPath, "arrow-center": generateArrowCenterPath, triangle: generateTrianglePath, "triangle-asymmetric": generateTriangleAsymmetricPath, curve: generateCurvePath, "curve-asymmetric": generateCurveAsymmetricPath, zigzag: generateZigzagPath, steps: generateStepsPath, drops: generateDropsPath, mountains: generateMountainsPath, clouds: generateCloudsPath, }; return pathGenerators[shape]?.() ?? generateWavePath(); }; // ============================================================================ // PRESETS // ============================================================================ export const dividerPresets: Record = { // Elegant waves elegantWave: { name: "Elegant Wave", description: "Smooth flowing wave for elegant transitions", shape: "wave-smooth", height: 100, color: "#1a1a2e", }, oceanWave: { name: "Ocean Wave", description: "Multi-layered ocean-inspired wave", shape: "wave-multi", height: 80, color: "#0077b6", animate: true, animationDuration: 8, }, // Geometric sharpDiagonal: { name: "Sharp Diagonal", description: "Clean diagonal cut for modern designs", shape: "oblique", height: 120, color: "#2d3436", }, splitDiagonal: { name: "Split Diagonal", description: "Double diagonal creating a peak", shape: "oblique-double", height: 100, color: "#6c5ce7", }, pointedArrow: { name: "Pointed Arrow", description: "Arrow pointing to content below", shape: "arrow-center", height: 80, color: "#e17055", }, // Organic softCurve: { name: "Soft Curve", description: "Gentle curve for subtle transitions", shape: "curve", height: 60, color: "#00b894", }, mountainRange: { name: "Mountain Range", description: "Jagged mountain silhouette", shape: "mountains", height: 100, color: "#2c3e50", }, cloudySky: { name: "Cloudy Sky", description: "Soft cloud-like bubbles", shape: "clouds", height: 90, color: "#74b9ff", }, // Playful funkyZigzag: { name: "Funky Zigzag", description: "Sharp zigzag pattern", shape: "zigzag", height: 80, color: "#fd79a8", }, retroDrops: { name: "Retro Drops", description: "Rounded drop shapes", shape: "drops", height: 100, color: "#a29bfe", }, steppedLayers: { name: "Stepped Layers", description: "Staircase effect", shape: "steps", height: 120, color: "#636e72", }, // Dark mode friendly darkWave: { name: "Dark Wave", description: "Subtle wave for dark themes", shape: "wave", height: 80, color: "#121212", opacity: 0.95, }, darkMountains: { name: "Dark Mountains", description: "Dark mountain silhouette", shape: "mountains", height: 100, color: "#1e1e1e", }, // Minimal subtleCurve: { name: "Subtle Curve", description: "Very subtle curved transition", shape: "curve", height: 40, color: "#f5f5f5", opacity: 0.9, }, thinDiagonal: { name: "Thin Diagonal", description: "Minimal diagonal separator", shape: "oblique", height: 50, color: "#eeeeee", }, }; // ============================================================================ // MAIN COMPONENT // ============================================================================ export const ShapeDivider: React.FC = ({ shape = "wave", position = "bottom", height = 80, color = "#ffffff", backgroundColor = "transparent", flipX = false, flipY = false, animate = false, animationDuration = 4, className = "", style = {}, opacity = 1, repeatWidth = 1, customPath, }) => { const svgPath = useMemo(() => { return customPath ?? getShapePath(shape); }, [customPath, shape]); const transformStyles = useMemo(() => { const transforms: string[] = []; // Position-based default flip if (position === "top") { transforms.push("rotateX(180deg)"); } // User-specified flips if (flipX) transforms.push("scaleX(-1)"); if (flipY) transforms.push("scaleY(-1)"); return transforms.join(" "); }, [position, flipX, flipY]); const animationStyles: React.CSSProperties = animate ? { animation: `shapeDividerFloat ${animationDuration}s ease-in-out infinite`, } : {}; const viewBoxWidth = 960 * repeatWidth; // Generate repeated path for patterns const repeatedPath = useMemo(() => { if (repeatWidth <= 1) return svgPath; let result = ""; for (let i = 0; i < repeatWidth; i++) { // Offset each repetition by 960 units const offset = i * 960; const offsetPath = svgPath.replace( /(\d+(?:\.\d+)?),(\d+(?:\.\d+)?)/g, (match, x, y) => `${parseFloat(x) + offset},${y}`, ); result += offsetPath + " "; } return result; }, [svgPath, repeatWidth]); return ( <>
1 ? repeatedPath : svgPath} fill={color} fillOpacity={opacity} />
); }; // ============================================================================ // PRESET COMPONENT // ============================================================================ interface PresetDividerProps extends Omit { preset: keyof typeof dividerPresets; /** Override preset values */ overrides?: Partial; } export const PresetDivider: React.FC = ({ preset, overrides = {}, ...props }) => { const presetConfig = dividerPresets[preset]; if (!presetConfig) { console.warn(`Preset "${preset}" not found. Using default wave.`); return ; } const { name, description, ...presetProps } = presetConfig; return ; }; // ============================================================================ // LAYERED DIVIDER (Multiple overlapping shapes) // ============================================================================ interface LayeredDividerProps { layers: Array; height?: number; className?: string; style?: React.CSSProperties; } export const LayeredDivider: React.FC = ({ layers, height = 120, className = "", style = {}, }) => { return (
{layers.map((layer, index) => { const { offset = 0, ...layerProps } = layer; return (
); })}
); }; // ============================================================================ // UTILITY: Get all available shapes // ============================================================================ export const availableShapes: DividerShape[] = [ "wave", "wave-smooth", "wave-multi", "oblique", "oblique-double", "arrow", "arrow-center", "triangle", "triangle-asymmetric", "curve", "curve-asymmetric", "zigzag", "steps", "drops", "mountains", "clouds", ]; export const getPresetNames = (): string[] => Object.keys(dividerPresets); export const getPresetByName = (name: string): DividerPreset | undefined => { return dividerPresets[name]; }; // ============================================================================ // DEFAULT EXPORT // ============================================================================ export default ShapeDivider;