/** * The design of a form (with nothing selected on the canvas): themes, saved styles, colours, * steps and progress, fonts, sizes, borders, background and the form's own CSS. Almost every value * is a style token (FormDocument.style.tokens), which the plugin turns into CSS variables * (Frontend\\StyleTokens); steps and progress also set a few form settings. */ import React, { useEffect, useRef, useState } from 'react'; import type { FormDocument } from '@funnelforms/form-schema'; import type { FontFile, GoogleFont, MediaImage, SavedStyle } from '../api'; import type { Recipe } from '../store'; import { applySavedStyle, changedFromTheme, contrastProblems, effectiveTokens, setMainFont, setStyleToken, splitAlpha, toHex, withAlpha, type ContrastProblem, type ThemeToken } from '../themes'; import { FONT_STACKS } from '../fonts'; import { BrandingImport } from './BrandingImport'; import { ColorField, ContrastChip, ImageColorDialog, type ColorTarget } from './ColorPick'; import { useBuilder, useEnv } from './context'; import { __, sprintf } from './i18n'; import { SiteDesignButton } from './SiteDesign'; import { ThemeGallery, themeName } from './ThemeGallery'; import { Button, ChoiceTiles, ColorInput, Dialog, Field as FieldRow, IconButton, icons, Notice, Range, Section, Select, Sketch, TextInput, Toggle } from './ui'; type SetToken = ( key: string, value: string | null ) => void; function useChange(): ( recipe: Recipe, key?: string ) => void { return useEnv().store.getState().change; } function contrastText( problem: ContrastProblem ): string { const ratio = problem.ratio.toFixed( 1 ); switch ( problem.pair ) { case 'button': return sprintf( __( 'Button text is hard to read on the button colour (%s:1).', 'funnelforms-pro' ), ratio ); case 'back': return sprintf( __( 'The text of the back button is hard to read on its colour (%s:1).', 'funnelforms-pro' ), ratio ); case 'heading': return sprintf( __( 'Questions are hard to read on the background (%s:1).', 'funnelforms-pro' ), ratio ); case 'muted': return sprintf( __( 'Hints are hard to read on the background (%s:1).', 'funnelforms-pro' ), ratio ); default: return sprintf( __( 'Text is hard to read on the background (%s:1).', 'funnelforms-pro' ), ratio ); } } /** Fonts the builder offers: bundled with the plugin or present on every device. */ const FONTS = [ 'inherit', 'Montserrat', 'Poppins', 'system-ui', ...Object.keys( FONT_STACKS ), 'serif', 'monospace' ]; function fontName( value: string ): string { const names: Record = { inherit: __( 'Font of the website', 'funnelforms-pro' ), 'system-ui': __( 'System font', 'funnelforms-pro' ), serif: __( 'Serif', 'funnelforms-pro' ), monospace: __( 'Monospace', 'funnelforms-pro' ), 'serif-classic': __( 'Classic serif', 'funnelforms-pro' ), 'serif-modern': __( 'Modern serif', 'funnelforms-pro' ), 'sans-humanist': __( 'Humanist sans serif', 'funnelforms-pro' ), 'sans-geometric': __( 'Geometric sans serif', 'funnelforms-pro' ), rounded: __( 'Rounded', 'funnelforms-pro' ), slab: __( 'Slab serif', 'funnelforms-pro' ), condensed: __( 'Condensed', 'funnelforms-pro' ), 'mono-code': __( 'Code', 'funnelforms-pro' ), }; return names[ value ] ?? value; } /** Fonts of the site: uploaded files and the Google Fonts it can download (with those it has). */ export interface SiteFonts { uploaded: FontFile[]; google: GoogleFont[]; addUploaded: ( font: FontFile ) => void; addGoogle: ( font: GoogleFont ) => void; } /** * A font: bundled, generic, uploaded or downloaded from Google Fonts to the site, and adding * one. With `sameAs` the first choice is "same as" another font (an empty value, the token is * removed). */ export function FontPicker( { label, font, onChange, sameAs, fonts }: { label: string; font: string; onChange: ( value: string ) => void; sameAs?: string; fonts: SiteFonts } ) { const { api, readOnly } = useEnv(); const fileInput = useRef( null ); const [ busy, setBusy ] = useState( false ); const [ browse, setBrowse ] = useState( false ); const [ error, setError ] = useState( null ); const options = [ ...( undefined !== sameAs ? [ { value: '', label: sameAs } ] : [] ), ...FONTS.map( ( f ) => ( { value: f, label: fontName( f ) } ) ), ...fonts.uploaded.map( ( f ) => ( { value: f.file, label: f.family } ) ), ...fonts.google.filter( ( f ) => f.installed ).map( ( f ) => ( { value: f.family, label: f.family } ) ), ]; if ( ! options.some( ( o ) => o.value === font ) ) { options.push( { value: font, label: font } ); } const upload = async ( file: File ): Promise => { setBusy( true ); setError( null ); try { const added = await api.uploadFont( file, file.name ); fonts.addUploaded( added ); onChange( added.file ); } catch ( e ) { setError( e instanceof Error ? e.message : String( e ) ); } setBusy( false ); }; return (
{ const file = e.target.files?.[ 0 ]; e.target.value = ''; if ( file ) { void upload( file ); } } } /> { error ? { error } : null } { browse ? ( setBrowse( false ) } onPick={ ( family ) => { onChange( family ); setBrowse( false ); } } /> ) : null }
); } const CATEGORIES: GoogleFont[ 'category' ][] = [ 'sans-serif', 'serif', 'display', 'handwriting', 'monospace' ]; function categoryName( category: string ): string { const names: Record = { '': __( 'All styles', 'funnelforms-pro' ), 'sans-serif': __( 'Sans serif', 'funnelforms-pro' ), serif: __( 'Serif', 'funnelforms-pro' ), display: __( 'Display', 'funnelforms-pro' ), handwriting: __( 'Handwriting', 'funnelforms-pro' ), monospace: __( 'Monospace', 'funnelforms-pro' ), }; return names[ category ] ?? category; } /** * The Google Fonts of the catalogue, searchable. Picking one the site does not have yet * downloads it to the site first; the form then names it like any other font. */ function GoogleFontsDialog( { fonts, current, onClose, onPick }: { fonts: SiteFonts; current: string; onClose: () => void; onPick: ( family: string ) => void } ) { const { api } = useEnv(); const [ query, setQuery ] = useState( '' ); const [ category, setCategory ] = useState( '' ); const [ busy, setBusy ] = useState( null ); const [ error, setError ] = useState( null ); const needle = query.trim().toLowerCase(); const list = fonts.google.filter( ( f ) => ( '' === category || f.category === category ) && ( '' === needle || f.family.toLowerCase().includes( needle ) ) ); const pick = async ( font: GoogleFont ): Promise => { if ( font.installed || ! api.installGoogleFont ) { onPick( font.family ); return; } setBusy( font.family ); setError( null ); try { fonts.addGoogle( await api.installGoogleFont( font.family ) ); onPick( font.family ); } catch ( e ) { setError( e instanceof Error ? e.message : String( e ) ); } setBusy( null ); }; return ( { __( 'Close', 'funnelforms-pro' ) } }>

{ __( 'A font you pick is downloaded once to your site. Visitors load it from there and nothing from Google.', 'funnelforms-pro' ) }

label={ __( 'Style', 'funnelforms-pro' ) } value={ category } options={ [ '', ...CATEGORIES ].map( ( c ) => ( { value: c, label: categoryName( c ) } ) ) } onChange={ setCategory } />
{ error ? { error } : null }
    { list.map( ( f ) => (
  • ) ) }
{ list.length ? null :

{ __( 'No font matches.', 'funnelforms-pro' ) }

}
); } /** Fonts of the site, loaded once for all font pickers. */ export function useSiteFonts(): SiteFonts { const { api } = useEnv(); const [ uploaded, setUploaded ] = useState( [] ); const [ google, setGoogle ] = useState( [] ); useEffect( () => { let live = true; api.fonts().then( ( list ) => live && setUploaded( list ), () => undefined ); api.googleFonts?.().then( ( list ) => live && setGoogle( list ), () => undefined ); return () => { live = false; }; }, [ api ] ); return { uploaded, google, addUploaded: ( font ) => setUploaded( ( list ) => [ ...list.filter( ( f ) => f.file !== font.file ), font ] ), addGoogle: ( font ) => setGoogle( ( list ) => list.map( ( f ) => ( f.family === font.family ? { ...f, installed: true } : f ) ) ), }; } /** Themes and the global styles of the form: colours, fonts, sizes, borders and background. */ export function DesignInspector( { doc }: { doc: FormDocument } ) { const change = useChange(); const { api, readOnly } = useEnv(); const tokens = effectiveTokens( doc.style.tokens ); const token: SetToken = ( key, value ) => change( ( d ) => setStyleToken( d, key, value ), 'style:' + key ); const font = doc.style.tokens[ 'font.family' ] ?? 'inherit'; const card = Number.parseInt( tokens[ 'radius.card' ], 10 ); const problems = contrastProblems( doc.style.tokens ); const fonts = useSiteFonts(); const [ picking, setPicking ] = useState<'logo' | 'image' | null>( null ); const buttonText = tokens[ 'color.onButton' ] ?? tokens[ 'color.onPrimary' ]; const buttonColor = tokens[ 'color.button' ] ?? tokens[ 'color.primary' ]; const colors: Array<[ ThemeToken, string, React.ReactNode ]> = [ [ 'color.primary', __( 'Accent colour', 'funnelforms-pro' ), ], [ 'color.text', __( 'Text', 'funnelforms-pro' ), ], [ 'color.background', __( 'Background', 'funnelforms-pro' ), null ], [ 'color.border', __( 'Borders', 'funnelforms-pro' ), null ], ]; // The colours of the form at a glance, as visitors see them. const palette: Array<[ string, string ]> = [ [ buttonColor, __( 'Next and send button', 'funnelforms-pro' ) ], [ tokens[ 'color.primary' ], __( 'Accent colour', 'funnelforms-pro' ) ], [ tokens[ 'color.text' ], __( 'Text', 'funnelforms-pro' ) ], [ tokens[ 'color.background' ], __( 'Background', 'funnelforms-pro' ) ], [ tokens[ 'color.border' ], __( 'Borders', 'funnelforms-pro' ) ], ]; const targets: ColorTarget[] = [ ...colors.map( ( [ key, label ] ) => ( { token: key, label } ) ), { token: 'color.button', label: __( 'Next and send button', 'funnelforms-pro' ) }, { token: 'color.heading', label: __( 'Questions and headings', 'funnelforms-pro' ) }, ]; return (
{ /* Reading the site's design is Funnelforms Pro, like the import from a website. */ } { 'free' === process.env.FUNNELFORMS_EDITION ? null : }
{ 'free' === process.env.FUNNELFORMS_EDITION ? null : } { 'free' === process.env.FUNNELFORMS_EDITION ? null : }

{ __( 'Colours', 'funnelforms-pro' ) }

{ changedFromTheme( doc ) ?

{ sprintf( __( 'Based on %s, with your changes.', 'funnelforms-pro' ), themeName( doc.style.theme ?? '' ) ) }

: null }
{ api.siteLogo ? ( ) : null }
{ colors.map( ( [ key, label, note ] ) => ( token( key, v ) } note={ note } /> ) ) } { picking ? ( setPicking( null ) } onPick={ ( hex, key ) => { token( key, hex ); setPicking( null ); } } /> ) : null }
{ problems.length ? (
    { problems.map( ( p ) => (
  • { contrastText( p ) }
  • ) ) }
{ __( 'Aim for at least 4.5:1 so everyone can read the form.', 'funnelforms-pro' ) }
) : null } change( ( d ) => setMainFont( d, v ), 'style:font.family' ) } fonts={ fonts } />

