Skip to content

Commit 59d7f8e

Browse files
Add utility classes to ColorPicker component (#322)
* feat(ColorPicker): Add classes prop for style overrides --------- Co-authored-by: Steven DeMartini <sjdemartini@users.noreply.github.com>
1 parent fde6c3e commit 59d7f8e

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/controls/ColorPicker.tsx

+48-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { TextField } from "@mui/material";
22
import { useEffect, useRef } from "react";
33
import { HexAlphaColorPicker, HexColorPicker } from "react-colorful";
44
import { makeStyles } from "tss-react/mui";
5+
import { getUtilityClasses } from "../styles";
56
import { colorToHex as colorToHexDefault } from "../utils/color";
67
import { ColorSwatchButton } from "./ColorSwatchButton";
78

9+
export type ColorPickerClasses = ReturnType<typeof useStyles>["classes"];
10+
811
export type ColorChangeSource = "gradient" | "text" | "swatch";
912

1013
export type SwatchColorOptionObject = {
@@ -98,8 +101,31 @@ export type ColorPickerProps = {
98101
*/
99102
textFieldPlaceholder?: string;
100103
};
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>;
101122
};
102123

124+
const colorPickerUtilityClasses: ColorPickerClasses = getUtilityClasses(
125+
"ColorPicker",
126+
["gradientPicker", "colorTextInput", "swatchContainer"]
127+
);
128+
103129
const useStyles = makeStyles({ name: { ColorPicker } })((theme) => ({
104130
gradientPicker: {
105131
// Increase specificity to override the styles
@@ -131,8 +157,11 @@ export function ColorPicker({
131157
colorToHex = colorToHexDefault,
132158
disableAlpha = false,
133159
labels = {},
160+
classes: overrideClasses = {},
134161
}: ColorPickerProps) {
135-
const { classes } = useStyles();
162+
const { classes, cx } = useStyles(undefined, {
163+
props: { classes: overrideClasses },
164+
});
136165
const { textFieldPlaceholder = 'Ex: "#7cb5ec"' } = labels;
137166

138167
const inputRef = useRef<HTMLInputElement | null>(null);
@@ -161,18 +190,24 @@ export function ColorPicker({
161190
{disableAlpha ? (
162191
<HexColorPicker
163192
color={colorValueAsHex ?? "#000000"}
193+
className={cx(
194+
colorPickerUtilityClasses.gradientPicker,
195+
classes.gradientPicker
196+
)}
164197
onChange={(color) => {
165198
onChange(color, "gradient");
166199
}}
167-
className={classes.gradientPicker}
168200
/>
169201
) : (
170202
<HexAlphaColorPicker
171203
color={colorValueAsHex ?? "#000000"}
204+
className={cx(
205+
colorPickerUtilityClasses.gradientPicker,
206+
classes.gradientPicker
207+
)}
172208
onChange={(color) => {
173209
onChange(color, "gradient");
174210
}}
175-
className={classes.gradientPicker}
176211
/>
177212
)}
178213

@@ -183,7 +218,10 @@ export function ColorPicker({
183218
defaultValue={value || ""}
184219
inputRef={inputRef}
185220
spellCheck={false}
186-
className={classes.colorTextInput}
221+
className={cx(
222+
colorPickerUtilityClasses.colorTextInput,
223+
classes.colorTextInput
224+
)}
187225
onChange={(event) => {
188226
const newColor = event.target.value;
189227
const newHexColor = colorToHex(newColor);
@@ -195,7 +233,12 @@ export function ColorPicker({
195233
/>
196234

197235
{swatchColorObjects.length > 0 && (
198-
<div className={classes.swatchContainer}>
236+
<div
237+
className={cx(
238+
colorPickerUtilityClasses.swatchContainer,
239+
classes.swatchContainer
240+
)}
241+
>
199242
{swatchColorObjects.map((swatchColor) => (
200243
<ColorSwatchButton
201244
key={swatchColor.value}

0 commit comments

Comments
 (0)