MaskedViewIOS.ios.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. const React = require('react');
  11. const StyleSheet = require('../../StyleSheet/StyleSheet');
  12. const View = require('../View/View');
  13. import type {ViewProps} from '../View/ViewPropTypes';
  14. import RCTMaskedViewNativeComponent from './RCTMaskedViewNativeComponent';
  15. type Props = $ReadOnly<{|
  16. ...ViewProps,
  17. children: React.Node,
  18. /**
  19. * Should be a React element to be rendered and applied as the
  20. * mask for the child element.
  21. */
  22. maskElement: React.Element<any>,
  23. |}>;
  24. /**
  25. * Renders the child view with a mask specified in the `maskElement` prop.
  26. *
  27. * ```
  28. * import React from 'react';
  29. * import { MaskedViewIOS, Text, View } from 'react-native';
  30. *
  31. * class MyMaskedView extends React.Component {
  32. * render() {
  33. * return (
  34. * <MaskedViewIOS
  35. * style={{ flex: 1 }}
  36. * maskElement={
  37. * <View style={styles.maskContainerStyle}>
  38. * <Text style={styles.maskTextStyle}>
  39. * Basic Mask
  40. * </Text>
  41. * </View>
  42. * }
  43. * >
  44. * <View style={{ flex: 1, backgroundColor: 'blue' }} />
  45. * </MaskedViewIOS>
  46. * );
  47. * }
  48. * }
  49. * ```
  50. *
  51. * The above example will render a view with a blue background that fills its
  52. * parent, and then mask that view with text that says "Basic Mask".
  53. *
  54. * The alpha channel of the view rendered by the `maskElement` prop determines how
  55. * much of the view's content and background shows through. Fully or partially
  56. * opaque pixels allow the underlying content to show through but fully
  57. * transparent pixels block that content.
  58. *
  59. */
  60. class MaskedViewIOS extends React.Component<Props> {
  61. _hasWarnedInvalidRenderMask = false;
  62. render(): React.Node {
  63. const {maskElement, children, ...otherViewProps} = this.props;
  64. if (!React.isValidElement(maskElement)) {
  65. if (!this._hasWarnedInvalidRenderMask) {
  66. console.warn(
  67. 'MaskedView: Invalid `maskElement` prop was passed to MaskedView. ' +
  68. 'Expected a React Element. No mask will render.',
  69. );
  70. this._hasWarnedInvalidRenderMask = true;
  71. }
  72. return <View {...otherViewProps}>{children}</View>;
  73. }
  74. return (
  75. <RCTMaskedViewNativeComponent {...otherViewProps}>
  76. <View pointerEvents="none" style={StyleSheet.absoluteFill}>
  77. {maskElement}
  78. </View>
  79. {children}
  80. </RCTMaskedViewNativeComponent>
  81. );
  82. }
  83. }
  84. module.exports = MaskedViewIOS;