2026-04-05 18:31:16 -07:00
|
|
|
import React, { CSSProperties } from 'react';
|
|
|
|
|
import { useNode, UserComponent } from '@craftjs/core';
|
|
|
|
|
import { cssPropsToString } from '../../utils/style-helpers';
|
2026-07-12 13:16:53 -07:00
|
|
|
import { CtaButton, normalizeCtas, ctaInlineStyle, ctasToHtml } from './_cta-helpers';
|
2026-07-12 15:56:25 -07:00
|
|
|
import { escapeHtml, escapeAttr, cssValue } from '../../utils/escape';
|
2026-04-05 18:31:16 -07:00
|
|
|
|
|
|
|
|
interface CallToActionProps {
|
|
|
|
|
heading?: string;
|
|
|
|
|
description?: string;
|
2026-05-25 12:43:28 -07:00
|
|
|
ctas?: CtaButton[];
|
|
|
|
|
/** Legacy props kept for backward compat with saved projects. */
|
2026-04-05 18:31:16 -07:00
|
|
|
buttonText?: string;
|
|
|
|
|
buttonHref?: string;
|
|
|
|
|
secondaryButtonText?: string;
|
|
|
|
|
secondaryButtonHref?: string;
|
|
|
|
|
bgType?: 'color' | 'gradient' | 'image';
|
|
|
|
|
bgValue?: string;
|
|
|
|
|
overlayColor?: string;
|
|
|
|
|
overlayOpacity?: number;
|
|
|
|
|
textColor?: string;
|
|
|
|
|
buttonColor?: string;
|
2026-05-25 12:43:28 -07:00
|
|
|
anchorId?: string;
|
2026-04-05 18:31:16 -07:00
|
|
|
style?: CSSProperties;
|
2026-07-14 06:47:58 -07:00
|
|
|
animation?: string;
|
|
|
|
|
animationDelay?: string;
|
|
|
|
|
hideOnDesktop?: boolean;
|
|
|
|
|
hideOnTablet?: boolean;
|
|
|
|
|
hideOnMobile?: boolean;
|
2026-04-05 18:31:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const defaultGradient = 'linear-gradient(135deg, #2563eb 0%, #7c3aed 100%)';
|
|
|
|
|
|
|
|
|
|
export const CallToAction: UserComponent<CallToActionProps> = ({
|
|
|
|
|
heading = 'Ready to Get Started?',
|
|
|
|
|
description = 'Join thousands of satisfied users and start building your dream website today.',
|
2026-05-25 12:43:28 -07:00
|
|
|
ctas,
|
|
|
|
|
buttonText,
|
|
|
|
|
buttonHref,
|
|
|
|
|
secondaryButtonText,
|
|
|
|
|
secondaryButtonHref,
|
2026-04-05 18:31:16 -07:00
|
|
|
bgType = 'gradient',
|
|
|
|
|
bgValue = defaultGradient,
|
|
|
|
|
overlayColor = '#000000',
|
|
|
|
|
overlayOpacity = 0,
|
|
|
|
|
textColor = '#ffffff',
|
|
|
|
|
buttonColor = '#ffffff',
|
2026-05-25 12:43:28 -07:00
|
|
|
anchorId,
|
2026-04-05 18:31:16 -07:00
|
|
|
style = {},
|
|
|
|
|
}) => {
|
|
|
|
|
const {
|
|
|
|
|
connectors: { connect, drag },
|
|
|
|
|
selected,
|
|
|
|
|
} = useNode((node) => ({
|
|
|
|
|
selected: node.events.selected,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const bgStyle: CSSProperties = {};
|
|
|
|
|
if (bgType === 'color') {
|
|
|
|
|
bgStyle.backgroundColor = bgValue;
|
|
|
|
|
} else if (bgType === 'gradient') {
|
|
|
|
|
bgStyle.background = bgValue;
|
|
|
|
|
} else if (bgType === 'image') {
|
|
|
|
|
bgStyle.backgroundImage = `url(${bgValue})`;
|
|
|
|
|
bgStyle.backgroundSize = 'cover';
|
|
|
|
|
bgStyle.backgroundPosition = 'center';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isButtonDark = buttonColor === '#ffffff' || buttonColor === '#f8fafc';
|
|
|
|
|
const buttonTextColor = isButtonDark ? '#18181b' : '#ffffff';
|
|
|
|
|
|
2026-05-25 12:43:28 -07:00
|
|
|
const effectiveCtas = normalizeCtas({ ctas, buttonText, buttonHref, secondaryButtonText, secondaryButtonHref });
|
|
|
|
|
const ctaDefaults = { primaryBg: buttonColor, primaryText: buttonTextColor, outlineText: textColor };
|
|
|
|
|
|
2026-04-05 18:31:16 -07:00
|
|
|
return (
|
|
|
|
|
<section
|
|
|
|
|
ref={(ref: HTMLElement | null): void => { if (ref) connect(drag(ref)); }}
|
2026-05-25 12:43:28 -07:00
|
|
|
id={anchorId || undefined}
|
2026-04-05 18:31:16 -07:00
|
|
|
style={{
|
|
|
|
|
position: 'relative',
|
|
|
|
|
padding: '80px 20px',
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
outline: selected ? '2px solid #3b82f6' : 'none',
|
|
|
|
|
...bgStyle,
|
|
|
|
|
...style,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{/* Overlay */}
|
|
|
|
|
{bgType === 'image' && overlayOpacity > 0 && (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
inset: 0,
|
|
|
|
|
backgroundColor: overlayColor,
|
|
|
|
|
opacity: overlayOpacity / 100,
|
|
|
|
|
pointerEvents: 'none',
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div style={{ maxWidth: '700px', margin: '0 auto', position: 'relative', zIndex: 1 }}>
|
|
|
|
|
<h2 style={{ fontSize: '36px', fontWeight: '700', color: textColor, marginBottom: '12px' }}>
|
|
|
|
|
{heading}
|
|
|
|
|
</h2>
|
|
|
|
|
<p style={{ fontSize: '18px', color: textColor, opacity: 0.85, marginBottom: '28px', lineHeight: '1.6' }}>
|
|
|
|
|
{description}
|
|
|
|
|
</p>
|
|
|
|
|
<div style={{ display: 'flex', gap: '12px', justifyContent: 'center', flexWrap: 'wrap' }}>
|
2026-05-25 12:43:28 -07:00
|
|
|
{effectiveCtas.map((cta, i) => (
|
|
|
|
|
<a key={i} href={cta.href || '#'} onClick={(e) => e.preventDefault()}
|
|
|
|
|
style={ctaInlineStyle(cta, ctaDefaults)}>
|
|
|
|
|
{cta.text}
|
2026-04-05 18:31:16 -07:00
|
|
|
</a>
|
2026-05-25 12:43:28 -07:00
|
|
|
))}
|
2026-04-05 18:31:16 -07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* ---------- Craft config ---------- */
|
|
|
|
|
|
|
|
|
|
CallToAction.craft = {
|
|
|
|
|
displayName: 'Call to Action',
|
|
|
|
|
props: {
|
|
|
|
|
heading: 'Ready to Get Started?',
|
|
|
|
|
description: 'Join thousands of satisfied users and start building your dream website today.',
|
2026-05-25 12:43:28 -07:00
|
|
|
ctas: [
|
|
|
|
|
{ text: 'Get Started', href: '#', variant: 'primary' },
|
|
|
|
|
{ text: 'Learn More', href: '#', variant: 'outline' },
|
|
|
|
|
] as CtaButton[],
|
|
|
|
|
anchorId: '',
|
2026-04-05 18:31:16 -07:00
|
|
|
bgType: 'gradient',
|
|
|
|
|
bgValue: defaultGradient,
|
|
|
|
|
overlayColor: '#000000',
|
|
|
|
|
overlayOpacity: 0,
|
|
|
|
|
textColor: '#ffffff',
|
|
|
|
|
buttonColor: '#ffffff',
|
|
|
|
|
style: {},
|
2026-07-14 06:47:58 -07:00
|
|
|
animation: '',
|
|
|
|
|
animationDelay: '',
|
|
|
|
|
hideOnDesktop: false,
|
|
|
|
|
hideOnTablet: false,
|
|
|
|
|
hideOnMobile: false,
|
2026-04-05 18:31:16 -07:00
|
|
|
},
|
|
|
|
|
rules: {
|
|
|
|
|
canDrag: () => true,
|
|
|
|
|
canMoveIn: () => false,
|
|
|
|
|
canMoveOut: () => true,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* ---------- HTML export ---------- */
|
|
|
|
|
|
|
|
|
|
(CallToAction as any).toHtml = (props: CallToActionProps, _childrenHtml: string) => {
|
|
|
|
|
const bgType = props.bgType || 'gradient';
|
|
|
|
|
const bgValue = props.bgValue || defaultGradient;
|
2026-07-12 15:56:25 -07:00
|
|
|
// Sanitized -- raw string-interpolation sink in the heading/description
|
|
|
|
|
// style attributes below.
|
|
|
|
|
const textColor = cssValue(props.textColor) || '#ffffff';
|
2026-04-05 18:31:16 -07:00
|
|
|
const buttonColor = props.buttonColor || '#ffffff';
|
|
|
|
|
const isButtonDark = buttonColor === '#ffffff' || buttonColor === '#f8fafc';
|
|
|
|
|
const buttonTextColor = isButtonDark ? '#18181b' : '#ffffff';
|
|
|
|
|
|
|
|
|
|
const sectionCss: CSSProperties = {
|
|
|
|
|
position: 'relative',
|
|
|
|
|
padding: '80px 20px',
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
...props.style,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (bgType === 'color') {
|
|
|
|
|
sectionCss.backgroundColor = bgValue;
|
|
|
|
|
} else if (bgType === 'gradient') {
|
|
|
|
|
sectionCss.background = bgValue;
|
|
|
|
|
} else if (bgType === 'image') {
|
|
|
|
|
sectionCss.backgroundImage = `url(${bgValue})`;
|
|
|
|
|
sectionCss.backgroundSize = 'cover';
|
|
|
|
|
sectionCss.backgroundPosition = 'center';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sectionStyle = cssPropsToString(sectionCss);
|
|
|
|
|
|
|
|
|
|
let overlayHtml = '';
|
|
|
|
|
if (bgType === 'image' && (props.overlayOpacity || 0) > 0) {
|
|
|
|
|
const overlayStyle = cssPropsToString({
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
inset: '0',
|
|
|
|
|
backgroundColor: props.overlayColor || '#000000',
|
|
|
|
|
opacity: String((props.overlayOpacity || 0) / 100) as any,
|
|
|
|
|
pointerEvents: 'none',
|
|
|
|
|
});
|
|
|
|
|
overlayHtml = `<div${overlayStyle ? ` style="${overlayStyle}"` : ''}></div>`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 12:43:28 -07:00
|
|
|
const ctas = normalizeCtas(props);
|
|
|
|
|
const buttonsHtml = ctasToHtml(ctas, { primaryBg: buttonColor, primaryText: buttonTextColor, outlineText: textColor });
|
2026-07-12 11:49:10 -07:00
|
|
|
const idAttr = props.anchorId ? ` id="${escapeAttr(props.anchorId)}"` : '';
|
2026-04-05 18:31:16 -07:00
|
|
|
|
|
|
|
|
return {
|
2026-05-25 12:43:28 -07:00
|
|
|
html: `<section${idAttr}${sectionStyle ? ` style="${sectionStyle}"` : ''}>
|
2026-04-05 18:31:16 -07:00
|
|
|
${overlayHtml}<div style="max-width:700px;margin:0 auto;position:relative;z-index:1">
|
2026-07-12 11:49:10 -07:00
|
|
|
<h2 style="font-size:36px;font-weight:700;color:${textColor};margin-bottom:12px">${escapeHtml(props.heading || '')}</h2>
|
|
|
|
|
<p style="font-size:18px;color:${textColor};opacity:0.85;margin-bottom:28px;line-height:1.6">${escapeHtml(props.description || '')}</p>
|
2026-05-25 12:43:28 -07:00
|
|
|
<div style="display:flex;gap:12px;justify-content:center;flex-wrap:wrap">${buttonsHtml}</div>
|
2026-04-05 18:31:16 -07:00
|
|
|
</div>
|
|
|
|
|
</section>`,
|
|
|
|
|
};
|
|
|
|
|
};
|