Skip to content

Commit

Permalink
add FocusAwareStatusBar, useIsFocused
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed May 15, 2023
1 parent e3b7ad9 commit 95a0b37
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ navigation://home/post/post-1
lazyWithPreload // only available on the web: see example app
Redirect,
StatusBar,
FocusAwareStatusBar,
NavigationRoot,
createNavigation,
// refreshBottomTabs, TODO: implement this
Expand All @@ -407,10 +408,14 @@ navigation://home/post/post-1
createSimpleTheme,
setPreloadResult, // should not need this as it is done automatically

// hooks
useTheme,
// common hooks
useParams,
useNavigation,


// extra
useTheme,
useIsFocused,
useFocus,// same as useNavigating
useNavigating,
useNavigated,
Expand Down
14 changes: 14 additions & 0 deletions src/FocusAwareStatusBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react';
import StatusBar from './StatusBar';
import type { StatusBarProps } from 'expo-status-bar';
import useIsFocused from './useIsFocused';

function FocusAwareStatusBar(props: StatusBarProps) {
const isFocused = useIsFocused();
if (!isFocused) {
return null;
}
return <StatusBar {...props} />;
}

export default React.memo(FocusAwareStatusBar);
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export { default as BottomTabLink } from './BottomTabLink';
export { usePreloadResult } from './Preloader';
export { default as lazy } from './lazy';
export { default as StatusBar } from './StatusBar';
export { default as FocusAwareStatusBar } from './FocusAwareStatusBar';
export {
useNavigating as useFocus,
useNavigating,
useNavigated,
useUnloaded,
useUnloading,
} from './useFocus';
export { default as useIsFocused } from './useIsFocused';
export { default as useParams } from './useParams';
export { default as useBottomTabBadges } from './useBottomTabBadges';
export { default as useBottomTabIndex } from './useBottomTabIndex';
Expand Down
14 changes: 14 additions & 0 deletions src/useIsFocused.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react';
import { useNavigated, useUnloaded } from './useFocus';

export default function useIsFocused() {
const [isFocused, setIsFocused] = React.useState(true);
useNavigated(() => {
console.log('isFocused');
setIsFocused(true);
});
useUnloaded(() => {
setIsFocused(false);
});
return isFocused;
}

0 comments on commit 95a0b37

Please sign in to comment.