@cashub/ui-elv 1.0.1 → 1.0.2

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/chart/BarChart.js CHANGED
@@ -8,8 +8,10 @@ var _react = require("react");
8
8
  var _styledComponents = require("styled-components");
9
9
  var _reactChartjs = require("react-chartjs-2");
10
10
  var _customTooltip = _interopRequireDefault(require("./utils/customTooltip"));
11
+ var _customTooltipLifecyclePlugin = _interopRequireDefault(require("./utils/customTooltipLifecyclePlugin"));
11
12
  var _padEmptyChartBar = _interopRequireDefault(require("./utils/padEmptyChartBar"));
12
13
  var _yAxisTopTitlePlugin = _interopRequireDefault(require("./utils/yAxisTopTitlePlugin"));
14
+ var _xAxisLabelTooltipPlugin = _interopRequireDefault(require("./utils/xAxisLabelTooltipPlugin"));
13
15
  var _jsxRuntime = require("react/jsx-runtime");
14
16
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
17
  const BarChart = _ref => {
@@ -39,7 +41,8 @@ const BarChart = _ref => {
39
41
  showLegend: options.showLegend === undefined ? false : options.showLegend,
40
42
  rotateLabel: options.rotateLabel === undefined ? true : options.rotateLabel,
41
43
  yAxisTopTitle: yAxisTopTitle ? yAxisTopTitle : '',
42
- tooltipLabelPostfix: tooltips.labelPostfix || ''
44
+ tooltipLabelPostfix: tooltips.labelPostfix || '',
45
+ showFullLabelOnHover: options.showFullLabelOnHover === undefined ? false : options.showFullLabelOnHover
43
46
  };
44
47
  }, [options]);
45
48
  const defaultOptions = (0, _react.useMemo)(() => {
@@ -96,6 +99,9 @@ const BarChart = _ref => {
96
99
  },
97
100
  yAxisTopTitle: {
98
101
  title: customOptions.yAxisTopTitle
102
+ },
103
+ xAxisLabelTooltip: {
104
+ enabled: customOptions.showFullLabelOnHover
99
105
  }
100
106
  },
101
107
  scales: {
@@ -141,7 +147,7 @@ const BarChart = _ref => {
141
147
  height: height,
142
148
  data: data,
143
149
  options: defaultOptions,
144
- plugins: [_padEmptyChartBar.default, _yAxisTopTitlePlugin.default],
150
+ plugins: [_padEmptyChartBar.default, _yAxisTopTitlePlugin.default, _xAxisLabelTooltipPlugin.default, _customTooltipLifecyclePlugin.default],
145
151
  ...props
146
152
  });
147
153
  };
@@ -10,6 +10,7 @@ var _reactChartjs = require("react-chartjs-2");
10
10
  var _lodash = _interopRequireDefault(require("lodash.uniqueid"));
11
11
  var _htmlLegendPlugin = _interopRequireDefault(require("./utils/htmlLegendPlugin"));
12
12
  var _customTooltip = _interopRequireDefault(require("./utils/customTooltip"));
13
+ var _customTooltipLifecyclePlugin = _interopRequireDefault(require("./utils/customTooltipLifecyclePlugin"));
13
14
  var _centerTextPlugin = _interopRequireDefault(require("./utils/centerTextPlugin"));
14
15
  var _jsxRuntime = require("react/jsx-runtime");
15
16
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -83,7 +84,7 @@ const DoughnutChart = _ref => {
83
84
  height: height,
84
85
  data: data,
85
86
  options: defaultOptions,
86
- plugins: [_htmlLegendPlugin.default, _centerTextPlugin.default],
87
+ plugins: [_htmlLegendPlugin.default, _centerTextPlugin.default, _customTooltipLifecyclePlugin.default],
87
88
  ...props
88
89
  })
89
90
  }), /*#__PURE__*/(0, _jsxRuntime.jsx)(LegendWrapper, {
@@ -8,6 +8,7 @@ var _react = require("react");
8
8
  var _styledComponents = require("styled-components");
9
9
  var _reactChartjs = require("react-chartjs-2");
10
10
  var _customTooltip = _interopRequireDefault(require("./utils/customTooltip"));
11
+ var _customTooltipLifecyclePlugin = _interopRequireDefault(require("./utils/customTooltipLifecyclePlugin"));
11
12
  var _yAxisTopTitlePlugin = _interopRequireDefault(require("./utils/yAxisTopTitlePlugin"));
12
13
  var _jsxRuntime = require("react/jsx-runtime");
13
14
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -139,7 +140,7 @@ const LineChart = _ref => {
139
140
  height: height,
140
141
  data: data,
141
142
  options: defaultOptions,
142
- plugins: [_yAxisTopTitlePlugin.default],
143
+ plugins: [_yAxisTopTitlePlugin.default, _customTooltipLifecyclePlugin.default],
143
144
  ...props
144
145
  });
145
146
  };
