Skip to content

Commit

Permalink
Add ability to override the ViewStyle of the root View component
Browse files Browse the repository at this point in the history
  • Loading branch information
acoates-ms committed May 23, 2024
1 parent b7fc586 commit c2fe647
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const AppContainer = ({
rootTag,
showArchitectureIndicator,
WrapperComponent,
rootViewStyle,
}: Props): React.Node => {
const appContainerRootViewRef: AppContainerRootViewRef = React.useRef(null);
const innerViewRef: InspectedViewRef = React.useRef(null);
Expand Down Expand Up @@ -141,7 +142,7 @@ const AppContainer = ({
collapsable={reactDevToolsAgent == null && !shouldRenderInspector}
pointerEvents="box-none"
key={key}
style={styles.container}
style={rootViewStyle || styles.container}
ref={innerViewRef}>
{children}
</View>
Expand All @@ -167,7 +168,7 @@ const AppContainer = ({
<RootTagContext.Provider value={createRootTag(rootTag)}>
<View
ref={appContainerRootViewRef}
style={styles.container}
style={rootViewStyle || styles.container}
pointerEvents="box-none">
{innerView}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const AppContainer = ({
rootTag,
showArchitectureIndicator,
WrapperComponent,
rootViewStyle,
}: Props): React.Node => {
let innerView = children;

Expand All @@ -39,7 +40,7 @@ const AppContainer = ({

return (
<RootTagContext.Provider value={createRootTag(rootTag)}>
<View style={styles.root} pointerEvents="box-none">
<View style={rootViewStyle || styles.root} pointerEvents="box-none">
{innerView}
</View>
</RootTagContext.Provider>
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native/Libraries/ReactNative/AppContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import type {RootTag} from './RootTag';
import type { ViewStyleProp } from '../StyleSheet/StyleSheet';

import * as React from 'react';

Expand All @@ -19,6 +20,7 @@ export type Props = $ReadOnly<{|
initialProps?: {...},
showArchitectureIndicator?: boolean,
WrapperComponent?: ?React.ComponentType<any>,
rootViewStyle?: ?ViewStyleProp,
internal_excludeLogBox?: boolean,
internal_excludeInspector?: boolean,
|}>;
Expand Down
9 changes: 9 additions & 0 deletions packages/react-native/Libraries/ReactNative/AppRegistry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type * as React from 'react';
import type {IPerformanceLogger} from '../Utilities/IPerformanceLogger';
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';

type Task = (taskData: any) => Promise<void>;
type TaskProvider = () => Task;
Expand All @@ -34,6 +35,10 @@ export type WrapperComponentProvider = (
appParameters: any,
) => React.ComponentType<any>;

export type RootViewStyleProvider = (
appParameters: any,
) => ViewStyleProp;

/**
* `AppRegistry` is the JS entry point to running all React Native apps. App
* root components should register themselves with
Expand All @@ -54,6 +59,10 @@ export namespace AppRegistry {
provider: WrapperComponentProvider,
): void;

export function setRootViewStyleProvider(
provider: RootViewStyleProvider,
): void;

export function registerConfig(config: AppConfig[]): void;

export function registerComponent(
Expand Down
10 changes: 10 additions & 0 deletions packages/react-native/Libraries/ReactNative/AppRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {RootTag} from '../Types/RootTagTypes';
import type {IPerformanceLogger} from '../Utilities/createPerformanceLogger';
import type {DisplayModeType} from './DisplayMode';
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';

import BatchedBridge from '../BatchedBridge/BatchedBridge';
import BugReporting from '../BugReporting/BugReporting';
Expand Down Expand Up @@ -60,6 +61,9 @@ export type Registry = {
export type WrapperComponentProvider = (
appParameters: Object,
) => React$ComponentType<any>;
export type RootViewStyleProvider = (
appParameters: Object,
) => ViewStyleProp;

const runnables: Runnables = {};
let runCount = 1;
Expand All @@ -70,6 +74,7 @@ let componentProviderInstrumentationHook: ComponentProviderInstrumentationHook =
(component: ComponentProvider) => component();

let wrapperComponentProvider: ?WrapperComponentProvider;
let rootViewStyleProvider: ?RootViewStyleProvider;
let showArchitectureIndicator = false;

/**
Expand All @@ -82,6 +87,10 @@ const AppRegistry = {
wrapperComponentProvider = provider;
},

setRootViewStyleProvider(provider: RootViewStyleProvider) {
rootViewStyleProvider = provider;
},

enableArchitectureIndicator(enabled: boolean): void {
showArchitectureIndicator = enabled;
},
Expand Down Expand Up @@ -130,6 +139,7 @@ const AppRegistry = {
appParameters.initialProps,
appParameters.rootTag,
wrapperComponentProvider && wrapperComponentProvider(appParameters),
rootViewStyleProvider && rootViewStyleProvider(appParameters),
appParameters.fabric,
showArchitectureIndicator,
scopedPerformanceLogger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import type {IPerformanceLogger} from '../Utilities/createPerformanceLogger';
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';

import GlobalPerformanceLogger from '../Utilities/GlobalPerformanceLogger';
import PerformanceLoggerContext from '../Utilities/PerformanceLoggerContext';
Expand All @@ -32,6 +33,7 @@ export default function renderApplication<Props: Object>(
initialProps: Props,
rootTag: any,
WrapperComponent?: ?React.ComponentType<any>,
rootViewStyle?: ?ViewStyleProp,
fabric?: boolean,
showArchitectureIndicator?: boolean,
scopedPerformanceLogger?: IPerformanceLogger,
Expand All @@ -52,6 +54,7 @@ export default function renderApplication<Props: Object>(
fabric={fabric}
showArchitectureIndicator={showArchitectureIndicator}
WrapperComponent={WrapperComponent}
rootViewStyle={rootViewStyle}
initialProps={initialProps ?? Object.freeze({})}
internal_excludeLogBox={isLogBox}>
<RootComponent {...initialProps} rootTag={rootTag} />
Expand Down

0 comments on commit c2fe647

Please sign in to comment.