import * as React from 'react'; import { createContext, ReactNode, useContext, useState } from 'react'; import { uniqueId } from '../util/DataUtils'; import { usePlotArea } from '../hooks'; const ClipPathIdContext = createContext(undefined); /** * Generates a unique clip path ID for use in SVG elements, * and puts it in a context provider. * * To read the clip path ID, use the `useClipPathId` hook, * or render `` component which will automatically use the ID from this context. * * @param props children - React children to be wrapped by the provider * @returns React Context Provider */ export const ClipPathProvider = ({ children }: { children: ReactNode }) => { const [clipPathId] = useState(`${uniqueId('recharts')}-clip`); const plotArea = usePlotArea(); if (plotArea == null) { return null; } const { x, y, width, height } = plotArea; return ( {children} ); }; export const useClipPathId = (): string | undefined => { return useContext(ClipPathIdContext); };