@scality/core-ui 0.166.0 → 0.168.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/.storybook/preview.js +6 -4
  2. package/README.md +27 -80
  3. package/dist/components/barchart/BarChart.component.d.ts +5 -0
  4. package/dist/components/barchart/BarChart.component.d.ts.map +1 -1
  5. package/dist/components/barchart/BarChart.component.js +5 -0
  6. package/dist/components/barchartv2/Barchart.component.d.ts +5 -1
  7. package/dist/components/barchartv2/Barchart.component.d.ts.map +1 -1
  8. package/dist/components/barchartv2/Barchart.component.js +6 -2
  9. package/dist/components/barchartv2/utils.js +2 -2
  10. package/dist/components/button/Button.component.d.ts +2 -1
  11. package/dist/components/button/Button.component.d.ts.map +1 -1
  12. package/dist/components/button/Button.component.js +2 -1
  13. package/dist/components/chartlegend/ChartLegend.d.ts.map +1 -1
  14. package/dist/components/chartlegend/ChartLegend.js +7 -13
  15. package/dist/components/chartlegend/ChartLegendWrapper.d.ts +1 -2
  16. package/dist/components/chartlegend/ChartLegendWrapper.d.ts.map +1 -1
  17. package/dist/components/chartlegend/ChartLegendWrapper.js +4 -9
  18. package/dist/components/date/FormattedDateTime.d.ts +6 -1
  19. package/dist/components/date/FormattedDateTime.d.ts.map +1 -1
  20. package/dist/components/date/FormattedDateTime.js +11 -0
  21. package/dist/components/icon/Icon.component.d.ts +9 -11
  22. package/dist/components/icon/Icon.component.d.ts.map +1 -1
  23. package/dist/components/icon/Icon.component.js +2 -0
  24. package/dist/components/linetemporalchart/ChartUtil.d.ts +3 -2
  25. package/dist/components/linetemporalchart/ChartUtil.d.ts.map +1 -1
  26. package/dist/components/linetemporalchart/ChartUtil.js +30 -20
  27. package/dist/components/linetemporalchart/LineTemporalChart.component.d.ts +4 -0
  28. package/dist/components/linetemporalchart/LineTemporalChart.component.d.ts.map +1 -1
  29. package/dist/components/linetemporalchart/LineTemporalChart.component.js +4 -0
  30. package/dist/components/linetimeseriechart/linetimeseriechart.component.d.ts +8 -2
  31. package/dist/components/linetimeseriechart/linetimeseriechart.component.d.ts.map +1 -1
  32. package/dist/components/linetimeseriechart/linetimeseriechart.component.js +17 -11
  33. package/dist/index.d.ts +1 -11
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +0 -11
  36. package/dist/next.d.ts +1 -0
  37. package/dist/next.d.ts.map +1 -1
  38. package/jest.config.js +1 -0
  39. package/package.json +16 -37
  40. package/src/lib/components/barchart/BarChart.component.tsx +5 -0
  41. package/src/lib/components/barchartv2/Barchart.component.test.tsx +11 -7
  42. package/src/lib/components/barchartv2/Barchart.component.tsx +15 -3
  43. package/src/lib/components/barchartv2/utils.test.ts +2 -2
  44. package/src/lib/components/barchartv2/utils.ts +2 -2
  45. package/src/lib/components/button/Button.component.tsx +2 -1
  46. package/src/lib/components/chartlegend/ChartLegend.test.tsx +17 -0
  47. package/src/lib/components/chartlegend/ChartLegend.tsx +6 -12
  48. package/src/lib/components/chartlegend/ChartLegendWrapper.tsx +5 -13
  49. package/src/lib/components/date/FormattedDateTime.tsx +14 -1
  50. package/src/lib/components/icon/Icon.component.tsx +12 -1
  51. package/src/lib/components/linetemporalchart/ChartUtil.test.ts +23 -35
  52. package/src/lib/components/linetemporalchart/ChartUtil.ts +38 -26
  53. package/src/lib/components/linetemporalchart/LineTemporalChart.component.tsx +19 -15
  54. package/src/lib/components/linetimeseriechart/linetimeseriechart.component.tsx +36 -13
  55. package/src/lib/components/linetimeseriechart/linetimeseriechart.test.tsx +68 -0
  56. package/src/lib/components/selectv2/selectv2.test.tsx +1 -1
  57. package/src/lib/components/toast/useMutationsHandler.test.tsx +22 -32
  58. package/src/lib/index.ts +6 -11
  59. package/src/lib/next.ts +1 -0
  60. package/stories/BarChart/barchart.stories.tsx +108 -13
  61. package/stories/barchart.stories.tsx +1 -1
  62. package/stories/card.stories.tsx +7 -5
  63. package/stories/controls.ts +19 -20
  64. package/stories/linecharttemporal.stories.tsx +1 -1
  65. package/stories/linetimeseriechart.stories.tsx +146 -0
  66. package/stories/navbar.stories.tsx +103 -0
  67. package/stories/tabsv2.stories.tsx +51 -56
  68. package/dist/components/areachart/AreaChart.component.d.ts +0 -13
  69. package/dist/components/areachart/AreaChart.component.d.ts.map +0 -1
  70. package/dist/components/areachart/AreaChart.component.js +0 -27
  71. package/dist/components/chips/Chips.component.d.ts +0 -21
  72. package/dist/components/chips/Chips.component.d.ts.map +0 -1
  73. package/dist/components/chips/Chips.component.js +0 -105
  74. package/dist/components/cloudprogressbar/CloudProgressBar.component.d.ts +0 -10
  75. package/dist/components/cloudprogressbar/CloudProgressBar.component.d.ts.map +0 -1
  76. package/dist/components/cloudprogressbar/CloudProgressBar.component.js +0 -38
  77. package/dist/components/collapsiblepanel/CollapsiblePanel.component.d.ts +0 -9
  78. package/dist/components/collapsiblepanel/CollapsiblePanel.component.d.ts.map +0 -1
  79. package/dist/components/collapsiblepanel/CollapsiblePanel.component.js +0 -44
  80. package/dist/components/linechart/LineChart.component.d.ts +0 -21
  81. package/dist/components/linechart/LineChart.component.d.ts.map +0 -1
  82. package/dist/components/linechart/LineChart.component.js +0 -109
  83. package/dist/components/multiselect/MultiSelect.component.d.ts +0 -28
  84. package/dist/components/multiselect/MultiSelect.component.d.ts.map +0 -1
  85. package/dist/components/multiselect/MultiSelect.component.js +0 -73
  86. package/dist/components/select/Select.component.d.ts +0 -14
  87. package/dist/components/select/Select.component.d.ts.map +0 -1
  88. package/dist/components/select/Select.component.js +0 -71
  89. package/dist/components/spacedbox/SpacedBox.d.ts +0 -34
  90. package/dist/components/spacedbox/SpacedBox.d.ts.map +0 -1
  91. package/dist/components/spacedbox/SpacedBox.js +0 -64
  92. package/dist/components/sparkline/SparkLine.component.d.ts +0 -18
  93. package/dist/components/sparkline/SparkLine.component.d.ts.map +0 -1
  94. package/dist/components/sparkline/SparkLine.component.js +0 -148
  95. package/dist/components/vegachart/VegaChart.component.d.ts +0 -13
  96. package/dist/components/vegachart/VegaChart.component.d.ts.map +0 -1
  97. package/dist/components/vegachart/VegaChart.component.js +0 -120
  98. package/plopfile.js +0 -38
  99. package/src/lib/components/areachart/AreaChart.component.tsx +0 -49
  100. package/src/lib/components/chips/Chips.component.tsx +0 -169
  101. package/src/lib/components/cloudprogressbar/CloudProgressBar.component.tsx +0 -105
  102. package/src/lib/components/collapsiblepanel/CollapsiblePanel.component.tsx +0 -77
  103. package/src/lib/components/linechart/LineChart.component.tsx +0 -152
  104. package/src/lib/components/multiselect/MultiSelect.component.tsx +0 -158
  105. package/src/lib/components/select/Select.component.tsx +0 -98
  106. package/src/lib/components/spacedbox/SpacedBox.ts +0 -116
  107. package/src/lib/components/sparkline/SparkLine.component.tsx +0 -176
  108. package/src/lib/components/vegachart/VegaChart.component.tsx +0 -146
  109. package/stories/areachart.stories.tsx +0 -120
  110. package/stories/chips.stories.tsx +0 -107
  111. package/stories/cloudprogressbar.stories.tsx +0 -93
  112. package/stories/collapsiblepanel.stories.tsx +0 -57
  113. package/stories/data/areachart.ts +0 -122
  114. package/stories/data/sparklinechart.ts +0 -164
  115. package/stories/linechart.stories.tsx +0 -319
  116. package/stories/multiselect.stories.tsx +0 -126
  117. package/stories/select.stories.tsx +0 -52
  118. package/stories/sparkline.stories.tsx +0 -85
  119. package/stories/vegachart.stories.tsx +0 -98
