-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32672 from margelo/@chrispader/fix-wrong-status-b…
…ar-colors Fix issues with scrollbar and status bar color (cherry picked from commit b4d1777)
- Loading branch information
Showing
17 changed files
with
184 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
src/components/CustomStatusBar/CustomStatusBarContextProvider.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/components/CustomStatusBarAndBackground/CustomStatusBarAndBackgroundContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {createContext} from 'react'; | ||
|
||
type CustomStatusBarAndBackgroundContextType = { | ||
isRootStatusBarDisabled: boolean; | ||
disableRootStatusBar: (isDisabled: boolean) => void; | ||
}; | ||
|
||
const CustomStatusBarAndBackgroundContext = createContext<CustomStatusBarAndBackgroundContextType>({isRootStatusBarDisabled: false, disableRootStatusBar: () => undefined}); | ||
|
||
export default CustomStatusBarAndBackgroundContext; | ||
export {type CustomStatusBarAndBackgroundContextType}; |
17 changes: 17 additions & 0 deletions
17
src/components/CustomStatusBarAndBackground/CustomStatusBarAndBackgroundContextProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React, {useMemo, useState} from 'react'; | ||
import CustomStatusBarAndBackgroundContext from './CustomStatusBarAndBackgroundContext'; | ||
|
||
function CustomStatusBarAndBackgroundContextProvider({children}: React.PropsWithChildren) { | ||
const [isRootStatusBarDisabled, disableRootStatusBar] = useState(false); | ||
const value = useMemo( | ||
() => ({ | ||
isRootStatusBarDisabled, | ||
disableRootStatusBar, | ||
}), | ||
[isRootStatusBarDisabled], | ||
); | ||
|
||
return <CustomStatusBarAndBackgroundContext.Provider value={value}>{children}</CustomStatusBarAndBackgroundContext.Provider>; | ||
} | ||
|
||
export default CustomStatusBarAndBackgroundContextProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import React, {useCallback, useContext, useEffect, useRef, useState} from 'react'; | ||
import useTheme from '@hooks/useTheme'; | ||
import {navigationRef} from '@libs/Navigation/Navigation'; | ||
import StatusBar from '@libs/StatusBar'; | ||
import CustomStatusBarAndBackgroundContext from './CustomStatusBarAndBackgroundContext'; | ||
import updateGlobalBackgroundColor from './updateGlobalBackgroundColor'; | ||
import updateStatusBarAppearance from './updateStatusBarAppearance'; | ||
|
||
type CustomStatusBarAndBackgroundProps = { | ||
/** Whether the CustomStatusBar is nested within another CustomStatusBar. | ||
* A nested CustomStatusBar will disable the "root" CustomStatusBar. */ | ||
isNested: boolean; | ||
}; | ||
|
||
function CustomStatusBarAndBackground({isNested = false}: CustomStatusBarAndBackgroundProps) { | ||
const {isRootStatusBarDisabled, disableRootStatusBar} = useContext(CustomStatusBarAndBackgroundContext); | ||
const theme = useTheme(); | ||
const [statusBarStyle, setStatusBarStyle] = useState(theme.statusBarStyle); | ||
|
||
const isDisabled = !isNested && isRootStatusBarDisabled; | ||
|
||
// Disable the root status bar when a nested status bar is rendered | ||
useEffect(() => { | ||
if (isNested) { | ||
disableRootStatusBar(true); | ||
} | ||
|
||
return () => { | ||
if (!isNested) { | ||
return; | ||
} | ||
disableRootStatusBar(false); | ||
}; | ||
}, [disableRootStatusBar, isNested]); | ||
|
||
const listenerCount = useRef(0); | ||
const updateStatusBarStyle = useCallback( | ||
(listenerId?: number) => { | ||
// Check if this function is either called through the current navigation listener or the general useEffect which listens for theme changes. | ||
if (listenerId !== undefined && listenerId !== listenerCount.current) { | ||
return; | ||
} | ||
|
||
// Set the status bar colour depending on the current route. | ||
// If we don't have any colour defined for a route, fall back to | ||
// appBG color. | ||
let currentRoute: ReturnType<typeof navigationRef.getCurrentRoute> | undefined; | ||
if (navigationRef.isReady()) { | ||
currentRoute = navigationRef.getCurrentRoute(); | ||
} | ||
|
||
let currentScreenBackgroundColor = theme.appBG; | ||
let newStatusBarStyle = theme.statusBarStyle; | ||
if (currentRoute && 'name' in currentRoute && currentRoute.name in theme.PAGE_THEMES) { | ||
const screenTheme = theme.PAGE_THEMES[currentRoute.name]; | ||
currentScreenBackgroundColor = screenTheme.backgroundColor; | ||
newStatusBarStyle = screenTheme.statusBarStyle; | ||
} | ||
|
||
// Don't update the status bar style if it's the same as the current one, to prevent flashing. | ||
if (newStatusBarStyle === statusBarStyle) { | ||
updateStatusBarAppearance({backgroundColor: currentScreenBackgroundColor}); | ||
} else { | ||
updateStatusBarAppearance({backgroundColor: currentScreenBackgroundColor, statusBarStyle: newStatusBarStyle}); | ||
setStatusBarStyle(newStatusBarStyle); | ||
} | ||
}, | ||
[statusBarStyle, theme.PAGE_THEMES, theme.appBG, theme.statusBarStyle], | ||
); | ||
|
||
// Add navigation state listeners to update the status bar every time the route changes | ||
// We have to pass a count as the listener id, because "react-navigation" somehow doesn't remove listeners properyl | ||
useEffect(() => { | ||
if (isDisabled) { | ||
return; | ||
} | ||
|
||
const listenerId = ++listenerCount.current; | ||
const listener = () => updateStatusBarStyle(listenerId); | ||
|
||
navigationRef.addListener('state', listener); | ||
return () => navigationRef.removeListener('state', listener); | ||
}, [isDisabled, theme.appBG, updateStatusBarStyle]); | ||
|
||
// Update the status bar style everytime the theme changes | ||
useEffect(() => { | ||
if (isDisabled) { | ||
return; | ||
} | ||
|
||
updateStatusBarStyle(); | ||
}, [isDisabled, theme, updateStatusBarStyle]); | ||
|
||
// Update the global background (on web) everytime the theme changes. | ||
// The background of the html element needs to be updated, otherwise you will see a big contrast when resizing the window or when the keyboard is open on iOS web. | ||
useEffect(() => { | ||
if (isDisabled) { | ||
return; | ||
} | ||
|
||
updateGlobalBackgroundColor(theme); | ||
}, [isDisabled, theme]); | ||
|
||
if (isDisabled) { | ||
return null; | ||
} | ||
|
||
return <StatusBar />; | ||
} | ||
|
||
CustomStatusBarAndBackground.displayName = 'CustomStatusBarAndBackground'; | ||
|
||
export default CustomStatusBarAndBackground; |
5 changes: 5 additions & 0 deletions
5
src/components/CustomStatusBarAndBackground/updateGlobalBackgroundColor/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type UpdateGlobalBackgroundColor from './types'; | ||
|
||
const updateGlobalBackgroundColor: UpdateGlobalBackgroundColor = () => undefined; | ||
|
||
export default updateGlobalBackgroundColor; |
8 changes: 8 additions & 0 deletions
8
src/components/CustomStatusBarAndBackground/updateGlobalBackgroundColor/index.website.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import UpdateGlobalBackgroundColor from './types'; | ||
|
||
const updateGlobalBackgroundColor: UpdateGlobalBackgroundColor = (theme) => { | ||
const htmlElement = document.getElementsByTagName('html')[0]; | ||
htmlElement.style.setProperty('background-color', theme.appBG); | ||
}; | ||
|
||
export default updateGlobalBackgroundColor; |
5 changes: 5 additions & 0 deletions
5
src/components/CustomStatusBarAndBackground/updateGlobalBackgroundColor/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {ThemeColors} from '@styles/theme/types'; | ||
|
||
type UpdateGlobalBackgroundColor = (theme: ThemeColors) => void; | ||
|
||
export default UpdateGlobalBackgroundColor; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters