| 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 { ColorSettingsWithToolsPanelProps } from "./types"; |
| 17 |
|
| 18 |
/** |
| 19 |
* Color settings component with ToolsPanel integration |
| 20 |
* Passes panelId to ColorGradientSettingsDropdown so it registers as a ToolsPanelItem |
| 21 |
* automatically inside an InspectorControls group="color" slot |
| 22 |
*/ |
| 23 |
const ColorSettingsWithToolsPanel: React.FC< |
| 24 |
ColorSettingsWithToolsPanelProps |
| 25 |
> = ({ label, attrKey, onAttributesUpdate = () => null }) => { |
| 26 |
const { clientId } = useBlockEditContext(); |
| 27 |
const { updateBlockAttributes } = useDispatch("core/block-editor") as any; |
| 28 |
|
| 29 |
const attributes = useSelect((select) => { |
| 30 |
return select("core/block-editor").getBlockAttributes(clientId); |
| 31 |
}) as Record<string, any>; |
| 32 |
|
| 33 |
const setAttributes = (newAttributes: Record<string, any>) => { |
| 34 |
updateBlockAttributes(clientId, newAttributes); |
| 35 |
onAttributesUpdate(newAttributes); |
| 36 |
}; |
| 37 |
|
| 38 |
const colorGradientSettings = useMultipleOriginColorsAndGradients(); |
| 39 |
|
| 40 |
const { defaultColors } = useSelect((select) => { |
| 41 |
return { |
| 42 |
defaultColors: |
| 43 |
select("core/block-editor")?.getSettings()?.__experimentalFeatures |
| 44 |
?.color?.palette?.default, |
| 45 |
}; |
| 46 |
}) as { defaultColors: any }; |
| 47 |
|
| 48 |
const mergedColors = [ |
| 49 |
...(colorGradientSettings.colors || []), |
| 50 |
...(defaultColors |
| 51 |
? [ |
| 52 |
{ |
| 53 |
name: "Default", |
| 54 |
slug: "default", |
| 55 |
colors: defaultColors, |
| 56 |
}, |
| 57 |
] |
| 58 |
: []), |
| 59 |
]; |
| 60 |
|
| 61 |
return ( |
| 62 |
<ColorGradientSettingsDropdown |
| 63 |
{...colorGradientSettings} |
| 64 |
colors={mergedColors} |
| 65 |
enableAlpha |
| 66 |
title={__("Color Settings", "sliderberg")} |
| 67 |
panelId={clientId} |
| 68 |
popoverProps={{ |
| 69 |
placement: "left start", |
| 70 |
}} |
| 71 |
settings={[ |
| 72 |
{ |
| 73 |
clearable: true, |
| 74 |
colorValue: attributes[attrKey], |
| 75 |
label: label, |
| 76 |
onColorChange: (newValue: any) => |
| 77 |
setAttributes({ [attrKey]: newValue }), |
| 78 |
}, |
| 79 |
]} |
| 80 |
/> |
| 81 |
); |
| 82 |
}; |
| 83 |
|
| 84 |
export default ColorSettingsWithToolsPanel; |
| 85 |
|