| 1 |
/** |
| 2 |
* WordPress Dependencies |
| 3 |
*/ |
| 4 |
import { __ } from "@wordpress/i18n"; |
| 5 |
import { useBlockEditContext } from "@wordpress/block-editor"; |
| 6 |
import { useColorPalettes } from "./useColorPalettes"; |
| 7 |
import { |
| 8 |
__experimentalToolsPanelItem as ToolsPanelItem, |
| 9 |
BorderBoxControl, |
| 10 |
} from "@wordpress/components"; |
| 11 |
import { Border as CSSBorder } from "@wordpress/components/build-types/border-control/types"; |
| 12 |
import { |
| 13 |
AnyBorder, |
| 14 |
Borders as CSSBorders, |
| 15 |
} from "@wordpress/components/build-types/border-box-control/types"; |
| 16 |
|
| 17 |
type Borders = { |
| 18 |
top: string; |
| 19 |
right: string; |
| 20 |
bottom: string; |
| 21 |
left: string; |
| 22 |
}; |
| 23 |
|
| 24 |
interface BorderControlPropTypes { |
| 25 |
label: string; |
| 26 |
value: Borders; |
| 27 |
hasValue: () => boolean; |
| 28 |
onChange: (newBorder: Borders) => any; |
| 29 |
resetAllFilter?: () => any; |
| 30 |
onDeselect: () => any; |
| 31 |
isShownByDefault?: boolean; |
| 32 |
} |
| 33 |
|
| 34 |
function getBorderStringFromObject(border?: CSSBorder) { |
| 35 |
if (!border) { |
| 36 |
return ""; |
| 37 |
} |
| 38 |
|
| 39 |
if (!border.width) { |
| 40 |
border.width = "0"; |
| 41 |
} |
| 42 |
if (!border.style) { |
| 43 |
border.style = "solid"; |
| 44 |
} |
| 45 |
if (!border.color) { |
| 46 |
border.color = "black"; |
| 47 |
} |
| 48 |
|
| 49 |
return `${border?.width} ${border?.style} ${border?.color}`; |
| 50 |
} |
| 51 |
|
| 52 |
function mutateOutgoingBorderValues(border: AnyBorder) { |
| 53 |
const newBorder: Borders = { |
| 54 |
top: "", |
| 55 |
right: "", |
| 56 |
bottom: "", |
| 57 |
left: "", |
| 58 |
}; |
| 59 |
|
| 60 |
if (!border) { |
| 61 |
return newBorder; |
| 62 |
} |
| 63 |
|
| 64 |
const sides = ["top", "right", "bottom", "left"]; |
| 65 |
|
| 66 |
if ("top" in border) { |
| 67 |
for (const side of sides) { |
| 68 |
const key = side as keyof CSSBorders; |
| 69 |
const val = border[key]; |
| 70 |
newBorder[key] = getBorderStringFromObject(val); |
| 71 |
} |
| 72 |
} else { |
| 73 |
const singleBorder = border as CSSBorder; |
| 74 |
|
| 75 |
for (const side of sides) { |
| 76 |
const key = side as keyof CSSBorders; |
| 77 |
newBorder[key] = getBorderStringFromObject(singleBorder); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return newBorder; |
| 82 |
} |
| 83 |
|
| 84 |
function mutateIncomingBorderValues(border: Borders): CSSBorders { |
| 85 |
const newBorder: CSSBorders = {}; |
| 86 |
const sides = ["top", "right", "bottom", "left"] as const; |
| 87 |
|
| 88 |
for (const side of sides) { |
| 89 |
const borderString = border[side]; |
| 90 |
if (borderString && borderString.trim()) { |
| 91 |
const parts = borderString.trim().split(/\s+/); |
| 92 |
|
| 93 |
newBorder[side] = { |
| 94 |
width: parts[0] || undefined, |
| 95 |
style: parts[1] || undefined, |
| 96 |
color: parts[2] || undefined, |
| 97 |
} as CSSBorder; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return newBorder; |
| 102 |
} |
| 103 |
|
| 104 |
function BorderControl({ |
| 105 |
label, |
| 106 |
isShownByDefault = true, |
| 107 |
value, |
| 108 |
hasValue, |
| 109 |
onChange, |
| 110 |
resetAllFilter, |
| 111 |
onDeselect = () => {}, |
| 112 |
}: BorderControlPropTypes) { |
| 113 |
const { clientId } = useBlockEditContext(); |
| 114 |
|
| 115 |
// Reading `color.palette.default` on its own left this picker with the |
| 116 |
// core swatches and no theme palette at all. |
| 117 |
const colors = useColorPalettes(); |
| 118 |
|
| 119 |
if (!resetAllFilter) { |
| 120 |
resetAllFilter = onDeselect; |
| 121 |
} |
| 122 |
|
| 123 |
return ( |
| 124 |
<ToolsPanelItem |
| 125 |
panelId={clientId} |
| 126 |
isShownByDefault={isShownByDefault} |
| 127 |
resetAllFilter={resetAllFilter} |
| 128 |
hasValue={hasValue} |
| 129 |
label={label} |
| 130 |
onDeselect={onDeselect} |
| 131 |
> |
| 132 |
<BorderBoxControl |
| 133 |
enableAlpha |
| 134 |
size={"__unstable-large"} |
| 135 |
colors={colors} |
| 136 |
label={label} |
| 137 |
onChange={newBorders => { |
| 138 |
return onChange(mutateOutgoingBorderValues(newBorders)); |
| 139 |
}} |
| 140 |
value={mutateIncomingBorderValues(value)} |
| 141 |
/> |
| 142 |
</ToolsPanelItem> |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
export default BorderControl; |
| 147 |
|