/** * ChatWindow — expanded chat container with header, messages, input. */ import { useRef, useEffect } from '@wordpress/element'; import MessageList from './MessageList'; import InputBar from './InputBar'; import LeadForm from './LeadForm'; import PoweredBy from './PoweredBy'; const ChatWindow = ( { config, chat, onClose } ) => { const windowRef = useRef( null ); // "start" trigger: lead form gates the chat — no dismiss, input hidden // until the visitor submits the form. const leadCfg = config.leadConfig || {}; const startGate = !! ( leadCfg.enabled && leadCfg.trigger === 'start' && ! chat.leadSubmitted ); // Initial focus + keyboard handling (Esc to close, Tab focus trap). useEffect( () => { const el = windowRef.current; if ( ! el ) return; const focusable = el.querySelector( 'input, textarea, button' ); focusable?.focus(); const handleKeyDown = ( e ) => { if ( e.key === 'Escape' ) { e.stopPropagation(); onClose(); return; } if ( e.key !== 'Tab' ) return; const nodes = el.querySelectorAll( 'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex="-1"])' ); if ( ! nodes.length ) return; const first = nodes[ 0 ]; const last = nodes[ nodes.length - 1 ]; if ( e.shiftKey && document.activeElement === first ) { e.preventDefault(); last.focus(); } else if ( ! e.shiftKey && document.activeElement === last ) { e.preventDefault(); first.focus(); } }; el.addEventListener( 'keydown', handleKeyDown ); return () => el.removeEventListener( 'keydown', handleKeyDown ); }, [ onClose ] ); return (
{ config.consent_message || 'We use cookies and chat data to provide you with the best support experience. By starting this chat, you consent to our collection and use of this data in accordance with our Privacy Policy. You can end the chat at any time.' }