import React, { useState } from 'react'; import { View, StyleSheet, type LayoutChangeEvent, type ViewStyle } from 'react-native'; import { gradientLine, parseColor, parseLinearGradient, pickOverlayFallbackColor } from '../../utils/styles'; // react-native-svg may be missing on some runtimes (e.g. VegaOS); fall back to a solid color. let SvgComponent: React.ComponentType | null = null; let DefsComponent: React.ComponentType | null = null; let LinearGradientComponent: React.ComponentType | null = null; let StopComponent: React.ComponentType | null = null; let RectComponent: React.ComponentType | null = null; try { // eslint-disable-next-line @typescript-eslint/no-require-imports const svg = require('react-native-svg'); SvgComponent = svg.default ?? svg.Svg ?? null; DefsComponent = svg.Defs ?? null; LinearGradientComponent = svg.LinearGradient ?? null; StopComponent = svg.Stop ?? null; RectComponent = svg.Rect ?? null; } catch { SvgComponent = null; } const RADIUS_KEYS = [ 'borderRadius', 'borderTopLeftRadius', 'borderTopRightRadius', 'borderBottomLeftRadius', 'borderBottomRightRadius', ] as const; interface Props { color: string; radiusStyle: ViewStyle; } // NAM-3538: scrim painted above the background container's children, below the page content. export const BackgroundOverlay: React.FC = ({ color, radiusStyle }) => { const [size, setSize] = useState({ w: 0, h: 0 }); const radii: Record = {}; RADIUS_KEYS.forEach((key) => { if (radiusStyle[key] !== undefined) radii[key] = radiusStyle[key]; }); const base = [styles.overlay, radii as ViewStyle]; const solid = parseColor(color); if (solid) { return ; } const spec = parseLinearGradient(color); if (!spec) return null; const canRenderSvg = Boolean( SvgComponent && DefsComponent && LinearGradientComponent && StopComponent && RectComponent, ); if (!canRenderSvg) { return ; } const Svg = SvgComponent as React.ComponentType; const Defs = DefsComponent as React.ComponentType; const Gradient = LinearGradientComponent as React.ComponentType; const Stop = StopComponent as React.ComponentType; const Rect = RectComponent as React.ComponentType; const onLayout = (e: LayoutChangeEvent) => { const { width, height } = e.nativeEvent.layout; setSize((prev) => (prev.w === width && prev.h === height ? prev : { w: width, h: height })); }; return ( {size.w > 0 && size.h > 0 ? ( {spec.stops.map((stop, i) => ( ))} ) : null} ); }; const styles = StyleSheet.create({ overlay: { ...StyleSheet.absoluteFillObject, overflow: 'hidden', }, });