@neo4j-ndl/react-charts 2.1.22 → 2.2.1

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 (209) hide show
  1. package/lib/LICENSES.txt +0 -295
  2. package/lib/NOTICE.txt +1383 -1392
  3. package/lib/{esm/charts/index.js → cjs/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/defineProperty.js} +13 -5
  4. package/lib/cjs/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/objectSpread2.js +46 -0
  5. package/lib/{esm/charts/tests/ChartWrapperWithTooltipFormatter.js → cjs/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/objectWithoutProperties.js} +12 -13
  6. package/lib/{esm/index.js → cjs/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/objectWithoutPropertiesLoose.js} +13 -2
  7. package/lib/cjs/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/toPrimitive.js +35 -0
  8. package/lib/{esm/charts/utils/chart-types.js → cjs/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/toPropertyKey.js} +10 -2
  9. package/lib/cjs/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/typeof.js +37 -0
  10. package/lib/cjs/_virtual/_rolldown/runtime.js +44 -0
  11. package/lib/cjs/charts/Chart.js +121 -111
  12. package/lib/cjs/charts/Chart.js.map +1 -1
  13. package/lib/cjs/charts/ChartContainer.js +67 -62
  14. package/lib/cjs/charts/ChartContainer.js.map +1 -1
  15. package/lib/cjs/charts/ChartEmpty.js +45 -30
  16. package/lib/cjs/charts/ChartEmpty.js.map +1 -1
  17. package/lib/cjs/charts/ChartRender.js +112 -87
  18. package/lib/cjs/charts/ChartRender.js.map +1 -1
  19. package/lib/cjs/charts/ChartTooltip.js +111 -65
  20. package/lib/cjs/charts/ChartTooltip.js.map +1 -1
  21. package/lib/cjs/charts/Legend.js +81 -69
  22. package/lib/cjs/charts/Legend.js.map +1 -1
  23. package/lib/cjs/charts/LegendItem.js +83 -62
  24. package/lib/cjs/charts/LegendItem.js.map +1 -1
  25. package/lib/cjs/charts/LegendLayout.js +330 -348
  26. package/lib/cjs/charts/LegendLayout.js.map +1 -1
  27. package/lib/cjs/charts/hooks/use-chart-instance.js +115 -122
  28. package/lib/cjs/charts/hooks/use-chart-instance.js.map +1 -1
  29. package/lib/cjs/charts/hooks/use-chart-option.js +79 -80
  30. package/lib/cjs/charts/hooks/use-chart-option.js.map +1 -1
  31. package/lib/cjs/charts/hooks/use-chart-refs.js +47 -44
  32. package/lib/cjs/charts/hooks/use-chart-refs.js.map +1 -1
  33. package/lib/cjs/charts/hooks/use-chart-zoom.js +102 -127
  34. package/lib/cjs/charts/hooks/use-chart-zoom.js.map +1 -1
  35. package/lib/cjs/charts/hooks/use-legend-interactions.js +127 -132
  36. package/lib/cjs/charts/hooks/use-legend-interactions.js.map +1 -1
  37. package/lib/cjs/charts/hooks/use-legend-series.js +143 -174
  38. package/lib/cjs/charts/hooks/use-legend-series.js.map +1 -1
  39. package/lib/cjs/charts/hooks/use-legend-visibility.js +66 -85
  40. package/lib/cjs/charts/hooks/use-legend-visibility.js.map +1 -1
  41. package/lib/cjs/charts/index.js +31 -40
  42. package/lib/cjs/charts/index.js.map +1 -1
  43. package/lib/cjs/charts/themes/ndl-echarts-theme.js +136 -168
  44. package/lib/cjs/charts/themes/ndl-echarts-theme.js.map +1 -1
  45. package/lib/cjs/charts/utils/aria-description.js +150 -214
  46. package/lib/cjs/charts/utils/aria-description.js.map +1 -1
  47. package/lib/cjs/charts/utils/build-chart-option.js +166 -174
  48. package/lib/cjs/charts/utils/build-chart-option.js.map +1 -1
  49. package/lib/cjs/charts/utils/chart-tooltip-formatter.js +156 -172
  50. package/lib/cjs/charts/utils/chart-tooltip-formatter.js.map +1 -1
  51. package/lib/cjs/charts/utils/chart-types.js +25 -20
  52. package/lib/cjs/charts/utils/chart-types.js.map +1 -1
  53. package/lib/cjs/charts/utils/defaults.js +69 -67
  54. package/lib/cjs/charts/utils/defaults.js.map +1 -1
  55. package/lib/cjs/charts/utils/format-utils.js +55 -66
  56. package/lib/cjs/charts/utils/format-utils.js.map +1 -1
  57. package/lib/cjs/charts/utils/legend-layout.js +54 -53
  58. package/lib/cjs/charts/utils/legend-layout.js.map +1 -1
  59. package/lib/cjs/charts/utils/legend-utils.js +82 -121
  60. package/lib/cjs/charts/utils/legend-utils.js.map +1 -1
  61. package/lib/cjs/charts/utils/scatter-tooltip-points.js +191 -266
  62. package/lib/cjs/charts/utils/scatter-tooltip-points.js.map +1 -1
  63. package/lib/cjs/charts/utils/threshold.js +89 -102
  64. package/lib/cjs/charts/utils/threshold.js.map +1 -1
  65. package/lib/cjs/charts/utils/user-option-utils.js +419 -347
  66. package/lib/cjs/charts/utils/user-option-utils.js.map +1 -1
  67. package/lib/cjs/index.js +31 -35
  68. package/lib/cjs/index.js.map +1 -1
  69. package/lib/esm/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/defineProperty.mjs +11 -0
  70. package/lib/esm/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/objectSpread2.mjs +24 -0
  71. package/lib/esm/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/objectWithoutProperties.mjs +12 -0
  72. package/lib/esm/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/objectWithoutPropertiesLoose.mjs +11 -0
  73. package/lib/esm/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/toPrimitive.mjs +13 -0
  74. package/lib/esm/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/toPropertyKey.mjs +8 -0
  75. package/lib/esm/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/typeof.mjs +10 -0
  76. package/lib/esm/charts/Chart.mjs +123 -0
  77. package/lib/esm/charts/Chart.mjs.map +1 -0
  78. package/lib/esm/charts/ChartContainer.mjs +71 -0
  79. package/lib/esm/charts/ChartContainer.mjs.map +1 -0
  80. package/lib/esm/charts/ChartEmpty.mjs +46 -0
  81. package/lib/esm/charts/ChartEmpty.mjs.map +1 -0
  82. package/lib/esm/charts/ChartRender.mjs +113 -0
  83. package/lib/esm/charts/ChartRender.mjs.map +1 -0
  84. package/lib/esm/charts/ChartTooltip.mjs +109 -0
  85. package/lib/esm/charts/ChartTooltip.mjs.map +1 -0
  86. package/lib/esm/charts/Legend.mjs +82 -0
  87. package/lib/esm/charts/Legend.mjs.map +1 -0
  88. package/lib/esm/charts/LegendItem.mjs +82 -0
  89. package/lib/esm/charts/LegendItem.mjs.map +1 -0
  90. package/lib/esm/charts/LegendLayout.mjs +337 -0
  91. package/lib/esm/charts/LegendLayout.mjs.map +1 -0
  92. package/lib/esm/charts/hooks/use-chart-instance.mjs +119 -0
  93. package/lib/esm/charts/hooks/use-chart-instance.mjs.map +1 -0
  94. package/lib/esm/charts/hooks/use-chart-option.mjs +79 -0
  95. package/lib/esm/charts/hooks/use-chart-option.mjs.map +1 -0
  96. package/lib/esm/charts/hooks/use-chart-refs.mjs +49 -0
  97. package/lib/esm/charts/hooks/use-chart-refs.mjs.map +1 -0
  98. package/lib/esm/charts/hooks/use-chart-zoom.mjs +102 -0
  99. package/lib/esm/charts/hooks/use-chart-zoom.mjs.map +1 -0
  100. package/lib/esm/charts/hooks/use-legend-interactions.mjs +138 -0
  101. package/lib/esm/charts/hooks/use-legend-interactions.mjs.map +1 -0
  102. package/lib/esm/charts/hooks/use-legend-series.mjs +151 -0
  103. package/lib/esm/charts/hooks/use-legend-series.mjs.map +1 -0
  104. package/lib/esm/charts/hooks/use-legend-visibility.mjs +67 -0
  105. package/lib/esm/charts/hooks/use-legend-visibility.mjs.map +1 -0
  106. package/lib/esm/charts/index.mjs +27 -0
  107. package/lib/esm/charts/index.mjs.map +1 -0
  108. package/lib/esm/charts/themes/ndl-echarts-theme.mjs +139 -0
  109. package/lib/esm/charts/themes/ndl-echarts-theme.mjs.map +1 -0
  110. package/lib/esm/charts/utils/aria-description.mjs +162 -0
  111. package/lib/esm/charts/utils/aria-description.mjs.map +1 -0
  112. package/lib/esm/charts/utils/build-chart-option.mjs +184 -0
  113. package/lib/esm/charts/utils/build-chart-option.mjs.map +1 -0
  114. package/lib/esm/charts/utils/chart-tooltip-formatter.mjs +159 -0
  115. package/lib/esm/charts/utils/chart-tooltip-formatter.mjs.map +1 -0
  116. package/lib/esm/charts/utils/chart-types.mjs +24 -0
  117. package/lib/esm/charts/utils/chart-types.mjs.map +1 -0
  118. package/lib/esm/charts/utils/defaults.mjs +73 -0
  119. package/lib/esm/charts/utils/defaults.mjs.map +1 -0
  120. package/lib/esm/charts/utils/format-utils.mjs +61 -0
  121. package/lib/esm/charts/utils/format-utils.mjs.map +1 -0
  122. package/lib/esm/charts/utils/legend-layout.mjs +57 -0
  123. package/lib/esm/charts/utils/legend-layout.mjs.map +1 -0
  124. package/lib/esm/charts/utils/legend-utils.mjs +90 -0
  125. package/lib/esm/charts/utils/legend-utils.mjs.map +1 -0
  126. package/lib/esm/charts/utils/scatter-tooltip-points.mjs +215 -0
  127. package/lib/esm/charts/utils/scatter-tooltip-points.mjs.map +1 -0
  128. package/lib/esm/charts/utils/threshold.mjs +92 -0
  129. package/lib/esm/charts/utils/threshold.mjs.map +1 -0
  130. package/lib/esm/charts/utils/user-option-utils.mjs +445 -0
  131. package/lib/esm/charts/utils/user-option-utils.mjs.map +1 -0
  132. package/lib/esm/index.mjs +27 -0
  133. package/lib/esm/index.mjs.map +1 -0
  134. package/package.json +12 -16
  135. package/skills/ndl-react-charts/SKILL.md +2 -2
  136. package/lib/cjs/charts/tests/ChartWrapperWithTooltipFormatter.js +0 -38
  137. package/lib/cjs/charts/tests/ChartWrapperWithTooltipFormatter.js.map +0 -1
  138. package/lib/cjs/charts/tests/ChartZoomHarness.js +0 -113
  139. package/lib/cjs/charts/tests/ChartZoomHarness.js.map +0 -1
  140. package/lib/cjs/charts/tests/HorizontalBarWithVerticalSlider.js +0 -78
  141. package/lib/cjs/charts/tests/HorizontalBarWithVerticalSlider.js.map +0 -1
  142. package/lib/cjs/charts/tests/PieChartWithRerender.js +0 -46
  143. package/lib/cjs/charts/tests/PieChartWithRerender.js.map +0 -1
  144. package/lib/cjs/charts/tests/chart-test-utils.js +0 -527
  145. package/lib/cjs/charts/tests/chart-test-utils.js.map +0 -1
  146. package/lib/esm/charts/Chart.js +0 -114
  147. package/lib/esm/charts/Chart.js.map +0 -1
  148. package/lib/esm/charts/ChartContainer.js +0 -67
  149. package/lib/esm/charts/ChartContainer.js.map +0 -1
  150. package/lib/esm/charts/ChartEmpty.js +0 -33
  151. package/lib/esm/charts/ChartEmpty.js.map +0 -1
  152. package/lib/esm/charts/ChartRender.js +0 -89
  153. package/lib/esm/charts/ChartRender.js.map +0 -1
  154. package/lib/esm/charts/ChartTooltip.js +0 -64
  155. package/lib/esm/charts/ChartTooltip.js.map +0 -1
  156. package/lib/esm/charts/Legend.js +0 -71
  157. package/lib/esm/charts/Legend.js.map +0 -1
  158. package/lib/esm/charts/LegendItem.js +0 -61
  159. package/lib/esm/charts/LegendItem.js.map +0 -1
  160. package/lib/esm/charts/LegendLayout.js +0 -323
  161. package/lib/esm/charts/LegendLayout.js.map +0 -1
  162. package/lib/esm/charts/hooks/use-chart-instance.js +0 -128
  163. package/lib/esm/charts/hooks/use-chart-instance.js.map +0 -1
  164. package/lib/esm/charts/hooks/use-chart-option.js +0 -82
  165. package/lib/esm/charts/hooks/use-chart-option.js.map +0 -1
  166. package/lib/esm/charts/hooks/use-chart-refs.js +0 -48
  167. package/lib/esm/charts/hooks/use-chart-refs.js.map +0 -1
  168. package/lib/esm/charts/hooks/use-chart-zoom.js +0 -129
  169. package/lib/esm/charts/hooks/use-chart-zoom.js.map +0 -1
  170. package/lib/esm/charts/hooks/use-legend-interactions.js +0 -145
  171. package/lib/esm/charts/hooks/use-legend-interactions.js.map +0 -1
  172. package/lib/esm/charts/hooks/use-legend-series.js +0 -184
  173. package/lib/esm/charts/hooks/use-legend-series.js.map +0 -1
  174. package/lib/esm/charts/hooks/use-legend-visibility.js +0 -87
  175. package/lib/esm/charts/hooks/use-legend-visibility.js.map +0 -1
  176. package/lib/esm/charts/index.js.map +0 -1
  177. package/lib/esm/charts/tests/ChartWrapperWithTooltipFormatter.js.map +0 -1
  178. package/lib/esm/charts/tests/ChartZoomHarness.js +0 -109
  179. package/lib/esm/charts/tests/ChartZoomHarness.js.map +0 -1
  180. package/lib/esm/charts/tests/HorizontalBarWithVerticalSlider.js +0 -74
  181. package/lib/esm/charts/tests/HorizontalBarWithVerticalSlider.js.map +0 -1
  182. package/lib/esm/charts/tests/PieChartWithRerender.js +0 -43
  183. package/lib/esm/charts/tests/PieChartWithRerender.js.map +0 -1
  184. package/lib/esm/charts/tests/chart-test-utils.js +0 -500
  185. package/lib/esm/charts/tests/chart-test-utils.js.map +0 -1
  186. package/lib/esm/charts/themes/ndl-echarts-theme.js +0 -172
  187. package/lib/esm/charts/themes/ndl-echarts-theme.js.map +0 -1
  188. package/lib/esm/charts/utils/aria-description.js +0 -228
  189. package/lib/esm/charts/utils/aria-description.js.map +0 -1
  190. package/lib/esm/charts/utils/build-chart-option.js +0 -193
  191. package/lib/esm/charts/utils/build-chart-option.js.map +0 -1
  192. package/lib/esm/charts/utils/chart-tooltip-formatter.js +0 -177
  193. package/lib/esm/charts/utils/chart-tooltip-formatter.js.map +0 -1
  194. package/lib/esm/charts/utils/chart-types.js.map +0 -1
  195. package/lib/esm/charts/utils/defaults.js +0 -73
  196. package/lib/esm/charts/utils/defaults.js.map +0 -1
  197. package/lib/esm/charts/utils/format-utils.js +0 -74
  198. package/lib/esm/charts/utils/format-utils.js.map +0 -1
  199. package/lib/esm/charts/utils/legend-layout.js +0 -59
  200. package/lib/esm/charts/utils/legend-layout.js.map +0 -1
  201. package/lib/esm/charts/utils/legend-utils.js +0 -131
  202. package/lib/esm/charts/utils/legend-utils.js.map +0 -1
  203. package/lib/esm/charts/utils/scatter-tooltip-points.js +0 -291
  204. package/lib/esm/charts/utils/scatter-tooltip-points.js.map +0 -1
  205. package/lib/esm/charts/utils/threshold.js +0 -106
  206. package/lib/esm/charts/utils/threshold.js.map +0 -1
  207. package/lib/esm/charts/utils/user-option-utils.js +0 -374
  208. package/lib/esm/charts/utils/user-option-utils.js.map +0 -1
  209. package/lib/esm/index.js.map +0 -1
