-
-
Notifications
You must be signed in to change notification settings - Fork 957
Expand file tree
/
Copy pathuseBottomSheetContentContainerStyle.ts
More file actions
89 lines (83 loc) · 2.58 KB
/
Copy pathuseBottomSheetContentContainerStyle.ts
File metadata and controls
89 lines (83 loc) · 2.58 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
import { useMemo, useState } from 'react';
import {
Platform,
StyleSheet,
type ViewProps,
type ViewStyle,
} from 'react-native';
import { useAnimatedReaction } from 'react-native-reanimated';
import { scheduleOnRN } from 'react-native-worklets';
import { useBottomSheetInternal } from './useBottomSheetInternal';
export function useBottomSheetContentContainerStyle(
enableFooterMarginAdjustment: boolean,
_style?: ViewProps['style']
) {
const [footerHeight, setFooterHeight] = useState(0);
//#region hooks
const { animatedLayoutState } = useBottomSheetInternal();
//#endregion
//#region styles
const flattenStyle = useMemo<ViewStyle>(() => {
return !_style
? {}
: Array.isArray(_style)
? // @ts-ignore
(StyleSheet.compose(..._style) as ViewStyle)
: (_style as ViewStyle);
}, [_style]);
const style = useMemo<ViewProps['style']>(() => {
if (!enableFooterMarginAdjustment) {
return flattenStyle;
}
let currentBottomPadding = 0;
if (flattenStyle && typeof flattenStyle === 'object') {
const { paddingBottom, padding, paddingVertical } = flattenStyle;
if (paddingBottom !== undefined && typeof paddingBottom === 'number') {
currentBottomPadding = paddingBottom;
} else if (
paddingVertical !== undefined &&
typeof paddingVertical === 'number'
) {
currentBottomPadding = paddingVertical;
} else if (padding !== undefined && typeof padding === 'number') {
currentBottomPadding = padding;
}
}
return [
flattenStyle,
{
paddingBottom: currentBottomPadding + footerHeight,
overflow: 'visible',
},
];
}, [footerHeight, enableFooterMarginAdjustment, flattenStyle]);
//#endregion
//#region effects
useAnimatedReaction(
() => animatedLayoutState.get().footerHeight,
(result, previousFooterHeight) => {
if (!enableFooterMarginAdjustment) {
return;
}
scheduleOnRN(setFooterHeight, result);
if (Platform.OS === 'web') {
/**
* a reaction that will append the footer height to the content
* height if margin adjustment is true.
*
* This is needed due to the web layout the footer after the content.
*/
if (result && !previousFooterHeight) {
animatedLayoutState.modify(state => {
'worklet';
state.contentHeight = state.contentHeight + result;
return state;
});
}
}
},
[animatedLayoutState, enableFooterMarginAdjustment]
);
//#endregion
return style;
}