@pie-lib/plot 2.27.3-next.2 → 2.27.3-next.205

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.
package/src/root.jsx CHANGED
@@ -1,16 +1,100 @@
1
1
  import React from 'react';
2
- import { withStyles } from '@material-ui/core/styles';
2
+ import { styled } from '@mui/material/styles';
3
3
  import PropTypes from 'prop-types';
4
4
  import { select, mouse } from 'd3-selection';
5
- import cn from 'classnames';
6
5
 
7
6
  import { color, Readable } from '@pie-lib/render-ui';
8
- import EditableHtml from '@pie-lib/editable-html';
7
+ import EditableHtml from '@pie-lib/editable-html-tip-tap';
9
8
  import { ChildrenType } from './types';
10
9
  import { GraphPropsType } from './types';
11
10
  import Label from './label';
12
11
  import { extractTextFromHTML, isEmptyObject, isEmptyString } from './utils';
13
12
 
13
+ const StyledRoot = styled('div')(({ theme }) => ({
14
+ border: `solid 1px ${color.primaryLight()}`,
15
+ color: color.defaults.TEXT,
16
+ backgroundColor: theme.palette.common.white,
17
+ touchAction: 'none',
18
+ position: 'relative',
19
+ boxSizing: 'unset', // to override the default border-box in IBX that breaks the component width layout
20
+ }));
21
+
22
+ const Wrapper = styled('div')({
23
+ display: 'flex',
24
+ position: 'relative',
25
+ });
26
+
27
+ const DefineChartSvg = styled('svg')({
28
+ paddingLeft: '50px',
29
+ overflow: 'visible',
30
+ });
31
+
32
+ const ChartSvg = styled('svg')({
33
+ overflow: 'visible',
34
+ });
35
+
36
+ const GraphBox = styled('g')({
37
+ cursor: 'pointer',
38
+ userSelect: 'none',
39
+ });
40
+
41
+ const GraphTitle = styled('div')(({ theme }) => ({
42
+ color: color.defaults.TEXT,
43
+ fontSize: theme.typography.fontSize + 2,
44
+ padding: `${theme.spacing(1.5)} ${theme.spacing(0.5)} 0`,
45
+ textAlign: 'center',
46
+ '&.disabled': {
47
+ pointerEvents: 'none',
48
+ },
49
+ '&.rightMargin': {
50
+ marginRight: '74px',
51
+ },
52
+ }));
53
+
54
+ const ChartTitle = styled('div')(({ theme }) => ({
55
+ color: color.defaults.TEXT,
56
+ fontSize: theme.typography.fontSize + 4,
57
+ padding: `${theme.spacing(1.5)} ${theme.spacing(0.5)} 0`,
58
+ textAlign: 'center',
59
+ '&.disabled': {
60
+ pointerEvents: 'none',
61
+ },
62
+ '&.rightMargin': {
63
+ marginRight: '74px',
64
+ },
65
+ }));
66
+
67
+ const TopPixelGuides = styled('div')({
68
+ display: 'flex',
69
+ paddingTop: '6px',
70
+ });
71
+
72
+ const TopPixelIndicator = styled('div')({
73
+ display: 'flex',
74
+ flexDirection: 'column',
75
+ alignItems: 'center',
76
+ width: '100px',
77
+ pointerEvents: 'none',
78
+ userSelect: 'none',
79
+ });
80
+
81
+ const SidePixelGuides = styled('div')({
82
+ width: '70px',
83
+ display: 'flex',
84
+ flexDirection: 'column',
85
+ marginRight: '6px',
86
+ });
87
+
88
+ const SidePixelIndicator = styled('div')({
89
+ textAlign: 'right',
90
+ height: '20px',
91
+ pointerEvents: 'none',
92
+ userSelect: 'none',
93
+ '&:not(:last-child)': {
94
+ marginBottom: '80px',
95
+ },
96
+ });
97
+
14
98
  export class Root extends React.Component {
15
99
  constructor(props) {
16
100
  super(props);
@@ -32,7 +116,6 @@ export class Root extends React.Component {
32
116
  labelsPlaceholders: PropTypes.object,
33
117
  onChangeTitle: PropTypes.func,
34
118
  onMouseMove: PropTypes.func,
35
- classes: PropTypes.object.isRequired,
36
119
  showLabels: PropTypes.bool,
37
120
  showTitle: PropTypes.bool,
38
121
  showPixelGuides: PropTypes.bool,
@@ -51,7 +134,7 @@ export class Root extends React.Component {
51
134
  }
52
135
 
53
136
  const { scale, snap } = graphProps;
54
- const coords = mouse(g._groups[0][0]);
137
+ const coords = mouse(g.node());
55
138
  const x = scale.x.invert(coords[0]);
56
139
  const y = scale.y.invert(coords[1]);
57
140
 
@@ -159,7 +242,6 @@ export class Root extends React.Component {
159
242
  titlePlaceholder,
160
243
  graphProps,
161
244
  children,
162
- classes,
163
245
  defineChart,
164
246
  onChangeTitle,
165
247
  isChart,
@@ -200,19 +282,20 @@ export class Root extends React.Component {
200
282
  const nbOfHorizontalLines = parseInt(actualHeight / 100);
201
283
  const sideGridlinesPadding = parseInt(actualHeight % 100);
202
284
  const { titleHeight } = this.state;
285
+
203
286
  return (
204
- <div className={classes.root}>
287
+ <StyledRoot>
205
288
  {showPixelGuides && (
206
- <div className={classes.topPixelGuides} style={{ marginLeft: isChart ? 80 : showLabels ? 30 : 10 }}>
289
+ <TopPixelGuides style={{ marginLeft: isChart ? 80 : showLabels ? 30 : 10 }}>
207
290
  {[...Array(nbOfVerticalLines + 1).keys()].map((value) => (
208
291
  <Readable false key={`top-guide-${value}`}>
209
- <div className={classes.topPixelIndicator}>
292
+ <TopPixelIndicator>
210
293
  <div>{value * 100}px</div>
211
294
  <div>|</div>
212
- </div>
295
+ </TopPixelIndicator>
213
296
  </Readable>
214
297
  ))}
215
- </div>
298
+ </TopPixelGuides>
216
299
  )}
217
300
  {showTitle &&
218
301
  (disabledTitle ? (
@@ -222,31 +305,56 @@ export class Root extends React.Component {
222
305
  ...(isChart && { width: finalWidth }),
223
306
  ...(isEmptyString(extractTextFromHTML(title)) && { display: 'none' }),
224
307
  }}
225
- className={cn(isChart ? classes.chartTitle : classes.graphTitle, classes.disabledTitle)}
226
- dangerouslySetInnerHTML={{ __html: title || '' }}
227
- />
308
+ >
309
+ {isChart ? (
310
+ <ChartTitle className="disabled" dangerouslySetInnerHTML={{ __html: title || '' }} />
311
+ ) : (
312
+ <GraphTitle className="disabled" dangerouslySetInnerHTML={{ __html: title || '' }} />
313
+ )}
314
+ </div>
228
315
  ) : (
229
316
  <div ref={(r) => (this.titleRef = r)}>
230
- <EditableHtml
231
- style={
232
- isChart && {
233
- width: finalWidth,
234
- }
235
- }
236
- className={cn(
237
- { [classes.rightMargin]: showPixelGuides },
238
- isChart ? classes.chartTitle : classes.graphTitle,
239
- )}
240
- markup={title || ''}
241
- onChange={onChangeTitle}
242
- placeholder={
243
- (defineChart && titlePlaceholder) || (!disabledTitle && 'Click here to add a title for this graph')
244
- }
245
- toolbarOpts={{ noPadding: true, noBorder: true }}
246
- activePlugins={activeTitlePlugins}
247
- disableScrollbar
248
- onKeyDown={this.handleKeyDown}
249
- />
317
+ {isChart ? (
318
+ <ChartTitle className={showPixelGuides ? 'rightMargin' : ''}>
319
+ <EditableHtml
320
+ style={
321
+ isChart && {
322
+ width: finalWidth,
323
+ }
324
+ }
325
+ markup={title || ''}
326
+ onChange={onChangeTitle}
327
+ placeholder={
328
+ (defineChart && titlePlaceholder) ||
329
+ (!disabledTitle && 'Click here to add a title for this graph')
330
+ }
331
+ toolbarOpts={{ noPadding: true, noBorder: true }}
332
+ activePlugins={activeTitlePlugins}
333
+ disableScrollbar
334
+ onKeyDown={this.handleKeyDown}
335
+ />
336
+ </ChartTitle>
337
+ ) : (
338
+ <GraphTitle className={showPixelGuides ? 'rightMargin' : ''}>
339
+ <EditableHtml
340
+ style={
341
+ isChart && {
342
+ width: finalWidth,
343
+ }
344
+ }
345
+ markup={title || ''}
346
+ onChange={onChangeTitle}
347
+ placeholder={
348
+ (defineChart && titlePlaceholder) ||
349
+ (!disabledTitle && 'Click here to add a title for this graph')
350
+ }
351
+ toolbarOpts={{ noPadding: true, noBorder: true }}
352
+ activePlugins={activeTitlePlugins}
353
+ disableScrollbar
354
+ onKeyDown={this.handleKeyDown}
355
+ />
356
+ </GraphTitle>
357
+ )}
250
358
  </div>
251
359
  ))}
252
360
  {showLabels && !isChart && (
@@ -262,7 +370,7 @@ export class Root extends React.Component {
262
370
  charactersLimit={labelsCharactersLimit}
263
371
  />
264
372
  )}
265
- <div className={classes.wrapper}>
373
+ <Wrapper>
266
374
  {showLabels && (
267
375
  <Label
268
376
  side="left"
@@ -278,20 +386,35 @@ export class Root extends React.Component {
278
386
  charactersLimit={labelsCharactersLimit}
279
387
  />
280
388
  )}
281
- <svg width={finalWidth} height={finalHeight} className={defineChart ? classes.defineChart : classes.chart}>
282
- <g
283
- ref={(r) => {
284
- this.g = r;
285
- if (rootRef) {
286
- rootRef(r);
287
- }
288
- }}
289
- className={classes.graphBox}
290
- transform={`translate(${leftPadding + (domain.padding || 0)}, ${topPadding + (range.padding || 0)})`}
291
- >
292
- {children}
293
- </g>
294
- </svg>
389
+ {defineChart ? (
390
+ <DefineChartSvg width={finalWidth} height={finalHeight}>
391
+ <GraphBox
392
+ ref={(r) => {
393
+ this.g = r;
394
+ if (rootRef) {
395
+ rootRef(r);
396
+ }
397
+ }}
398
+ transform={`translate(${leftPadding + (domain.padding || 0)}, ${topPadding + (range.padding || 0)})`}
399
+ >
400
+ {children}
401
+ </GraphBox>
402
+ </DefineChartSvg>
403
+ ) : (
404
+ <ChartSvg width={finalWidth} height={finalHeight}>
405
+ <GraphBox
406
+ ref={(r) => {
407
+ this.g = r;
408
+ if (rootRef) {
409
+ rootRef(r);
410
+ }
411
+ }}
412
+ transform={`translate(${leftPadding + (domain.padding || 0)}, ${topPadding + (range.padding || 0)})`}
413
+ >
414
+ {children}
415
+ </GraphBox>
416
+ </ChartSvg>
417
+ )}
295
418
  {showLabels && !isChart && (
296
419
  <Label
297
420
  side="right"
@@ -306,8 +429,7 @@ export class Root extends React.Component {
306
429
  />
307
430
  )}
308
431
  {showPixelGuides && (
309
- <div
310
- className={classes.sidePixelGuides}
432
+ <SidePixelGuides
311
433
  style={{
312
434
  paddingTop: sideGridlinesPadding,
313
435
  marginTop: 31,
@@ -315,12 +437,12 @@ export class Root extends React.Component {
315
437
  >
316
438
  {[...Array(nbOfHorizontalLines + 1).keys()].reverse().map((value) => (
317
439
  <Readable false key={`top-guide-${value}`}>
318
- <div className={classes.sidePixelIndicator}>━ {value * 100}px</div>
440
+ <SidePixelIndicator>━ {value * 100}px</SidePixelIndicator>
319
441
  </Readable>
320
442
  ))}
321
- </div>
443
+ </SidePixelGuides>
322
444
  )}
323
- </div>
445
+ </Wrapper>
324
446
  {showLabels && (
325
447
  <Label
326
448
  side="bottom"
@@ -337,83 +459,9 @@ export class Root extends React.Component {
337
459
  charactersLimit={labelsCharactersLimit}
338
460
  />
339
461
  )}
340
- </div>
462
+ </StyledRoot>
341
463
  );
342
464
  }
343
465
  }
344
466
 
345
- // use default color theme style to avoid color contrast issues
346
- const styles = (theme) => ({
347
- root: {
348
- border: `solid 1px ${color.primaryLight()}`,
349
- color: color.defaults.TEXT,
350
- backgroundColor: theme.palette.common.white,
351
- touchAction: 'none',
352
- position: 'relative',
353
- boxSizing: 'unset', // to override the default border-box in IBX that breaks the component width layout
354
- },
355
- wrapper: {
356
- display: 'flex',
357
- position: 'relative',
358
- },
359
- svg: {},
360
- defineChart: {
361
- paddingLeft: '50px',
362
- overflow: 'visible',
363
- },
364
- chart: {
365
- overflow: 'visible',
366
- },
367
- graphBox: {
368
- cursor: 'pointer',
369
- userSelect: 'none',
370
- },
371
- graphTitle: {
372
- color: color.defaults.TEXT,
373
- fontSize: theme.typography.fontSize + 2,
374
- padding: `${theme.spacing.unit * 1.5}px ${theme.spacing.unit / 2}px 0`,
375
- textAlign: 'center',
376
- },
377
- chartTitle: {
378
- color: color.defaults.TEXT,
379
- fontSize: theme.typography.fontSize + 4,
380
- padding: `${theme.spacing.unit * 1.5}px ${theme.spacing.unit / 2}px 0`,
381
- textAlign: 'center',
382
- },
383
- disabledTitle: {
384
- pointerEvents: 'none',
385
- },
386
- rightMargin: {
387
- marginRight: '74px',
388
- },
389
- topPixelGuides: {
390
- display: 'flex',
391
- paddingTop: '6px',
392
- },
393
- topPixelIndicator: {
394
- display: 'flex',
395
- flexDirection: 'column',
396
- alignItems: 'center',
397
- width: '100px',
398
- pointerEvents: 'none',
399
- userSelect: 'none',
400
- },
401
- sidePixelGuides: {
402
- width: '70px',
403
- display: 'flex',
404
- flexDirection: 'column',
405
- marginRight: '6px',
406
- },
407
- sidePixelIndicator: {
408
- textAlign: 'right',
409
- height: '20px',
410
- pointerEvents: 'none',
411
- userSelect: 'none',
412
-
413
- '&:not(:last-child)': {
414
- marginBottom: '80px',
415
- },
416
- },
417
- });
418
-
419
- export default withStyles(styles)(Root);
467
+ export default Root;