-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVG.jsx
More file actions
62 lines (49 loc) · 1.54 KB
/
SVG.jsx
File metadata and controls
62 lines (49 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { useRef, useState, useEffect, useCallback } from "react";
import styled from "styled-components";
import { getId } from "@utils";
import { SVGContext } from "./plot-utils";
import { PlotMenu } from "./menu/PlotMenu";
import { PlotContainer } from "./menu/PlotContainer";
const SVGstyled = styled.svg`
user-select: none;
fill: var(--color-font);
&:fullscreen {
background: var(--color-background);
}
`;
const useSVGsize = (width, height) => {
const [size, setSize] = useState({ width, height });
useEffect(() => {
setSize((s) => {
if (width !== s.width || height !== s.height) {
return { width, height };
} else {
return s;
}
});
}, [width, height]);
const resetSize = useCallback(() => {
setSize({ width, height });
}, [width, height]);
return [size, setSize, resetSize];
};
export const SVG = ({ width: propWidth, height: propHeight, children, ...rest }) => {
const svgRef = useRef();
const containerRef = useRef();
const idRef = useRef(getId());
const [{ width, height }, setSize, resetSize] = useSVGsize(propWidth, propHeight);
return (
<SVGContext.Provider value={{ width, height, svgRef, containerRef, setSize, resetSize }}>
<PlotContainer ref={containerRef} id={idRef.current} style={{ width, height }}>
<SVGstyled ref={svgRef} width={width} height={height} {...rest}>
{children}
</SVGstyled>
<PlotMenu />
</PlotContainer>
</SVGContext.Provider>
);
};
SVG.defaultProps = {
width: 900,
height: 600,
};