forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorpicker.tsx
55 lines (50 loc) · 1.79 KB
/
Colorpicker.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
import React from "react"
import { action } from "mobx"
import { observer } from "mobx-react"
import { SketchPicker } from "react-color"
import { ColorSchemeName, lastOfNonEmptyArray } from "@ourworldindata/utils"
import {
ColorSchemes,
getColorNameOwidDistinctAndSemanticPalettes,
getColorNameOwidDistinctLinesAndSemanticPalettes,
} from "@ourworldindata/grapher"
interface ColorpickerProps {
color?: string
showLineChartColors: boolean
onColor: (color: string | undefined) => void
}
@observer
export class Colorpicker extends React.Component<ColorpickerProps> {
@action.bound onColor(color: string) {
if (color === "") {
this.props.onColor(undefined)
} else {
this.props.onColor(color)
}
}
render() {
const scheme = this.props.showLineChartColors
? ColorSchemes.get(ColorSchemeName.OwidDistinctLines)
: ColorSchemes.get(ColorSchemeName["owid-distinct"])
const availableColors: string[] = lastOfNonEmptyArray(scheme.colorSets)
const colorNameLookupFn = (color: string) => {
const nameLines = this.props.showLineChartColors
? getColorNameOwidDistinctLinesAndSemanticPalettes(color)
: getColorNameOwidDistinctAndSemanticPalettes(color)
return nameLines.join(" ")
}
return (
<React.Fragment>
<SketchPicker
disableAlpha
presetColors={availableColors.map((color) => ({
color,
title: colorNameLookupFn(color),
}))}
color={this.props.color}
onChange={(color) => this.onColor(color.hex)}
/>
</React.Fragment>
)
}
}