| 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 { ColorSettingsWithGradientWithToolsPanelProps } from "./types"; |
| 17 |
|
| 18 |
/** |
| 19 |
* Color settings with gradient 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 ColorSettingsWithGradientWithToolsPanel: React.FC< |
| 24 |
ColorSettingsWithGradientWithToolsPanelProps |
| 25 |
> = ({ label, attrBackgroundKey, attrGradientKey }) => { |
| 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 |
|
| 36 |
const colorGradientSettings = useMultipleOriginColorsAndGradients(); |
| 37 |
|
| 38 |
const { defaultColors, defaultGradients } = useSelect((select) => { |
| 39 |
return { |
| 40 |
defaultColors: |
| 41 |
select("core/block-editor")?.getSettings()?.__experimentalFeatures |
| 42 |
?.color?.palette?.default, |
| 43 |
defaultGradients: |
| 44 |
select("core/block-editor")?.getSettings()?.__experimentalFeatures |
| 45 |
?.color?.gradients?.default, |
| 46 |
}; |
| 47 |
}) as { defaultColors: any; defaultGradients: any }; |
| 48 |
|
| 49 |
const mergedColors = [ |
| 50 |
...(colorGradientSettings.colors || []), |
| 51 |
...(defaultColors |
| 52 |
? [ |
| 53 |
{ |
| 54 |
name: "Default", |
| 55 |
slug: "default", |
| 56 |
colors: defaultColors, |
| 57 |
}, |
| 58 |
] |
| 59 |
: []), |
| 60 |
]; |
| 61 |
|
| 62 |
const mergedGradients = [ |
| 63 |
...(colorGradientSettings.gradients || []), |
| 64 |
...(defaultGradients |
| 65 |
? [ |
| 66 |
{ |
| 67 |
name: "Default", |
| 68 |
slug: "default", |
| 69 |
gradients: defaultGradients, |
| 70 |
}, |
| 71 |
] |
| 72 |
: []), |
| 73 |
]; |
| 74 |
|
| 75 |
return ( |
| 76 |
<ColorGradientSettingsDropdown |
| 77 |
{...colorGradientSettings} |
| 78 |
colors={mergedColors} |
| 79 |
gradients={mergedGradients} |
| 80 |
enableAlpha |
| 81 |
title={__("Color Settings", "sliderberg")} |
| 82 |
panelId={clientId} |
| 83 |
popoverProps={{ |
| 84 |
placement: "left start", |
| 85 |
}} |
| 86 |
settings={[ |
| 87 |
{ |
| 88 |
clearable: true, |
| 89 |
colorValue: attributes[attrBackgroundKey], |
| 90 |
gradientValue: attributes[attrGradientKey], |
| 91 |
label: label, |
| 92 |
onColorChange: (newValue: any) => |
| 93 |
setAttributes({ |
| 94 |
[attrBackgroundKey]: newValue, |
| 95 |
}), |
| 96 |
onGradientChange: (newValue: any) => |
| 97 |
setAttributes({ |
| 98 |
[attrGradientKey]: newValue, |
| 99 |
}), |
| 100 |
}, |
| 101 |
]} |
| 102 |
/> |
| 103 |
); |
| 104 |
}; |
| 105 |
|
| 106 |
export default ColorSettingsWithGradientWithToolsPanel; |
| 107 |
|