| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import React from "react"; |
| 5 |
import { useDispatch, useSelect } from "@wordpress/data"; |
| 6 |
import { __ } from "@wordpress/i18n"; |
| 7 |
import { |
| 8 |
useBlockEditContext, |
| 9 |
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown, |
| 10 |
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients, |
| 11 |
} from "@wordpress/block-editor"; |
| 12 |
|
| 13 |
/** |
| 14 |
* Internal dependencies |
| 15 |
*/ |
| 16 |
import { ColorSettingsProps } from "./types"; |
| 17 |
|
| 18 |
/** |
| 19 |
* Color settings component |
| 20 |
* Provides color picker dropdown with multi-origin palette support |
| 21 |
*/ |
| 22 |
const ColorSettings: React.FC<ColorSettingsProps> = ({ |
| 23 |
label, |
| 24 |
attrKey, |
| 25 |
onAttributesUpdate = () => null, |
| 26 |
}) => { |
| 27 |
const { clientId } = useBlockEditContext(); |
| 28 |
const { updateBlockAttributes } = useDispatch("core/block-editor") as any; |
| 29 |
|
| 30 |
const attributes = useSelect((select) => { |
| 31 |
return select("core/block-editor").getBlockAttributes(clientId); |
| 32 |
}) as Record<string, any>; |
| 33 |
|
| 34 |
const setAttributes = (newAttributes: Record<string, any>) => { |
| 35 |
updateBlockAttributes(clientId, newAttributes); |
| 36 |
onAttributesUpdate(newAttributes); |
| 37 |
}; |
| 38 |
|
| 39 |
const colorGradientSettings = useMultipleOriginColorsAndGradients(); |
| 40 |
|
| 41 |
const { defaultColors } = useSelect((select) => { |
| 42 |
return { |
| 43 |
defaultColors: |
| 44 |
select("core/block-editor")?.getSettings()?.__experimentalFeatures |
| 45 |
?.color?.palette?.default, |
| 46 |
}; |
| 47 |
}) as { defaultColors: any }; |
| 48 |
|
| 49 |
// Merge default colors with theme and custom colors |
| 50 |
const mergedColors = [ |
| 51 |
...(colorGradientSettings.colors || []), |
| 52 |
...(defaultColors |
| 53 |
? [ |
| 54 |
{ |
| 55 |
name: "Default", |
| 56 |
slug: "default", |
| 57 |
colors: defaultColors, |
| 58 |
}, |
| 59 |
] |
| 60 |
: []), |
| 61 |
]; |
| 62 |
|
| 63 |
return ( |
| 64 |
<ColorGradientSettingsDropdown |
| 65 |
{...colorGradientSettings} |
| 66 |
colors={mergedColors} |
| 67 |
enableAlpha |
| 68 |
title={__("Color Settings", "sliderberg")} |
| 69 |
popoverProps={{ |
| 70 |
placement: "left start", |
| 71 |
}} |
| 72 |
settings={[ |
| 73 |
{ |
| 74 |
clearable: true, |
| 75 |
colorValue: attributes[attrKey], |
| 76 |
label: label, |
| 77 |
onColorChange: (newValue: any) => |
| 78 |
setAttributes({ [attrKey]: newValue }), |
| 79 |
}, |
| 80 |
]} |
| 81 |
/> |
| 82 |
); |
| 83 |
}; |
| 84 |
|
| 85 |
export default ColorSettings; |
| 86 |
|