| 1 | import * as React from 'react';
|
|---|
| 2 | import { createContext, useContext, useState } from 'react';
|
|---|
| 3 | import { uniqueId } from '../util/DataUtils';
|
|---|
| 4 | import { usePlotArea } from '../hooks';
|
|---|
| 5 | var ClipPathIdContext = /*#__PURE__*/createContext(undefined);
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * Generates a unique clip path ID for use in SVG elements,
|
|---|
| 9 | * and puts it in a context provider.
|
|---|
| 10 | *
|
|---|
| 11 | * To read the clip path ID, use the `useClipPathId` hook,
|
|---|
| 12 | * or render `<ClipPath>` component which will automatically use the ID from this context.
|
|---|
| 13 | *
|
|---|
| 14 | * @param props children - React children to be wrapped by the provider
|
|---|
| 15 | * @returns React Context Provider
|
|---|
| 16 | */
|
|---|
| 17 | export var ClipPathProvider = _ref => {
|
|---|
| 18 | var {
|
|---|
| 19 | children
|
|---|
| 20 | } = _ref;
|
|---|
| 21 | var [clipPathId] = useState("".concat(uniqueId('recharts'), "-clip"));
|
|---|
| 22 | var plotArea = usePlotArea();
|
|---|
| 23 | if (plotArea == null) {
|
|---|
| 24 | return null;
|
|---|
| 25 | }
|
|---|
| 26 | var {
|
|---|
| 27 | x,
|
|---|
| 28 | y,
|
|---|
| 29 | width,
|
|---|
| 30 | height
|
|---|
| 31 | } = plotArea;
|
|---|
| 32 | return /*#__PURE__*/React.createElement(ClipPathIdContext.Provider, {
|
|---|
| 33 | value: clipPathId
|
|---|
| 34 | }, /*#__PURE__*/React.createElement("defs", null, /*#__PURE__*/React.createElement("clipPath", {
|
|---|
| 35 | id: clipPathId
|
|---|
| 36 | }, /*#__PURE__*/React.createElement("rect", {
|
|---|
| 37 | x: x,
|
|---|
| 38 | y: y,
|
|---|
| 39 | height: height,
|
|---|
| 40 | width: width
|
|---|
| 41 | }))), children);
|
|---|
| 42 | };
|
|---|
| 43 | export var useClipPathId = () => {
|
|---|
| 44 | return useContext(ClipPathIdContext);
|
|---|
| 45 | }; |
|---|