source: node_modules/recharts/es6/zIndex/ZIndexLayer.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[a762898]1import { useLayoutEffect } from 'react';
2import { createPortal } from 'react-dom';
3import { noop } from '../util/DataUtils';
4import { useAppDispatch, useAppSelector } from '../state/hooks';
5import { selectZIndexPortalElement } from './zIndexSelectors';
6import { registerZIndexPortal, unregisterZIndexPortal } from '../state/zIndexSlice';
7import { useIsInChartContext } from '../context/chartLayoutContext';
8import { useIsPanorama } from '../context/PanoramaContext';
9
10/**
11 * @since 3.4
12 */
13
14/**
15 * A layer that renders its children into a portal corresponding to the given zIndex.
16 * We can't use regular CSS `z-index` because SVG does not support it.
17 * So instead, we create separate DOM nodes for each zIndex layer
18 * and render the children into the corresponding DOM node using React portals.
19 *
20 * This component must be used inside a Chart component.
21 *
22 * @param zIndex numeric zIndex value, higher values are rendered on top of lower values
23 * @param children the content to render inside this zIndex layer
24 *
25 * @since 3.4
26 */
27export function ZIndexLayer(_ref) {
28 var {
29 zIndex,
30 children
31 } = _ref;
32 /*
33 * If we are outside of chart, then we can't rely on the zIndex portal state,
34 * so we just render normally.
35 */
36 var isInChartContext = useIsInChartContext();
37 /*
38 * If zIndex is undefined then we render normally without portals.
39 * Also, if zIndex is 0, we render normally without portals,
40 * because 0 is the default layer that does not need a portal.
41 */
42 var shouldRenderInPortal = isInChartContext && zIndex !== undefined && zIndex !== 0;
43 var isPanorama = useIsPanorama();
44 var dispatch = useAppDispatch();
45 useLayoutEffect(() => {
46 if (!shouldRenderInPortal) {
47 // Nothing to do. We have to call the hook because of the rules of hooks.
48 return noop;
49 }
50 /*
51 * Because zIndexes are dynamic (meaning, we're not working with a predefined set of layers,
52 * but we allow users to define any zIndex at any time), we need to register
53 * the requested zIndex in the global store. This way, the ZIndexPortals component
54 * can render the corresponding portals and only the requested ones.
55 */
56 dispatch(registerZIndexPortal({
57 zIndex
58 }));
59 return () => {
60 dispatch(unregisterZIndexPortal({
61 zIndex
62 }));
63 };
64 }, [dispatch, zIndex, shouldRenderInPortal]);
65 var portalElement = useAppSelector(state => selectZIndexPortalElement(state, zIndex, isPanorama));
66 if (!shouldRenderInPortal) {
67 // If no zIndex is provided or zIndex is 0, render normally without portals
68 return children;
69 }
70 if (!portalElement) {
71 /*
72 * If we don't have a portal element yet, this means that the registration
73 * has not been processed yet by the ZIndexPortals component.
74 * So here we render null and wait for the next render cycle.
75 */
76 return null;
77 }
78 return /*#__PURE__*/createPortal(children, portalElement);
79}
Note: See TracBrowser for help on using the repository browser.