import React, { CSSProperties } from 'react'; import { useNode, UserComponent } from '@craftjs/core'; import { cssPropsToString } from '../../utils/style-helpers'; import { CtaButton, normalizeCtas, ctaInlineStyle, ctasToHtml } from './_cta-helpers'; import { escapeHtml, escapeAttr } from '../../utils/escape'; import { tokenRef } from '../../utils/tokens'; interface CTASectionProps { heading?: string; description?: string; ctas?: CtaButton[]; /** Legacy props kept for backward compat with saved projects. */ buttonText?: string; buttonHref?: string; gradient?: string; anchorId?: string; style?: CSSProperties; animation?: string; animationDelay?: string; hideOnDesktop?: boolean; hideOnTablet?: boolean; hideOnMobile?: boolean; } const defaultGradient = `linear-gradient(135deg, ${tokenRef('primary')} 0%, ${tokenRef('secondary')} 100%)`; // Fixed white: the default background is a primary->secondary gradient / image, so heading // and outline-button text stay light regardless of the palette (no token means "on gradient"). const WHITE_ON_GRADIENT = '#ffffff'; export const CTASection: UserComponent = ({ heading = 'Ready to Get Started?', description = 'Join thousands of satisfied users and start building your dream website today.', ctas, buttonText, buttonHref, gradient = defaultGradient, anchorId, style = {}, }) => { const { connectors: { connect, drag }, selected, } = useNode((node) => ({ selected: node.events.selected, })); const effectiveCtas = normalizeCtas({ ctas, buttonText, buttonHref }); const ctaDefaults = { primaryBg: tokenRef('bg'), primaryText: tokenRef('text'), outlineText: WHITE_ON_GRADIENT }; return (
{ if (ref) connect(drag(ref)); }} id={anchorId || undefined} style={{ background: gradient, padding: '80px 20px', textAlign: 'center', outline: selected ? '2px solid #3b82f6' : 'none', ...style, }} >

{heading}

{description}

{effectiveCtas.map((cta, i) => ( e.preventDefault()} style={ctaInlineStyle(cta, ctaDefaults)}> {cta.text} ))}
); }; /* ---------- Craft config ---------- */ CTASection.craft = { displayName: 'CTA Section', props: { heading: 'Ready to Get Started?', description: 'Join thousands of satisfied users and start building your dream website today.', ctas: [ { text: 'Start Free Trial', href: '#', variant: 'primary' }, ] as CtaButton[], gradient: defaultGradient, anchorId: '', style: {}, animation: '', animationDelay: '', hideOnDesktop: false, hideOnTablet: false, hideOnMobile: false, }, rules: { canDrag: () => true, canMoveIn: () => false, canMoveOut: () => true, }, }; /* ---------- HTML export ---------- */ (CTASection as any).toHtml = (props: CTASectionProps, _childrenHtml: string) => { const sectionStyle = cssPropsToString({ background: props.gradient || defaultGradient, padding: '80px 20px', textAlign: 'center', ...props.style, }); const ctas = normalizeCtas(props); const buttonsHtml = ctasToHtml(ctas, { primaryBg: tokenRef('bg'), primaryText: tokenRef('text'), outlineText: WHITE_ON_GRADIENT }); const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : ''; return { html: `

${escapeHtml(props.heading || '')}

${escapeHtml(props.description || '')}

${buttonsHtml}
`, }; };