2018-02-02 12:21:14 +00:00
|
|
|
// @flow
|
|
|
|
|
2022-05-06 10:18:57 +00:00
|
|
|
import React, { useCallback, useEffect } from 'react';
|
2020-06-02 09:03:17 +00:00
|
|
|
import { StyleSheet, View } from 'react-native';
|
2022-05-06 10:18:57 +00:00
|
|
|
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
2018-02-02 12:21:14 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The "onLayout" handler.
|
|
|
|
*/
|
|
|
|
onDimensionsChanged: Function,
|
|
|
|
|
2022-05-06 10:18:57 +00:00
|
|
|
/**
|
|
|
|
* The safe are insets handler.
|
|
|
|
*/
|
|
|
|
onSafeAreaInsetsChanged: Function,
|
|
|
|
|
2018-02-02 12:21:14 +00:00
|
|
|
/**
|
|
|
|
* Any nested components.
|
|
|
|
*/
|
|
|
|
children: React$Node
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A {@link View} which captures the 'onLayout' event and calls a prop with the
|
|
|
|
* component size.
|
2022-05-06 10:18:57 +00:00
|
|
|
*
|
|
|
|
* @param {Props} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
* @returns {Component} - Renders the root view and it's children.
|
2018-02-02 12:21:14 +00:00
|
|
|
*/
|
2022-05-06 10:18:57 +00:00
|
|
|
export default function DimensionsDetector(props: Props) {
|
|
|
|
const { top = 0, right = 0, bottom = 0, left = 0 } = useSafeAreaInsets();
|
|
|
|
const { children, onDimensionsChanged, onSafeAreaInsetsChanged } = props;
|
2018-02-02 12:21:14 +00:00
|
|
|
|
2022-05-06 10:18:57 +00:00
|
|
|
useEffect(() => {
|
|
|
|
onSafeAreaInsetsChanged && onSafeAreaInsetsChanged({
|
|
|
|
top,
|
|
|
|
right,
|
|
|
|
bottom,
|
|
|
|
left
|
|
|
|
});
|
|
|
|
}, [ onSafeAreaInsetsChanged, top, right, bottom, left ]);
|
2018-02-02 12:21:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the "on layout" View's event and calls the onDimensionsChanged
|
|
|
|
* prop.
|
|
|
|
*
|
|
|
|
* @param {Object} event - The "on layout" event object/structure passed
|
|
|
|
* by react-native.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-05-06 10:18:57 +00:00
|
|
|
const onLayout = useCallback(({ nativeEvent: { layout: { height, width } } }) => {
|
2018-02-02 12:21:14 +00:00
|
|
|
onDimensionsChanged && onDimensionsChanged(width, height);
|
2022-05-06 10:18:57 +00:00
|
|
|
}, [ onDimensionsChanged ]);
|
2018-02-02 12:21:14 +00:00
|
|
|
|
2022-05-06 10:18:57 +00:00
|
|
|
return (
|
|
|
|
<View
|
|
|
|
onLayout = { onLayout }
|
|
|
|
style = { StyleSheet.absoluteFillObject } >
|
|
|
|
{ children }
|
|
|
|
</View>
|
|
|
|
);
|
2018-02-02 12:21:14 +00:00
|
|
|
}
|