import React, { CSSProperties } from 'react'; import { useNode, UserComponent } from '@craftjs/core'; import { cssPropsToString } from '../../utils/style-helpers'; import { useSiteDesign } from '../../state/SiteDesignContext'; import { escapeHtml, escapeAttr, safeUrl, safeImageUrl } from '../../utils/escape'; /* ---------- Types ---------- */ interface LogoProps { type?: 'text' | 'image'; text?: string; imageSrc?: string; imageWidth?: string; href?: string; fontFamily?: string; fontSize?: string; fontWeight?: string; color?: string; style?: CSSProperties; } /* ---------- Component ---------- */ export const Logo: UserComponent = ({ type = 'text', text = 'MySite', imageSrc = '', imageWidth = '120px', href = '/', fontFamily = 'Inter, sans-serif', fontSize = '20px', fontWeight = '700', color, style = {}, }) => { const { connectors: { connect, drag }, } = useNode(); const { design } = useSiteDesign(); const resolvedColor = color || design.textColor; return ( { if (ref) connect(drag(ref)); }} href={href} onClick={(e) => e.preventDefault()} style={{ textDecoration: 'none', display: 'inline-flex', alignItems: 'center', flexShrink: 0, ...style, }} > {type === 'image' && imageSrc ? ( {text ) : ( {text} )} ); }; /* ---------- Craft config ---------- */ Logo.craft = { displayName: 'Logo', props: { type: 'text', text: 'MySite', imageSrc: '', imageWidth: '120px', href: '/', fontFamily: 'Inter, sans-serif', fontSize: '20px', fontWeight: '700', color: undefined, style: {}, } as LogoProps, rules: { canDrag: () => true, canMoveIn: () => false, canMoveOut: () => true, }, }; /* ---------- HTML export ---------- */ (Logo as any).toHtml = (props: LogoProps, _childrenHtml: string) => { const href = props.href || '/'; let innerHtml: string; if (props.type === 'image' && props.imageSrc) { const imgStyle = cssPropsToString({ width: props.imageWidth || '120px', height: 'auto', display: 'block' }); innerHtml = `${escapeAttr(props.text || 'Logo')}`; } else { const spanStyle = cssPropsToString({ fontWeight: props.fontWeight || '700', fontSize: props.fontSize || '20px', fontFamily: props.fontFamily || 'Inter, sans-serif', color: props.color || '#1f2937', }); innerHtml = `${escapeHtml(props.text || 'MySite')}`; } const aStyle = cssPropsToString({ textDecoration: 'none', display: 'inline-flex', alignItems: 'center', flexShrink: '0', ...props.style, }); return { html: `${innerHtml}`, }; };