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

Add onInsetsChange property to SafeAreaView #19627

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2e75e28
Add onInsetsChange prop to SafeAreaView
clmntcrl Jun 8, 2018
982f893
Remove @providesModule
clmntcrl Jun 8, 2018
26a8d5c
Fix issues to pass Flow tests
clmntcrl Jun 8, 2018
1a557b7
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Jun 10, 2018
d957014
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Jun 11, 2018
58dc07a
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Jul 4, 2018
70c6055
Add onInsetsChange prop to SafeAreaView
clmntcrl Jun 8, 2018
758ae93
Remove @providesModule
clmntcrl Jun 8, 2018
59faec2
Fix issues to pass Flow tests
clmntcrl Jun 8, 2018
0d26336
Merge branch 'components-safeareaview-oninsetschange' of https://gith…
clmntcrl Aug 4, 2018
5dc2ff9
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Aug 6, 2018
35cbf1b
Merge branch 'components-safeareaview-oninsetschange' of https://gith…
clmntcrl Aug 6, 2018
c2d4cb6
Fix `children` is missing in props flow test error
clmntcrl Aug 7, 2018
48e697c
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Aug 7, 2018
ca61a6b
A better children PropType
clmntcrl Aug 10, 2018
d956812
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Aug 23, 2018
1a93364
Better Flow type for onInsetsChange
clmntcrl Aug 24, 2018
dc366ed
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Aug 24, 2018
3f9412a
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Aug 30, 2018
f0e872b
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Sep 5, 2018
b07d007
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Sep 21, 2018
97de2f6
Merge branch 'master' into components-safeareaview-oninsetschange
clmntcrl Oct 8, 2018
485286a
Merge branch 'master' into components-safeareaview-oninsetschange
hramos Mar 27, 2019
c9b52f7
Update SafeAreaViewExample.js
hramos Mar 27, 2019
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
15 changes: 12 additions & 3 deletions Libraries/Components/SafeAreaView/SafeAreaView.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ const React = require('React');

const requireNativeComponent = require('requireNativeComponent');

import PropTypes from 'prop-types';
import type {ViewProps} from 'ViewPropTypes';

const RCTSafeAreaView = requireNativeComponent('RCTSafeAreaView');

type Props = ViewProps & {
children: any,
};
type Props = $ReadOnly<{|
...ViewProps,
children?: ?React.Node,
onInsetsChange?: ?Function,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|}>;

/**
* Renders nested content and automatically applies paddings reflect the portion of the view
Expand All @@ -30,6 +33,12 @@ type Props = ViewProps & {
class SafeAreaView extends React.Component<Props> {
static propTypes = {
...DeprecatedViewPropTypes,
/**
* Callback that is called when the safe area view insets changed.
* This will be called with
* `{ nativeEvent: { insets: { left, top, right, bottom } } }`.
*/
onInsetsChange: PropTypes.func,
};

render() {
Expand Down
20 changes: 18 additions & 2 deletions RNTester/js/SafeAreaViewExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,28 @@ exports.title = '<SafeAreaView>';
exports.description =
'SafeAreaView automatically applies paddings reflect the portion of the view that is not covered by other (special) ancestor views.';

type Insets = {left: number, top: number, right: number, bottom: number} | void;

class SafeAreaViewExample extends React.Component<
{},
{|modalVisible: boolean|},
{|
modalVisible: boolean,
insets: Insets,
|},
> {
state = {
modalVisible: false,
insets: undefined,
};

_setModalVisible = visible => {
this.setState({modalVisible: visible});
};

_onSafeAreaViewInsetsChange = event => {
this.setState({insets: event.nativeEvent.insets});
};

render() {
return (
<View>
Expand All @@ -45,8 +55,14 @@ class SafeAreaViewExample extends React.Component<
animationType="slide"
supportedOrientations={['portrait', 'landscape']}>
<View style={styles.modal}>
<SafeAreaView style={styles.safeArea}>
<SafeAreaView
style={styles.safeArea}
onInsetsChange={this._onSafeAreaViewInsetsChange}>
<View style={styles.safeAreaContent}>
<Text>
{this.state.insets &&
`safeAreaViewInsets:\n${JSON.stringify(this.state.insets)}`}
</Text>
<Button
onPress={this._setModalVisible.bind(this, false)}
title="Close"
Expand Down
14 changes: 14 additions & 0 deletions React/Views/SafeAreaView/RCTSafeAreaView.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

#import "RCTSafeAreaViewLocalData.h"

@interface RCTSafeAreaView ()

@property (nonatomic, copy) RCTBubblingEventBlock onInsetsChange;

@end

@implementation RCTSafeAreaView {
__weak RCTBridge *_bridge;
UIEdgeInsets _currentSafeAreaInsets;
Expand Down Expand Up @@ -56,6 +62,14 @@ - (void)safeAreaInsetsDidChange
RCTSafeAreaViewLocalData *localData =
[[RCTSafeAreaViewLocalData alloc] initWithInsets:safeAreaInsets];
[_bridge.uiManager setLocalData:localData forView:self];

if (_onInsetsChange) {
_onInsetsChange(@{ @"insets": @{
@"left": @(safeAreaInsets.left),
@"top": @(safeAreaInsets.top),
@"right": @(safeAreaInsets.right),
@"bottom": @(safeAreaInsets.right) } });
}
}

#endif
Expand Down
2 changes: 2 additions & 0 deletions React/Views/SafeAreaView/RCTSafeAreaViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ - (RCTSafeAreaShadowView *)shadowView
return [RCTSafeAreaShadowView new];
}

RCT_EXPORT_VIEW_PROPERTY(onInsetsChange, RCTBubblingEventBlock)

@end