| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import React from "react"; |
| 5 |
import { |
| 6 |
useBlockEditContext, |
| 7 |
__experimentalSpacingSizesControl as SpacingSizesControl, |
| 8 |
} from "@wordpress/block-editor"; |
| 9 |
import { useSelect, useDispatch } from "@wordpress/data"; |
| 10 |
|
| 11 |
/** |
| 12 |
* Internal dependencies |
| 13 |
*/ |
| 14 |
import { SpacingControlWithToolsPanelProps } from "./types"; |
| 15 |
|
| 16 |
/** |
| 17 |
* Spacing control with ToolsPanel component |
| 18 |
* Provides spacing control wrapped in ToolsPanel for reset functionality |
| 19 |
*/ |
| 20 |
const SpacingControlWithToolsPanel: React.FC< |
| 21 |
SpacingControlWithToolsPanelProps |
| 22 |
> = ({ label, attrKey, minimumCustomValue = 0 }) => { |
| 23 |
const { clientId } = useBlockEditContext(); |
| 24 |
|
| 25 |
const attributes = useSelect((select) => |
| 26 |
select("core/block-editor").getBlockAttributes(clientId), |
| 27 |
) as Record<string, any>; |
| 28 |
|
| 29 |
const { updateBlockAttributes } = useDispatch("core/block-editor") as any; |
| 30 |
|
| 31 |
const setAttributes = (newAttributes: Record<string, any>) => { |
| 32 |
updateBlockAttributes(clientId, newAttributes); |
| 33 |
}; |
| 34 |
|
| 35 |
return ( |
| 36 |
<SpacingSizesControl |
| 37 |
minimumCustomValue={minimumCustomValue} |
| 38 |
allowReset={true} |
| 39 |
label={label} |
| 40 |
values={attributes[attrKey]} |
| 41 |
sides={["top", "right", "bottom", "left"]} |
| 42 |
onChange={(newValue: any) => { |
| 43 |
setAttributes({ |
| 44 |
[attrKey]: newValue, |
| 45 |
}); |
| 46 |
}} |
| 47 |
/> |
| 48 |
); |
| 49 |
}; |
| 50 |
|
| 51 |
export default SpacingControlWithToolsPanel; |
| 52 |
|