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

[TS migration] Migrate 'SafeAreaConsumer.js' component to TypeScript #29755

Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import PropTypes from 'prop-types';
import React from 'react';
import {SafeAreaInsetsContext} from 'react-native-safe-area-context';
import type {DimensionValue} from 'react-native';
import {EdgeInsets, SafeAreaInsetsContext} from 'react-native-safe-area-context';
import * as StyleUtils from '@styles/StyleUtils';

const propTypes = {
/** Children to render. */
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]).isRequired,
type ChildrenProps = {
paddingTop?: DimensionValue;
paddingBottom?: DimensionValue;
insets?: EdgeInsets;
safeAreaPaddingBottomStyle: {
paddingBottom?: DimensionValue;
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved
};
};

type SafeAreaConsumerProps = {
children: React.FC<ChildrenProps>;
};

/**
* This component is a light wrapper around the SafeAreaInsetsContext.Consumer. There are several places where we
* may need not just the insets, but the computed styles so we save a few lines of code with this.
*
* @param {Object} props
* @returns {React.Component}
*/
function SafeAreaConsumer(props) {
function SafeAreaConsumer({children}: SafeAreaConsumerProps) {
return (
<SafeAreaInsetsContext.Consumer>
{(insets) => {
const {paddingTop, paddingBottom} = StyleUtils.getSafeAreaPadding(insets);
return props.children({
const {paddingTop, paddingBottom} = StyleUtils.getSafeAreaPadding(insets ?? undefined);
return children({
paddingTop,
paddingBottom,
insets,
insets: insets ?? undefined,
safeAreaPaddingBottomStyle: {paddingBottom},
});
}}
Expand All @@ -32,5 +37,5 @@ function SafeAreaConsumer(props) {
}

SafeAreaConsumer.displayName = 'SafeAreaConsumer';
SafeAreaConsumer.propTypes = propTypes;

export default SafeAreaConsumer;
Loading