| 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 { SpacingControlProps } from "./types"; |
| 15 |
|
| 16 |
/** |
| 17 |
* Spacing control component |
| 18 |
* Provides control for padding/margin spacing |
| 19 |
*/ |
| 20 |
const SpacingControl: React.FC<SpacingControlProps> = ({ |
| 21 |
label, |
| 22 |
attrKey, |
| 23 |
minimumCustomValue = 0, |
| 24 |
sides = ["top", "right", "bottom", "left"], |
| 25 |
}) => { |
| 26 |
const { clientId } = useBlockEditContext(); |
| 27 |
|
| 28 |
const attributes = useSelect( |
| 29 |
(select) => select("core/block-editor").getSelectedBlock()?.attributes |
| 30 |
) as Record<string, any>; |
| 31 |
|
| 32 |
const { updateBlockAttributes } = useDispatch("core/block-editor") as any; |
| 33 |
|
| 34 |
const setAttributes = (newAttributes: Record<string, any>) => { |
| 35 |
updateBlockAttributes(clientId, newAttributes); |
| 36 |
}; |
| 37 |
|
| 38 |
return ( |
| 39 |
<> |
| 40 |
<SpacingSizesControl |
| 41 |
minimumCustomValue={minimumCustomValue} |
| 42 |
allowReset={true} |
| 43 |
label={label} |
| 44 |
values={attributes[attrKey]} |
| 45 |
sides={sides} |
| 46 |
onChange={(newValue: any) => { |
| 47 |
setAttributes({ |
| 48 |
[attrKey]: newValue, |
| 49 |
}); |
| 50 |
}} |
| 51 |
/> |
| 52 |
</> |
| 53 |
); |
| 54 |
}; |
| 55 |
|
| 56 |
export default SpacingControl; |
| 57 |
|