@@ -1,158 +0,0 @@
1
- import styled from 'styled-components';
2
- import { spacing } from '../../spacing';
3
- import { fontSize, fontWeight } from '../../style/theme';
4
- import { getThemePropSelector } from '../../utils';
5
- import { Icon } from '../icon/Icon.component';
6
- import { Button } from './../button/Button.component';
7
- import { Checkbox } from './../checkbox/Checkbox.component';
8
- import { Select } from './../select/Select.component';
9
-
10
- export type ItemProps = {
11
- selected?: boolean;
12
- label: string;
13
- description?: string;
14
- onSelect?: (arg0: any, arg1: any) => void;
15
- //on checkbox click
16
- onItemRemove?: (arg0: any, arg1: any) => void;
17
- };
18
- export type OptionProps = {
19
- label: string;
20
- value: string | number;
21
- };
22
- export type SearchProps = {
23
- placeholder?: string;
24
- options: Array<OptionProps>;
25
- //The options displayed in search suggestion
26
- onSelect: (arg0: any) => void;
27
- //on option click
28
- onAdd?: (arg0: any) => void;
29
- selectedOption: any;
30
- };
31
- type MultiSelectProps = {
32
- title?: string;
33
- items: Array<ItemProps>;
34
- search?: SearchProps;
35
- onItemRemove?: (arg0: any, arg1: any) => void; //on item remove button click
36
- };
37
- const MultiSelectContainer = styled.div`
38
- color: ${getThemePropSelector('textPrimary')};
39
- `;
40
- const MultiSelectTitle = styled.h3`
41
- padding: ${spacing.r16} 0;
42
- margin: 0;
43
- font-weight: ${fontWeight.bold};
44
- font-size: ${fontSize.large};
45
- `;
46
- const MultiSelectItemContainer = styled.div`
47
- margin: ${spacing.r4} 0;
48
- padding: ${spacing.r8} 0;
49
- display: flex;
50
- align-items: center;
51
- border-bottom: 1px solid gray;
52
- &:last-child {
53
- border: none;
54
- }
55
- `;
56
- const MultiSelectSearchContainer = styled.div`
57
- display: flex;
58
- align-items: center;
59
- .sc-select-container {
60
- width: 100%;
61
- }
62
- .sc-button {
63
- margin: 0 ${spacing.r8};
64
- }
65
- `;
66
- const MultiSelectItemLeft = styled.div`
67
- .sc-checkbox,
68
- .sc-button {
69
- margin: 0 ${spacing.r8};
70
- }
71
- `;
72
- const MultiSelectItemCenter = styled.div`
73
- display: flex;
74
- flex-grow: 1;
75
- align-items: center;
76
- justify-content: space-between;
77
- `;
78
- const MultiSelectItemRight = styled.div`
79
- padding: 0 ${spacing.r16};
80
- `;
81
- const MultiSelectItemLabel = styled.span`
82
- font-size: ${fontSize.large};
83
- `;
84
- const MultiSelectItemDescription = styled.span``;
85
-
86
- function MultiSelectItem(props: ItemProps) {
87
- const { selected, label, description, onItemRemove, onSelect } = props;
88
- return (
89
- <MultiSelectItemContainer className="sc-multi-select-item">
90
- <MultiSelectItemLeft className="sc-multi-select-item-left">
91
- {typeof selected === 'boolean' && onSelect && (
92
- <Checkbox
93
- checked={selected}
94
- onChange={(event) => onSelect(label, event)}
95
- />
96
- )}
97
- </MultiSelectItemLeft>
98
- <MultiSelectItemCenter className="sc-multi-select-item-center">
99
- <MultiSelectItemLabel className="sc-multi-select-item-label">
100
- {label}
101
- </MultiSelectItemLabel>
102
- {description && (
103
- <MultiSelectItemDescription className="sc-multi-select-item-description">
104
- {description}
105
- </MultiSelectItemDescription>
106
- )}
107
- </MultiSelectItemCenter>
108
- <MultiSelectItemRight className="sc-multi-select-item-right">
109
- {onItemRemove && (
110
- <Button
111
- inverted={true}
112
- variant="buttonDelete"
113
- onClick={(event) => onItemRemove(label, event)}
114
- icon={<Icon name="Delete" />}
115
- />
116
- )}
117
- </MultiSelectItemRight>
118
- </MultiSelectItemContainer>
119
- );
120
- }
121
-
122
- function MultiSelectSearch(props: SearchProps) {
123
- const { selectedOption, onSelect, onAdd, ...rest } = props;
124
- return (
125
- <MultiSelectSearchContainer className="sc-multi-select-list-search">
126
- <Select
127
- // @ts-ignore
128
- onChange={onSelect}
129
- value={selectedOption}
130
- {...rest}
131
- />
132
- {onAdd && <Button onClick={onAdd} icon={<Icon name="Create-add" />} />}
133
- </MultiSelectSearchContainer>
134
- );
135
- }
136
-
137
- function MultiSelectList({
138
- title = '',
139
- items = [],
140
- search,
141
- onItemRemove,
142
- }: MultiSelectProps) {
143
- return (
144
- <MultiSelectContainer className="sc-multi-select-list">
145
- {title && <MultiSelectTitle>{title}</MultiSelectTitle>}
146
- {search && <MultiSelectSearch {...search} />}
147
- {items.map((item, index) => (
148
- <MultiSelectItem
149
- key={`sc-multi-select-item-${index}`}
150
- onItemRemove={onItemRemove}
151
- {...item}
152
- />
153
- ))}
154
- </MultiSelectContainer>
155
- );
156
- }
157
-
158
- export const MultiSelect = MultiSelectList;
@@ -1,98 +0,0 @@
1
- import ReactSelect from 'react-select';
2
- import styled from 'styled-components';
3
-
4
- import { fontSize, fontWeight, gray, zIndex } from '../../style/theme';
5
- import { getThemePropSelector } from '../../utils';
6
- export type Item = {
7
- label: string;
8
- value: string | number;
9
- };
10
- type Items = Array<Item>;
11
- type Props = {
12
- id?: any;
13
- options: Items;
14
- formatOptionLabel?: (arg0: any) => JSX.Element;
15
- };
16
- const SelectContainer = styled.div`
17
- font-size: ${fontSize.base};
18
- .sc-select__control {
19
- background-color: ${getThemePropSelector('backgroundLevel1')};
20
- border-radius: 4px;
21
- border: 1px solid ${getThemePropSelector('border')};
22
- height: auto;
23
-
24
- .sc-select__placeholder,
25
- .sc-select__single-value {
26
- color: ${getThemePropSelector('textSecondary')};
27
- }
28
- &.sc-select__control--is-focused {
29
- border-color: ${getThemePropSelector('selectedActive')};
30
- box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
31
- 0 0 0 1px rgba(0, 126, 255, 0.1);
32
- outline: none;
33
- }
34
- .sc-select__indicator {
35
- color: ${getThemePropSelector('textSecondary')};
36
- &.sc-select__dropdown-indicator:hover {
37
- color: ${getThemePropSelector('selectedActive')};
38
- }
39
- &.sc-select__clear-indicator:hover {
40
- color: ${gray};
41
- }
42
- }
43
- .sc-select__multi-value__remove {
44
- border-radius: 0;
45
- color: ${getThemePropSelector('textSecondary')};
46
- background-color: ${getThemePropSelector('backgroundLevel4')};
47
- &:hover {
48
- color: ${gray};
49
- }
50
- }
51
- .sc-select__multi-value__label {
52
- border-radius: 0;
53
- color: ${getThemePropSelector('selectedActive')};
54
- background-color: ${getThemePropSelector('backgroundLevel4')};
55
- vertical-align: initial;
56
- }
57
- }
58
- .sc-select__menu {
59
- background-color: ${getThemePropSelector('backgroundLevel1')};
60
- color: ${getThemePropSelector('textSecondary')};
61
- border: 1px solid ${getThemePropSelector('selectedActive')};
62
- box-sizing: border-box;
63
- overflow: hidden;
64
- z-index: ${zIndex.dropdown};
65
- .sc-select__option {
66
- &.sc-select__option--is-focused {
67
- background-color: ${getThemePropSelector('backgroundLevel1')};
68
- }
69
- &.sc-select__option--is-selected {
70
- background-color: ${getThemePropSelector('backgroundLevel1')};
71
- color: ${getThemePropSelector('selectedActive')};
72
- font-weight: ${fontWeight.bold};
73
- }
74
- }
75
- }
76
- `;
77
-
78
- const defaultFormatOptionLabel = ({ label, ...rest }) => (
79
- <div className="sc-select-option-label" {...rest}>
80
- {label}
81
- </div>
82
- );
83
-
84
- function SelectBox({ options, formatOptionLabel, ...rest }: Props) {
85
- return (
86
- <SelectContainer className="sc-select-container">
87
- <ReactSelect
88
- className="sc-select"
89
- classNamePrefix="sc-select"
90
- options={options}
91
- formatOptionLabel={formatOptionLabel || defaultFormatOptionLabel}
92
- {...rest}
93
- />
94
- </SelectContainer>
95
- );
96
- }
97
-
98
- export const Select = SelectBox;
@@ -1,116 +0,0 @@
1
- import styled from 'styled-components';
2
- import { spacing } from '../../style/theme';
3
- type SpacingValue = 0 | 1 | 2 | 4 | 8 | 12 | 14 | 16 | 20 | 24 | 28 | 32;
4
- export type Props = {
5
- m?: SpacingValue;
6
- mt?: SpacingValue;
7
- mr?: SpacingValue;
8
- mb?: SpacingValue;
9
- ml?: SpacingValue;
10
- mx?: SpacingValue;
11
- my?: SpacingValue;
12
- p?: SpacingValue;
13
- pt?: SpacingValue;
14
- pr?: SpacingValue;
15
- pb?: SpacingValue;
16
- pl?: SpacingValue;
17
- px?: SpacingValue;
18
- py?: SpacingValue;
19
- };
20
- type PropsName =
21
- | 'm'
22
- | 'mt'
23
- | 'mr'
24
- | 'mb'
25
- | 'ml'
26
- | 'mx'
27
- | 'my'
28
- | 'p'
29
- | 'pt'
30
- | 'pr'
31
- | 'pb'
32
- | 'pl'
33
- | 'px'
34
- | 'py';
35
- type CSSField =
36
- | 'margin'
37
- | 'margin-top'
38
- | 'margin-right'
39
- | 'margin-bottom'
40
- | 'margin-left'
41
- | 'padding'
42
- | 'padding-top'
43
- | 'padding-right'
44
- | 'padding-bottom'
45
- | 'padding-left';
46
- const validValue = [0, 1, 2, 4, 8, 12, 14, 16, 20, 24, 28, 32];
47
-
48
- const createSpacingFn =
49
- (propsName: PropsName, spacingFields: Array<CSSField>) => (props: Props) => {
50
- if (!props[propsName] && props[propsName] !== 0) {
51
- return null;
52
- }
53
-
54
- //@ts-ignore -- this is ensured below
55
- let value: SpacingValue = props[propsName];
56
-
57
- if (!validValue.includes(value)) {
58
- console.warn(
59
- `'${propsName}' props should be a number in this list: ` +
60
- '0, 1, 2, 4, 8, 12, 14, 16, 20, 24, 28, 32',
61
- );
62
- return null;
63
- }
64
-
65
- const result = spacingFields
66
- .map((param: CSSField) => `${param}: ${spacing[`sp${value}`]}`)
67
- .join(';');
68
- return result;
69
- };
70
-
71
- const m = createSpacingFn('m', ['margin']);
72
- const mt = createSpacingFn('mt', ['margin-top']);
73
- const mr = createSpacingFn('mr', ['margin-right']);
74
- const mb = createSpacingFn('mb', ['margin-bottom']);
75
- const ml = createSpacingFn('ml', ['margin-left']);
76
- const mx = createSpacingFn('mx', ['margin-left', 'margin-right']);
77
- const my = createSpacingFn('my', ['margin-top', 'margin-bottom']);
78
- const p = createSpacingFn('p', ['padding']);
79
- const pt = createSpacingFn('pt', ['padding-top']);
80
- const pr = createSpacingFn('pr', ['padding-right']);
81
- const pb = createSpacingFn('pb', ['padding-bottom']);
82
- const pl = createSpacingFn('pl', ['padding-left']);
83
- const px = createSpacingFn('px', ['padding-left', 'padding-right']);
84
- const py = createSpacingFn('py', ['padding-top', 'padding-bottom']);
85
-
86
- /**
87
- * @deprecated
88
- * Please use Box with import { Box } from '@scality/core-ui/dist/next';
89
- *
90
- * Most common use cases:
91
- *
92
- * `<SpacedBox m={14}><Component /></SpacedBox>`
93
- *
94
- * `<SpacedBox as={SpanComponent}><Component /></SpacedBox>`
95
- *
96
- * Props : m, mt, mr, mb, ml, mx, my, p, pt, pr, pb, pl, px, py
97
- *
98
- * Values: 0, 1, 2, 4, 8, 12, 14, 16, 20, 24, 28, 32
99
- */
100
- const SpacedBox = styled.div`
101
- ${m};
102
- ${mt};
103
- ${mr};
104
- ${mb};
105
- ${ml};
106
- ${mx};
107
- ${my};
108
- ${p};
109
- ${pt};
110
- ${pr};
111
- ${pb};
112
- ${pl};
113
- ${px};
114
- ${py};
115
- `;
116
- export { SpacedBox };
@@ -1,176 +0,0 @@
1
- import { VegaChart } from '../vegachart/VegaChart.component';
2
- import { yellowOrange, green } from '../../style/theme';
3
- type Props = {
4
- id: string;
5
- title?: string;
6
- width?: number;
7
- height?: number;
8
- data: Array<Record<string, any>>;
9
- xAxis: Record<string, any>;
10
- yAxis: Record<string, any>;
11
- row: Record<string, any>;
12
- lineConfig?: Record<string, any>;
13
- sparkLineColor?: string;
14
- upTrendColor?: string;
15
- bottomTrendColor?: string;
16
- };
17
-
18
- function SparkLine({
19
- id,
20
- title,
21
- width = 300,
22
- height = 80,
23
- data,
24
- xAxis,
25
- yAxis,
26
- row,
27
- lineConfig,
28
- sparkLineColor,
29
- upTrendColor = yellowOrange,
30
- bottomTrendColor = green,
31
- }: Props) {
32
- const spec = {
33
- title: {
34
- text: title,
35
- },
36
- data: {
37
- values: data,
38
- },
39
- facet: {
40
- row,
41
- },
42
- spec: {
43
- width,
44
- height,
45
- layer: [
46
- // display the sparkline chart
47
- {
48
- mark: {
49
- type: 'line',
50
- ...lineConfig,
51
- },
52
- encoding: {
53
- x: xAxis,
54
- y: yAxis,
55
- color: {
56
- value: sparkLineColor,
57
- },
58
- },
59
- }, // display the label to specify the max/min data
60
- {
61
- mark: {
62
- type: 'text',
63
- style: 'labelMin',
64
- align: 'bottom',
65
- dy: height / 2,
66
- dx: width / 2 + 10,
67
- },
68
- encoding: {
69
- text: {
70
- aggregate: 'min',
71
- field: 'y',
72
- type: 'quantitative',
73
- },
74
- },
75
- },
76
- {
77
- mark: {
78
- type: 'text',
79
- style: 'labelMax',
80
- align: 'top',
81
- dy: -(height / 2),
82
- dx: width / 2 + 10,
83
- },
84
- encoding: {
85
- text: {
86
- aggregate: 'max',
87
- field: 'y',
88
- type: 'quantitative',
89
- },
90
- },
91
- }, // display the up and bottom trend line
92
- {
93
- mark: {
94
- type: 'rule',
95
- style: 'ruleMaxEnd',
96
- color: upTrendColor,
97
- },
98
- encoding: {
99
- y: {
100
- aggregate: 'max',
101
- field: 'y',
102
- type: 'quantitative',
103
- },
104
- x: {
105
- value: width - 15,
106
- },
107
- x2: {
108
- value: width,
109
- },
110
- },
111
- },
112
- {
113
- mark: {
114
- type: 'rule',
115
- style: 'ruleMaxStart',
116
- color: upTrendColor,
117
- opacity: 0.1,
118
- },
119
- encoding: {
120
- y: {
121
- aggregate: 'max',
122
- field: 'y',
123
- type: 'quantitative',
124
- },
125
- x: {
126
- value: 0,
127
- },
128
- x2: {
129
- value: width - 15,
130
- },
131
- },
132
- },
133
- {
134
- mark: {
135
- type: 'rule',
136
- style: 'ruleMinEnd',
137
- color: bottomTrendColor,
138
- },
139
- encoding: {
140
- y: {
141
- value: height,
142
- },
143
- x: {
144
- value: width - 15,
145
- },
146
- x2: {
147
- value: width,
148
- },
149
- },
150
- },
151
- {
152
- mark: {
153
- type: 'rule',
154
- style: 'ruleMinStart',
155
- color: bottomTrendColor,
156
- opacity: 0.1,
157
- },
158
- encoding: {
159
- y: {
160
- value: height,
161
- },
162
- x: {
163
- value: 0,
164
- },
165
- x2: {
166
- value: width - 15,
167
- },
168
- },
169
- },
170
- ],
171
- },
172
- };
173
- return <VegaChart className="sc-sparkline" id={id} spec={spec}></VegaChart>;
174
- }
175
-
176
- export const Sparkline = SparkLine;
@@ -1,146 +0,0 @@
1
- // @ts-nocheck
2
- import { useEffect, useContext, useRef } from 'react';
3
- import vegaEmbed, { Result } from 'vega-embed';
4
- import { ThemeContext, createGlobalStyle } from 'styled-components';
5
- import { getThemePropSelector } from '../../utils';
6
- export const TOP = 'top';
7
- export const BOTTOM = 'bottom';
8
- type Position = typeof TOP | typeof BOTTOM;
9
- type Props = {
10
- id: string;
11
- spec: Record<string, any>;
12
- tooltipPosition?: Position;
13
- theme?: 'light' | 'dark' | 'custom';
14
- className?: string;
15
- };
16
-
17
- /* How to theme tooltip:
18
- https://github.com/vega/vega-tooltip/blob/master/docs/customizing_your_tooltip.md
19
- */
20
- const VegaTooltipTheme = createGlobalStyle`
21
- #vg-tooltip-element.vg-tooltip.custom-theme {
22
- padding: 8px;
23
- position: fixed;
24
- z-index: 1000;
25
- font-family: 'Lato';
26
- font-size: 12px;
27
- border-radius: 3px;
28
- box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
29
- color: ${getThemePropSelector('textPrimary')};
30
- background-color: ${getThemePropSelector('backgroundLevel1')};
31
- border: 1px solid ${getThemePropSelector('border')};
32
- // customize the title
33
- h2 {
34
- color: ${getThemePropSelector('textSecondary')};
35
- margin-bottom: 10px;
36
- font-size: 12px;
37
- }
38
- }
39
- `;
40
-
41
- function VegaChart({
42
- id,
43
- spec,
44
- tooltipPosition = BOTTOM,
45
- theme = 'custom',
46
- }: Props) {
47
- const themeContext = useContext(ThemeContext);
48
- const currentBackgroundColor =
49
- themeContext && themeContext.brand
50
- ? themeContext.brand.backgroundLevel4
51
- : themeContext.backgroundLevel4;
52
- // the background color of the view
53
- const currentBackgroundColor2 =
54
- themeContext && themeContext.brand
55
- ? themeContext.brand.backgroundLevel1
56
- : themeContext.backgroundLevel1;
57
- const brandText =
58
- themeContext && themeContext.brand
59
- ? themeContext.brand.textPrimary
60
- : themeContext.textPrimary;
61
- const themeConfig = {
62
- config: {
63
- background: currentBackgroundColor,
64
- axis: {
65
- labelColor: brandText,
66
- titleColor: brandText,
67
- grid: false,
68
- domainColor: currentBackgroundColor2,
69
- },
70
- title: {
71
- color: brandText,
72
- font: 'Lato',
73
- },
74
- view: {
75
- stroke: 'transparent',
76
- fill: currentBackgroundColor2,
77
- },
78
- // the headers provide a title and labels for faceted plots.
79
- header: {
80
- labelColor: brandText,
81
- },
82
- // the label of max/min
83
- text: {
84
- color: brandText,
85
- font: 'Lato',
86
- },
87
- // the up, bottom trend line and verticle line when hover
88
- rule: {
89
- color: brandText,
90
- },
91
- legend: {
92
- labelColor: brandText,
93
- titleColor: brandText,
94
- },
95
- },
96
- };
97
- const themedSpec = { ...spec, ...themeConfig };
98
- const vegaInstance = useRef<Result>();
99
- let tooltipOptions = {
100
- theme: theme,
101
- };
102
-
103
- if (tooltipPosition === TOP) {
104
- tooltipOptions = {
105
- theme: theme,
106
- offsetX: -85,
107
- offsetY: -140,
108
- };
109
- }
110
-
111
- useEffect(() => {
112
- let isMounted = true;
113
- vegaEmbed(`#${id}`, themedSpec, {
114
- renderer: 'svg',
115
- // Override the DEFAULT_OPTIONS https://github.com/vega/vega-tooltip/blob/master/src/defaults.ts
116
- tooltip: tooltipOptions,
117
-
118
- /* Determines if action links
119
- ("Export as PNG/SVG", "View Source", "View Vega" (only for Vega-Lite), "Open in Vega Editor") are included with the embedded view.
120
- If the value is true, all action links will be shown and none if the value is false. */
121
- actions: false,
122
- })
123
- .then((result) => {
124
- vegaInstance.current = result; // result.view contains the Vega view
125
- })
126
- .catch((...args) => {
127
- if (isMounted) {
128
- console.error(...args); // TODO: we should handle this with a retry or an error state of the component
129
- }
130
- });
131
- return () => {
132
- isMounted = false;
133
-
134
- if (vegaInstance.current) {
135
- vegaInstance.current.view.finalize();
136
- }
137
- };
138
- }, [id, themedSpec, tooltipOptions, vegaInstance]);
139
- return (
140
- <div id={id} className="sc-vegachart">
141
- <VegaTooltipTheme />
142
- </div>
143
- );
144
- }
145
-
146
- export { VegaChart };