Skip to content

Commit f619fa6

Browse files
committed
refactor(container): use shared value
1 parent ad879ce commit f619fa6

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

lib/src/components/Container.tsx

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, {
22
Children,
33
cloneElement,
44
useMemo,
5-
useRef,
65
type FC,
76
type PropsWithChildren,
87
type ReactElement,
@@ -68,17 +67,17 @@ const Container: FC<ContainerProps> = ({children}) => {
6867
]
6968
}, [children])
7069

71-
const heightCollapsed = useRef<number>(50)
72-
const heightExpanded = useRef<number>(50)
73-
const heightKnob = useRef<number>(50)
70+
const heightCollapsed = useSharedValue<number>(0)
71+
const heightExpanded = useSharedValue<number>(0)
72+
const heightKnob = useSharedValue<number>(0)
7473
const heightOffset = useSharedValue<number>(0)
7574
const pressed = useSharedValue<boolean>(false)
7675
const expanded = useSharedValue<boolean>(false)
7776

7877
const animateHeightStyles = useAnimatedStyle(() => ({
7978
height:
80-
(expanded.value ? heightExpanded.current : heightCollapsed.current) +
81-
heightKnob.current +
79+
(expanded.value ? heightExpanded.value : heightCollapsed.value) +
80+
heightKnob.value +
8281
heightOffset.value,
8382
}))
8483

@@ -88,7 +87,7 @@ const Container: FC<ContainerProps> = ({children}) => {
8887
opacity: expanded.value ? 0 : 1,
8988
}
9089
}
91-
const heightDiff = heightExpanded.current - heightCollapsed.current
90+
const heightDiff = heightExpanded.value - heightCollapsed.value
9291
const diff = (1 / heightDiff) * Math.abs(heightOffset.value)
9392
return {
9493
opacity: expanded.value ? diff : 1 - diff,
@@ -102,7 +101,7 @@ const Container: FC<ContainerProps> = ({children}) => {
102101
<SectionContainer
103102
style={styles.containerExpanded}
104103
onLayout={(e) => {
105-
heightExpanded.current = e.nativeEvent.layout.height
104+
heightExpanded.value = e.nativeEvent.layout.height
106105
}}>
107106
{expandedChildren}
108107
</SectionContainer>
@@ -111,7 +110,7 @@ const Container: FC<ContainerProps> = ({children}) => {
111110
<SectionContainer
112111
style={animateCollapsedStyles}
113112
onLayout={(e) => {
114-
heightCollapsed.current = e.nativeEvent.layout.height
113+
heightCollapsed.value = e.nativeEvent.layout.height
115114
}}>
116115
{collapsedChildren}
117116
</SectionContainer>
@@ -125,7 +124,7 @@ const Container: FC<ContainerProps> = ({children}) => {
125124
heightExpanded={heightExpanded}
126125
heightKnob={heightKnob}
127126
onLayout={(e) => {
128-
heightKnob.current = e.nativeEvent.layout.height
127+
heightKnob.value = e.nativeEvent.layout.height
129128
}}
130129
pressed={pressed}
131130
expanded={expanded}>

lib/src/components/KnobContainer.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ export type KnobContainerProps = PropsWithChildren<{
1414
accessibilityHint?: ViewProps['accessibilityHint']
1515
accessibilityLabel?: ViewProps['accessibilityLabel']
1616
expanded: SharedValue<boolean>
17-
heightCollapsed: RefObject<number>
18-
heightExpanded: RefObject<number>
19-
heightKnob: RefObject<number>
17+
heightCollapsed: SharedValue<number>
18+
heightExpanded: SharedValue<number>
19+
heightKnob: SharedValue<number>
2020
heightOffset: SharedValue<number>
2121
onLayout?: ViewProps['onLayout']
2222
pressed: SharedValue<boolean>
@@ -42,18 +42,18 @@ const KnobContainer: FC<KnobContainerProps> = ({
4242
base.value = offset.value !== 0 ? offset.value : 0
4343
})
4444
.onChange((event) => {
45-
const maxDragDistance = heightExpanded.current! - heightCollapsed.current!
45+
const maxDragDistance = heightExpanded.value! - heightCollapsed.value!
4646
offset.value =
4747
base.value +
4848
(expanded.value
4949
? Math.min(Math.max(event.translationY, -maxDragDistance), 0)
5050
: Math.min(Math.max(0, event.translationY), maxDragDistance))
5151
heightOffset.value = expanded.value
52-
? offset.value - heightCollapsed.current!
52+
? offset.value - heightCollapsed.value!
5353
: offset.value
5454
})
5555
.onFinalize(() => {
56-
const maxDragDistance = heightExpanded.current! - heightCollapsed.current!
56+
const maxDragDistance = heightExpanded.value! - heightCollapsed.value!
5757
const toggleLimit = maxDragDistance / 2
5858
expanded.value = +offset.value > toggleLimit
5959
offset.value = withClamp(
@@ -69,7 +69,7 @@ const KnobContainer: FC<KnobContainerProps> = ({
6969
})
7070

7171
const handleTapEnd = () => {
72-
const maxDragDistance = heightExpanded.current! - heightCollapsed.current!
72+
const maxDragDistance = heightExpanded.value! - heightCollapsed.value!
7373
expanded.value = !expanded.value
7474
offset.value = withClamp(
7575
{min: 0, max: maxDragDistance},

0 commit comments

Comments
 (0)