/
usePasteHighchartsTheme.tsx
125 lines (119 loc) · 4.11 KB
/
usePasteHighchartsTheme.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as React from 'react';
import {ThemeContext} from '@twilio-paste/styling-library';
import type {ThemeShape} from '@twilio-paste/theme';
import type Highcharts from 'highcharts';
const merge = require('deepmerge');
/**
* A hook to apply the Paste theme to Highcharts options.
*
* @param {Highcharts.Options} options - options to be provided into Highcharts for rendering.
* @returns {Highcharts.Options} optionsWithPasteTheme - Passed options with deepmerged Paste Theme
*/
export const usePasteHighchartsTheme = (options: Highcharts.Options): Highcharts.Options => {
if (options == null) {
throw new Error(
`[usePasteHighchartsTheme]: Must provide highcharts options into this function. Options are deepmerged against the theme and returned for usage.`
);
}
const context = React.useContext(ThemeContext) as ThemeShape;
if (!context) {
throw new Error(
'[usePasteHighchartsTheme]: must be used within the @twilio-paste/theme provider. https://paste.twilio.design/introduction/for-engineers/manual-installation#setting-up-the-theme-provider'
);
}
/*
* context.dataVisualization returns the keys sorted alphabetically, which is a problem because:
* -1 comes before -10 comes before -2. We want -1, -2, ... -10 (numerical sorting).
* For this reason we need to sort the keys numerically before retrieving their values.
*/
const colors: string[] = React.useMemo(() => {
const collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
const tokenNames = Object.keys(context.dataVisualization) as Array<keyof ThemeShape['dataVisualization']>;
const sortedTokenNames = tokenNames.sort((a, b) => collator.compare(a, b));
// Now we can get the color array in the correct order
return sortedTokenNames.map((name) => context.dataVisualization[name]);
}, [context]);
return merge(options, {
colors,
chart: {
backgroundColor: context.backgroundColors.colorBackgroundBody,
},
title: {
style: {
color: context.textColors.colorText,
font: `${context.fontWeights.fontWeightBold} ${context.fontSizes.fontSize40} ${context.fonts.fontFamilyText}`,
},
},
subtitle: {
style: {
color: context.textColors.colorTextWeak,
font: `${context.fontSizes.fontSize20} ${context.fonts.fontFamilyText}`,
},
},
legend: {
itemStyle: {
font: `${context.fontSizes.fontSize10} ${context.fonts.fontFamilyText}`,
color: context.textColors.colorText,
},
itemHoverStyle: {
color: context.textColors.colorTextWeak,
},
title: {
style: {
color: context.textColors.colorText,
},
},
},
xAxis: {
lineColor: context.textColors.colorText,
tickColor: context.textColors.colorText,
labels: {
style: {
color: context.textColors.colorText,
font: `${context.fontWeights.fontWeightMedium} ${context.fontSizes.fontSize10} ${context.fonts.fontFamilyText}`,
},
},
title: {
style: {
color: context.textColors.colorText,
font: `${context.fontWeights.fontWeightSemibold} ${context.fontSizes.fontSize30} ${context.fonts.fontFamilyText}`,
},
},
},
yAxis: {
labels: {
style: {
color: context.textColors.colorText,
font: `${context.fontWeights.fontWeightMedium} ${context.fontSizes.fontSize10} ${context.fonts.fontFamilyText}`,
},
},
title: {
style: {
color: context.textColors.colorText,
font: `${context.fontWeights.fontWeightSemibold} ${context.fontSizes.fontSize30} ${context.fonts.fontFamilyText}`,
},
},
},
labels: {
style: {
color: context.textColors.colorText,
},
},
tooltip: {
backgroundColor: context.backgroundColors.colorBackground,
style: {
color: context.textColors.colorText,
},
},
credits: {
style: {
color: context.textColors.colorTextWeaker,
},
},
plotOptions: {
series: {
borderColor: 'transparent',
},
},
});
};