Skip to content

Commit

Permalink
Merge pull request Expensify#29755 from software-mansion-labs/ts-migr…
Browse files Browse the repository at this point in the history
…ation/SafeAreaConsumer

[TS migration] Migrate 'SafeAreaConsumer.js' component to TypeScript
  • Loading branch information
Hayata Suenaga authored Nov 2, 2023
2 parents 207266c + 1ce8ffd commit 9ee3b4a
Showing 1 changed file with 18 additions and 13 deletions.
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;
};
};

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;

0 comments on commit 9ee3b4a

Please sign in to comment.