/** * Settings of "Text and media": the text, an image or video, and how the step goes on * (a button, by itself after a wait, or a loading screen). A loading screen is the * "Loading animation" of the palette: its settings come first, with a preview. */ import React, { useEffect, useState } from 'react'; import type { ContentField, ContentLoader, ContentMedia } from '@funnelforms/form-schema'; import type { Edit } from './Answers'; import { useBuilder, useEnv } from './context'; import { __ } from './i18n'; import { editText, setText } from '../languages'; import { effectiveTokens } from '../themes'; import { LinesInput, put, TextProp } from './texts'; import { Button, ChoiceTiles, NumberInput, Select, Sketch, TextInput, Toggle } from './ui'; type Mode = 'button' | 'auto' | 'loader'; type LoaderStyle = ContentLoader[ 'style' ]; type LoaderSize = NonNullable | 'medium'; export function ContentProps( { field, edit, propText }: { field: ContentField; edit: Edit; propText: ( key: string ) => ( value: string ) => void } ) { const { api, ctx, lang, readOnly } = useEnv(); const p = field.props; const content = ( recipe: ( props: ContentField[ 'props' ] ) => void, key?: string ): void => edit( ( f ) => recipe( ( f as ContentField ).props ), key ); const media = p.media; const setMedia = ( recipe: ( m: ContentMedia ) => void, key: string ): void => content( ( props ) => { if ( props.media ) { recipe( props.media ); } }, 'media.' + key ); const mode: Mode = p.loader ? 'loader' : p.button.show ? 'button' : 'auto'; const pickImage = api.pickImage?.bind( api ); const textAndMedia = ( <> }, { value: 'image', label: __( 'Image', 'funnelforms-pro' ), icon: }, { value: 'video', label: __( 'Video', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => content( ( props ) => { if ( '' === v ) { delete props.media; } else { props.media = { ...props.media, kind: v, url: v === props.media?.kind ? props.media.url : '' }; } }, 'media.kind' ) } /> { media ? ( <> { 'image' === media.kind && pickImage ? ( ) : null } setMedia( ( m ) => void ( m.url = v.trim() ), 'url' ) } /> { 'image' === media.kind ? ( setMedia( ( m ) => put( m, 'alt', lang, v, ctx.locale ), 'alt' ) } hint={ __( 'Describes the image for screen readers.', 'funnelforms-pro' ) } /> ) : null }
setMedia( ( m ) => put( m, 'caption', lang, v, ctx.locale ), 'caption' ) } /> setMedia( ( m ) => { if ( undefined === v || v >= 100 ) { delete m.width; } else { m.width = Math.max( 10, Math.round( v ) ); } }, 'width' ) } />
) : null } ); const goesOn = ( <> label={ __( 'How the form goes on', 'funnelforms-pro' ) } value={ mode } options={ [ { value: 'button', label: __( 'With a button', 'funnelforms-pro' ), icon: }, { value: 'auto', label: __( 'By itself after a wait', 'funnelforms-pro' ), icon: }, { value: 'loader', label: __( 'Loading animation', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => content( ( props ) => { props.button.show = 'button' === v; if ( 'button' === v ) { delete props.autoAdvanceMs; } else { props.autoAdvanceMs ||= 3000; } if ( 'loader' === v ) { props.loader ??= { style: 'spinner', messages: [] }; } else { delete props.loader; } }, 'mode' ) } /> { 'button' === mode ? ( content( ( props ) => put( props.button, 'label', lang, v, ctx.locale ), 'button' ) } /> ) : null } { 'auto' === mode ? : null } ); if ( p.loader ) { return ( <>
{ __( 'Text, image or video above the animation', 'funnelforms-pro' ) } { textAndMedia }
{ goesOn } ); } return ( <> { textAndMedia } { goesOn } ); } /** The waiting time in seconds, from half a second to a minute. */ function Wait( { label, field, edit }: { label: string; field: ContentField; edit: Edit } ) { return ( undefined !== v && v > 0 && edit( ( f ) => void ( ( f as ContentField ).props.autoAdvanceMs = Math.round( Math.min( 60, v ) * 1000 ) ), 'autoAdvanceMs' ) } /> ); } /** Settings of a loading animation: the animation, how long, its texts and its look. */ function LoaderProps( { field, loader, edit }: { field: ContentField; loader: ContentLoader; edit: Edit } ) { const { ctx, lang } = useEnv(); const set = ( recipe: ( l: ContentLoader ) => void, key: string ): void => edit( ( f ) => { const l = ( f as ContentField ).props.loader; if ( l ) { recipe( l ); } }, 'loader.' + key ); const style = loader.style; const counts = 'bar' === style || 'circle' === style; return ( <> label={ __( 'Animation', 'funnelforms-pro' ) } value={ style } options={ [ { value: 'bar', label: __( 'Progress bar', 'funnelforms-pro' ), icon: }, { value: 'circle', label: __( 'Ring with percent', 'funnelforms-pro' ), icon: }, { value: 'checklist', label: __( 'Checklist', 'funnelforms-pro' ), icon: }, { value: 'spinner', label: __( 'Spinner', 'funnelforms-pro' ), icon: }, { value: 'dots', label: __( 'Dots', 'funnelforms-pro' ), icon: }, { value: 'search', label: __( 'Magnifying glass', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => set( ( l ) => void ( l.style = v ), 'style' ) } /> editText( m, lang, ctx.locale ) ) } resetKey={ field.id + lang } onLines={ ( lines ) => set( ( l ) => { if ( lang !== ctx.locale ) { l.messages.forEach( ( m, i ) => setText( m, lang, lines[ i ] ?? '', ctx.locale ) ); } else { l.messages = lines.slice( 0, 10 ).map( ( line, i ) => { const known = l.messages[ i ]; if ( known ) { setText( known, lang, line, ctx.locale ); return known; } return { [ lang ]: line }; } ); } }, 'messages' ) } /> { counts ? set( ( l ) => void ( l.percent = v ), 'percent' ) } /> : null }
label={ __( 'Size', 'funnelforms-pro' ) } value={ loader.size ?? 'medium' } options={ [ { value: 'small', label: __( 'Small', 'funnelforms-pro' ) }, { value: 'medium', label: __( 'Medium', 'funnelforms-pro' ) }, { value: 'large', label: __( 'Large', 'funnelforms-pro' ) }, ] } onChange={ ( v ) => set( ( l ) => { if ( 'medium' === v ) { delete l.size; } else { l.size = v; } }, 'size' ) } /> { 'checklist' === style ? null : ( label={ __( 'Texts', 'funnelforms-pro' ) } value={ loader.textPosition ?? 'below' } options={ [ { value: 'below', label: __( 'Below the animation', 'funnelforms-pro' ) }, { value: 'above', label: __( 'Above the animation', 'funnelforms-pro' ) }, ] } onChange={ ( v ) => set( ( l ) => { if ( 'below' === v ) { delete l.textPosition; } else { l.textPosition = v; } }, 'textPosition' ) } /> ) }
set( ( l ) => { if ( v ) { l.color = '#2563eb'; } else { delete l.color; } }, 'color' ) } /> { undefined !== loader.color ? ( /^#[0-9a-f]{6}$/i.test( v ) && set( ( l ) => void ( l.color = v ), 'color' ) } /> ) : null } ); } /** * The loading animation as visitors see it, over and over: the texts change and the bar or ring * fills in the duration, then it starts again after a short pause. */ function LoaderPreview( { loader, ms }: { loader: ContentLoader; ms: number } ) { const { ctx, lang } = useEnv(); const primary = useBuilder( ( s ) => ( s.doc ? effectiveTokens( s.doc.style.tokens )[ 'color.primary' ] : undefined ) ); const wait = Math.max( 500, ms ); const [ elapsed, setElapsed ] = useState( 0 ); useEffect( () => { const started = Date.now(); const timer = setInterval( () => setElapsed( ( Date.now() - started ) % ( wait + 1200 ) ), 100 ); return () => clearInterval( timer ); }, [ wait ] ); const messages = loader.messages.map( ( m ) => editText( m, lang, ctx.locale ).replace( /<[^>]*>/g, '' ).trim() ).filter( Boolean ); const done = Math.min( 100, Math.round( ( elapsed / wait ) * 100 ) ); const current = Math.min( messages.length - 1, Math.floor( ( elapsed / wait ) * messages.length ) ); const style = 'checklist' === loader.style && ! messages.length ? 'spinner' : loader.style; const percent = ( loader.percent ?? 'circle' === style ) && ( 'bar' === style || 'circle' === style ); const color = loader.color ?? primary; const css = { '--ffxb-loader-color': color, '--ffxb-loader-done': String( done ) } as React.CSSProperties; const classes = `ffxb-loader-preview ffxb-loader-preview--${ style }${ loader.size ? ` ffxb-loader-preview--${ loader.size }` : '' }${ 'above' === loader.textPosition ? ' ffxb-loader-preview--above' : '' }`; return (
{ 'checklist' === style ? (
    { messages.map( ( m, i ) => (
  • = 100 ? 'is-done' : i === current ? 'is-active' : '' }> { m }
  • ) ) }
) : ( <> { 'bar' === style && percent ? { `${ done } %` } : null }

{ messages[ Math.max( 0, current ) ] ?? '' }

) }
{ __( 'Preview', 'funnelforms-pro' ) }
); } /* Small drawings of "Text and media" for the tiles of the properties panel. */ function MediaSketch( { kind }: { kind: '' | ContentMedia[ 'kind' ] } ) { return ( { 'image' === kind ? ( <> ) : null } { 'video' === kind ? ( <> ) : null } ); } function GoOnSketch( { mode }: { mode: Mode } ) { return ( { 'button' === mode ? : null } { 'auto' === mode ? ( <> ) : null } { 'loader' === mode ? ( <> ) : null } ); } function LoaderSketch( { style }: { style: LoaderStyle } ) { return ( { 'spinner' === style ? : null } { 'dots' === style ? ( <> ) : null } { 'bar' === style ? ( <> ) : null } { 'circle' === style ? ( <> % ) : null } { 'search' === style ? ( <> ) : null } { 'checklist' === style ? ( <> ) : null } ); }