import React, { CSSProperties } from 'react'; import { useNode, Element, UserComponent } from '@craftjs/core'; import { Container } from './Container'; import { cssPropsToString } from '../../utils/style-helpers'; import { AnchorIdField } from '../../ui/AnchorIdField'; interface BackgroundSectionProps { bgImage?: string; bgColor?: string; overlayColor?: string; overlayOpacity?: number; innerMaxWidth?: string; style?: CSSProperties; children?: React.ReactNode; anchorId?: string; } export const BackgroundSection: UserComponent = ({ bgImage = '', bgColor = '#1e293b', overlayColor = '#000000', overlayOpacity = 0.4, innerMaxWidth = '1200px', style = {}, anchorId, }) => { const { connectors: { connect, drag } } = useNode(); return (
{ if (ref) connect(drag(ref)); }} id={anchorId || undefined} style={{ position: 'relative', width: '100%', minHeight: '200px', backgroundColor: bgColor, backgroundImage: bgImage ? `url(${bgImage})` : undefined, backgroundSize: 'cover', backgroundPosition: 'center', ...style, }} > {/* Overlay */}
{/* Content */}
); }; /* ---------- Settings panel ---------- */ const BackgroundSectionSettings: React.FC = () => { const { actions: { setProp }, props } = useNode((node) => ({ props: node.data.props as BackgroundSectionProps, })); const bgColorPresets = ['#1e293b', '#0f172a', '#18181b', '#1e3a5f', '#312e81', '#064e3b', '#7f1d1d', '#ffffff']; const overlayPresets = ['#000000', '#1e293b', '#0f172a', '#312e81', '#064e3b', '#7f1d1d']; return (
setProp((p: BackgroundSectionProps) => { p.bgImage = e.target.value; })} placeholder="https://... or /storage/assets/..." style={{ width: '100%', padding: '4px 8px', background: '#27272a', color: '#e4e4e7', border: '1px solid #3f3f46', borderRadius: 4, fontSize: 12 }} />
{bgColorPresets.map((c) => (
{overlayPresets.map((c) => (
setProp((p: BackgroundSectionProps) => { p.overlayOpacity = parseInt(e.target.value, 10) / 100; })} style={{ width: '100%' }} />
setProp((p: BackgroundSectionProps) => { p.innerMaxWidth = e.target.value; })} style={{ width: '100%', padding: '4px 8px', background: '#27272a', color: '#e4e4e7', border: '1px solid #3f3f46', borderRadius: 4, fontSize: 11 }} />
); }; /* ---------- Craft config ---------- */ BackgroundSection.craft = { displayName: 'Background Section', props: { bgImage: '', bgColor: '#1e293b', overlayColor: '#000000', overlayOpacity: 0.4, innerMaxWidth: '1200px', style: { padding: '0' }, anchorId: '', }, rules: { canDrag: () => true, canMoveIn: () => false, canMoveOut: () => true, }, related: { settings: BackgroundSectionSettings, }, }; /* ---------- HTML export ---------- */ (BackgroundSection as any).toHtml = (props: BackgroundSectionProps, childrenHtml: string) => { const esc = (s: any) => String(s ?? "").replace(//g, '>').replace(/"/g, '"'); const outerStyle = cssPropsToString({ position: 'relative', width: '100%', minHeight: '200px', backgroundColor: props.bgColor || '#1e293b', backgroundImage: props.bgImage ? `url(${props.bgImage})` : undefined, backgroundSize: 'cover', backgroundPosition: 'center', ...props.style, }); const overlayStyle = cssPropsToString({ position: 'absolute', inset: '0', backgroundColor: props.overlayColor || '#000000', opacity: String(props.overlayOpacity ?? 0.4), pointerEvents: 'none', }); const innerStyle = cssPropsToString({ position: 'relative', zIndex: '1', maxWidth: props.innerMaxWidth || '1200px', margin: '0 auto', padding: '60px 20px', }); const idAttr = props.anchorId ? ` id="${esc(props.anchorId)}"` : ''; return { html: `${childrenHtml}`, }; };