Skip to content

Commit 54efd60

Browse files
committed
Add StatusBar.getVerticalBarEdgeIOS and useVerticalBarEdgeIOS for iPhone Duo
1 parent 206bc5b commit 54efd60

13 files changed

Lines changed: 236 additions & 6 deletions

File tree

‎packages/react-native/Libraries/Components/StatusBar/StatusBar.js‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ export type StatusBarStyle = keyof {
4040
...
4141
};
4242

43+
/**
44+
* The edge where the system places the vertical bar that holds the status bar,
45+
* such as the side rail on iPhone Duo.
46+
*/
47+
export type StatusBarVerticalBarEdgeIOS = 'leading' | 'trailing';
48+
4349
/**
4450
* Status bar animation
4551
*/
@@ -72,7 +78,8 @@ export type StatusBarPropsIOS = Readonly<{
7278

7379
type StatusBarBaseProps = Readonly<{
7480
/**
75-
* If the status bar is hidden.
81+
* If the status bar is hidden. Has no effect on iPhone Duo, where the
82+
* status bar is in a vertical bar.
7683
*
7784
* @default `false`
7885
*/
@@ -273,7 +280,8 @@ class StatusBar extends React.Component<StatusBarProps> {
273280
// See the corresponding prop for more detail.
274281

275282
/**
276-
* Show or hide the status bar
283+
* Show or hide the status bar. Has no effect on iPhone Duo, where the status
284+
* bar is in a vertical bar.
277285
* @param hidden Hide the status bar.
278286
* @param animation Optional animation when
279287
* changing the status bar hidden property.
@@ -304,6 +312,22 @@ class StatusBar extends React.Component<StatusBarProps> {
304312
}
305313
}
306314

315+
/**
316+
* Get the edge where the system places the vertical bar that holds the
317+
* status bar beside the content, such as the side rail on iPhone Duo.
318+
* Returns `null` when the system places no vertical bar, such as on other
319+
* devices, in orientations that keep the status bar at the top, or before
320+
* iOS 27.1.
321+
*
322+
* @platform ios
323+
*/
324+
static getVerticalBarEdgeIOS(): ?StatusBarVerticalBarEdgeIOS {
325+
if (Platform.OS !== 'ios') {
326+
return null;
327+
}
328+
return toVerticalBarEdge(NativeStatusBarManagerIOS.getVerticalBarEdge());
329+
}
330+
307331
/**
308332
* Push a StatusBar entry onto the stack.
309333
* The return value should be passed to `popStackEntry` when complete.
@@ -448,4 +472,8 @@ class StatusBar extends React.Component<StatusBarProps> {
448472

449473
export type StatusBarInstance = StatusBar;
450474

475+
function toVerticalBarEdge(edge: ?string): ?StatusBarVerticalBarEdgeIOS {
476+
return edge === 'leading' || edge === 'trailing' ? edge : null;
477+
}
478+
451479
export default StatusBar;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
jest.mock('../NativeStatusBarManagerIOS', () => ({
14+
__esModule: true,
15+
default: {
16+
addListener: jest.fn(),
17+
removeListeners: jest.fn(),
18+
getVerticalBarEdge: jest.fn(() => 'trailing'),
19+
},
20+
}));
21+
22+
const useVerticalBarEdgeIOS = require('../useVerticalBarEdgeIOS').default;
23+
const {create} = require('@react-native/jest-preset/jest/renderer');
24+
const React = require('react');
25+
26+
test('returns the vertical bar edge from the native module', async () => {
27+
let edge;
28+
function Probe() {
29+
edge = useVerticalBarEdgeIOS();
30+
return null;
31+
}
32+
33+
await create(<Probe />);
34+
35+
expect(edge).toBe('trailing');
36+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {StatusBarVerticalBarEdgeIOS} from './StatusBar';
12+
13+
import NativeEventEmitter from '../../EventEmitter/NativeEventEmitter';
14+
import Platform from '../../Utilities/Platform';
15+
import NativeStatusBarManagerIOS from './NativeStatusBarManagerIOS';
16+
import StatusBar from './StatusBar';
17+
import {useSyncExternalStore} from 'react';
18+
19+
// Read once and refreshed by events, so renders don't each call into native.
20+
let cachedEdge: ?StatusBarVerticalBarEdgeIOS;
21+
22+
function subscribe(onStoreChange: () => void): () => void {
23+
if (Platform.OS !== 'ios') {
24+
return () => {};
25+
}
26+
cachedEdge = StatusBar.getVerticalBarEdgeIOS();
27+
const subscription = new NativeEventEmitter<{
28+
verticalBarEdgeDidChange: [{edge: ?string}],
29+
}>(NativeStatusBarManagerIOS).addListener('verticalBarEdgeDidChange', () => {
30+
cachedEdge = StatusBar.getVerticalBarEdgeIOS();
31+
onStoreChange();
32+
});
33+
return () => subscription.remove();
34+
}
35+
36+
function getSnapshot(): ?StatusBarVerticalBarEdgeIOS {
37+
if (cachedEdge === undefined) {
38+
cachedEdge = StatusBar.getVerticalBarEdgeIOS();
39+
}
40+
return cachedEdge;
41+
}
42+
43+
/**
44+
* React hook that returns the edge where the system places the vertical bar
45+
* that holds the status bar, such as the side rail on iPhone Duo, and updates
46+
* when it changes. Returns `null` when the system places no vertical bar.
47+
*
48+
* @platform ios
49+
*/
50+
export default function useVerticalBarEdgeIOS(): ?StatusBarVerticalBarEdgeIOS {
51+
return useSyncExternalStore(subscribe, getSnapshot);
52+
}

‎packages/react-native/React/CoreModules/RCTStatusBarManager.mm‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@
1818

1919
static NSString *const kStatusBarFrameDidChange = @"statusBarFrameDidChange";
2020
static NSString *const kStatusBarFrameWillChange = @"statusBarFrameWillChange";
21+
static NSString *const kVerticalBarEdgeDidChange = @"verticalBarEdgeDidChange";
22+
23+
static NSString *_Nullable RCTVerticalBarEdge(UITraitCollection *traitCollection)
24+
{
25+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 270100 /* __IPHONE_27_1 */
26+
if (@available(iOS 27.1, tvOS 27.1, *)) {
27+
switch (traitCollection.verticalBarEdge) {
28+
case UIVerticalBarEdgeLeading:
29+
return @"leading";
30+
case UIVerticalBarEdgeTrailing:
31+
return @"trailing";
32+
default:
33+
return nil;
34+
}
35+
}
36+
#endif
37+
return nil;
38+
}
2139

2240
@implementation RCTConvert (UIStatusBar)
2341

@@ -54,6 +72,8 @@ @interface RCTStatusBarManager () <NativeStatusBarManagerIOSSpec, RCTInitializin
5472

5573
@implementation RCTStatusBarManager {
5674
facebook::react::ModuleConstants<JS::NativeStatusBarManagerIOS::Constants> _constants;
75+
__weak UIWindow *_verticalBarEdgeWindow;
76+
id _verticalBarEdgeRegistration;
5777
}
5878

5979
static BOOL RCTViewControllerBasedStatusBarAppearance()
@@ -108,7 +128,7 @@ - (void)initialize
108128

109129
- (NSArray<NSString *> *)supportedEvents
110130
{
111-
return @[ kStatusBarFrameDidChange, kStatusBarFrameWillChange ];
131+
return @[ kStatusBarFrameDidChange, kStatusBarFrameWillChange, kVerticalBarEdgeDidChange ];
112132
}
113133

114134
- (void)startObserving
@@ -125,11 +145,48 @@ - (void)startObserving
125145
name:UIApplicationWillChangeStatusBarFrameNotification
126146
object:nil];
127147
#pragma clang diagnostic pop
148+
149+
dispatch_async(dispatch_get_main_queue(), ^{
150+
[self observeVerticalBarEdge];
151+
});
128152
}
129153

130154
- (void)stopObserving
131155
{
132156
[[NSNotificationCenter defaultCenter] removeObserver:self];
157+
158+
dispatch_async(dispatch_get_main_queue(), ^{
159+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 270100 /* __IPHONE_27_1 */
160+
if (self->_verticalBarEdgeRegistration != nil) {
161+
[self->_verticalBarEdgeWindow unregisterForTraitChanges:self->_verticalBarEdgeRegistration];
162+
}
163+
#endif
164+
self->_verticalBarEdgeRegistration = nil;
165+
self->_verticalBarEdgeWindow = nil;
166+
});
167+
}
168+
169+
- (void)observeVerticalBarEdge
170+
{
171+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 270100 /* __IPHONE_27_1 */
172+
if (@available(iOS 27.1, tvOS 27.1, *)) {
173+
UIWindow *window = RCTKeyWindow();
174+
if (_verticalBarEdgeRegistration != nil || window == nil) {
175+
return;
176+
}
177+
__weak __typeof(self) weakSelf = self;
178+
_verticalBarEdgeWindow = window;
179+
_verticalBarEdgeRegistration = [window
180+
registerForTraitChanges:UITraitCollection.systemTraitsAffectingVerticalBarEdge
181+
withHandler:^(id<UITraitEnvironment> traitEnvironment, UITraitCollection *previousTraitCollection) {
182+
NSString *edge = RCTVerticalBarEdge(traitEnvironment.traitCollection);
183+
NSString *previousEdge = RCTVerticalBarEdge(previousTraitCollection);
184+
if (edge != previousEdge && ![edge isEqualToString:previousEdge]) {
185+
[weakSelf sendEventWithName:kVerticalBarEdgeDidChange body:@{@"edge" : edge ?: [NSNull null]}];
186+
}
187+
}];
188+
}
189+
#endif
133190
}
134191

135192
- (void)emitEvent:(NSString *)eventName forNotification:(NSNotification *)notification
@@ -166,6 +223,15 @@ - (void)getHeight:(RCTResponseSenderBlock)callback
166223
} ]);
167224
}
168225

