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