@@ -2,9 +2,12 @@ import { TextField } from "@mui/material";
2
2
import { useEffect , useRef } from "react" ;
3
3
import { HexAlphaColorPicker , HexColorPicker } from "react-colorful" ;
4
4
import { makeStyles } from "tss-react/mui" ;
5
+ import { getUtilityClasses } from "../styles" ;
5
6
import { colorToHex as colorToHexDefault } from "../utils/color" ;
6
7
import { ColorSwatchButton } from "./ColorSwatchButton" ;
7
8
9
+ export type ColorPickerClasses = ReturnType < typeof useStyles > [ "classes" ] ;
10
+
8
11
export type ColorChangeSource = "gradient" | "text" | "swatch" ;
9
12
10
13
export type SwatchColorOptionObject = {
@@ -98,8 +101,31 @@ export type ColorPickerProps = {
98
101
*/
99
102
textFieldPlaceholder ?: string ;
100
103
} ;
104
+ /**
105
+ * Override or extend existing styles. These classes are merged with the
106
+ * default utility classes. For instance, if you need to conditionally hide
107
+ * the swatch container, you could do:
108
+ *
109
+ * ColorPickerProps={{
110
+ * classes: {
111
+ * swatchContainer: shouldHide ? "hide" : undefined
112
+ * }
113
+ * }}
114
+ *
115
+ * And in your CSS:
116
+ *
117
+ * .MuiTiptap-ColorPicker-swatchContainer.hide {
118
+ * display: none;
119
+ * }
120
+ */
121
+ classes ?: Partial < ColorPickerClasses > ;
101
122
} ;
102
123
124
+ const colorPickerUtilityClasses : ColorPickerClasses = getUtilityClasses (
125
+ "ColorPicker" ,
126
+ [ "gradientPicker" , "colorTextInput" , "swatchContainer" ]
127
+ ) ;
128
+
103
129
const useStyles = makeStyles ( { name : { ColorPicker } } ) ( ( theme ) => ( {
104
130
gradientPicker : {
105
131
// Increase specificity to override the styles
@@ -131,8 +157,11 @@ export function ColorPicker({
131
157
colorToHex = colorToHexDefault ,
132
158
disableAlpha = false ,
133
159
labels = { } ,
160
+ classes : overrideClasses = { } ,
134
161
} : ColorPickerProps ) {
135
- const { classes } = useStyles ( ) ;
162
+ const { classes, cx } = useStyles ( undefined , {
163
+ props : { classes : overrideClasses } ,
164
+ } ) ;
136
165
const { textFieldPlaceholder = 'Ex: "#7cb5ec"' } = labels ;
137
166
138
167
const inputRef = useRef < HTMLInputElement | null > ( null ) ;
@@ -161,18 +190,24 @@ export function ColorPicker({
161
190
{ disableAlpha ? (
162
191
< HexColorPicker
163
192
color = { colorValueAsHex ?? "#000000" }
193
+ className = { cx (
194
+ colorPickerUtilityClasses . gradientPicker ,
195
+ classes . gradientPicker
196
+ ) }
164
197
onChange = { ( color ) => {
165
198
onChange ( color , "gradient" ) ;
166
199
} }
167
- className = { classes . gradientPicker }
168
200
/>
169
201
) : (
170
202
< HexAlphaColorPicker
171
203
color = { colorValueAsHex ?? "#000000" }
204
+ className = { cx (
205
+ colorPickerUtilityClasses . gradientPicker ,
206
+ classes . gradientPicker
207
+ ) }
172
208
onChange = { ( color ) => {
173
209
onChange ( color , "gradient" ) ;
174
210
} }
175
- className = { classes . gradientPicker }
176
211
/>
177
212
) }
178
213
@@ -183,7 +218,10 @@ export function ColorPicker({
183
218
defaultValue = { value || "" }
184
219
inputRef = { inputRef }
185
220
spellCheck = { false }
186
- className = { classes . colorTextInput }
221
+ className = { cx (
222
+ colorPickerUtilityClasses . colorTextInput ,
223
+ classes . colorTextInput
224
+ ) }
187
225
onChange = { ( event ) => {
188
226
const newColor = event . target . value ;
189
227
const newHexColor = colorToHex ( newColor ) ;
@@ -195,7 +233,12 @@ export function ColorPicker({
195
233
/>
196
234
197
235
{ swatchColorObjects . length > 0 && (
198
- < div className = { classes . swatchContainer } >
236
+ < div
237
+ className = { cx (
238
+ colorPickerUtilityClasses . swatchContainer ,
239
+ classes . swatchContainer
240
+ ) }
241
+ >
199
242
{ swatchColorObjects . map ( ( swatchColor ) => (
200
243
< ColorSwatchButton
201
244
key = { swatchColor . value }
0 commit comments