PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.21.0
Code Block Pro – Beautiful Syntax Highlighting v1.21.0
1.27.1 1.27.2 1.27.3 1.27.4 1.27.5 1.27.6 1.27.7 1.28.0 1.3.0 1.4.0 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3 trunk 1.1.0 1.10.0 1.11.0 1.11.1 All 63 releases
code-block-pro / src / editor / components / HeaderSelect.tsx

HeaderSelect.tsx in Code Block Pro – Beautiful Syntax Highlighting 1.21.0, at src/editor/components/HeaderSelect.tsx

99 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 import { UnsupportedTheme } from './misc/Unsupported';
9
10 type HeaderSelectProps = {
11 attributes: Attributes;
12 onClick: (slug: string) => void;
13 };
14
15 const unsupportedWithCssVars = ['headlightsMutedAlt'];
16
17 export const HeaderSelect = ({ attributes, onClick }: HeaderSelectProps) => {
18 const { headerType, ...attributesWithoutHeaderType }: Partial<Attributes> =
19 attributes;
20 const { bgColor } = attributes;
21 const types = {
22 none: __('None', 'code-block-pro'),
23 headlights: __('Headlights', 'code-block-pro'),
24 headlightsMuted: __('Headlights muted', 'code-block-pro'),
25 headlightsMutedAlt: __('Headlights muted alt', 'code-block-pro'),
26 simpleString: __('Simple string', 'code-block-pro'),
27 };
28 const isUnsupported = bgColor?.startsWith('var(');
29
30 return (
31 <div className="code-block-pro-editor">
32 {Object.entries(types).map(([slug, type]) => (
33 <BaseControl
34 id={`code-block-pro-header-${slug}`}
35 label={
36 headerType === slug
37 ? sprintf(
38 __('%s (current)', 'code-block-pro'),
39 type,
40 )
41 : type
42 }
43 help={
44 ['simpleString'].includes(slug)
45 ? // Settings refers to the panel that can be expanded
46 __(
47 'Update extra settings above',
48 'code-block-pro',
49 )
50 : undefined
51 }
52 key={slug}>
53 <button
54 id={`code-block-pro-header-${slug}`}
55 type="button"
56 onClick={() => onClick(slug)}
57 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">
58 <span className="pointer-events-none w-full">
59 <HeaderType
60 headerType={slug}
61 {...attributesWithoutHeaderType}
62 />
63 <span
64 className="block w-full h-8"
65 style={{ backgroundColor: bgColor }}
66 />
67 </span>
68 </button>
69 {isUnsupported && unsupportedWithCssVars.includes(slug) && (
70 <UnsupportedTheme />
71 )}
72 </BaseControl>
73 ))}
74 </div>
75 );
76 };
77
78 export const HeaderType = (attributes: Partial<Attributes>) => {
79 const { headerType, bgColor } = attributes;
80 const isACustomTheme = bgColor?.startsWith('var(');
81 if (isACustomTheme && unsupportedWithCssVars.includes(headerType ?? '')) {
82 return null;
83 }
84
85 if (headerType === 'headlights') {
86 return <Headlights {...attributes} />;
87 }
88 if (headerType === 'headlightsMuted') {
89 return <HeadlightsMuted {...attributes} />;
90 }
91 if (headerType === 'headlightsMutedAlt') {
92 return <HeadlightsMutedAlt {...attributes} />;
93 }
94 if (headerType === 'simpleString') {
95 return <SimpleString {...attributes} />;
96 }
97 return null;
98 };
99