| 1 |
import { __ } from "@wordpress/i18n"; |
| 2 |
import { |
| 3 |
useBlockEditContext, |
| 4 |
__experimentalBorderRadiusControl as RadiusControl, |
| 5 |
} from "@wordpress/block-editor"; |
| 6 |
import { useColorPalettes } from "./useColorPalettes"; |
| 7 |
import { |
| 8 |
__experimentalToolsPanelItem as ToolsPanelItem, |
| 9 |
__experimentalBorderBoxControl as BorderBoxControl, |
| 10 |
PanelBody, |
| 11 |
} from "@wordpress/components"; |
| 12 |
import { Border } from "@wordpress/components/build-types/border-control/types"; |
| 13 |
import { Borders } from "@wordpress/components/build-types/border-box-control/types"; |
| 14 |
import { CSSProperties } from "react"; |
| 15 |
|
| 16 |
interface BorderWithRadiusControlPropTypes { |
| 17 |
label: string; |
| 18 |
value: Border | Borders; |
| 19 |
onChange: (newBorder: Border | Borders | undefined) => any; |
| 20 |
radiusValue: any; |
| 21 |
onRadiusChange: (newRadius: any) => any; |
| 22 |
resetAllFilter?: () => any; |
| 23 |
onDeselect: () => any; |
| 24 |
isShownByDefault?: boolean; |
| 25 |
children?: any; |
| 26 |
} |
| 27 |
|
| 28 |
const objectifyRadius = (radius: CSSProperties["borderRadius"] | string) => { |
| 29 |
if (typeof radius === "string") { |
| 30 |
return { |
| 31 |
topLeft: radius, |
| 32 |
topRight: radius, |
| 33 |
bottomRight: radius, |
| 34 |
bottomLeft: radius, |
| 35 |
}; |
| 36 |
} |
| 37 |
|
| 38 |
return radius; |
| 39 |
}; |
| 40 |
|
| 41 |
function BorderControl({ |
| 42 |
label, |
| 43 |
isShownByDefault = true, |
| 44 |
value, |
| 45 |
onChange = () => {}, |
| 46 |
radiusValue = {}, |
| 47 |
onRadiusChange = () => {}, |
| 48 |
resetAllFilter, |
| 49 |
onDeselect = () => {}, |
| 50 |
children, |
| 51 |
}: BorderWithRadiusControlPropTypes) { |
| 52 |
const { clientId } = useBlockEditContext(); |
| 53 |
|
| 54 |
// Theme, Default and Custom origins. |
| 55 |
const colors = useColorPalettes(); |
| 56 |
|
| 57 |
if (!resetAllFilter) { |
| 58 |
resetAllFilter = onDeselect; |
| 59 |
} |
| 60 |
|
| 61 |
return ( |
| 62 |
<ToolsPanelItem |
| 63 |
panelId={clientId} |
| 64 |
isShownByDefault={isShownByDefault} |
| 65 |
resetAllFilter={resetAllFilter} |
| 66 |
hasValue={() => true} |
| 67 |
label={label} |
| 68 |
onDeselect={onDeselect} |
| 69 |
> |
| 70 |
<PanelBody |
| 71 |
title={label} |
| 72 |
initialOpen={isShownByDefault} |
| 73 |
className="tableberg-border-with-radius-control-handle" |
| 74 |
> |
| 75 |
{children} |
| 76 |
<BorderBoxControl |
| 77 |
enableAlpha |
| 78 |
size={"__unstable-large"} |
| 79 |
colors={colors} |
| 80 |
label={label} |
| 81 |
onChange={onChange} |
| 82 |
value={value} |
| 83 |
/> |
| 84 |
<RadiusControl |
| 85 |
onChange={(val: any) => |
| 86 |
onRadiusChange(objectifyRadius(val)) |
| 87 |
} |
| 88 |
values={radiusValue} |
| 89 |
/> |
| 90 |
</PanelBody> |
| 91 |
</ToolsPanelItem> |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
export default BorderControl; |
| 96 |
|