Skip to content

Commit

Permalink
fix: remove keyboard context
Browse files Browse the repository at this point in the history
  • Loading branch information
lordspace74 committed Mar 24, 2023
1 parent 25b587f commit 7e9881c
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 132 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ We believe this is a better approach than the alternatives:

**Note!** At the moment there exist some parallel solutions, a legacy one which uses react context and a newer one, which we recommend, using hooks (utilizes [zustand](https:/pmndrs/zustand) under the hood). They are:

- `useKeyboardLayout`
- `useSharedPortalArea`
- `useSnackbar`

Expand Down
117 changes: 0 additions & 117 deletions src/contexts/Keyboard.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './Auth'
export * from './Keyboard'
export * from './SharedPortalArea'
export * from './Snackbar'
export * from './Strings'
Expand Down
4 changes: 1 addition & 3 deletions src/example-app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import { SafeAreaProvider } from 'react-native-safe-area-context'
import NativePortal from '../components/NativePortal'
import DefaultSnackbarComponent from '../components/SnackbarComponent'
import { StringsProvider } from '../contexts/Strings'
import { useIsKeyboardShown, useKeyboardHeight, useWillKeyboardBeShown } from '../hooks'
import useAlert from '../hooks/useAlert'
import useConfirm from '../hooks/useConfirm'
import {
useIsKeyboardShown, useKeyboardHeight, useWillKeyboardBeShown,
} from '../hooks/useKeyboardLayout'
import {
SharedPortalAreaProvider, SharedPortalPresentationArea, useSharedPortalAreaInsets, useSharedPortalAreaSize,
} from '../hooks/useSharedPortalArea'
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ export * from './useDebounce'
export * from './useDecodedToken'
export * from './useEvent'
export * from './useInterval'
export * from './useIsKeyboardShown'
export * from './useIsLoggedIn'
export * from './useIsOnline'
export * from './useIsStandalonePWA'
export * from './useKeyboardHeight'
export * from './useNullableState'
export * from './usePrevious'
export * from './useRunIfMounted'
export * from './useTheme'
export * from './useToggle'
export * from './useToggleWithHaptics'
export * from './useWillKeyboardBeShown'
7 changes: 7 additions & 0 deletions src/hooks/useIsKeyboardShown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import useKeyboardLayoutStore from './useKeyboardLayoutStore'

export function useIsKeyboardShown() {
return useKeyboardLayoutStore((state) => state.isKeyboardShown)
}

export default useIsKeyboardShown
9 changes: 9 additions & 0 deletions src/hooks/useKeyboardHeight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import useKeyboardLayoutStore from './useKeyboardLayoutStore'

/**
* The height of the keyboard will not be reset to 0 when the keyboard is hidden,
* use {@link useIsKeyboardShown} to check if the keyboard is currently shown.
*/
export function useKeyboardHeight() {
return useKeyboardLayoutStore((state) => state.keyboardHeight)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ interface KeyboardLayoutStore {
readonly setWillKeyboardBeHidden: () => void
}

const useKeyboardLayoutStore = create<KeyboardLayoutStore>()(
/**
* This zustand store is used to keep track of the keyboard layout.
*
* Prefer using the individual hooks
* {@link useIsKeyboardShown}, {@link useKeyboardHeight}, {@link useWillKeyboardBeShown}
* over this directly.
*/
export const useKeyboardLayoutStore = create<KeyboardLayoutStore>()(
devtools(
(set) => {
Keyboard.addListener('keyboardDidShow', (event) => set({
Expand All @@ -41,12 +48,4 @@ const useKeyboardLayoutStore = create<KeyboardLayoutStore>()(
),
)

export const useIsKeyboardShown = () => useKeyboardLayoutStore((state) => state.isKeyboardShown)

/**
* The height of the keyboard will not be reset to 0 when the keyboard is hidden,
* use {@link useIsKeyboardShown} to check if the keyboard is currently shown.
*/
export const useKeyboardHeight = () => useKeyboardLayoutStore((state) => state.keyboardHeight)

export const useWillKeyboardBeShown = () => useKeyboardLayoutStore((state) => state.willKeyboardBeShown)
export default useKeyboardLayoutStore
7 changes: 7 additions & 0 deletions src/hooks/useWillKeyboardBeShown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import useKeyboardLayoutStore from './useKeyboardLayoutStore'

export function useWillKeyboardBeShown() {
return useKeyboardLayoutStore((state) => state.willKeyboardBeShown)
}

export default useWillKeyboardBeShown

0 comments on commit 7e9881c

Please sign in to comment.