mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React, {useContext, useMemo} from 'react'
2import {View, ViewStyle} from 'react-native'
3import {StyleProp} from 'react-native'
4import {useSafeAreaInsets} from 'react-native-safe-area-context'
5
6import {ViewHeader} from '#/view/com/util/ViewHeader'
7import {ScrollView} from '#/view/com/util/Views'
8import {CenteredView} from '#/view/com/util/Views'
9import {atoms as a} from '#/alf'
10
11// Every screen should have a Layout component wrapping it.
12// This component provides a default padding for the top of the screen.
13// This allows certain screens to avoid the top padding if they want to.
14
15const LayoutContext = React.createContext({
16 withinScreen: false,
17 topPaddingDisabled: false,
18 withinScrollView: false,
19})
20
21/**
22 * Every screen should have a Layout.Screen component wrapping it.
23 * This component provides a default padding for the top of the screen
24 * and height/minHeight
25 */
26let Screen = ({
27 disableTopPadding = false,
28 style,
29 ...props
30}: React.ComponentProps<typeof View> & {
31 disableTopPadding?: boolean
32 style?: StyleProp<ViewStyle>
33}): React.ReactNode => {
34 const {top} = useSafeAreaInsets()
35 const context = useMemo(
36 () => ({
37 withinScreen: true,
38 topPaddingDisabled: disableTopPadding,
39 withinScrollView: false,
40 }),
41 [disableTopPadding],
42 )
43 return (
44 <LayoutContext.Provider value={context}>
45 <View
46 style={[
47 {paddingTop: disableTopPadding ? 0 : top},
48 a.util_screen_outer,
49 style,
50 ]}
51 {...props}
52 />
53 </LayoutContext.Provider>
54 )
55}
56Screen = React.memo(Screen)
57export {Screen}
58
59let Header = (
60 props: React.ComponentProps<typeof ViewHeader>,
61): React.ReactNode => {
62 const {withinScrollView} = useContext(LayoutContext)
63 if (!withinScrollView) {
64 return (
65 <CenteredView topBorder={false} sideBorders>
66 <ViewHeader showOnDesktop showBorder {...props} />
67 </CenteredView>
68 )
69 } else {
70 return <ViewHeader showOnDesktop showBorder {...props} />
71 }
72}
73Header = React.memo(Header)
74export {Header}
75
76let Content = ({
77 style,
78 contentContainerStyle,
79 ...props
80}: React.ComponentProps<typeof ScrollView> & {
81 style?: StyleProp<ViewStyle>
82 contentContainerStyle?: StyleProp<ViewStyle>
83}): React.ReactNode => {
84 const context = useContext(LayoutContext)
85 const newContext = useMemo(
86 () => ({...context, withinScrollView: true}),
87 [context],
88 )
89 return (
90 <LayoutContext.Provider value={newContext}>
91 <ScrollView
92 style={[a.flex_1, style]}
93 contentContainerStyle={[{paddingBottom: 100}, contentContainerStyle]}
94 {...props}
95 />
96 </LayoutContext.Provider>
97 )
98}
99Content = React.memo(Content)
100export {Content}