/** * WordPress dependencies */ import { BaseControl, SelectControl, TextControl, } from '@safe-wordpress/components'; import { useState } from '@safe-wordpress/element'; import { _x, sprintf } from '@safe-wordpress/i18n'; /** * External dependencies */ import clsx from 'clsx'; import { last, noop } from 'lodash'; import { Editor } from 'primereact/editor'; import { CodeEditor } from '@nab/components'; import { getElementLabel, hasHead, isDefined } from '@nab/utils'; /** * Internal dependencies */ import './style.scss'; import type { AlternativeAttributes } from '../../types'; export type ValuePreviewProps = { readonly attributes: AlternativeAttributes; }; export const ValuePreview = ( { attributes: { css, content }, }: ValuePreviewProps ): JSX.Element => { const [ tab, setTab ] = useState< Tab[ 'name' ] >( 'css' ); const tabs = [ !! css.trim ? TABS.css : undefined, !! content.length ? TABS.content : undefined, ].filter( isDefined ); const [ contentSelector, setContentSelector ] = useState( content[ 0 ]?.selector ?? '' ); return ( { ( { name }: Tab ): JSX.Element => { switch ( name ) { case 'content': return ( <> ( { value: c.selector, label: sprintf( '%1$s – %2$s', getElementLabel( c.selector ), c.selector ), } ) ) } value={ contentSelector } onChange={ setContentSelector } /> c.selector === contentSelector ) } /> ); case 'css': return ( ); } } } ); }; // ============ // HELPER VIEWS // ============ const ContentView = ( { value, }: { readonly value?: AlternativeAttributes[ 'content' ][ number ]; } ) => { switch ( value?.type ) { case undefined: return null; case 'element': return ( ); case 'image': return ( <> ); } }; type TabPanelProps = { readonly className: string; readonly activeClass: string; readonly orientation: 'horizontal'; readonly onSelect: ( tab: Tab[ 'name' ] ) => void; readonly children: ( tab: Tab ) => JSX.Element; readonly activeTab: Tab[ 'name' ]; readonly tabs: Readonly< [ Tab, ...Tab[] ] >; }; // NOTICE. This is a workaround until “TabPanel” can be controlled/uncontrolled. const TabPanel = ( { className, activeClass, orientation, onSelect, children, activeTab, tabs, }: TabPanelProps ) => { if ( 1 === tabs.length ) { return children( tabs[ 0 ] ); } return (
{ tabs.map( ( tab ) => ( ) ) }
{ children( tabs.find( ( t ) => t.name === activeTab ) || tabs[ 0 ] ) }
); }; // ==== // DATA // ==== type Tab = { readonly name: 'css' | 'content'; readonly title: string; readonly className: string; }; const TABS: Record< Tab[ 'name' ], Tab > = { css: { name: 'css', title: _x( 'CSS', 'text', 'nelio-ab-testing' ), className: 'nab-css-and-text-preview__tab nab-css__tab', }, content: { name: 'content', title: _x( 'Content', 'text', 'nelio-ab-testing' ), className: 'nab-css-and-text-preview__tab nab-content__tab', }, }; // ======= // HELPERS // ======= const getTagName = ( selector: string ) => last( selector.split( ' ' ) )?.split( /[#.:]/ )[ 0 ] || '';