From 3044e02ddd57d007f4a39721d89a779a0829e3f0 Mon Sep 17 00:00:00 2001 From: kirillzyusko Date: Sun, 29 Oct 2023 21:55:53 +0100 Subject: [PATCH] refactor: port to fabric --- .../KeyboardAwareScrollView.tsx | 15 +++--- .../src/components/AwareScrollView/index.ts | 1 + .../useSmoothKeyboardHandler.ts | 0 .../TextInput/index.tsx} | 2 +- FabricExample/src/constants/screenNames.ts | 1 + .../src/navigation/ExamplesStack/index.tsx | 10 ++++ .../Examples/AwareScrollView/index.tsx | 6 +-- .../AwareScrollViewStickyFooter/index.tsx | 48 +++++++++++++++++++ .../AwareScrollViewStickyFooter/styles.ts | 26 ++++++++++ .../src/screens/Examples/Main/constants.ts | 5 ++ TODO | 10 ---- .../AwareScrollViewStickyFooter/index.tsx | 2 - 12 files changed, 104 insertions(+), 22 deletions(-) rename FabricExample/src/{screens/Examples => components}/AwareScrollView/KeyboardAwareScrollView.tsx (95%) create mode 100644 FabricExample/src/components/AwareScrollView/index.ts rename FabricExample/src/{screens/Examples => components}/AwareScrollView/useSmoothKeyboardHandler.ts (100%) rename FabricExample/src/{screens/Examples/AwareScrollView/TextInput.tsx => components/TextInput/index.tsx} (96%) create mode 100644 FabricExample/src/screens/Examples/AwareScrollViewStickyFooter/index.tsx create mode 100644 FabricExample/src/screens/Examples/AwareScrollViewStickyFooter/styles.ts delete mode 100644 TODO diff --git a/FabricExample/src/screens/Examples/AwareScrollView/KeyboardAwareScrollView.tsx b/FabricExample/src/components/AwareScrollView/KeyboardAwareScrollView.tsx similarity index 95% rename from FabricExample/src/screens/Examples/AwareScrollView/KeyboardAwareScrollView.tsx rename to FabricExample/src/components/AwareScrollView/KeyboardAwareScrollView.tsx index 6019acf6e..3d028f080 100644 --- a/FabricExample/src/screens/Examples/AwareScrollView/KeyboardAwareScrollView.tsx +++ b/FabricExample/src/components/AwareScrollView/KeyboardAwareScrollView.tsx @@ -13,7 +13,9 @@ import Reanimated, { } from 'react-native-reanimated'; import { useSmoothKeyboardHandler } from './useSmoothKeyboardHandler'; -const BOTTOM_OFFSET = 50; +type KeyboardAwareScrollViewProps = { + bottomOffset?: number; +} & ScrollViewProps; /** * Everything begins from `onStart` handler. This handler is called every time, @@ -53,8 +55,9 @@ const BOTTOM_OFFSET = 50; * +============================+ +============================+ +=====================================+ * */ -const KeyboardAwareScrollView: FC = ({ +const KeyboardAwareScrollView: FC = ({ children, + bottomOffset = 0, ...rest }) => { const scrollViewAnimatedRef = useAnimatedRef(); @@ -85,11 +88,11 @@ const KeyboardAwareScrollView: FC = ({ const visibleRect = height - keyboardHeight.value; const point = (layout.value?.layout.absoluteY || 0) + (layout.value?.layout.height || 0); - if (visibleRect - point <= BOTTOM_OFFSET) { + if (visibleRect - point <= bottomOffset) { const interpolatedScrollTo = interpolate( e, [initialKeyboardSize.value, keyboardHeight.value], - [0, keyboardHeight.value - (height - point) + BOTTOM_OFFSET] + [0, keyboardHeight.value - (height - point) + bottomOffset] ); const targetScrollY = Math.max(interpolatedScrollTo, 0) + scrollPosition.value; @@ -99,7 +102,7 @@ const KeyboardAwareScrollView: FC = ({ } return 0; - }, []); + }, [bottomOffset]); useSmoothKeyboardHandler( { @@ -158,7 +161,7 @@ const KeyboardAwareScrollView: FC = ({ scrollPosition.value = position.value; }, }, - [height] + [height, maybeScroll] ); useAnimatedReaction(() => input.value, (current, previous) => { diff --git a/FabricExample/src/components/AwareScrollView/index.ts b/FabricExample/src/components/AwareScrollView/index.ts new file mode 100644 index 000000000..5903f0ef7 --- /dev/null +++ b/FabricExample/src/components/AwareScrollView/index.ts @@ -0,0 +1 @@ +export { default } from "./KeyboardAwareScrollView"; diff --git a/FabricExample/src/screens/Examples/AwareScrollView/useSmoothKeyboardHandler.ts b/FabricExample/src/components/AwareScrollView/useSmoothKeyboardHandler.ts similarity index 100% rename from FabricExample/src/screens/Examples/AwareScrollView/useSmoothKeyboardHandler.ts rename to FabricExample/src/components/AwareScrollView/useSmoothKeyboardHandler.ts diff --git a/FabricExample/src/screens/Examples/AwareScrollView/TextInput.tsx b/FabricExample/src/components/TextInput/index.tsx similarity index 96% rename from FabricExample/src/screens/Examples/AwareScrollView/TextInput.tsx rename to FabricExample/src/components/TextInput/index.tsx index 00846b8f8..f9b92bf5a 100644 --- a/FabricExample/src/screens/Examples/AwareScrollView/TextInput.tsx +++ b/FabricExample/src/components/TextInput/index.tsx @@ -7,7 +7,7 @@ const TextInput = (props: TextInputProps) => { placeholderTextColor="#6c6c6c" style={styles.container} multiline - numberOfLines={10} + numberOfLines={2} {...props} placeholder={`${props.placeholder} (${props.keyboardType === 'default' ? 'text' : 'numeric'})`} /> diff --git a/FabricExample/src/constants/screenNames.ts b/FabricExample/src/constants/screenNames.ts index 6020257e6..911d71bba 100644 --- a/FabricExample/src/constants/screenNames.ts +++ b/FabricExample/src/constants/screenNames.ts @@ -3,6 +3,7 @@ export enum ScreenNames { REANIMATED_CHAT = 'REANIMATED_CHAT', EVENTS = 'EVENTS', AWARE_SCROLL_VIEW = 'AWARE_SCROLL_VIEW', + AWARE_SCROLL_VIEW_STICKY_FOOTER = 'AWARE_SCROLL_VIEW_STICKY_FOOTER', STATUS_BAR = 'STATUS_BAR', LOTTIE = 'LOTTIE', EXAMPLES_STACK = 'EXAMPLES_STACK', diff --git a/FabricExample/src/navigation/ExamplesStack/index.tsx b/FabricExample/src/navigation/ExamplesStack/index.tsx index b6c7e497d..61d8f62bc 100644 --- a/FabricExample/src/navigation/ExamplesStack/index.tsx +++ b/FabricExample/src/navigation/ExamplesStack/index.tsx @@ -15,12 +15,14 @@ import InteractiveKeyboardIOS from '../../screens/Examples/InteractiveKeyboardIO import NativeStack from '../NestedStack'; import KeyboardAvoidingViewExample from '../../screens/Examples/KeyboardAvoidingView'; import EnabledDisabled from '../../screens/Examples/EnabledDisabled'; +import AwareScrollViewStickyFooter from '../../screens/Examples/AwareScrollViewStickyFooter'; export type ExamplesStackParamList = { [ScreenNames.ANIMATED_EXAMPLE]: undefined; [ScreenNames.REANIMATED_CHAT]: undefined; [ScreenNames.EVENTS]: undefined; [ScreenNames.AWARE_SCROLL_VIEW]: undefined; + [ScreenNames.AWARE_SCROLL_VIEW_STICKY_FOOTER]: undefined; [ScreenNames.STATUS_BAR]: undefined; [ScreenNames.LOTTIE]: undefined; [ScreenNames.NON_UI_PROPS]: undefined; @@ -46,6 +48,9 @@ const options = { [ScreenNames.AWARE_SCROLL_VIEW]: { title: 'Aware scroll view', }, + [ScreenNames.AWARE_SCROLL_VIEW_STICKY_FOOTER]: { + title: 'Aware scroll view sticky footer', + }, [ScreenNames.STATUS_BAR]: { headerShown: false, title: 'Status bar manipulation', @@ -95,6 +100,11 @@ const ExamplesStack = () => ( component={AwareScrollView} options={options[ScreenNames.AWARE_SCROLL_VIEW]} /> + + {new Array(10).fill(0).map((_, i) => ( { + setFooterHeight(evt.nativeEvent.layout.height); + }, []); + const offset = useMemo(() => ({closed: 0, opened: bottom }), [bottom]); + + return ( + + + {new Array(10).fill(0).map((_, i) => ( + + ))} + + + + A mocked sticky footer +