{ __( 'Fonts are hosted on your site, so visitors load nothing from other servers. Check that the font\'s license allows use on websites.', 'funnelforms-pro' ) }

change( ( d ) => { setStyleToken( d, 'radius.card', String( v ) ); setStyleToken( d, 'radius.input', String( Math.round( ( v * 2 ) / 3 ) ) ); }, 'style:radius' ) } /> { /* Custom CSS is Funnelforms Pro: Free shows it only to remove it. */ } { 'free' !== process.env.FUNNELFORMS_EDITION || doc.style.css ? : null }
); } /** A whole number of a token, or the fallback. */ function num( doc: FormDocument, key: string, fallback: number ): number { const n = Number.parseFloat( doc.style.tokens[ key ] ?? '' ); return Number.isFinite( n ) ? n : fallback; } /** A colour token that can be left to its default (empty). */ function OptionalColor( { doc, token, name, label, fallback, note }: { doc: FormDocument; token: SetToken; name: string; label: string; fallback: string; note?: React.ReactNode } ) { const value = doc.style.tokens[ name ] ?? ''; return token( name, v ) } note={ note } />; } function MoreColors( { doc, token }: { doc: FormDocument; token: SetToken } ) { const t = effectiveTokens( doc.style.tokens ); const primary = t[ 'color.primary' ]; const readable = __( 'On the background', 'funnelforms-pro' ); const chip = ( text: string | undefined, background: string | undefined, label = readable ): React.ReactNode => ; return (
{ __( 'More colours', 'funnelforms-pro' ) }

{ __( 'Empty fields follow the colours above.', 'funnelforms-pro' ) }

{ undefined !== doc.settings.heading ? ( ) : null }
); } function Typography( { doc, token, fonts }: { doc: FormDocument; token: SetToken; fonts: SiteFonts } ) { const size = num( doc, 'font.size', 16 ); const heading = num( doc, 'font.size.heading', Math.round( size * 1.25 ) ); const field = num( doc, 'font.size.field', size ); const same = __( 'Same as the text', 'funnelforms-pro' ); return (
{ __( 'Fonts and sizes', 'funnelforms-pro' ) } token( 'font.heading', v ) } fonts={ fonts } /> token( 'font.field', v ) } fonts={ fonts } /> token( 'border.style', 'solid' === v ? null : v ) } /> token( 'border.container', on ? 'on' : null ) } /> token( 'tile.shadow', 'soft' === v ? null : v ) } /> ( { value: String( a ), label: `${ a }°` } ) ) } onChange={ ( v ) => token( 'background.angle', '160' === v ? null : v ) } /> ) : null } { api.pickImage ? ( { () => (
{ image ? : null } { Number.isFinite( imageId ) ? ( ) : null }
) }
) : null } { Number.isFinite( imageId ) ? ( <> token( 'color.overlay', v ) } /> token( 'background.position', 'center' === v ? null : v ) } />

