-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResponsiveChart.stories.tsx
More file actions
157 lines (141 loc) · 3.8 KB
/
ResponsiveChart.stories.tsx
File metadata and controls
157 lines (141 loc) · 3.8 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import type { Meta } from '@storybook/react-vite';
import type { ReactNode } from 'react';
import { useEffect, useState } from 'react';
import type { ResponsiveChartProps } from '../../src/index.js';
import { ResponsiveChart } from '../../src/index.js';
export default {
title: 'components/ResponsiveChart',
component: ResponsiveChart,
} as Meta;
function TestChart(props: { width: number; height: number }) {
return (
<svg width={props.width} height={props.height}>
<rect x="0" y="0" width={props.width} height={props.height} fill="blue" />
<text x="5" y="5" dominantBaseline="hanging" fill="white">
{props.width} x {props.height}
</text>
</svg>
);
}
function VariableDiv(props: { children: ReactNode }) {
const [isDown, setIsDown] = useState(true);
const [size, setSize] = useState(250);
useEffect(() => {
const frame = requestAnimationFrame(() => {
setSize((previousSize) => {
if (previousSize <= 0 && isDown) {
setIsDown(false);
} else if (previousSize >= 250 && !isDown) {
setIsDown(true);
}
return isDown ? previousSize - 2 : previousSize + 2;
});
});
return () => cancelAnimationFrame(frame);
}, [size, isDown]);
return (
<div style={{ width: size, height: size, backgroundColor: 'yellow' }}>
{props.children}
</div>
);
}
export function Control({
parentWidth,
parentHeight,
siblingWidth,
...args
}: ResponsiveChartProps & {
parentWidth: number;
parentHeight: number;
siblingWidth: number;
}) {
return (
<div
style={{
display: 'flex',
flexDirection: 'row',
height: parentHeight,
width: parentWidth,
}}
>
<ResponsiveChart {...args}>
{({ width, height }) => <TestChart width={width} height={height} />}
</ResponsiveChart>
<div
style={{
backgroundColor: 'red',
height: '100%',
color: 'white',
width: siblingWidth,
}}
>
Hi!
</div>
</div>
);
}
Control.args = {
parentWidth: 600,
parentHeight: 400,
siblingWidth: 200,
};
export function ParentFixed() {
return (
<div style={{ width: 200, height: 100 }}>
<ResponsiveChart>
{({ width, height }) => <TestChart width={width} height={height} />}
</ResponsiveChart>
</div>
);
}
ParentFixed.storyName = 'Parent with fixed size';
export function ParentVariable() {
return (
<VariableDiv>
<ResponsiveChart>
{({ width, height }) => <TestChart width={width} height={height} />}
</ResponsiveChart>
</VariableDiv>
);
}
ParentVariable.storyName = 'Parent with variable size';
export function SiblingVariable() {
return (
<div
style={{ display: 'flex', flexDirection: 'row', height: 100, width: 500 }}
>
<div style={{ flex: 2, height: 100, backgroundColor: 'yellow' }} />
<ResponsiveChart>
{({ width, height }) => (
<TestChart width={Math.round(width)} height={Math.round(height)} />
)}
</ResponsiveChart>
<VariableDiv>
<div style={{ backgroundColor: 'red', height: '100%', color: 'white' }}>
Hi!
</div>
</VariableDiv>
</div>
);
}
SiblingVariable.storyName = 'Flex siblings with variable size';
export function Max() {
return (
<VariableDiv>
<ResponsiveChart maxWidth={100} maxHeight={50}>
{({ width, height }) => <TestChart width={width} height={height} />}
</ResponsiveChart>
</VariableDiv>
);
}
Max.storyName = 'With max width and height';
export function FixedHeight() {
return (
<VariableDiv>
<ResponsiveChart height={50}>
{({ width, height }) => <TestChart width={width} height={height} />}
</ResponsiveChart>
</VariableDiv>
);
}
FixedHeight.storyName = 'With fixed height';