-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResponsiveChart.tsx
More file actions
44 lines (39 loc) · 1.16 KB
/
ResponsiveChart.tsx
File metadata and controls
44 lines (39 loc) · 1.16 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
import type { ReactNode } from 'react';
import { useResizeObserver } from '../hooks/use_resize_observer.js';
export interface ResponsiveChartProps {
width?: number | `${number}%`;
height?: number | `${number}%`;
minWidth?: number | `${number}%`;
minHeight?: number | `${number}%`;
maxWidth?: number | `${number}%`;
maxHeight?: number | `${number}%`;
children: (size: { width: number; height: number }) => ReactNode;
}
export function ResponsiveChart(props: ResponsiveChartProps) {
const { width, height, minWidth, minHeight, maxWidth, maxHeight, children } =
props;
const [observedRef, observedSize] = useResizeObserver();
return (
<div
ref={observedRef}
style={{
position: 'relative',
flex: 1,
width: width || '100%',
height: height || '100%',
minWidth: minWidth || 0,
minHeight: minHeight || 0,
maxWidth,
maxHeight,
}}
>
<div
style={{ position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 }}
>
{observedSize
? children({ width: observedSize.width, height: observedSize.height })
: null}
</div>
</div>
);
}