Skip to content

Commit 00d85a1

Browse files
alduzyja1ns
authored andcommitted
feat(Android): added navigationBarTranslucent option (software-mansion#2152)
## Description Once upon a time there was an [issue](software-mansion#1719) regarding navigation bar. The problem was that the background color set to the navigation bar covered the content behind, see examples: <table> <tr> <th>navigationBarColor: 'transparent'</th> <th>navigationBarColor: 'yellow'</th> </tr> <tr> <td> ![Screenshot 2024-05-22 at 10 33 28](https://github.com/software-mansion/react-native-screens/assets/91994767/eb3ea5a5-4617-4547-a052-ab0612872697) </td> <td> ![Screenshot 2024-05-22 at 10 34 08](https://github.com/software-mansion/react-native-screens/assets/91994767/805fa199-b90c-40aa-9ef5-b5ab8abef71a) </td> </tr> </table> Then [PR 1988](software-mansion#1988) solved the issue, but introduced another problem: sometimes it may be intended to have content behind the navigation bar. <table> <tr> <th>navigationBarColor: 'transparent' after PR 1988</th> <th>navigationBarColor: 'transparent' before PR 1988</th> </tr> <tr> <td> ![Screenshot 2024-05-21 at 13 00 23](https://github.com/software-mansion/react-native-screens/assets/91994767/76ec2fa1-e212-4725-a1da-32bcf8c5eb84) </td> <td> ![Screenshot 2024-05-21 at 13 07 19](https://github.com/software-mansion/react-native-screens/assets/91994767/8fa15e57-7168-4fec-b8e7-3cce29eeebdc) </td> </tr> </table> This PR intents to add a new screen option, enabling navigation bar translucency control, so that the developers can decide if they want to show content behind the navigation bar independently. Fixes software-mansion#2122 software-mansion#1719 ## Changes - added navigationBarTranslucent option ## Test code and steps to reproduce <!-- Please include code that can be used to test this change and short description how this example should work. This snippet should be as minimal as possible and ready to be pasted into editor (don't exclude exports or remove "not important" parts of reproduction example) --> ## Checklist - [ ] Included code example that can be used to test this change - [ ] Updated TS types - [ ] Updated documentation: <!-- For adding new props to native-stack --> - [ ] https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md - [ ] https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md - [ ] https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx - [ ] https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx - [ ] Ensured that CI passes
1 parent fb8c7e6 commit 00d85a1

File tree

10 files changed

+57
-1
lines changed

10 files changed

+57
-1
lines changed

android/src/main/java/com/swmansion/rnscreens/Screen.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ class Screen(context: ReactContext?) : FabricEnabledViewGroup(context) {
216216
fragmentWrapper?.let { ScreenWindowTraits.setNavigationBarColor(this, it.tryGetActivity()) }
217217
}
218218

219+
var isNavigationBarTranslucent: Boolean? = null
220+
set(navigationBarTranslucent) {
221+
if (navigationBarTranslucent != null) {
222+
ScreenWindowTraits.applyDidSetNavigationBarAppearance()
223+
}
224+
field = navigationBarTranslucent
225+
fragmentWrapper?.let {
226+
ScreenWindowTraits.setNavigationBarTranslucent(
227+
this,
228+
it.tryGetActivity(),
229+
)
230+
}
231+
}
232+
219233
var isNavigationBarHidden: Boolean? = null
220234
set(navigationBarHidden) {
221235
if (navigationBarHidden != null) {
@@ -276,6 +290,6 @@ class Screen(context: ReactContext?) : FabricEnabledViewGroup(context) {
276290
}
277291

278292
enum class WindowTraits {
279-
ORIENTATION, COLOR, STYLE, TRANSLUCENT, HIDDEN, ANIMATED, NAVIGATION_BAR_COLOR, NAVIGATION_BAR_HIDDEN
293+
ORIENTATION, COLOR, STYLE, TRANSLUCENT, HIDDEN, ANIMATED, NAVIGATION_BAR_COLOR, NAVIGATION_BAR_TRANSLUCENT, NAVIGATION_BAR_HIDDEN
280294
}
281295
}

android/src/main/java/com/swmansion/rnscreens/ScreenViewManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ open class ScreenViewManager : ViewGroupManager<Screen>(), RNSScreenManagerInter
142142
view.navigationBarColor = navigationBarColor
143143
}
144144

145+
@ReactProp(name = "navigationBarTranslucent")
146+
override fun setNavigationBarTranslucent(view: Screen, navigationBarTranslucent: Boolean) {
147+
view.isNavigationBarTranslucent = navigationBarTranslucent
148+
}
149+
145150
@ReactProp(name = "navigationBarHidden")
146151
override fun setNavigationBarHidden(view: Screen, navigationBarHidden: Boolean) {
147152
view.isNavigationBarHidden = navigationBarHidden

android/src/main/java/com/swmansion/rnscreens/ScreenWindowTraits.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import android.os.Build
1111
import android.view.ViewParent
1212
import androidx.core.graphics.Insets
1313
import androidx.core.view.ViewCompat
14+
import androidx.core.view.WindowCompat
1415
import androidx.core.view.WindowInsetsCompat
1516
import androidx.core.view.WindowInsetsControllerCompat
1617
import com.facebook.react.bridge.GuardedRunnable
@@ -185,6 +186,20 @@ object ScreenWindowTraits {
185186
window.navigationBarColor = color
186187
}
187188

189+
internal fun setNavigationBarTranslucent(screen: Screen, activity: Activity?) {
190+
if (activity == null) {
191+
return
192+
}
193+
194+
val window = activity.window
195+
196+
val screenForNavBarTranslucent = findScreenForTrait(screen, WindowTraits.NAVIGATION_BAR_TRANSLUCENT)
197+
val translucent = screenForNavBarTranslucent?.isNavigationBarTranslucent ?: false
198+
199+
// Following method controls whether to display edge-to-edge content that draws behind the navigation bar
200+
WindowCompat.setDecorFitsSystemWindows(window, !translucent)
201+
}
202+
188203
internal fun setNavigationBarHidden(screen: Screen, activity: Activity?) {
189204
if (activity == null) {
190205
return
@@ -221,6 +236,7 @@ object ScreenWindowTraits {
221236
}
222237
if (didSetNavigationBarAppearance) {
223238
setNavigationBarColor(screen, activity)
239+
setNavigationBarTranslucent(screen, activity)
224240
setNavigationBarHidden(screen, activity)
225241
}
226242
}
@@ -281,6 +297,7 @@ object ScreenWindowTraits {
281297
WindowTraits.HIDDEN -> screen.isStatusBarHidden != null
282298
WindowTraits.ANIMATED -> screen.isStatusBarAnimated != null
283299
WindowTraits.NAVIGATION_BAR_COLOR -> screen.navigationBarColor != null
300+
WindowTraits.NAVIGATION_BAR_TRANSLUCENT -> screen.isNavigationBarTranslucent != null
284301
WindowTraits.NAVIGATION_BAR_HIDDEN -> screen.isNavigationBarHidden != null
285302
}
286303
}

android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenManagerDelegate.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ public void setProperty(T view, String propName, @Nullable Object value) {
9898
case "navigationBarColor":
9999
mViewManager.setNavigationBarColor(view, ColorPropConverter.getColor(value, view.getContext()));
100100
break;
101+
case "navigationBarTranslucent":
102+
mViewManager.setNavigationBarTranslucent(view, value == null ? false : (boolean) value);
103+
break;
101104
case "navigationBarHidden":
102105
mViewManager.setNavigationBarHidden(view, value == null ? false : (boolean) value);
103106
break;

android/src/paper/java/com/facebook/react/viewmanagers/RNSScreenManagerInterface.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public interface RNSScreenManagerInterface<T extends View> {
3939
void setHideKeyboardOnSwipe(T view, boolean value);
4040
void setActivityState(T view, float value);
4141
void setNavigationBarColor(T view, @Nullable Integer value);
42+
void setNavigationBarTranslucent(T view, boolean value);
4243
void setNavigationBarHidden(T view, boolean value);
4344
void setNativeBackButtonDismissalEnabled(T view, boolean value);
4445
}

src/fabric/ModalScreenNativeComponent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export interface NativeProps extends ViewProps {
9595
hideKeyboardOnSwipe?: boolean;
9696
activityState?: WithDefault<Float, -1.0>;
9797
navigationBarColor?: ColorValue;
98+
navigationBarTranslucent?: boolean;
9899
navigationBarHidden?: boolean;
99100
nativeBackButtonDismissalEnabled?: boolean;
100101
}

src/fabric/ScreenNativeComponent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export interface NativeProps extends ViewProps {
9595
hideKeyboardOnSwipe?: boolean;
9696
activityState?: WithDefault<Float, -1.0>;
9797
navigationBarColor?: ColorValue;
98+
navigationBarTranslucent?: boolean;
9899
navigationBarHidden?: boolean;
99100
nativeBackButtonDismissalEnabled?: boolean;
100101
}

src/native-stack/types.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ export type NativeStackNavigationOptions = {
305305
* @platform android
306306
*/
307307
navigationBarColor?: ColorValue;
308+
/**
309+
* Boolean indicating whether the content should be visible behind the navigation bar. Defaults to `false`.
310+
*
311+
* @platform android
312+
*/
313+
navigationBarTranslucent?: boolean;
308314
/**
309315
* Sets the visibility of the navigation bar. Defaults to `false`.
310316
*

src/native-stack/views/NativeStackView.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ const RouteView = ({
185185
sheetExpandsWhenScrolledToEdge = true,
186186
nativeBackButtonDismissalEnabled = false,
187187
navigationBarColor,
188+
navigationBarTranslucent,
188189
navigationBarHidden,
189190
replaceAnimation = 'pop',
190191
screenOrientation,
@@ -299,6 +300,7 @@ const RouteView = ({
299300
gestureResponseDistance={gestureResponseDistance}
300301
nativeBackButtonDismissalEnabled={nativeBackButtonDismissalEnabled}
301302
navigationBarColor={navigationBarColor}
303+
navigationBarTranslucent={navigationBarTranslucent}
302304
navigationBarHidden={navigationBarHidden}
303305
replaceAnimation={replaceAnimation}
304306
screenOrientation={screenOrientation}

src/types.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ export interface ScreenProps extends ViewProps {
171171
* @platform android
172172
*/
173173
navigationBarColor?: ColorValue;
174+
/**
175+
* Boolean indicating whether the content should be visible behind the navigation bar. Defaults to `false`.
176+
*
177+
* @platform android
178+
*/
179+
navigationBarTranslucent?: boolean;
174180
/**
175181
* Sets the visibility of the navigation bar. Defaults to `false`.
176182
*

0 commit comments

Comments
 (0)