@@ -4,18 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _tooltipElement = require("./tooltipElement");
7
8
  const customTooltip = theme => {
8
9
  // optimize: render in react way
9
10
  return function (context) {
10
11
  // Tooltip Element
11
- let tooltipEl = document.getElementById('chartjs-tooltip');
12
-
13
- // Create element on first render
14
- if (!tooltipEl) {
15
- tooltipEl = document.createElement('div');
16
- tooltipEl.id = 'chartjs-tooltip';
12
+ const tooltipEl = (0, _tooltipElement.getOrCreateTooltipEl)(_tooltipElement.CUSTOM_TOOLTIP_ID);
13
+ if (!tooltipEl.querySelector('table')) {
17
14
  tooltipEl.innerHTML = '<table></table>';
18
- document.body.appendChild(tooltipEl);
19
15
  }
20
16
 
21
17
  // Hide tooltip, if no tooltip or no data
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _tooltipElement = require("./tooltipElement");
8
+ // customTooltip.js renders #chartjs-tooltip outside React's tree (appended to
9
+ // document.body), so it needs an explicit hook to hide it when the chart's
10
+ // container resizes or the chart instance is destroyed - otherwise the last
11
+ // rendered tooltip is left floating, detached from the (moved/gone) chart.
12
+ const customTooltipLifecyclePlugin = {
13
+ id: 'customTooltipLifecycle',
14
+ resize() {
15
+ (0, _tooltipElement.hideTooltipEl)(_tooltipElement.CUSTOM_TOOLTIP_ID);
16
+ },
17
+ afterDestroy() {
18
+ (0, _tooltipElement.hideTooltipEl)(_tooltipElement.CUSTOM_TOOLTIP_ID);
19
+ }
20
+ };
21
+ var _default = exports.default = customTooltipLifecyclePlugin;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.hideTooltipEl = exports.getOrCreateTooltipEl = exports.CUSTOM_TOOLTIP_ID = exports.AXIS_TOOLTIP_ID = void 0;
7
+ const CUSTOM_TOOLTIP_ID = exports.CUSTOM_TOOLTIP_ID = 'chartjs-tooltip';
8
+ const AXIS_TOOLTIP_ID = exports.AXIS_TOOLTIP_ID = 'chartjs-axis-tooltip';
9
+ const getOrCreateTooltipEl = id => {
10
+ let tooltipEl = document.getElementById(id);
11
+ if (!tooltipEl) {
12
+ tooltipEl = document.createElement('div');
13
+ tooltipEl.id = id;
14
+ document.body.appendChild(tooltipEl);
15
+ }
16
+ return tooltipEl;
17
+ };
18
+ exports.getOrCreateTooltipEl = getOrCreateTooltipEl;
19
+ const hideTooltipEl = id => {
20
+ const tooltipEl = document.getElementById(id);
21
+ if (tooltipEl) {
22
+ tooltipEl.style.display = 'none';
23
+ }
24
+ };
25
+ exports.hideTooltipEl = hideTooltipEl;
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _tooltipElement = require("./tooltipElement");
8
+ const hideTooltip = () => (0, _tooltipElement.hideTooltipEl)(_tooltipElement.AXIS_TOOLTIP_ID);
9
+ const getTooltipEl = () => {
10
+ const tooltipEl = (0, _tooltipElement.getOrCreateTooltipEl)(_tooltipElement.AXIS_TOOLTIP_ID);
11
+ tooltipEl.style.position = 'absolute';
12
+ tooltipEl.style.pointerEvents = 'none';
13
+ tooltipEl.style.zIndex = 80;
14
+ tooltipEl.style.maxWidth = '50vw';
15
+ tooltipEl.style.wordBreak = 'break-word';
16
+ tooltipEl.style.whiteSpace = 'nowrap';
17
+ tooltipEl.style.textAlign = 'left';
18
+ tooltipEl.style.border = '1px solid var(--border-color)';
19
+ tooltipEl.style.color = 'var(--font-on-background)';
20
+ tooltipEl.style.background = 'var(--color-background2)';
21
+ tooltipEl.style.boxShadow = 'var(--box-shadow)';
22
+ tooltipEl.style.borderRadius = 'calc(var(--border-radius) / 2)';
23
+ tooltipEl.style.padding = 'var(--spacing-xs)';
24
+ tooltipEl.style.fontSize = 'var(--font-body1)';
25
+ return tooltipEl;
26
+ };
27
+
28
+ // approximate hit-box per rendered tick: stable public getPixelForTick() + chartArea,
29
+ // not Chart.js's private _labelItems/_computeLabelItems (undocumented, version-fragile).
30
+ //
31
+ // Ticks are matched via xScale.ticks (the array Chart.js actually renders) rather than
32
+ // chart.data.labels directly, because ticks.autoSkip (on by default) drops entries when
33
+ // labels don't all fit - exactly the case this feature targets. Each rendered tick's
34
+ // `.value` is the original index into chart.data.labels, which getPixelForTick(i) does
35
+ // NOT return (it returns a pixel, and its `i` argument is a position in the possibly-
36
+ // skipped ticks array, not a label index) - so the original label index must be read
37
+ // back off ticks[i].value, not off the loop counter.
38
+ const findHoveredTickIndex = (chart, x, y) => {
39
+ const xScale = chart.scales.x;
40
+ if (!xScale) {
41
+ return -1;
42
+ }
43
+ const {
44
+ ticks
45
+ } = xScale;
46
+ if (!ticks || ticks.length === 0) {
47
+ return -1;
48
+ }
49
+ const {
50
+ chartArea
51
+ } = chart;
52
+ if (y < chartArea.bottom || y > xScale.bottom || x < chartArea.left || x > chartArea.right) {
53
+ return -1;
54
+ }
55
+ const halfStep = ticks.length > 1 ? Math.abs(xScale.getPixelForTick(1) - xScale.getPixelForTick(0)) / 2 : (xScale.right - xScale.left) / 2;
56
+ for (let i = 0; i < ticks.length; i += 1) {
57
+ const tickPixel = xScale.getPixelForTick(i);
58
+ if (tickPixel !== null && Math.abs(x - tickPixel) <= halfStep) {
59
+ return ticks[i].value;
60
+ }
61
+ }
62
+ return -1;
63
+ };
64
+ const xAxisLabelTooltipPlugin = {
65
+ id: 'xAxisLabelTooltip',
66
+ afterEvent(chart, args) {
67
+ const config = chart.config.options.plugins.xAxisLabelTooltip;
68
+ if (!config || !config.enabled) {
69
+ hideTooltip();
70
+ return;
71
+ }
72
+ const {
73
+ event
74
+ } = args;
75
+ if (event.type === 'mouseout') {
76
+ hideTooltip();
77
+ return;
78
+ }
79
+ if (event.type !== 'mousemove') {
80
+ return;
81
+ }
82
+ const index = findHoveredTickIndex(chart, event.x, event.y);
83
+ const label = index === -1 ? undefined : chart.data.labels[index];
84
+ if (typeof label !== 'string' || !label) {
85
+ hideTooltip();
86
+ return;
87
+ }
88
+ const tooltipEl = getTooltipEl();
89
+ tooltipEl.textContent = label;
90
+ tooltipEl.style.display = 'block';
91
+ const canvasRect = chart.canvas.getBoundingClientRect();
92
+ // index here is the original label index (see findHoveredTickIndex), so the pixel
93
+ // must come from getPixelForValue(index), not getPixelForTick(index) - the latter
94
+ // expects a position in the (possibly autoSkip-reduced) ticks array, not a label index.
95
+ const tickX = chart.scales.x.getPixelForValue(index);
96
+ tooltipEl.style.transform = 'translateX(-50%)';
97
+ tooltipEl.style.left = `${canvasRect.left + window.pageXOffset + tickX}px`;
98
+ tooltipEl.style.top = `${canvasRect.top + window.pageYOffset + chart.chartArea.bottom + 8}px`;
99
+ },
100
+ resize() {
101
+ hideTooltip();
102
+ },
103
+ afterDestroy() {
104
+ hideTooltip();
105
+ }
106
+ };
107
+ var _default = exports.default = xAxisLabelTooltipPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cashub/ui-elv",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "private": false,
5
5
  "author": "CasHUB Team",
6
6
  "description": "CasHUB UI components library (Elavon)",