-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathChartItem.tsx
More file actions
130 lines (116 loc) · 3.04 KB
/
ChartItem.tsx
File metadata and controls
130 lines (116 loc) · 3.04 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
import { Grid } from '@mui/material';
import { useEffect, useLayoutEffect, useRef } from 'react';
import { StyleProp, StyleSheet, Text, View, ViewStyle } from 'react-native';
import ChartManager, { Item, WAVE_DELAY_MS } from './ChartManager';
import Animated, {
useAnimatedStyle,
useSharedValue,
withSpring,
} from 'react-native-reanimated';
export type Coordinate = {
x: number;
y: number;
};
interface ChartItemProps {
item: Item;
chartManager: ChartManager;
updateCoordinates?: (id: number, coordinate: Coordinate) => void;
style?: StyleProp<ViewStyle>;
}
const getCenter = (side: number, size: number) => side + size / 2;
export default function ChartItem({
item,
chartManager,
updateCoordinates,
style,
}: ChartItemProps) {
const ref = useRef<View>(null);
const progress = useSharedValue(0);
useEffect(() => {
if (item.id !== ChartManager.EMPTY_SPACE_ID) {
const listenerId = chartManager.addListener(item.id, (isActive) => {
progress.value = withSpring(isActive ? 1 : 0, {
duration: 2 * WAVE_DELAY_MS,
});
});
return () => {
chartManager.removeListener(item.id, listenerId);
};
}
}, [chartManager, item.id, progress]);
const animatedStyle = useAnimatedStyle(() => {
return {
backgroundColor:
progress.value > 0.5
? item.highlightColor
: 'var(--ifm-background-color)',
borderColor: progress.value > 0.5 ? 'transparent' : 'var(--swm-border)',
};
});
const animatedTextStyle = useAnimatedStyle(() => {
return {
color:
progress.value > 0.5
? 'var(--swm-navy-light-100)'
: 'var(--swm-border)',
};
});
useLayoutEffect(() => {
const box = (
ref.current as unknown as HTMLElement
)?.getBoundingClientRect?.();
if (!box) {
return; // no-op on undefined view ref
}
updateCoordinates(item.id, {
x: getCenter(box.left, box.width),
y: getCenter(box.top, box.height),
});
}, [item, updateCoordinates]);
return (
<Grid style={styles.box} size={3}>
<Animated.View
style={[
styles.item,
item.isVisible ? null : styles.hidden,
animatedStyle,
style,
]}
ref={ref}>
<Animated.Text style={[animatedTextStyle, styles.label, style]}>
{item.label}
</Animated.Text>
</Animated.View>
<Text style={styles.subtext}>{item.subtext}</Text>
</Grid>
);
}
const styles = StyleSheet.create({
box: {
flex: 1,
flexDirection: 'column',
textAlign: 'center',
maxWidth: 900,
},
item: {
paddingVertical: 16,
backgroundColor: 'var(--ifm-background-color)',
borderWidth: 1,
borderColor: 'var(--swm-border)',
transition: 'all 350ms ease-in-out',
},
label: {
color: 'var(--swm-border)',
transition: 'color 350ms ease-in-out',
fontWeight: '500',
fontSize: 22,
},
subtext: {
fontWeight: '300',
fontSize: 14,
backgroundColor: 'var(--swm-off-background)',
},
hidden: {
opacity: 0,
},
});