| 1 |
import { BaseControl } from '@wordpress/components'; |
| 2 |
import { sprintf, __ } from '@wordpress/i18n'; |
| 3 |
import { Attributes } from '../../types'; |
| 4 |
import { Headlights } from './headers/Headlights'; |
| 5 |
import { HeadlightsMuted } from './headers/HeadlightsMuted'; |
| 6 |
import { HeadlightsMutedAlt } from './headers/HeadlightsMutedAlt'; |
| 7 |
import { SimpleString } from './headers/SimpleString'; |
| 8 |
|
| 9 |
type HeaderSelectProps = { |
| 10 |
attributes: Attributes; |
| 11 |
onClick: (slug: string) => void; |
| 12 |
}; |
| 13 |
export const HeaderSelect = ({ attributes, onClick }: HeaderSelectProps) => { |
| 14 |
const { headerType, ...attributesWithoutHeaderType }: Partial<Attributes> = |
| 15 |
attributes; |
| 16 |
const { bgColor } = attributes; |
| 17 |
const types = { |
| 18 |
none: __('None', 'code-block-pro'), |
| 19 |
headlights: __('Headlights', 'code-block-pro'), |
| 20 |
headlightsMuted: __('Headlights muted', 'code-block-pro'), |
| 21 |
headlightsMutedAlt: __('Headlights muted alt', 'code-block-pro'), |
| 22 |
simpleString: __('Simple string', 'code-block-pro'), |
| 23 |
}; |
| 24 |
|
| 25 |
return ( |
| 26 |
<div className="code-block-pro-editor"> |
| 27 |
{Object.entries(types).map(([slug, type]) => ( |
| 28 |
<BaseControl |
| 29 |
id={`code-block-pro-header-${slug}`} |
| 30 |
label={ |
| 31 |
headerType === slug |
| 32 |
? sprintf( |
| 33 |
__('%s (current)', 'code-block-pro'), |
| 34 |
type, |
| 35 |
) |
| 36 |
: type |
| 37 |
} |
| 38 |
help={ |
| 39 |
['simpleString'].includes(slug) |
| 40 |
? // Settings refers to the panel that can be expanded |
| 41 |
__( |
| 42 |
'Update extras in the Settings panel', |
| 43 |
'code-block-pro', |
| 44 |
) |
| 45 |
: undefined |
| 46 |
} |
| 47 |
key={slug}> |
| 48 |
<button |
| 49 |
id={`code-block-pro-header-${slug}`} |
| 50 |
type="button" |
| 51 |
onClick={() => onClick(slug)} |
| 52 |
className="p-0 border flex items-start w-full text-left outline-none cursor-pointer no-underline ring-offset-2 ring-offset-white focus:shadow-none focus:ring-wp overflow-x-auto"> |
| 53 |
<span className="pointer-events-none w-full"> |
| 54 |
<HeaderType |
| 55 |
headerType={slug} |
| 56 |
{...attributesWithoutHeaderType} |
| 57 |
/> |
| 58 |
<span |
| 59 |
className="block w-full h-8" |
| 60 |
style={{ backgroundColor: bgColor }} |
| 61 |
/> |
| 62 |
</span> |
| 63 |
</button> |
| 64 |
</BaseControl> |
| 65 |
))} |
| 66 |
</div> |
| 67 |
); |
| 68 |
}; |
| 69 |
|
| 70 |
export const HeaderType = (attributes: Partial<Attributes>) => { |
| 71 |
const { headerType } = attributes; |
| 72 |
if (headerType === 'headlights') { |
| 73 |
return <Headlights {...attributes} />; |
| 74 |
} |
| 75 |
if (headerType === 'headlightsMuted') { |
| 76 |
return <HeadlightsMuted {...attributes} />; |
| 77 |
} |
| 78 |
if (headerType === 'headlightsMutedAlt') { |
| 79 |
return <HeadlightsMutedAlt {...attributes} />; |
| 80 |
} |
| 81 |
if (headerType === 'simpleString') { |
| 82 |
return <SimpleString {...attributes} />; |
| 83 |
} |
| 84 |
return null; |
| 85 |
}; |
| 86 |
|