您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DimensionsDetector.native.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // @flow
  2. import React, { useCallback, useEffect } from 'react';
  3. import { StyleSheet, View } from 'react-native';
  4. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  5. type Props = {
  6. /**
  7. * The "onLayout" handler.
  8. */
  9. onDimensionsChanged: Function,
  10. /**
  11. * The safe are insets handler.
  12. */
  13. onSafeAreaInsetsChanged: Function,
  14. /**
  15. * Any nested components.
  16. */
  17. children: React$Node
  18. };
  19. /**
  20. * A {@link View} which captures the 'onLayout' event and calls a prop with the
  21. * component size.
  22. *
  23. * @param {Props} props - The read-only properties with which the new
  24. * instance is to be initialized.
  25. * @returns {Component} - Renders the root view and it's children.
  26. */
  27. export default function DimensionsDetector(props: Props) {
  28. const { top = 0, right = 0, bottom = 0, left = 0 } = useSafeAreaInsets();
  29. const { children, onDimensionsChanged, onSafeAreaInsetsChanged } = props;
  30. useEffect(() => {
  31. onSafeAreaInsetsChanged && onSafeAreaInsetsChanged({
  32. top,
  33. right,
  34. bottom,
  35. left
  36. });
  37. }, [ onSafeAreaInsetsChanged, top, right, bottom, left ]);
  38. /**
  39. * Handles the "on layout" View's event and calls the onDimensionsChanged
  40. * prop.
  41. *
  42. * @param {Object} event - The "on layout" event object/structure passed
  43. * by react-native.
  44. * @private
  45. * @returns {void}
  46. */
  47. const onLayout = useCallback(({ nativeEvent: { layout: { height, width } } }) => {
  48. onDimensionsChanged && onDimensionsChanged(width, height);
  49. }, [ onDimensionsChanged ]);
  50. return (
  51. <View
  52. onLayout = { onLayout }
  53. style = { StyleSheet.absoluteFillObject } >
  54. { children }
  55. </View>
  56. );
  57. }