226+
- (NSString *)getVerticalBarEdge
227+
{
228+
__block NSString *edge;
229+
RCTUnsafeExecuteOnMainQueueSync(^{
230+
edge = RCTVerticalBarEdge(RCTKeyWindow().traitCollection);
231+
});
232+
return edge;
233+
}
234+
169235
- (void)setStyle:(NSString *)style animated:(BOOL)animated
170236
{
171237
dispatch_async(dispatch_get_main_queue(), ^{

‎packages/react-native/ReactNativeApi.d.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<6ee0da046817c3326773621f8330ade3>>
7+
* @generated SignedSource<<ddfcd9551ffa4653060b625425e3a75d>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -4821,6 +4821,7 @@ declare class StatusBar extends React.Component<StatusBarProps> {
48214821
componentDidMount(): void
48224822
componentDidUpdate(): void
48234823
componentWillUnmount(): void
4824+
static getVerticalBarEdgeIOS(): null | StatusBarVerticalBarEdgeIOS | undefined
48244825
static popStackEntry(entry: StackProps): void
48254826
static pushStackEntry(props: StatusBarProps): StackProps
48264827
render(): React.ReactNode
@@ -4849,6 +4850,7 @@ declare type StatusBarStyle = keyof {
48494850
default: string
48504851
"light-content": string
48514852
}
4853+
declare type StatusBarVerticalBarEdgeIOS = "leading" | "trailing"
48524854
declare type StickyHeaderComponentType = (
48534855
props: ScrollViewStickyHeaderProps & {
48544856
ref?: React.Ref<{
@@ -5524,6 +5526,8 @@ declare function useColorScheme(): ColorSchemeName | null
55245526
declare function usePressability(
55255527
config: null | PressabilityConfig | undefined,
55265528
): null | PressabilityEventHandlers
5529+
declare function useVerticalBarEdgeIOS():
5530+
null | StatusBarVerticalBarEdgeIOS | undefined
55275531
declare function useWindowDimensions(): DisplayMetrics | DisplayMetricsAndroid
55285532
declare type UTFSequence = typeof UTFSequence
55295533
declare type Value = null | {
@@ -6083,11 +6087,12 @@ export {
60836087
ShareActionSheetIOSOptions, // eff574f5
60846088
ShareContent, // 7c627896
60856089
ShareOptions, // 800c3a4e
6086-
StatusBar, // 5748d574
6090+
StatusBar, // 8bda633d
60876091
StatusBarAnimation, // 7fd047e6
6088-
StatusBarInstance, // de13e4a1
6092+
StatusBarInstance, // 6696fd7f
60896093
StatusBarProps, // c2a44d88
60906094
StatusBarStyle, // 78f53eea
6095+
StatusBarVerticalBarEdgeIOS, // f0ffbe54
60916096
StyleProp, // fa0e9b4a
60926097
StyleSheet, // cf156a8b
60936098
SubmitBehavior, // c4ddf490
@@ -6164,5 +6169,6 @@ export {
61646169
useAppState, // 9338748a
61656170
useColorScheme, // d585efdb
61666171
usePressability, // 0b21f83a
6172+
useVerticalBarEdgeIOS, // a0670270
61676173
useWindowDimensions, // bb4b683f
61686174
}

‎packages/react-native/index.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ module.exports = {
403403
get usePressability() {
404404
return require('./Libraries/Pressability/usePressability').default;
405405
},
406+
get useVerticalBarEdgeIOS() {
407+
return require('./Libraries/Components/StatusBar/useVerticalBarEdgeIOS')
408+
.default;
409+
},
406410
get useWindowDimensions() {
407411
return require('./Libraries/Utilities/useWindowDimensions').default;
408412
},

‎packages/react-native/index.js.flow‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export type {
136136
StatusBarInstance,
137137
StatusBarProps,
138138
StatusBarStyle,
139+
StatusBarVerticalBarEdgeIOS,
139140
} from './Libraries/Components/StatusBar/StatusBar';
140141
export {default as StatusBar} from './Libraries/Components/StatusBar/StatusBar';
141142

@@ -465,6 +466,7 @@ export type {
465466
export {default as usePressability} from './Libraries/Pressability/usePressability';
466467
export {default as useAppState} from './Libraries/AppState/useAppState';
467468
export {default as useColorScheme} from './Libraries/Utilities/useColorScheme';
469+
export {default as useVerticalBarEdgeIOS} from './Libraries/Components/StatusBar/useVerticalBarEdgeIOS';
468470
export {default as useWindowDimensions} from './Libraries/Utilities/useWindowDimensions';
469471
export {default as UTFSequence} from './Libraries/UTFSequence';
470472
export {default as Vibration} from './Libraries/Vibration/Vibration';

‎packages/react-native/src/private/specs_DEPRECATED/modules/NativeStatusBarManagerIOS.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export interface Spec extends TurboModule {
3434
* - withAnimation can be: 'none' | 'fade' | 'slide'
3535
*/
3636
readonly setHidden: (hidden: boolean, withAnimation: string) => void;
37+
/**
38+
* - returns 'leading', 'trailing', or null when there is no vertical bar
39+
*/
40+
readonly getVerticalBarEdge?: () => ?string;
3741
}
3842

3943
const NativeModule = TurboModuleRegistry.getEnforcing<Spec>('StatusBarManager');
@@ -79,6 +83,13 @@ const NativeStatusBarManager = {
7983
setHidden(hidden: boolean, withAnimation: string): void {
8084
NativeModule.setHidden(hidden, withAnimation);
8185
},
86+
87+
/**
88+
* - returns 'leading', 'trailing', or null when there is no vertical bar
89+
*/
90+
getVerticalBarEdge(): ?string {
91+
return NativeModule.getVerticalBarEdge?.() ?? null;
92+
},
8293
};
8394

8495
export default NativeStatusBarManager;

0 commit comments

Comments
 (0)