@@ -0,0 +1,337 @@
1
+ import { getCollapsedVisibleItemCount, getComputedElementWidth } from "./utils/legend-layout.mjs";
2
+ import _objectSpread2 from "../_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/objectSpread2.mjs";
3
+ import _objectWithoutProperties from "../_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/objectWithoutProperties.mjs";
4
+ import React, { useCallback, useEffect, useRef, useState } from "react";
5
+ import { jsx, jsxs } from "react/jsx-runtime";
6
+ import { TextLink } from "@neo4j-ndl/react";
7
+ import classNames from "classnames";
8
+ // src/charts/LegendLayout.tsx
9
+ /*!
10
+ *
11
+ * Copyright (c) "Neo4j"
12
+ * Neo4j Sweden AB [http://neo4j.com]
13
+ *
14
+ * This file is part of Neo4j.
15
+ *
16
+ * Neo4j is free software: you can redistribute it and/or modify
17
+ * it under the terms of the GNU General Public License as published by
18
+ * the Free Software Foundation, either version 3 of the License, or
19
+ * (at your option) any later version.
20
+ *
21
+ * This program is distributed in the hope that it will be useful,
22
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ * GNU General Public License for more details.
25
+ *
26
+ * You should have received a copy of the GNU General Public License
27
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28
+ */
29
+ const _excluded = [
30
+ "children",
31
+ "className",
32
+ "isLayoutReady",
33
+ "itemCount",
34
+ "onLayoutReady",
35
+ "htmlAttributes"
36
+ ];
37
+ const getLegendElementsContentHeight = (legendContent, elements) => {
38
+ const legendContentStyle = getComputedStyle(legendContent);
39
+ const paddingBottom = parseFloat(legendContentStyle.paddingBottom) || 0;
40
+ return Math.max(...elements.map((element) => element.offsetTop + element.offsetHeight)) + paddingBottom;
41
+ };
42
+ const getCollapsedLegendContentHeight = ({ availableRowWidth, elements, legendContent }) => {
43
+ const legendContentStyle = getComputedStyle(legendContent);
44
+ const paddingTop = parseFloat(legendContentStyle.paddingTop) || 0;
45
+ const paddingBottom = parseFloat(legendContentStyle.paddingBottom) || 0;
46
+ const rowGap = parseFloat(legendContentStyle.rowGap) || 0;
47
+ let currentRowHeight = 0;
48
+ let currentRowWidth = 0;
49
+ const rowHeights = [];
50
+ elements.forEach((element) => {
51
+ const elementWidth = getComputedElementWidth(element);
52
+ const elementHeight = element.getBoundingClientRect().height;
53
+ if (currentRowWidth > 0 && currentRowWidth + elementWidth > availableRowWidth) {
54
+ rowHeights.push(currentRowHeight);
55
+ currentRowHeight = 0;
56
+ currentRowWidth = 0;
57
+ }
58
+ currentRowWidth += elementWidth;
59
+ currentRowHeight = Math.max(currentRowHeight, elementHeight);
60
+ });
61
+ if (currentRowHeight > 0) rowHeights.push(currentRowHeight);
62
+ return paddingTop + rowHeights.reduce((height, rowHeight) => {
63
+ return height + rowHeight;
64
+ }, 0) + rowGap * Math.max(rowHeights.length - 1, 0) + paddingBottom;
65
+ };
66
+ /**
67
+ * Tracks legend container width for responsive collapsed-layout recalculation.
68
+ *
69
+ * The width is kept in state so `useCollapsibleLegendLayout` can re-measure item
70
+ * wrapping whenever the available legend width changes.
71
+ */
72
+ const useLegendContainerWidth = (legendContainerRef) => {
73
+ const [legendWidth, setLegendWidth] = useState(Infinity);
74
+ const updateLegendWidth = useCallback(() => {
75
+ var _legendContainerRef$c;
76
+ const nextWidth = (_legendContainerRef$c = legendContainerRef.current) === null || _legendContainerRef$c === void 0 ? void 0 : _legendContainerRef$c.clientWidth;
77
+ if (nextWidth === void 0) return;
78
+ setLegendWidth(nextWidth);
79
+ }, [legendContainerRef]);
80
+ useEffect(() => {
81
+ updateLegendWidth();
82
+ const legendContainer = legendContainerRef.current;
83
+ if (!legendContainer) return;
84
+ const resizeObserver = new ResizeObserver(updateLegendWidth);
85
+ resizeObserver.observe(legendContainer);
86
+ return () => {
87
+ resizeObserver.disconnect();
88
+ };
89
+ }, [legendContainerRef, updateLegendWidth]);
90
+ return legendWidth;
91
+ };
92
+ /**
93
+ * Owns the collapsed, measuring, and expanded layout state for wrapping legends.
94
+ *
95
+ * Collapsed legends only render the visible subset of items. When the item count
96
+ * or container width changes, the hook temporarily renders every item in an
97
+ * invisible measuring pass so it can calculate wrapping, overflow, and the next
98
+ * visible item count. The last collapsed height is preserved during that pass so
99
+ * the chart area does not shrink while the hidden full legend is being measured.
100
+ */
101
+ const useCollapsibleLegendLayout = ({ itemCount, legendContainerRef, legendContentRef, legendMeasurementRef, viewMoreButtonMeasurementRef }) => {
102
+ const [isExpanded, setIsExpanded] = useState(false);
103
+ const [isMeasuring, setIsMeasuring] = useState(false);
104
+ const [layoutSnapshot, setLayoutSnapshot] = useState({
105
+ contentHeight: 0,
106
+ hasOverflow: false,
107
+ visibleItemCount: itemCount
108
+ });
109
+ const skipNextCollapseMeasurementRef = useRef(false);
110
+ const legendWidth = useLegendContainerWidth(legendContainerRef);
111
+ const updateVisibleItems = useCallback(() => {
112
+ var _legendContainer$clie;
113
+ const legendContent = legendMeasurementRef.current;
114
+ const viewMoreButton = viewMoreButtonMeasurementRef.current;
115
+ if (!legendContent || !viewMoreButton) {
116
+ setIsMeasuring(false);
117
+ return;
118
+ }
119
+ const legendItems = Array.from(legendContent.querySelectorAll(".ndl-chart-legend-item")).filter((element) => element.offsetParent !== null);
120
+ if (legendItems.length === 0) {
121
+ setLayoutSnapshot((snapshot) => _objectSpread2(_objectSpread2({}, snapshot), {}, {
122
+ contentHeight: 0,
123
+ hasOverflow: false,
124
+ visibleItemCount: itemCount
125
+ }));
126
+ setIsMeasuring(false);
127
+ return;
128
+ }
129
+ const rowTops = Array.from(new Set(legendItems.map((element) => element.offsetTop))).sort((left, right) => left - right);
130
+ const nextLegendContentHeight = legendContent.scrollHeight;
131
+ const nextLegendItemsContentHeight = getLegendElementsContentHeight(legendContent, legendItems);
132
+ if (rowTops.length <= 2) {
133
+ setLayoutSnapshot((snapshot) => _objectSpread2(_objectSpread2({}, snapshot), {}, {
134
+ contentHeight: nextLegendItemsContentHeight,
135
+ hasOverflow: false,
136
+ visibleItemCount: itemCount
137
+ }));
138
+ setIsMeasuring(false);
139
+ return;
140
+ }
141
+ const legendContainer = legendContainerRef.current;
142
+ const legendContentStyle = getComputedStyle(legendContent);
143
+ const availableRowWidth = ((_legendContainer$clie = legendContainer === null || legendContainer === void 0 ? void 0 : legendContainer.clientWidth) !== null && _legendContainer$clie !== void 0 ? _legendContainer$clie : legendContent.clientWidth) - (parseFloat(legendContentStyle.paddingLeft) || 0) - (parseFloat(legendContentStyle.paddingRight) || 0);
144
+ const viewMoreButtonWidth = getComputedElementWidth(viewMoreButton);
145
+ const legendItemWidths = legendItems.map(getComputedElementWidth);
146
+ const nextVisibleItemCount = getCollapsedVisibleItemCount(legendItemWidths, availableRowWidth, viewMoreButtonWidth);
147
+ const nextCollapsedContentHeight = getCollapsedLegendContentHeight({
148
+ availableRowWidth,
149
+ elements: [...legendItems.slice(0, nextVisibleItemCount), viewMoreButton],
150
+ legendContent
151
+ });
152
+ setLayoutSnapshot((snapshot) => _objectSpread2(_objectSpread2({}, snapshot), {}, {
153
+ collapsedHeight: Math.min(nextCollapsedContentHeight, 64),
154
+ contentHeight: nextLegendContentHeight,
155
+ hasOverflow: nextVisibleItemCount < itemCount,
156
+ visibleItemCount: nextVisibleItemCount
157
+ }));
158
+ setIsMeasuring(false);
159
+ }, [
160
+ itemCount,
161
+ legendContainerRef,
162
+ legendMeasurementRef,
163
+ viewMoreButtonMeasurementRef
164
+ ]);
165
+ useEffect(() => {
166
+ setIsExpanded(false);
167
+ }, [itemCount]);
168
+ useEffect(() => {
169
+ if (isExpanded) {
170
+ setIsMeasuring(false);
171
+ return;
172
+ }
173
+ if (skipNextCollapseMeasurementRef.current) {
174
+ skipNextCollapseMeasurementRef.current = false;
175
+ setIsMeasuring(false);
176
+ return;
177
+ }
178
+ setIsMeasuring(true);
179
+ }, [
180
+ isExpanded,
181
+ itemCount,
182
+ legendWidth
183
+ ]);
184
+ useEffect(() => {
185
+ const isMeasuringCollapsed = !isExpanded && isMeasuring;
186
+ const animationFrame = requestAnimationFrame(() => {
187
+ if (isMeasuringCollapsed) updateVisibleItems();
188
+ else {
189
+ setLayoutSnapshot((snapshot) => {
190
+ var _legendContentRef$cur, _legendContentRef$cur2;
191
+ return _objectSpread2(_objectSpread2({}, snapshot), {}, { contentHeight: (_legendContentRef$cur = (_legendContentRef$cur2 = legendContentRef.current) === null || _legendContentRef$cur2 === void 0 ? void 0 : _legendContentRef$cur2.scrollHeight) !== null && _legendContentRef$cur !== void 0 ? _legendContentRef$cur : 0 });
192
+ });
193
+ if (!isMeasuring && !isExpanded && layoutSnapshot.hasOverflow) setLayoutSnapshot((snapshot) => {
194
+ var _legendContentRef$cur3;
195
+ return _objectSpread2(_objectSpread2({}, snapshot), {}, { collapsedHeight: (_legendContentRef$cur3 = legendContentRef.current) === null || _legendContentRef$cur3 === void 0 ? void 0 : _legendContentRef$cur3.getBoundingClientRect().height });
196
+ });
197
+ }
198
+ });
199
+ return () => {
200
+ cancelAnimationFrame(animationFrame);
201
+ };
202
+ }, [
203
+ isExpanded,
204
+ isMeasuring,
205
+ legendContentRef,
206
+ layoutSnapshot.hasOverflow,
207
+ updateVisibleItems
208
+ ]);
209
+ const expandLegend = useCallback(() => {
210
+ setIsMeasuring(false);
211
+ setIsExpanded(true);
212
+ }, []);
213
+ return {
214
+ collapseLegend: useCallback(() => {
215
+ skipNextCollapseMeasurementRef.current = true;
216
+ setIsExpanded(false);
217
+ }, []),
218
+ expandLegend,
219
+ isExpanded,
220
+ isMeasuring,
221
+ layoutSnapshot
222
+ };
223
+ };
224
+ /**
225
+ * Renders the responsive legend layout shell.
226
+ *
227
+ * The component switches between a two-row collapsed view, an invisible
228
+ * measurement view, and an expanded scrollable view while keeping the
229
+ * "View more"/"View less" action wired to the measured overflow state.
230
+ */
231
+ const LegendLayout = (_ref) => {
232
+ let { children, className, isLayoutReady = true, itemCount, onLayoutReady, htmlAttributes } = _ref, restProps = _objectWithoutProperties(_ref, _excluded);
233
+ const legendContainerRef = useRef(null);
234
+ const legendContentRef = useRef(null);
235
+ const legendMeasurementRef = useRef(null);
236
+ const pendingFocusLegendItemIndexRef = useRef(null);
237
+ const viewMoreButtonMeasurementRef = useRef(null);
238
+ const { collapseLegend, expandLegend, isExpanded, isMeasuring, layoutSnapshot } = useCollapsibleLegendLayout({
239
+ itemCount,
240
+ legendContainerRef,
241
+ legendContentRef,
242
+ legendMeasurementRef,
243
+ viewMoreButtonMeasurementRef
244
+ });
245
+ const childList = React.Children.toArray(children);
246
+ const childrenToRender = !isExpanded ? childList.slice(0, layoutSnapshot.visibleItemCount) : childList;
247
+ const legendHeight = !isMeasuring && !isExpanded && layoutSnapshot.hasOverflow ? layoutSnapshot.collapsedHeight : !isMeasuring && !layoutSnapshot.hasOverflow && layoutSnapshot.contentHeight ? layoutSnapshot.contentHeight : void 0;
248
+ useEffect(() => {
249
+ if (!isMeasuring && layoutSnapshot.contentHeight > 0) {
250
+ let secondFrame;
251
+ const firstFrame = requestAnimationFrame(() => {
252
+ secondFrame = requestAnimationFrame(() => {
253
+ onLayoutReady === null || onLayoutReady === void 0 || onLayoutReady();
254
+ });
255
+ });
256
+ return () => {
257
+ cancelAnimationFrame(firstFrame);
258
+ if (secondFrame !== void 0) cancelAnimationFrame(secondFrame);
259
+ };
260
+ }
261
+ }, [
262
+ isMeasuring,
263
+ layoutSnapshot.contentHeight,
264
+ onLayoutReady
265
+ ]);
266
+ useEffect(() => {
267
+ var _legendContentRef$cur4, _legendItems$pendingF;
268
+ const pendingFocusLegendItemIndex = pendingFocusLegendItemIndexRef.current;
269
+ if (!isExpanded || pendingFocusLegendItemIndex === null) return;
270
+ const legendItems = (_legendContentRef$cur4 = legendContentRef.current) === null || _legendContentRef$cur4 === void 0 ? void 0 : _legendContentRef$cur4.querySelectorAll(".ndl-chart-legend-item");
271
+ legendItems === null || legendItems === void 0 || (_legendItems$pendingF = legendItems[pendingFocusLegendItemIndex]) === null || _legendItems$pendingF === void 0 || _legendItems$pendingF.focus();
272
+ pendingFocusLegendItemIndexRef.current = null;
273
+ }, [isExpanded]);
274
+ const handleLegendActionClick = useCallback(() => {
275
+ if (isExpanded) {
276
+ collapseLegend();
277
+ return;
278
+ }
279
+ pendingFocusLegendItemIndexRef.current = layoutSnapshot.visibleItemCount;
280
+ expandLegend();
281
+ }, [
282
+ collapseLegend,
283
+ expandLegend,
284
+ isExpanded,
285
+ layoutSnapshot.visibleItemCount
286
+ ]);
287
+ const classes = classNames("ndl-chart-legend", {
288
+ "ndl-chart-legend-calculating": !isExpanded && isMeasuring,
289
+ "ndl-chart-legend-collapsed": layoutSnapshot.hasOverflow && !isExpanded,
290
+ "ndl-chart-legend-expanded": layoutSnapshot.hasOverflow && isExpanded,
291
+ "ndl-chart-legend-hidden": !isLayoutReady,
292
+ "ndl-chart-legend-scrollable": layoutSnapshot.hasOverflow && isExpanded && layoutSnapshot.contentHeight > 64,
293
+ "ndl-chart-legend-wrapping": true
294
+ }, className);
295
+ return /* @__PURE__ */ jsx("div", _objectSpread2(_objectSpread2(_objectSpread2({
296
+ ref: legendContainerRef,
297
+ className: "ndl-chart-legend-container"
298
+ }, restProps), htmlAttributes), {}, { children: /* @__PURE__ */ jsxs("div", {
299
+ className: classes,
300
+ ref: legendContentRef,
301
+ style: {
302
+ height: legendHeight,
303
+ maxHeight: layoutSnapshot.hasOverflow && isExpanded ? "var(--ndl-chart-legend-expanded-max-height)" : layoutSnapshot.hasOverflow ? 64 : void 0
304
+ },
305
+ children: [
306
+ childrenToRender,
307
+ layoutSnapshot.hasOverflow && /* @__PURE__ */ jsx(TextLink, {
308
+ as: "button",
309
+ className: "ndl-chart-legend-action",
310
+ htmlAttributes: {
311
+ "aria-expanded": isExpanded,
312
+ onClick: handleLegendActionClick,
313
+ type: "button"
314
+ },
315
+ children: isExpanded ? "View less" : "View more"
316
+ }),
317
+ isMeasuring && /* @__PURE__ */ jsxs("div", {
318
+ "aria-hidden": "true",
319
+ className: "ndl-chart-legend-measurement",
320
+ ref: legendMeasurementRef,
321
+ children: [childList, /* @__PURE__ */ jsx(TextLink, {
322
+ as: "button",
323
+ ref: viewMoreButtonMeasurementRef,
324
+ className: "ndl-chart-legend-action",
325
+ htmlAttributes: {
326
+ tabIndex: -1,
327
+ type: "button"
328
+ },
329
+ children: "View more"
330
+ })]
331
+ })
332
+ ]
333
+ }) }));
334
+ };
335
+ export { LegendLayout };
336
+
337
+ //# sourceMappingURL=LegendLayout.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LegendLayout.mjs","names":[],"sources":["../../../src/charts/LegendLayout.tsx"],"sourcesContent":["/*!\n *\n * Copyright (c) \"Neo4j\"\n * Neo4j Sweden AB [http://neo4j.com]\n *\n * This file is part of Neo4j.\n *\n * Neo4j is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n * This program is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU General Public License for more details.\n *\n * You should have received a copy of the GNU General Public License\n * along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport { TextLink } from '@neo4j-ndl/react';\nimport classNames from 'classnames';\nimport React, { useCallback, useEffect, useRef, useState } from 'react';\n\nimport { type CommonProps } from './utils/chart-types';\nimport {\n COLLAPSED_LEGEND_MAX_HEIGHT,\n COLLAPSED_LEGEND_ROWS,\n getCollapsedVisibleItemCount,\n getComputedElementWidth,\n} from './utils/legend-layout';\n\ntype LegendLayoutProps = {\n children: React.ReactNode;\n isLayoutReady?: boolean;\n itemCount: number;\n onLayoutReady?: () => void;\n};\n\ntype LegendLayoutSnapshot = {\n collapsedHeight?: number;\n contentHeight: number;\n hasOverflow: boolean;\n visibleItemCount: number;\n};\n\nconst getLegendElementsContentHeight = (\n legendContent: HTMLElement,\n elements: HTMLElement[],\n) => {\n const legendContentStyle = getComputedStyle(legendContent);\n const paddingBottom = parseFloat(legendContentStyle.paddingBottom) || 0;\n const elementsBottom = Math.max(\n ...elements.map((element) => element.offsetTop + element.offsetHeight),\n );\n\n return elementsBottom + paddingBottom;\n};\n\nconst getCollapsedLegendContentHeight = ({\n availableRowWidth,\n elements,\n legendContent,\n}: {\n availableRowWidth: number;\n elements: HTMLElement[];\n legendContent: HTMLElement;\n}) => {\n const legendContentStyle = getComputedStyle(legendContent);\n const paddingTop = parseFloat(legendContentStyle.paddingTop) || 0;\n const paddingBottom = parseFloat(legendContentStyle.paddingBottom) || 0;\n const rowGap = parseFloat(legendContentStyle.rowGap) || 0;\n let currentRowHeight = 0;\n let currentRowWidth = 0;\n const rowHeights: number[] = [];\n\n elements.forEach((element) => {\n const elementWidth = getComputedElementWidth(element);\n const elementHeight = element.getBoundingClientRect().height;\n const shouldWrap =\n currentRowWidth > 0 && currentRowWidth + elementWidth > availableRowWidth;\n\n if (shouldWrap) {\n rowHeights.push(currentRowHeight);\n currentRowHeight = 0;\n currentRowWidth = 0;\n }\n\n currentRowWidth += elementWidth;\n currentRowHeight = Math.max(currentRowHeight, elementHeight);\n });\n\n if (currentRowHeight > 0) {\n rowHeights.push(currentRowHeight);\n }\n\n const rowsHeight = rowHeights.reduce((height, rowHeight) => {\n return height + rowHeight;\n }, 0);\n\n return (\n paddingTop +\n rowsHeight +\n rowGap * Math.max(rowHeights.length - 1, 0) +\n paddingBottom\n );\n};\n\n/**\n * Tracks legend container width for responsive collapsed-layout recalculation.\n *\n * The width is kept in state so `useCollapsibleLegendLayout` can re-measure item\n * wrapping whenever the available legend width changes.\n */\nconst useLegendContainerWidth = (\n legendContainerRef: React.RefObject<HTMLDivElement | null>,\n) => {\n const [legendWidth, setLegendWidth] = useState(Infinity);\n\n const updateLegendWidth = useCallback(() => {\n const nextWidth = legendContainerRef.current?.clientWidth;\n if (nextWidth === undefined) {\n return;\n }\n\n setLegendWidth(nextWidth);\n }, [legendContainerRef]);\n\n useEffect(() => {\n updateLegendWidth();\n\n const legendContainer = legendContainerRef.current;\n if (!legendContainer) {\n return;\n }\n\n const resizeObserver = new ResizeObserver(updateLegendWidth);\n resizeObserver.observe(legendContainer);\n\n return () => {\n resizeObserver.disconnect();\n };\n }, [legendContainerRef, updateLegendWidth]);\n\n return legendWidth;\n};\n\n/**\n * Owns the collapsed, measuring, and expanded layout state for wrapping legends.\n *\n * Collapsed legends only render the visible subset of items. When the item count\n * or container width changes, the hook temporarily renders every item in an\n * invisible measuring pass so it can calculate wrapping, overflow, and the next\n * visible item count. The last collapsed height is preserved during that pass so\n * the chart area does not shrink while the hidden full legend is being measured.\n */\nconst useCollapsibleLegendLayout = ({\n itemCount,\n legendContainerRef,\n legendContentRef,\n legendMeasurementRef,\n viewMoreButtonMeasurementRef,\n}: {\n itemCount: number;\n legendContainerRef: React.RefObject<HTMLDivElement | null>;\n legendContentRef: React.RefObject<HTMLDivElement | null>;\n legendMeasurementRef: React.RefObject<HTMLDivElement | null>;\n viewMoreButtonMeasurementRef: React.RefObject<HTMLButtonElement | null>;\n}) => {\n const [isExpanded, setIsExpanded] = useState(false);\n const [isMeasuring, setIsMeasuring] = useState(false);\n const [layoutSnapshot, setLayoutSnapshot] = useState<LegendLayoutSnapshot>({\n contentHeight: 0,\n hasOverflow: false,\n visibleItemCount: itemCount,\n });\n const skipNextCollapseMeasurementRef = useRef(false);\n const legendWidth = useLegendContainerWidth(legendContainerRef);\n\n const updateVisibleItems = useCallback(() => {\n const legendContent = legendMeasurementRef.current;\n const viewMoreButton = viewMoreButtonMeasurementRef.current;\n if (!legendContent || !viewMoreButton) {\n setIsMeasuring(false);\n return;\n }\n\n const legendItems = Array.from(\n legendContent.querySelectorAll<HTMLElement>('.ndl-chart-legend-item'),\n ).filter((element) => element.offsetParent !== null);\n\n if (legendItems.length === 0) {\n setLayoutSnapshot((snapshot) => ({\n ...snapshot,\n contentHeight: 0,\n hasOverflow: false,\n visibleItemCount: itemCount,\n }));\n setIsMeasuring(false);\n return;\n }\n\n const rowTops = Array.from(\n new Set(legendItems.map((element) => element.offsetTop)),\n ).sort((left, right) => left - right);\n const nextLegendContentHeight = legendContent.scrollHeight;\n const nextLegendItemsContentHeight = getLegendElementsContentHeight(\n legendContent,\n legendItems,\n );\n\n if (rowTops.length <= COLLAPSED_LEGEND_ROWS) {\n setLayoutSnapshot((snapshot) => ({\n ...snapshot,\n contentHeight: nextLegendItemsContentHeight,\n hasOverflow: false,\n visibleItemCount: itemCount,\n }));\n setIsMeasuring(false);\n return;\n }\n\n const legendContainer = legendContainerRef.current;\n const legendContentStyle = getComputedStyle(legendContent);\n const availableRowWidth =\n (legendContainer?.clientWidth ?? legendContent.clientWidth) -\n (parseFloat(legendContentStyle.paddingLeft) || 0) -\n (parseFloat(legendContentStyle.paddingRight) || 0);\n const viewMoreButtonWidth = getComputedElementWidth(viewMoreButton);\n const legendItemWidths = legendItems.map(getComputedElementWidth);\n const nextVisibleItemCount = getCollapsedVisibleItemCount(\n legendItemWidths,\n availableRowWidth,\n viewMoreButtonWidth,\n );\n const nextCollapsedContentHeight = getCollapsedLegendContentHeight({\n availableRowWidth,\n elements: [...legendItems.slice(0, nextVisibleItemCount), viewMoreButton],\n legendContent,\n });\n\n setLayoutSnapshot((snapshot) => ({\n ...snapshot,\n collapsedHeight: Math.min(\n nextCollapsedContentHeight,\n COLLAPSED_LEGEND_MAX_HEIGHT,\n ),\n contentHeight: nextLegendContentHeight,\n hasOverflow: nextVisibleItemCount < itemCount,\n visibleItemCount: nextVisibleItemCount,\n }));\n setIsMeasuring(false);\n }, [\n itemCount,\n legendContainerRef,\n legendMeasurementRef,\n viewMoreButtonMeasurementRef,\n ]);\n\n useEffect(() => {\n setIsExpanded(false);\n }, [itemCount]);\n\n useEffect(() => {\n if (isExpanded) {\n setIsMeasuring(false);\n return;\n }\n\n if (skipNextCollapseMeasurementRef.current) {\n skipNextCollapseMeasurementRef.current = false;\n setIsMeasuring(false);\n return;\n }\n\n setIsMeasuring(true);\n }, [isExpanded, itemCount, legendWidth]);\n\n useEffect(() => {\n const isMeasuringCollapsed = !isExpanded && isMeasuring;\n\n const animationFrame = requestAnimationFrame(() => {\n if (isMeasuringCollapsed) {\n updateVisibleItems();\n } else {\n setLayoutSnapshot((snapshot) => ({\n ...snapshot,\n contentHeight: legendContentRef.current?.scrollHeight ?? 0,\n }));\n\n if (!isMeasuring && !isExpanded && layoutSnapshot.hasOverflow) {\n setLayoutSnapshot((snapshot) => ({\n ...snapshot,\n collapsedHeight:\n legendContentRef.current?.getBoundingClientRect().height,\n }));\n }\n }\n });\n\n return () => {\n cancelAnimationFrame(animationFrame);\n };\n }, [\n isExpanded,\n isMeasuring,\n legendContentRef,\n layoutSnapshot.hasOverflow,\n updateVisibleItems,\n ]);\n\n const expandLegend = useCallback(() => {\n setIsMeasuring(false);\n setIsExpanded(true);\n }, []);\n\n const collapseLegend = useCallback(() => {\n skipNextCollapseMeasurementRef.current = true;\n setIsExpanded(false);\n }, []);\n\n return {\n collapseLegend,\n expandLegend,\n isExpanded,\n isMeasuring,\n layoutSnapshot,\n };\n};\n\n/**\n * Renders the responsive legend layout shell.\n *\n * The component switches between a two-row collapsed view, an invisible\n * measurement view, and an expanded scrollable view while keeping the\n * \"View more\"/\"View less\" action wired to the measured overflow state.\n */\nexport const LegendLayout = ({\n children,\n className,\n isLayoutReady = true,\n itemCount,\n onLayoutReady,\n htmlAttributes,\n ...restProps\n}: Omit<CommonProps<'div', LegendLayoutProps>, 'ref'>) => {\n const legendContainerRef = useRef<HTMLDivElement>(null);\n const legendContentRef = useRef<HTMLDivElement>(null);\n const legendMeasurementRef = useRef<HTMLDivElement>(null);\n const pendingFocusLegendItemIndexRef = useRef<number | null>(null);\n const viewMoreButtonMeasurementRef = useRef<HTMLButtonElement>(null);\n const {\n collapseLegend,\n expandLegend,\n isExpanded,\n isMeasuring,\n layoutSnapshot,\n } = useCollapsibleLegendLayout({\n itemCount,\n legendContainerRef,\n legendContentRef,\n legendMeasurementRef,\n viewMoreButtonMeasurementRef,\n });\n\n const childList = React.Children.toArray(children);\n const childrenToRender = !isExpanded\n ? childList.slice(0, layoutSnapshot.visibleItemCount)\n : childList;\n const legendHeight =\n !isMeasuring && !isExpanded && layoutSnapshot.hasOverflow\n ? layoutSnapshot.collapsedHeight\n : !isMeasuring &&\n !layoutSnapshot.hasOverflow &&\n layoutSnapshot.contentHeight\n ? layoutSnapshot.contentHeight\n : undefined;\n\n useEffect(() => {\n if (!isMeasuring && layoutSnapshot.contentHeight > 0) {\n let secondFrame: number | undefined;\n const firstFrame = requestAnimationFrame(() => {\n secondFrame = requestAnimationFrame(() => {\n onLayoutReady?.();\n });\n });\n\n return () => {\n cancelAnimationFrame(firstFrame);\n if (secondFrame !== undefined) {\n cancelAnimationFrame(secondFrame);\n }\n };\n }\n }, [isMeasuring, layoutSnapshot.contentHeight, onLayoutReady]);\n\n useEffect(() => {\n const pendingFocusLegendItemIndex = pendingFocusLegendItemIndexRef.current;\n if (!isExpanded || pendingFocusLegendItemIndex === null) {\n return;\n }\n\n const legendItems = legendContentRef.current?.querySelectorAll<HTMLElement>(\n '.ndl-chart-legend-item',\n );\n legendItems?.[pendingFocusLegendItemIndex]?.focus();\n pendingFocusLegendItemIndexRef.current = null;\n }, [isExpanded]);\n\n const handleLegendActionClick = useCallback(() => {\n if (isExpanded) {\n collapseLegend();\n return;\n }\n\n pendingFocusLegendItemIndexRef.current = layoutSnapshot.visibleItemCount;\n expandLegend();\n }, [\n collapseLegend,\n expandLegend,\n isExpanded,\n layoutSnapshot.visibleItemCount,\n ]);\n\n const classes = classNames(\n 'ndl-chart-legend',\n {\n 'ndl-chart-legend-calculating': !isExpanded && isMeasuring,\n 'ndl-chart-legend-collapsed': layoutSnapshot.hasOverflow && !isExpanded,\n 'ndl-chart-legend-expanded': layoutSnapshot.hasOverflow && isExpanded,\n 'ndl-chart-legend-hidden': !isLayoutReady,\n 'ndl-chart-legend-scrollable':\n layoutSnapshot.hasOverflow &&\n isExpanded &&\n layoutSnapshot.contentHeight > COLLAPSED_LEGEND_MAX_HEIGHT,\n 'ndl-chart-legend-wrapping': true,\n },\n className,\n );\n return (\n <div\n ref={legendContainerRef}\n className=\"ndl-chart-legend-container\"\n {...restProps}\n {...htmlAttributes}\n >\n <div\n className={classes}\n ref={legendContentRef}\n style={{\n height: legendHeight,\n maxHeight:\n layoutSnapshot.hasOverflow && isExpanded\n ? 'var(--ndl-chart-legend-expanded-max-height)'\n : layoutSnapshot.hasOverflow\n ? COLLAPSED_LEGEND_MAX_HEIGHT\n : undefined,\n }}\n >\n {childrenToRender}\n {layoutSnapshot.hasOverflow && (\n <TextLink\n as=\"button\"\n className=\"ndl-chart-legend-action\"\n htmlAttributes={{\n 'aria-expanded': isExpanded,\n onClick: handleLegendActionClick,\n type: 'button',\n }}\n >\n {isExpanded ? 'View less' : 'View more'}\n </TextLink>\n )}\n {isMeasuring && (\n <div\n aria-hidden=\"true\"\n className=\"ndl-chart-legend-measurement\"\n ref={legendMeasurementRef}\n >\n {childList}\n <TextLink\n as=\"button\"\n ref={viewMoreButtonMeasurementRef}\n className=\"ndl-chart-legend-action\"\n htmlAttributes={{\n tabIndex: -1,\n type: 'button',\n }}\n >\n View more\n </TextLink>\n </div>\n )}\n </div>\n </div>\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmVE;CACA;CACA;CACA;CACA;CACA;;AAzSF,MAAM,kCACJ,eACA,aACG;CACH,MAAM,qBAAqB,iBAAiB,aAAa;CACzD,MAAM,gBAAgB,WAAW,mBAAmB,aAAa,KAAK;CAKtE,OAJuB,KAAK,IAC1B,GAAG,SAAS,KAAK,YAAY,QAAQ,YAAY,QAAQ,YAAY,CAGnD,IAAI;AAC1B;AAEA,MAAM,mCAAmC,EACvC,mBACA,UACA,oBAKI;CACJ,MAAM,qBAAq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;IACE,IAAG;IACH,WAAU;IACV,gBAAgB;KACd,iBAAiB;KACjB,SAAS;KACT,MAAM;IACR;IAEC,UAAA,aAAa,cAAc;GACpB,CAAA;GAEX,eACC,qBAAC,OAAD;IACE,eAAY;IACZ,WAAU;IACV,KAAK;IAHP,UAAA,CAKG,WACD,oBAAC,UAAD;KACE,IAAG;KACH,KAAK;KACL,WAAU;KACV,gBAAgB;MACd,UAAU;MACV,MAAM;KACR;KACD,UAAA;IAES,CAAA,CACP;;EAEJ;CACF,CAAA,EAAA,CAAA,CAAA;AAET"}
@@ -0,0 +1,119 @@
1
+ import { ndlEchartsTheme } from "../themes/ndl-echarts-theme.mjs";
2
+ import { useChartRefsContext } from "./use-chart-refs.mjs";
3
+ import { useCallback, useEffect, useState } from "react";
4
+ import { tokens } from "@neo4j-ndl/base";
5
+ import { getInstanceByDom, init, registerTheme } from "echarts";
6
+ // src/charts/hooks/use-chart-instance.ts
7
+ /*!
8
+ *
9
+ * Copyright (c) "Neo4j"
10
+ * Neo4j Sweden AB [http://neo4j.com]
11
+ *
12
+ * This file is part of Neo4j.
13
+ *
14
+ * Neo4j is free software: you can redistribute it and/or modify
15
+ * it under the terms of the GNU General Public License as published by
16
+ * the Free Software Foundation, either version 3 of the License, or
17
+ * (at your option) any later version.
18
+ *
19
+ * This program is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License
25
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
+ */
27
+ /**
28
+ * Registers Needle ECharts themes and initializes the chart instance.
29
+ *
30
+ * Theme registration runs with the active palette so ECharts can resolve Needle
31
+ * colors before options are applied. The hook only calls `init` when the DOM
32
+ * element does not already have an ECharts instance, allowing later renders to
33
+ * update registered theme data without replacing the existing chart.
34
+ */
35
+ function useChartLifecycle({ palette, theme }) {
36
+ const { chartEchartRef } = useChartRefsContext();
37
+ useEffect(() => {
38
+ if (chartEchartRef.current === null) return;
39
+ registerTheme("ndl-light", ndlEchartsTheme("light", palette));
40
+ registerTheme("ndl-dark", ndlEchartsTheme("dark", palette));
41
+ if (getInstanceByDom(chartEchartRef.current)) return;
42
+ const echartsTheme = theme === "light" ? "ndl-light" : "ndl-dark";
43
+ init(chartEchartRef.current, echartsTheme, { renderer: "svg" });
44
+ });
45
+ }
46
+ /**
47
+ * Mirrors React loading state into ECharts' loading overlay.
48
+ *
49
+ * The overlay also stays visible while the chart waits for its first resize,
50
+ * preventing users from seeing an unmeasured SVG before ECharts has been sized
51
+ * to its container. Loading colors come from the active Needle theme.
52
+ */
53
+ function useChartLoading({ isLoading, isWaitingForFirstResize, theme }) {
54
+ const { chartEchartRef } = useChartRefsContext();
55
+ useEffect(() => {
56
+ if (chartEchartRef.current === null) return;
57
+ const chart = getInstanceByDom(chartEchartRef.current);
58
+ if (isLoading === true || isWaitingForFirstResize) chart === null || chart === void 0 || chart.showLoading({
59
+ color: tokens.theme[theme].color.primary.bg.status,
60
+ fontFamily: tokens.typography["label"].fontFamily,
61
+ fontSize: tokens.typography["label"].fontSize,
62
+ fontWeight: tokens.typography["label"].fontWeight,
63
+ maskColor: `rgb( from ${tokens.theme[theme].color.neutral.text.inverse} r g b / 0.8)`,
64
+ text: "Loading",
65
+ textColor: tokens.theme[theme].color.neutral.text.default
66
+ });
67
+ else chart === null || chart === void 0 || chart.hideLoading();
68
+ }, [
69
+ chartEchartRef,
70
+ isLoading,
71
+ isWaitingForFirstResize,
72
+ theme
73
+ ]);
74
+ }
75
+ /**
76
+ * Keeps the ECharts canvas sized to the DOM box assigned by the chart layout.
77
+ *
78
+ * The hook listens to both window resize events and direct element resize
79
+ * observations because the chart can change size through parent layout changes
80
+ * that do not emit a window resize. It exposes `isWaitingForFirstResize` so the
81
+ * rest of the chart can delay rendering dependent UI until ECharts has been
82
+ * given its initial dimensions.
83
+ */
84
+ function useChartResize() {
85
+ const { chartEchartRef } = useChartRefsContext();
86
+ const [isWaitingForFirstResize, setIsWaitingForFirstResize] = useState(true);
87
+ const resizeChart = useCallback(() => {
88
+ if (chartEchartRef.current === null) return;
89
+ const chart = getInstanceByDom(chartEchartRef.current);
90
+ const { clientHeight: height, clientWidth: width } = chartEchartRef.current;
91
+ chart === null || chart === void 0 || chart.resize({
92
+ height,
93
+ width
94
+ });
95
+ }, [chartEchartRef]);
96
+ useEffect(() => {
97
+ window.addEventListener("resize", resizeChart);
98
+ const resizeObserver = new ResizeObserver(() => {
99
+ resizeChart();
100
+ });
101
+ if (chartEchartRef.current) resizeObserver.observe(chartEchartRef.current);
102
+ const animationFrame = requestAnimationFrame(() => {
103
+ resizeChart();
104
+ setIsWaitingForFirstResize(false);
105
+ });
106
+ return () => {
107
+ cancelAnimationFrame(animationFrame);
108
+ window.removeEventListener("resize", resizeChart);
109
+ resizeObserver.disconnect();
110
+ };
111
+ }, [chartEchartRef, resizeChart]);
112
+ return {
113
+ isWaitingForFirstResize,
114
+ resizeChart
115
+ };
116
+ }
117
+ export { useChartLifecycle, useChartLoading, useChartResize };
118
+
119
+ //# sourceMappingURL=use-chart-instance.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-chart-instance.mjs","names":[],"sources":["../../../../src/charts/hooks/use-chart-instance.ts"],"sourcesContent":["/*!\n *\n * Copyright (c) \"Neo4j\"\n * Neo4j Sweden AB [http://neo4j.com]\n *\n * This file is part of Neo4j.\n *\n * Neo4j is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n * This program is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU General Public License for more details.\n *\n * You should have received a copy of the GNU General Public License\n * along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport { tokens } from '@neo4j-ndl/base';\nimport { getInstanceByDom, init, registerTheme } from 'echarts';\nimport { useCallback, useEffect, useState } from 'react';\n\nimport { ndlEchartsTheme } from '../themes/ndl-echarts-theme';\nimport type { HexColor } from '../utils/chart-types';\nimport { useChartRefsContext } from './use-chart-refs';\n\ntype UseChartLifecycleParams = {\n palette?: HexColor[];\n theme: 'light' | 'dark';\n};\n\ntype UseChartLoadingParams = {\n isLoading?: boolean;\n isWaitingForFirstResize: boolean;\n theme: 'light' | 'dark';\n};\n\n/**\n * Registers Needle ECharts themes and initializes the chart instance.\n *\n * Theme registration runs with the active palette so ECharts can resolve Needle\n * colors before options are applied. The hook only calls `init` when the DOM\n * element does not already have an ECharts instance, allowing later renders to\n * update registered theme data without replacing the existing chart.\n */\nexport function useChartLifecycle({ palette, theme }: UseChartLifecycleParams) {\n const { chartEchartRef } = useChartRefsContext();\n\n useEffect(() => {\n if (chartEchartRef.current === null) {\n return;\n }\n\n registerTheme('ndl-light', ndlEchartsTheme('light', palette));\n registerTheme('ndl-dark', ndlEchartsTheme('dark', palette));\n\n const currentChart = getInstanceByDom(chartEchartRef.current);\n if (currentChart) {\n return;\n }\n\n const echartsTheme = theme === 'light' ? 'ndl-light' : 'ndl-dark';\n init(chartEchartRef.current, echartsTheme, {\n renderer: 'svg',\n });\n });\n}\n\n/**\n * Mirrors React loading state into ECharts' loading overlay.\n *\n * The overlay also stays visible while the chart waits for its first resize,\n * preventing users from seeing an unmeasured SVG before ECharts has been sized\n * to its container. Loading colors come from the active Needle theme.\n */\nexport function useChartLoading({\n isLoading,\n isWaitingForFirstResize,\n theme,\n}: UseChartLoadingParams) {\n const { chartEchartRef } = useChartRefsContext();\n\n useEffect(() => {\n if (chartEchartRef.current === null) {\n return;\n }\n\n const chart = getInstanceByDom(chartEchartRef.current);\n\n if (isLoading === true || isWaitingForFirstResize) {\n chart?.showLoading({\n color: tokens.theme[theme].color.primary.bg.status,\n fontFamily: tokens.typography['label'].fontFamily,\n fontSize: tokens.typography['label'].fontSize,\n fontWeight: tokens.typography['label'].fontWeight,\n maskColor: `rgb( from ${tokens.theme[theme].color.neutral.text.inverse} r g b / 0.8)`,\n text: 'Loading',\n textColor: tokens.theme[theme].color.neutral.text.default,\n });\n } else {\n chart?.hideLoading();\n }\n }, [chartEchartRef, isLoading, isWaitingForFirstResize, theme]);\n}\n\n/**\n * Keeps the ECharts canvas sized to the DOM box assigned by the chart layout.\n *\n * The hook listens to both window resize events and direct element resize\n * observations because the chart can change size through parent layout changes\n * that do not emit a window resize. It exposes `isWaitingForFirstResize` so the\n * rest of the chart can delay rendering dependent UI until ECharts has been\n * given its initial dimensions.\n */\nexport function useChartResize() {\n const { chartEchartRef } = useChartRefsContext();\n const [isWaitingForFirstResize, setIsWaitingForFirstResize] = useState(true);\n\n const resizeChart = useCallback(() => {\n if (chartEchartRef.current === null) {\n return;\n }\n\n const chart = getInstanceByDom(chartEchartRef.current);\n const { clientHeight: height, clientWidth: width } = chartEchartRef.current;\n\n chart?.resize({\n height,\n width,\n });\n }, [chartEchartRef]);\n\n useEffect(() => {\n window.addEventListener('resize', resizeChart);\n const resizeObserver = new ResizeObserver(() => {\n resizeChart();\n });\n if (chartEchartRef.current) {\n resizeObserver.observe(chartEchartRef.current);\n }\n const animationFrame = requestAnimationFrame(() => {\n resizeChart();\n setIsWaitingForFirstResize(false);\n });\n\n return () => {\n cancelAnimationFrame(animationFrame);\n window.removeEventListener('resize', resizeChart);\n resizeObserver.disconnect();\n };\n }, [chartEchartRef, resizeChart]);\n\n return {\n isWaitingForFirstResize,\n resizeChart,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,kBAAkB,EAAE,SAAS,SAAkC;CAC7E,MAAM,EAAE,mBAAmB,oBAAoB;CAE/C,gBAAgB;EACd,IAAI,eAAe,YAAY,MAC7B;EAGF,cAAc,aAAa,gBAAgB,SAAS,OAAO,CAAC;EAC5D,cAAc,YAAY,gBAAgB,QAAQ,O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W;EAC7C,MAAM,iBAAiB,IAAI,qBAAqB;GAC9C,YAAY;EACd,CAAC;EACD,IAAI,eAAe,SACjB,eAAe,QAAQ,eAAe,OAAO;EAE/C,MAAM,iBAAiB,4BAA4B;GACjD,YAAY;GACZ,2BAA2B,KAAK;EAClC,CAAC;EAED,aAAa;GACX,qBAAqB,cAAc;GACnC,OAAO,oBAAoB,UAAU,WAAW;GAChD,eAAe,WAAW;EAC5B;CACF,GAAG,CAAC,gBAAgB,WAAW,CAAC;CAEhC,OAAO;EACL;EACA;CACF;AACF"}
@@ -0,0 +1,79 @@
1
+ import { buildChartOption } from "../utils/build-chart-option.mjs";
2
+ import { useChartRefsContext } from "./use-chart-refs.mjs";
3
+ import { useEffect, useState } from "react";
4
+ import { getInstanceByDom } from "echarts";
5
+ // src/charts/hooks/use-chart-option.ts
6
+ /*!
7
+ *
8
+ * Copyright (c) "Neo4j"
9
+ * Neo4j Sweden AB [http://neo4j.com]
10
+ *
11
+ * This file is part of Neo4j.
12
+ *
13
+ * Neo4j is free software: you can redistribute it and/or modify
14
+ * it under the terms of the GNU General Public License as published by
15
+ * the Free Software Foundation, either version 3 of the License, or
16
+ * (at your option) any later version.
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
+ */
26
+ /**
27
+ * Builds, applies, and exposes the ECharts option for the current React props.
28
+ *
29
+ * `buildChartOption` merges Needle defaults, normalized axes/series, threshold
30
+ * lines, zoom settings, toolbox options, user overrides, and the current legend
31
+ * selection. After `setOption`, the hook returns ECharts' normalized option so
32
+ * downstream code, especially the custom legend, can read the final series and
33
+ * dataset shape ECharts is actually using.
34
+ */
35
+ function useChartOption({ dataZoom, dataset, hasCategoryXAxis, propsSeries, series, settings, thresholdLines, toolboxOptions, userOption, xAxis, yAxis, hasMarkArea }) {
36
+ const { chartEchartRef, legendSelectedRef } = useChartRefsContext();
37
+ const [chartOption, setChartOption] = useState();
38
+ useEffect(() => {
39
+ if (chartEchartRef.current === null) return;
40
+ const chart = getInstanceByDom(chartEchartRef.current);
41
+ const option = buildChartOption({
42
+ dataZoom,
43
+ dataset,
44
+ getChartInstance: () => chartEchartRef.current === null ? void 0 : getInstanceByDom(chartEchartRef.current),
45
+ getLegendSelected: () => legendSelectedRef.current,
46
+ hasCategoryXAxis,
47
+ hasMarkArea,
48
+ legendSelected: legendSelectedRef.current || {},
49
+ propsSeries,
50
+ series,
51
+ thresholdLines,
52
+ toolboxOptions,
53
+ userOption,
54
+ xAxis,
55
+ yAxis
56
+ });
57
+ chart === null || chart === void 0 || chart.setOption(option, settings);
58
+ setChartOption(chart === null || chart === void 0 ? void 0 : chart.getOption());
59
+ }, [
60
+ chartEchartRef,
61
+ dataZoom,
62
+ dataset,
63
+ hasCategoryXAxis,
64
+ hasMarkArea,
65
+ legendSelectedRef,
66
+ propsSeries,
67
+ series,
68
+ settings,
69
+ thresholdLines,
70
+ toolboxOptions,
71
+ userOption,
72
+ xAxis,
73
+ yAxis
74
+ ]);
75
+ return chartOption;
76
+ }
77
+ export { useChartOption };
78
+
79
+ //# sourceMappingURL=use-chart-option.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-chart-option.mjs","names":[],"sources":["../../../../src/charts/hooks/use-chart-option.ts"],"sourcesContent":["/*!\n *\n * Copyright (c) \"Neo4j\"\n * Neo4j Sweden AB [http://neo4j.com]\n *\n * This file is part of Neo4j.\n *\n * Neo4j is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n * This program is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU General Public License for more details.\n *\n * You should have received a copy of the GNU General Public License\n * along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport type { EChartsOption } from 'echarts';\nimport { getInstanceByDom } from 'echarts';\nimport { useEffect, useState } from 'react';\n\nimport { buildChartOption } from '../utils/build-chart-option';\nimport type { ChartProps, EchartsSeries } from '../utils/chart-types';\nimport type { NormalizedThresholdLine } from '../utils/threshold';\nimport { useChartRefsContext } from './use-chart-refs';\n\nexport type UseChartOptionParams = {\n dataZoom: EChartsOption['dataZoom'];\n dataset: ChartProps['dataset'];\n hasCategoryXAxis: boolean;\n propsSeries: ChartProps['series'];\n series: EchartsSeries;\n settings: ChartProps['settings'];\n thresholdLines: NormalizedThresholdLine[];\n toolboxOptions: EChartsOption['toolbox'];\n userOption: ChartProps['option'];\n xAxis: EChartsOption['xAxis'];\n yAxis: EChartsOption['yAxis'];\n hasMarkArea: boolean;\n};\n\n/**\n * Builds, applies, and exposes the ECharts option for the current React props.\n *\n * `buildChartOption` merges Needle defaults, normalized axes/series, threshold\n * lines, zoom settings, toolbox options, user overrides, and the current legend\n * selection. After `setOption`, the hook returns ECharts' normalized option so\n * downstream code, especially the custom legend, can read the final series and\n * dataset shape ECharts is actually using.\n */\nexport function useChartOption({\n dataZoom,\n dataset,\n hasCategoryXAxis,\n propsSeries,\n series,\n settings,\n thresholdLines,\n toolboxOptions,\n userOption,\n xAxis,\n yAxis,\n hasMarkArea,\n}: UseChartOptionParams) {\n const { chartEchartRef, legendSelectedRef } = useChartRefsContext();\n const [chartOption, setChartOption] = useState<EChartsOption>();\n\n useEffect(() => {\n if (chartEchartRef.current === null) {\n return;\n }\n\n const chart = getInstanceByDom(chartEchartRef.current);\n\n const option = buildChartOption({\n dataZoom,\n dataset,\n // Resolved on each tooltip hover: the scatter tooltip measures distances\n // against the live chart, and legend toggles bypass this option rebuild.\n getChartInstance: () =>\n chartEchartRef.current === null\n ? undefined\n : getInstanceByDom(chartEchartRef.current),\n getLegendSelected: () => legendSelectedRef.current,\n hasCategoryXAxis,\n hasMarkArea,\n legendSelected: legendSelectedRef.current || {},\n propsSeries,\n series,\n thresholdLines,\n toolboxOptions,\n userOption,\n xAxis,\n yAxis,\n });\n\n chart?.setOption(option, settings);\n\n setChartOption(chart?.getOption() as EChartsOption | undefined);\n }, [\n chartEchartRef,\n dataZoom,\n dataset,\n hasCategoryXAxis,\n hasMarkArea,\n legendSelectedRef,\n propsSeries,\n series,\n settings,\n thresholdLines,\n toolboxOptions,\n userOption,\n xAxis,\n yAxis,\n ]);\n\n return chartOption;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDA,SAAgB,eAAe,EAC7B,UACA,SACA,kBACA,aACA,QACA,UACA,gBACA,gBACA,YACA,OACA,OACA,eACuB;CACvB,MAAM,EAAE,gBAAgB,sBAAsB,oBAAoB;CAClE,MAAM,CAAC,aAAa,kBAAkB,SAAwB;CAE9D,gBAAgB;EACd,IAAI,eAAe,YAAY,MAC7B;EAGF,MAAM,QAAQ,iBAAiB,eAAe,OAAO;EAErD,MAAM,SAAS,iBAAiB;GAC9B;GACA;GAGA,wBACE,eAAe,YAAY,OACvB,KAAA,IACA,iBAAiB,eAAe,OAAO;GAC7C,yBAAyB,kBAAkB;GAC3C;GACA;GACA,gBAAgB,kBAAkB,WAAW,CAAC;GAC9C;GACA;GACA;GACA;GACA;GACA;GACA;EACF,CAAC;EAED,UAAA,QAAA,UAAA,KAAA,KAAA,MAAO,UAAU,QAAQ,QAAQ;EAEjC,eAAA,UAAA,QAAA,UAAA,KAAA,IAAA,KAAA,IAAe,MAAO,UAAU,CAA8B;CAChE,GAAG;EACD;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CAED,OAAO;AACT"}
@@ -0,0 +1,49 @@
1
+ import { createContext, createElement, useContext, useMemo, useRef } from "react";
2
+ // src/charts/hooks/use-chart-refs.ts
3
+ /*!
4
+ *
5
+ * Copyright (c) "Neo4j"
6
+ * Neo4j Sweden AB [http://neo4j.com]
7
+ *
8
+ * This file is part of Neo4j.
9
+ *
10
+ * Neo4j is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * This program is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+ const ChartRefsContext = createContext(void 0);
24
+ /**
25
+ * Creates the mutable refs shared between `ChartRender` and legend components.
26
+ *
27
+ * `chartEchartRef` points at the ECharts host element, while the legend refs act
28
+ * as a bridge for state that ECharts needs synchronously during option rebuilds
29
+ * without forcing extra React renders.
30
+ */
31
+ function ChartRefsProvider({ children }) {
32
+ const chartEchartRef = useRef(null);
33
+ const legendSelectedRef = useRef({});
34
+ const legendColorCacheRef = useRef(/* @__PURE__ */ new Map());
35
+ const refs = useMemo(() => ({
36
+ chartEchartRef,
37
+ legendColorCacheRef,
38
+ legendSelectedRef
39
+ }), []);
40
+ return createElement(ChartRefsContext.Provider, { value: refs }, children);
41
+ }
42
+ function useChartRefsContext() {
43
+ const refs = useContext(ChartRefsContext);
44
+ if (refs === void 0) throw new Error("useChartRefsContext must be used within ChartRefsProvider");
45
+ return refs;
46
+ }
47
+ export { ChartRefsProvider, useChartRefsContext };
48
+
49
+ //# sourceMappingURL=use-chart-refs.mjs.map