-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ColorSwatch.tsx
40 lines (34 loc) · 1.25 KB
/
ColorSwatch.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
import {AriaColorSwatchProps, useColorSwatch} from '@react-aria/color';
import {Color} from '@react-types/color';
import {ContextValue, SlotProps, StyleRenderProps, useContextProps, useRenderProps} from './utils';
import React, {createContext, ForwardedRef, forwardRef} from 'react';
export interface ColorSwatchRenderProps {
/** The color of the swatch. */
color: Color
}
export interface ColorSwatchProps extends AriaColorSwatchProps, StyleRenderProps<ColorSwatchRenderProps>, SlotProps {}
export const ColorSwatchContext = createContext<ContextValue<ColorSwatchProps, HTMLDivElement>>(null);
function ColorSwatch(props: ColorSwatchProps, ref: ForwardedRef<HTMLDivElement>) {
[props, ref] = useContextProps(props, ref, ColorSwatchContext);
let {colorSwatchProps, color} = useColorSwatch(props);
let renderProps = useRenderProps({
...props,
defaultClassName: 'react-aria-ColorSwatch',
defaultStyle: colorSwatchProps.style,
values: {
color
}
});
return (
<div
{...colorSwatchProps}
{...renderProps}
slot={props.slot || undefined}
ref={ref} />
);
}
/**
* A ColorSwatch displays a preview of a selected color.
*/
const _ColorSwatch = forwardRef(ColorSwatch);
export {_ColorSwatch as ColorSwatch};