| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import React from "react"; |
| 5 |
import { |
| 6 |
useBlockEditContext, |
| 7 |
__experimentalBorderRadiusControl as BorderRadiusControl, |
| 8 |
} from "@wordpress/block-editor"; |
| 9 |
import { useSelect, useDispatch } from "@wordpress/data"; |
| 10 |
import { |
| 11 |
BaseControl, |
| 12 |
__experimentalBorderBoxControl as BorderBoxControl, |
| 13 |
__experimentalToolsPanelItem as ToolsPanelItem, |
| 14 |
} from "@wordpress/components"; |
| 15 |
|
| 16 |
/** |
| 17 |
* Internal dependencies |
| 18 |
*/ |
| 19 |
import { BorderControlWithToolsPanelProps } from "./types"; |
| 20 |
import { splitBorderRadius } from "./utils/styling-helpers"; |
| 21 |
|
| 22 |
/** |
| 23 |
* Border control component with ToolsPanel integration |
| 24 |
* Each sub-control (border + border radius) is wrapped in its own ToolsPanelItem |
| 25 |
*/ |
| 26 |
const BorderControlWithToolsPanel: React.FC< |
| 27 |
BorderControlWithToolsPanelProps |
| 28 |
> = ({ |
| 29 |
borderLabel, |
| 30 |
attrBorderKey, |
| 31 |
borderRadiusLabel, |
| 32 |
attrBorderRadiusKey, |
| 33 |
isShowBorder = true, |
| 34 |
isShowBorderRadius = true, |
| 35 |
showDefaultBorder = false, |
| 36 |
showDefaultBorderRadius = false, |
| 37 |
}) => { |
| 38 |
const { clientId } = useBlockEditContext(); |
| 39 |
|
| 40 |
const attributes = useSelect((select) => |
| 41 |
select("core/block-editor").getBlockAttributes(clientId), |
| 42 |
) as Record<string, any>; |
| 43 |
|
| 44 |
const { updateBlockAttributes } = useDispatch("core/block-editor") as any; |
| 45 |
|
| 46 |
const setAttributes = (newAttributes: Record<string, any>) => { |
| 47 |
updateBlockAttributes(clientId, newAttributes); |
| 48 |
}; |
| 49 |
|
| 50 |
const { defaultColors } = useSelect((select) => { |
| 51 |
return { |
| 52 |
defaultColors: |
| 53 |
select("core/block-editor")?.getSettings()?.__experimentalFeatures |
| 54 |
?.color?.palette?.default, |
| 55 |
}; |
| 56 |
}) as { defaultColors: any }; |
| 57 |
|
| 58 |
return ( |
| 59 |
<> |
| 60 |
{isShowBorder && ( |
| 61 |
<ToolsPanelItem |
| 62 |
panelId={clientId} |
| 63 |
isShownByDefault={showDefaultBorder} |
| 64 |
label={borderLabel} |
| 65 |
hasValue={() => |
| 66 |
attributes[attrBorderKey] != null && |
| 67 |
Object.keys(attributes[attrBorderKey] || {}).length > 0 |
| 68 |
} |
| 69 |
onDeselect={() => setAttributes({ [attrBorderKey]: {} })} |
| 70 |
resetAllFilter={() => setAttributes({ [attrBorderKey]: {} })} |
| 71 |
> |
| 72 |
<BorderBoxControl |
| 73 |
enableAlpha |
| 74 |
size={"__unstable-large"} |
| 75 |
colors={defaultColors} |
| 76 |
label={borderLabel} |
| 77 |
onChange={(newBorder: any) => { |
| 78 |
setAttributes({ [attrBorderKey]: newBorder }); |
| 79 |
}} |
| 80 |
value={attributes[attrBorderKey]} |
| 81 |
/> |
| 82 |
</ToolsPanelItem> |
| 83 |
)} |
| 84 |
|
| 85 |
{isShowBorderRadius && ( |
| 86 |
<ToolsPanelItem |
| 87 |
panelId={clientId} |
| 88 |
isShownByDefault={showDefaultBorderRadius} |
| 89 |
label={borderRadiusLabel} |
| 90 |
hasValue={() => |
| 91 |
attributes[attrBorderRadiusKey] != null && |
| 92 |
Object.keys(attributes[attrBorderRadiusKey] || {}).length > 0 |
| 93 |
} |
| 94 |
onDeselect={() => setAttributes({ [attrBorderRadiusKey]: {} })} |
| 95 |
resetAllFilter={() => setAttributes({ [attrBorderRadiusKey]: {} })} |
| 96 |
> |
| 97 |
<BaseControl.VisualLabel as="legend"> |
| 98 |
{borderRadiusLabel} |
| 99 |
</BaseControl.VisualLabel> |
| 100 |
<div className="sliderberg-border-radius-control"> |
| 101 |
<BorderRadiusControl |
| 102 |
values={attributes[attrBorderRadiusKey]} |
| 103 |
onChange={(newBorderRadius: any) => { |
| 104 |
const splitted = splitBorderRadius(newBorderRadius); |
| 105 |
setAttributes({ [attrBorderRadiusKey]: splitted }); |
| 106 |
}} |
| 107 |
/> |
| 108 |
</div> |
| 109 |
</ToolsPanelItem> |
| 110 |
)} |
| 111 |
</> |
| 112 |
); |
| 113 |
}; |
| 114 |
|
| 115 |
export default BorderControlWithToolsPanel; |
| 116 |
|