Centered layout
This commit is contained in:
+162
-28
@@ -1,12 +1,17 @@
|
|||||||
import React, {useContext, useMemo} from 'react'
|
import React, {useContext, useMemo} from 'react'
|
||||||
import {View, ViewStyle} from 'react-native'
|
import {StyleSheet, View, ViewProps, ViewStyle} from 'react-native'
|
||||||
import {StyleProp} from 'react-native'
|
import {StyleProp} from 'react-native'
|
||||||
|
import {
|
||||||
|
KeyboardAwareScrollView,
|
||||||
|
KeyboardAwareScrollViewProps,
|
||||||
|
} from 'react-native-keyboard-controller'
|
||||||
|
import Animated, {AnimatedScrollViewProps} from 'react-native-reanimated'
|
||||||
import {useSafeAreaInsets} from 'react-native-safe-area-context'
|
import {useSafeAreaInsets} from 'react-native-safe-area-context'
|
||||||
|
|
||||||
|
import {isWeb} from '#/platform/detection'
|
||||||
import {ViewHeader} from '#/view/com/util/ViewHeader'
|
import {ViewHeader} from '#/view/com/util/ViewHeader'
|
||||||
import {ScrollView} from '#/view/com/util/Views'
|
|
||||||
import {CenteredView} from '#/view/com/util/Views'
|
import {CenteredView} from '#/view/com/util/Views'
|
||||||
import {atoms as a} from '#/alf'
|
import {atoms as a, useBreakpoints, useTheme, ViewStyleProp} from '#/alf'
|
||||||
|
|
||||||
// Every screen should have a Layout component wrapping it.
|
// Every screen should have a Layout component wrapping it.
|
||||||
// This component provides a default padding for the top of the screen.
|
// This component provides a default padding for the top of the screen.
|
||||||
@@ -18,19 +23,21 @@ const LayoutContext = React.createContext({
|
|||||||
withinScrollView: false,
|
withinScrollView: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export type ScreenProps = React.ComponentProps<typeof View> & {
|
||||||
|
disableTopPadding?: boolean
|
||||||
|
style?: StyleProp<ViewStyle>
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Every screen should have a Layout.Screen component wrapping it.
|
* Every screen should have a Layout.Screen component wrapping it.
|
||||||
* This component provides a default padding for the top of the screen
|
* This component provides a default padding for the top of the screen
|
||||||
* and height/minHeight
|
* and height/minHeight
|
||||||
*/
|
*/
|
||||||
let Screen = ({
|
export const Screen = React.memo(function Screen({
|
||||||
disableTopPadding = false,
|
disableTopPadding = false,
|
||||||
style,
|
style,
|
||||||
...props
|
...props
|
||||||
}: React.ComponentProps<typeof View> & {
|
}: ScreenProps) {
|
||||||
disableTopPadding?: boolean
|
|
||||||
style?: StyleProp<ViewStyle>
|
|
||||||
}): React.ReactNode => {
|
|
||||||
const {top} = useSafeAreaInsets()
|
const {top} = useSafeAreaInsets()
|
||||||
const context = useMemo(
|
const context = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
@@ -52,13 +59,11 @@ let Screen = ({
|
|||||||
/>
|
/>
|
||||||
</LayoutContext.Provider>
|
</LayoutContext.Provider>
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
Screen = React.memo(Screen)
|
|
||||||
export {Screen}
|
|
||||||
|
|
||||||
let Header = (
|
export const Header = React.memo(function Header(
|
||||||
props: React.ComponentProps<typeof ViewHeader>,
|
props: React.ComponentProps<typeof ViewHeader>,
|
||||||
): React.ReactNode => {
|
) {
|
||||||
const {withinScrollView} = useContext(LayoutContext)
|
const {withinScrollView} = useContext(LayoutContext)
|
||||||
if (!withinScrollView) {
|
if (!withinScrollView) {
|
||||||
return (
|
return (
|
||||||
@@ -69,18 +74,19 @@ let Header = (
|
|||||||
} else {
|
} else {
|
||||||
return <ViewHeader showOnDesktop showBorder {...props} />
|
return <ViewHeader showOnDesktop showBorder {...props} />
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
Header = React.memo(Header)
|
|
||||||
export {Header}
|
|
||||||
|
|
||||||
let Content = ({
|
export type ContentProps = AnimatedScrollViewProps & {
|
||||||
|
style?: StyleProp<ViewStyle>
|
||||||
|
contentContainerStyle?: StyleProp<ViewStyle>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Content = React.memo(function Content({
|
||||||
|
children,
|
||||||
style,
|
style,
|
||||||
contentContainerStyle,
|
contentContainerStyle,
|
||||||
...props
|
...props
|
||||||
}: React.ComponentProps<typeof ScrollView> & {
|
}: ContentProps) {
|
||||||
style?: StyleProp<ViewStyle>
|
|
||||||
contentContainerStyle?: StyleProp<ViewStyle>
|
|
||||||
}): React.ReactNode => {
|
|
||||||
const context = useContext(LayoutContext)
|
const context = useContext(LayoutContext)
|
||||||
const newContext = useMemo(
|
const newContext = useMemo(
|
||||||
() => ({...context, withinScrollView: true}),
|
() => ({...context, withinScrollView: true}),
|
||||||
@@ -88,13 +94,141 @@ let Content = ({
|
|||||||
)
|
)
|
||||||
return (
|
return (
|
||||||
<LayoutContext.Provider value={newContext}>
|
<LayoutContext.Provider value={newContext}>
|
||||||
<ScrollView
|
<Animated.ScrollView
|
||||||
style={[a.flex_1, style]}
|
id="content"
|
||||||
contentContainerStyle={[{paddingBottom: 100}, contentContainerStyle]}
|
style={[
|
||||||
{...props}
|
styles.scrollViewCommonStyles,
|
||||||
/>
|
style,
|
||||||
|
styles.scrollViewOverrideStyles,
|
||||||
|
]}
|
||||||
|
contentContainerStyle={[
|
||||||
|
styles.scrollViewContentContainer,
|
||||||
|
contentContainerStyle,
|
||||||
|
]}
|
||||||
|
{...props}>
|
||||||
|
{isWeb ? (
|
||||||
|
// @ts-ignore web only -esb
|
||||||
|
<Center>{children}</Center>
|
||||||
|
) : (
|
||||||
|
children
|
||||||
|
)}
|
||||||
|
</Animated.ScrollView>
|
||||||
</LayoutContext.Provider>
|
</LayoutContext.Provider>
|
||||||
)
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
export type KeyboardAwareContentProps = KeyboardAwareScrollViewProps & {
|
||||||
|
children: React.ReactNode
|
||||||
|
contentContainerStyle?: StyleProp<ViewStyle>
|
||||||
}
|
}
|
||||||
Content = React.memo(Content)
|
|
||||||
export {Content}
|
export const KeyboardAwareContent = React.forwardRef(function LayoutScrollView(
|
||||||
|
{children, style, contentContainerStyle, ...props}: KeyboardAwareContentProps,
|
||||||
|
_ref: React.Ref<typeof KeyboardAwareScrollView>,
|
||||||
|
) {
|
||||||
|
return (
|
||||||
|
<KeyboardAwareScrollView
|
||||||
|
style={[
|
||||||
|
styles.scrollViewCommonStyles,
|
||||||
|
style,
|
||||||
|
styles.scrollViewOverrideStyles,
|
||||||
|
]}
|
||||||
|
contentContainerStyle={[
|
||||||
|
styles.scrollViewContentContainer,
|
||||||
|
contentContainerStyle,
|
||||||
|
]}
|
||||||
|
keyboardShouldPersistTaps="handled"
|
||||||
|
{...props}>
|
||||||
|
{isWeb ? <Center>{children}</Center> : children}
|
||||||
|
</KeyboardAwareScrollView>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
export const Center = React.forwardRef(function LayoutContent(
|
||||||
|
{children, style, ...props}: ViewProps,
|
||||||
|
ref: React.Ref<View>,
|
||||||
|
) {
|
||||||
|
const t = useTheme()
|
||||||
|
const {gtMobile} = useBreakpoints()
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{gtMobile && (
|
||||||
|
<View
|
||||||
|
style={[
|
||||||
|
a.fixed,
|
||||||
|
a.inset_0,
|
||||||
|
a.border_l,
|
||||||
|
a.border_r,
|
||||||
|
t.atoms.border_contrast_low,
|
||||||
|
{
|
||||||
|
width: 602,
|
||||||
|
left: '50%',
|
||||||
|
transform: [
|
||||||
|
{
|
||||||
|
translateX: '-50%',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<View
|
||||||
|
ref={ref}
|
||||||
|
style={[
|
||||||
|
a.util_screen_outer,
|
||||||
|
a.w_full,
|
||||||
|
a.mx_auto,
|
||||||
|
gtMobile && {
|
||||||
|
maxWidth: 600,
|
||||||
|
},
|
||||||
|
style,
|
||||||
|
]}
|
||||||
|
{...props}>
|
||||||
|
{children}
|
||||||
|
</View>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
export function Gutter({
|
||||||
|
children,
|
||||||
|
top,
|
||||||
|
bottom,
|
||||||
|
style,
|
||||||
|
}: ViewStyleProp & {
|
||||||
|
children: React.ReactNode
|
||||||
|
top?: boolean
|
||||||
|
bottom?: boolean
|
||||||
|
}) {
|
||||||
|
const {gtMobile} = useBreakpoints()
|
||||||
|
return (
|
||||||
|
<View
|
||||||
|
style={[
|
||||||
|
a.px_lg,
|
||||||
|
top && a.pt_lg,
|
||||||
|
bottom && a.pb_lg,
|
||||||
|
gtMobile && [a.px_xl, top && a.pt_xl, bottom && a.pb_xl],
|
||||||
|
style,
|
||||||
|
]}>
|
||||||
|
{children}
|
||||||
|
</View>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const styles = StyleSheet.create({
|
||||||
|
scrollViewCommonStyles: {
|
||||||
|
width: '100%',
|
||||||
|
},
|
||||||
|
scrollViewContentContainer: {
|
||||||
|
paddingBottom: 100,
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Applied last to ensure they override any locally-defined styles. Use sparingly.
|
||||||
|
*/
|
||||||
|
scrollViewOverrideStyles: {
|
||||||
|
/*
|
||||||
|
* Ensures fixed items within `ScrollView` are fixed relative to window.
|
||||||
|
*/
|
||||||
|
transform: 'unset',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user