Skip to content

Commit

Permalink
refactor: move useKeyboard into hook
Browse files Browse the repository at this point in the history
  • Loading branch information
paulschreiber committed Jun 19, 2024
1 parent 3588ffc commit b785809
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
39 changes: 39 additions & 0 deletions dev-client/src/hooks/useKeyboardStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright © 2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import {useLayoutEffect, useState} from 'react';
import {Keyboard} from 'react-native';

export const useKeyboardStatus = () => {
const [keyboardStatus, setKeyboardStatus] = useState(false);

useLayoutEffect(() => {
const showSubscription = Keyboard.addListener('keyboardDidShow', () => {
setKeyboardStatus(true);
});
const hideSubscription = Keyboard.addListener('keyboardDidHide', () => {
setKeyboardStatus(false);
});

return () => {
showSubscription.remove();
hideSubscription.remove();
};
}, []);

return {keyboardStatus};
};
20 changes: 3 additions & 17 deletions dev-client/src/screens/BottomTabsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import {memo, useLayoutEffect, useState} from 'react';
import {Keyboard} from 'react-native';
import {memo} from 'react';

import {NavigationHelpers} from '@react-navigation/native';

import {useKeyboardStatus} from 'terraso-mobile-client/hooks/useKeyboardStatus';
import {
BottomNavigator,
BottomTabs,
Expand All @@ -30,21 +30,7 @@ import {ProjectListScreen} from 'terraso-mobile-client/screens/ProjectListScreen
import {UserSettingsScreen} from 'terraso-mobile-client/screens/UserSettingsScreen/UserSettingsScreen';

export const BottomTabsScreen = memo(() => {
const [keyboardStatus, setKeyboardStatus] = useState(false);

useLayoutEffect(() => {
const showSubscription = Keyboard.addListener('keyboardDidShow', () => {
setKeyboardStatus(true);
});
const hideSubscription = Keyboard.addListener('keyboardDidHide', () => {
setKeyboardStatus(false);
});

return () => {
showSubscription.remove();
hideSubscription.remove();
};
}, []);
const {keyboardStatus} = useKeyboardStatus();

return (
<BottomTabs.Navigator
Expand Down

0 comments on commit b785809

Please sign in to comment.