| 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 |
|