Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow web's SafeAreaView to forward ref to its underlying view #383

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/SafeArea.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type * as React from 'react';
import type { NativeSyntheticEvent, ViewProps } from 'react-native';
import NativeSafeAreaView from './specs/NativeSafeAreaView';

export type Edge = 'top' | 'right' | 'bottom' | 'left';

Expand Down Expand Up @@ -36,3 +37,7 @@ export interface NativeSafeAreaViewProps extends ViewProps {
mode?: 'padding' | 'margin';
edges?: ReadonlyArray<Edge>;
}

export type NativeSafeAreaViewInstance = InstanceType<
typeof NativeSafeAreaView
>;
7 changes: 4 additions & 3 deletions src/SafeAreaView.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as React from 'react';
import type { NativeSafeAreaViewProps } from './SafeArea.types';
import type {
NativeSafeAreaViewInstance,
NativeSafeAreaViewProps,
} from './SafeArea.types';
import NativeSafeAreaView from './specs/NativeSafeAreaView';

type NativeSafeAreaViewInstance = InstanceType<typeof NativeSafeAreaView>;

export type SafeAreaViewProps = NativeSafeAreaViewProps;

export const SafeAreaView: React.ForwardRefExoticComponent<
Expand Down
115 changes: 59 additions & 56 deletions src/SafeAreaView.web.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as React from 'react';
import { View, StyleSheet } from 'react-native';
import { useSafeAreaInsets } from './SafeAreaContext';
import type { Edge, NativeSafeAreaViewProps } from './SafeArea.types';
import type {
Edge,
NativeSafeAreaViewInstance,
NativeSafeAreaViewProps,
} from './SafeArea.types';

// prettier-ignore
const TOP = 0b1000,
Expand All @@ -19,67 +23,66 @@ const edgeBitmaskMap: Record<Edge, number> = {
left: LEFT,
};

export function SafeAreaView({
style = {},
mode,
edges,
...rest
}: NativeSafeAreaViewProps) {
const insets = useSafeAreaInsets();
export const SafeAreaView: React.ForwardRefExoticComponent<
janicduplessis marked this conversation as resolved.
Show resolved Hide resolved
NativeSafeAreaViewProps & React.RefAttributes<NativeSafeAreaViewInstance>
> = React.forwardRef<NativeSafeAreaViewInstance, NativeSafeAreaViewProps>(
({ style = {}, mode, edges, ...rest }, ref) => {
janicduplessis marked this conversation as resolved.
Show resolved Hide resolved
const insets = useSafeAreaInsets();

const edgeBitmask =
edges != null
? edges.reduce((accum, edge) => accum | edgeBitmaskMap[edge], 0)
: ALL;
const edgeBitmask =
edges != null
? edges.reduce((accum, edge) => accum | edgeBitmaskMap[edge], 0)
: ALL;

const appliedStyle = React.useMemo(() => {
const insetTop = edgeBitmask & TOP ? insets.top : 0;
const insetRight = edgeBitmask & RIGHT ? insets.right : 0;
const insetBottom = edgeBitmask & BOTTOM ? insets.bottom : 0;
const insetLeft = edgeBitmask & LEFT ? insets.left : 0;
const appliedStyle = React.useMemo(() => {
const insetTop = edgeBitmask & TOP ? insets.top : 0;
const insetRight = edgeBitmask & RIGHT ? insets.right : 0;
const insetBottom = edgeBitmask & BOTTOM ? insets.bottom : 0;
const insetLeft = edgeBitmask & LEFT ? insets.left : 0;

const flatStyle = StyleSheet.flatten(style) as Record<string, number>;
const flatStyle = StyleSheet.flatten(style) as Record<string, number>;

if (mode === 'margin') {
const {
margin = 0,
marginVertical = margin,
marginHorizontal = margin,
marginTop = marginVertical,
marginRight = marginHorizontal,
marginBottom = marginVertical,
marginLeft = marginHorizontal,
} = flatStyle;
if (mode === 'margin') {
const {
margin = 0,
marginVertical = margin,
marginHorizontal = margin,
marginTop = marginVertical,
marginRight = marginHorizontal,
marginBottom = marginVertical,
marginLeft = marginHorizontal,
} = flatStyle;

const marginStyle = {
marginTop: marginTop + insetTop,
marginRight: marginRight + insetRight,
marginBottom: marginBottom + insetBottom,
marginLeft: marginLeft + insetLeft,
};
const marginStyle = {
marginTop: marginTop + insetTop,
marginRight: marginRight + insetRight,
marginBottom: marginBottom + insetBottom,
marginLeft: marginLeft + insetLeft,
};

return [style, marginStyle];
} else {
const {
padding = 0,
paddingVertical = padding,
paddingHorizontal = padding,
paddingTop = paddingVertical,
paddingRight = paddingHorizontal,
paddingBottom = paddingVertical,
paddingLeft = paddingHorizontal,
} = flatStyle;
return [style, marginStyle];
} else {
const {
padding = 0,
paddingVertical = padding,
paddingHorizontal = padding,
paddingTop = paddingVertical,
paddingRight = paddingHorizontal,
paddingBottom = paddingVertical,
paddingLeft = paddingHorizontal,
} = flatStyle;

const paddingStyle = {
paddingTop: paddingTop + insetTop,
paddingRight: paddingRight + insetRight,
paddingBottom: paddingBottom + insetBottom,
paddingLeft: paddingLeft + insetLeft,
};
const paddingStyle = {
paddingTop: paddingTop + insetTop,
paddingRight: paddingRight + insetRight,
paddingBottom: paddingBottom + insetBottom,
paddingLeft: paddingLeft + insetLeft,
};

return [style, paddingStyle];
}
}, [style, insets, mode, edgeBitmask]);
return [style, paddingStyle];
}
}, [style, insets, mode, edgeBitmask]);

return <View style={appliedStyle} {...rest} />;
}
return <View style={appliedStyle} {...rest} ref={ref} />;
},
);