{ __( 'Check that text stays readable on the image; a colour over it helps.', 'funnelforms-pro' ) }

) : null }
); } function stepAnimations(): Array<{ value: string; label: string }> { return [ { value: 'none', label: __( 'None', 'funnelforms-pro' ) }, { value: 'fade', label: __( 'Fade in', 'funnelforms-pro' ) }, { value: 'slide', label: __( 'Slide in', 'funnelforms-pro' ) }, { value: 'fadeRight', label: __( 'Fade in from the left', 'funnelforms-pro' ) }, { value: 'backInRight', label: __( 'Swing in from the right', 'funnelforms-pro' ) }, { value: 'up', label: __( 'Rise up', 'funnelforms-pro' ) }, { value: 'zoom', label: __( 'Zoom in', 'funnelforms-pro' ) }, ]; } /** * How visitors move through the form, in one place high up in the design: progress bar, the * height of the steps, scrolling, alignment, buttons and the transition between steps. The * progress settings are form settings (settings.progressBar, autoHeight, scrollToTop); the rest * are style tokens. */ function StepsAndProgress( { doc, token }: { doc: FormDocument; token: SetToken } ) { const change = useChange(); const settings = doc.settings; const bar = settings.progressBar; const progress = ! bar.show ? 'none' : bar.percent ? 'percent' : 'bar'; const animation = doc.style.tokens.animation ?? 'none'; return (

{ __( 'Steps and progress', 'funnelforms-pro' ) }

), }, { value: 'bar', label: __( 'Progress bar', 'funnelforms-pro' ), icon: ( ), }, { value: 'percent', label: __( 'With percentage', 'funnelforms-pro' ), icon: ( % ), }, ] } onChange={ ( v ) => change( ( d ) => { d.settings.progressBar.show = 'none' !== v; if ( 'percent' === v ) { d.settings.progressBar.percent = true; } else { delete d.settings.progressBar.percent; } }, 'settings:progress' ) } /> ( { value: a.value, label: a.label, icon: ( ), } ) ) } onChange={ ( v ) => token( 'animation', 'none' === v ? null : v ) } />

{ __( 'Visitors who turned off animations on their device see no transition.', 'funnelforms-pro' ) }

}, { value: 'center', label: __( 'Centre', 'funnelforms-pro' ), icon: }, { value: 'right', label: __( 'Right', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => token( 'align.text', 'center' === v ? null : v ) } /> }, { value: 'center', label: __( 'Centred', 'funnelforms-pro' ), icon: }, { value: 'end', label: __( 'Both right', 'funnelforms-pro' ), icon: }, { value: 'full', label: __( 'Full width', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => token( 'button.align', 'apart' === v ? null : v ) } /> change( ( d ) => void ( d.settings.autoHeight = v ) ) } /> change( ( d ) => void ( d.settings.scrollToTop.mobile = v ) ) } />
); } function CustomCss( { doc }: { doc: FormDocument } ) { const change = useChange(); const uid = useBuilder( ( s ) => s.form?.uid ) ?? ''; return (
{ __( 'Custom CSS', 'funnelforms-pro' ) } { ( id ) => (