PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.1.5
Tableberg – Simple Gutenberg Table Block v1.1.5
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / components / editor / ToolbarWithDropdown.tsx

ToolbarWithDropdown.tsx in Tableberg – Simple Gutenberg Table Block 1.1.5, at components/editor/ToolbarWithDropdown.tsx

133 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { ToolbarGroup } from "@wordpress/components";
2 import {
3 alignNone,
4 positionLeft,
5 positionCenter,
6 positionRight,
7 stretchFullWidth,
8 stretchWide,
9 } from "@wordpress/icons";
10
11 interface ToolbarGroupControlProps {
12 icon?: JSX.Element;
13 title: string;
14 value?: string;
15 info?: string;
16 isDisabled?: boolean;
17 onClick?: () => void;
18 }
19
20 interface AlignmentToolbarProps {
21 icon?: JSX.Element;
22 title: string;
23 onChange: (newAlign?: string) => void;
24 value: string | undefined;
25 controls?: ToolbarGroupControlProps[];
26 controlset?: "alignment" | "all";
27 disabled?: boolean;
28 }
29
30 const alignControls: ToolbarGroupControlProps[] = [
31 {
32 icon: alignNone,
33 title: "None",
34 value: undefined,
35 },
36 {
37 icon: positionLeft,
38 title: "Align left",
39 value: "left",
40 },
41 {
42 icon: positionCenter,
43 title: "Align center",
44 value: "center",
45 },
46 {
47 icon: positionRight,
48 title: "Align right",
49 value: "right",
50 },
51 ];
52
53 const alignControlsWithWidth: ToolbarGroupControlProps[] = [
54 {
55 icon: alignNone,
56 title: "None",
57 value: undefined,
58 },
59 {
60 icon: positionLeft,
61 title: "Align left",
62 value: "left",
63 },
64 {
65 icon: positionCenter,
66 title: "Align center",
67 value: "center",
68 },
69 {
70 icon: positionRight,
71 title: "Align right",
72 value: "right",
73 },
74 {
75 icon: stretchWide,
76 title: "Wide width",
77 value: "wide",
78 },
79 {
80 icon: stretchFullWidth,
81 title: "Full Width",
82 value: "full",
83 },
84 ];
85
86 export default function ToolbarWithDropdown({
87 title,
88 onChange,
89 value,
90 controls,
91 controlset,
92 disabled,
93 }: AlignmentToolbarProps) {
94 const controlsets: Record<string, ToolbarGroupControlProps[]> = {
95 alignment: alignControls,
96 all: alignControlsWithWidth,
97 };
98
99 if (controlset) {
100 controls = controlsets[controlset];
101 }
102
103 if (controls) {
104 const activeControl =
105 controls.find(i => i.value === value) || controls[0];
106
107 return (
108 <ToolbarGroup
109 icon={activeControl?.icon}
110 title={title}
111 isCollapsed
112 controls={controls.map(control => {
113 const controlIsDisabled =
114 !!disabled || !!control.isDisabled;
115
116 return {
117 ...control,
118 isDisabled: controlIsDisabled,
119 onClick: () => {
120 if (controlIsDisabled) {
121 return;
122 }
123
124 onChange(control.value);
125 },
126 isActive: value === control.value,
127 };
128 })}
129 />
130 );
131 }
132 }
133