PluginProbe
Slider Block by Sliderberg – WordPress Slider & Carousel Plugin for Gutenberg / trunk
Slider Block by Sliderberg – WordPress Slider & Carousel Plugin for Gutenberg vtrunk
1.2.3 1.2.2 1.2.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.7 1.0.8 1.0.9
sliderberg / shared / src / components / ColorSettingsWithGradient.tsx

ColorSettingsWithGradient.tsx in Slider Block by Sliderberg – WordPress Slider & Carousel Plugin for Gutenberg trunk, at shared/src/components/ColorSettingsWithGradient.tsx

110 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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