-
Notifications
You must be signed in to change notification settings - Fork 2
/
Swatch.tsx
69 lines (62 loc) · 2.15 KB
/
Swatch.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Color
*/
import "./Swatch.scss";
import classnames from "classnames";
import * as React from "react";
import type { ColorDef } from "@itwin/core-common";
import type { CommonProps } from "@itwin/core-react";
import { getCSSColorFromDef } from "./getCSSColorFromDef";
/** Properties for the [[ColorSwatch]] React component
* @beta
* @deprecated in 4.11.x. Props of deprecated component {@link ColorSwatch}.
*/
export interface ColorSwatchProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
CommonProps {
/** color specification */
colorDef: ColorDef;
/** function to run when user selects color swatch */
onColorPick?: ((color: ColorDef, e: React.MouseEvent) => void) | undefined;
/** Show swatches as squares unless round is set to true */
round?: boolean;
}
/** ColorSwatch Functional component displays a color swatch in a button
* @beta
* @deprecated in 4.11.x. Use {@link https://itwinui.bentley.com/docs/colorpicker iTwinUI color picker} instead.
*/
// eslint-disable-next-line deprecation/deprecation
export function ColorSwatch(props: ColorSwatchProps) {
const rgbaString = getCSSColorFromDef(props.colorDef);
const colorStyle: React.CSSProperties = {
backgroundColor: rgbaString,
...props.style,
};
const handleClick = (e: React.MouseEvent) => {
// istanbul ignore else
if (props && props.onColorPick) props.onColorPick(props.colorDef, e);
};
const classes = classnames(
"components-color-swatch",
props.round && "round",
props.className
);
const {
onColorPick,
colorDef,
round, // do not pass on color swatch specific props
...otherProps // pass-through props
} = props;
return (
<button
{...otherProps}
style={colorStyle}
className={classes}
onClick={handleClick}
/>
);
}