|
1 | | -import { LayoutConfig } from "@/domain/layout/types"; |
| 1 | +import { LayoutConfig, CustomGradient } from "@/domain/layout/types"; |
2 | 2 | import { Asset } from "@/domain/asset/types"; |
3 | | -import { customGradientToCss } from "@/domain/layout/gradients"; |
| 3 | +import { customGradientToCss, isAdvancedGradient, isLegacyGradient } from "@/domain/layout/gradients"; |
4 | 4 | import { getGradientById } from "@/domain/layout/gradient-presets"; |
5 | 5 | import { tokenToCssColor } from "@/components/layouts/shared/color-utils"; |
6 | 6 |
|
| 7 | +type LayoutGeometry = |
| 8 | + | { type: "radial"; direction: string } |
| 9 | + | { type: "linear"; angle: number }; |
| 10 | + |
| 11 | +/** |
| 12 | + * Get layout-specific gradient geometry based on layout type and variant. |
| 13 | + * This allows the same gradient colors to render differently per layout. |
| 14 | + * |
| 15 | + * Note: We use linear gradients for all layouts because the 3-stop gradient |
| 16 | + * structure (color → dark → color) doesn't work well with radial gradients |
| 17 | + * (creates a glow/ring effect instead of smooth coverage). |
| 18 | + */ |
| 19 | +function getLayoutGeometry(layoutId: string, variant?: string): LayoutGeometry { |
| 20 | + // Spotlight: diagonal toward screenshot side |
| 21 | + if (layoutId.startsWith("hero-center")) { |
| 22 | + // Left variant: text on left, screenshot on right → gradient flows left-to-right |
| 23 | + // Right variant: text on right, screenshot on left → gradient flows right-to-left |
| 24 | + return { |
| 25 | + type: "linear", |
| 26 | + angle: variant === "right" ? 270 : 90, |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + // Peak: linear perpendicular to screenshot edge |
| 31 | + if (layoutId.startsWith("popup-gradient")) { |
| 32 | + const angles: Record<string, number> = { |
| 33 | + left: 90, |
| 34 | + right: 270, |
| 35 | + center: 180, |
| 36 | + }; |
| 37 | + return { type: "linear", angle: angles[variant ?? "center"] ?? 180 }; |
| 38 | + } |
| 39 | + |
| 40 | + // Backdrop: vertical gradient for centered screenshot |
| 41 | + if (layoutId === "adaptive-stage") { |
| 42 | + return { type: "linear", angle: 180 }; |
| 43 | + } |
| 44 | + |
| 45 | + // Code snippet: diagonal linear |
| 46 | + if (layoutId.startsWith("code-snippet")) { |
| 47 | + return { type: "linear", angle: 135 }; |
| 48 | + } |
| 49 | + |
| 50 | + // Default: diagonal linear |
| 51 | + return { type: "linear", angle: 135 }; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Extract gradient stops as a CSS string from a CustomGradient |
| 56 | + */ |
| 57 | +function getGradientStopsString(gradient: CustomGradient): string { |
| 58 | + if (isAdvancedGradient(gradient)) { |
| 59 | + return gradient.stops |
| 60 | + .map((stop) => { |
| 61 | + if (stop.position !== undefined) { |
| 62 | + const position = stop.position <= 1 ? `${stop.position * 100}%` : `${stop.position}%`; |
| 63 | + return `${stop.color} ${position}`; |
| 64 | + } |
| 65 | + return stop.color; |
| 66 | + }) |
| 67 | + .join(", "); |
| 68 | + } |
| 69 | + |
| 70 | + if (isLegacyGradient(gradient)) { |
| 71 | + return `${gradient.from}, ${gradient.to}`; |
| 72 | + } |
| 73 | + |
| 74 | + return "#6366f1, #8b5cf6"; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Convert gradient to CSS with layout-specific geometry. |
| 79 | + * Colors come from the stored gradient; geometry is determined by layout type. |
| 80 | + */ |
| 81 | +function gradientToCssWithLayout( |
| 82 | + gradient: CustomGradient, |
| 83 | + layoutId: string, |
| 84 | + variant?: string |
| 85 | +): string { |
| 86 | + const stops = getGradientStopsString(gradient); |
| 87 | + const geometry = getLayoutGeometry(layoutId, variant); |
| 88 | + |
| 89 | + if (geometry.type === "radial") { |
| 90 | + return `radial-gradient(${geometry.direction}, ${stops})`; |
| 91 | + } |
| 92 | + return `linear-gradient(${geometry.angle}deg, ${stops})`; |
| 93 | +} |
| 94 | + |
7 | 95 | export function getBackgroundStyle(config: LayoutConfig, assetMap: Map<string, Asset>): string { |
8 | 96 | if (config.background?.type === "gradient") { |
9 | | - if (config.background.customGradient) { |
10 | | - return customGradientToCss(config.background.customGradient); |
11 | | - } |
| 97 | + const gradient = |
| 98 | + config.background.customGradient ?? getGradientById(config.background.value)?.gradient; |
12 | 99 |
|
13 | | - const gradient = getGradientById(config.background.value); |
14 | 100 | if (gradient) { |
15 | | - return customGradientToCss(gradient.gradient); |
| 101 | + // Apply layout-specific geometry at render time |
| 102 | + return gradientToCssWithLayout(gradient, config.layoutId, config.variant); |
16 | 103 | } |
17 | 104 | } else if (config.background?.type === "image") { |
18 | 105 | const bgAsset = assetMap.get(config.background.value); |
|
0 commit comments