You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* @flow */
  2. import { Platform } from '../react';
  3. import { ColorPalette } from './components';
  4. declare type StyleSheet = Object;
  5. /**
  6. * The list of the well-known style properties which may not be numbers on Web
  7. * but must be numbers on React Native.
  8. *
  9. * @private
  10. */
  11. const _WELL_KNOWN_NUMBER_PROPERTIES = [ 'height', 'width' ];
  12. /* eslint-disable flowtype/space-before-type-colon */
  13. /**
  14. * Create a style sheet using the provided style definitions.
  15. *
  16. * @param {StyleSheet} styles - A dictionary of named style definitions.
  17. * @param {StyleSheet} [overrides={}] - Optional set of additional (often
  18. * platform-dependent/specific) style definitions that will override the base
  19. * (often platform-independent) styles.
  20. * @returns {StyleSheet}
  21. */
  22. export function createStyleSheet(styles: StyleSheet, overrides: StyleSheet = {})
  23. : StyleSheet {
  24. /* eslint-enable flowtype/space-before-type-colon */
  25. const combinedStyles = {};
  26. for (const k of Object.keys(styles)) {
  27. combinedStyles[k]
  28. = _shimStyles({
  29. ...styles[k],
  30. ...overrides[k]
  31. });
  32. }
  33. return combinedStyles;
  34. }
  35. /**
  36. * Works around a bug in react-native or react-native-webrtc on Android which
  37. * causes Views overlaying RTCView to be clipped. Even though we (may) display
  38. * multiple RTCViews, it is enough to apply the fix only to a View with a
  39. * bounding rectangle containing all RTCviews and their overlaying Views.
  40. *
  41. * @param {StyleSheet} styles - An object which represents a stylesheet.
  42. * @public
  43. * @returns {StyleSheet}
  44. */
  45. export function fixAndroidViewClipping<T: StyleSheet>(styles: T): T {
  46. if (Platform.OS === 'android') {
  47. styles.borderColor = ColorPalette.appBackground;
  48. styles.borderWidth = 1;
  49. }
  50. return styles;
  51. }
  52. /**
  53. * Shims style properties to work correctly on native. Allows us to minimize the
  54. * number of style declarations that need to be set or overridden for specific
  55. * platforms.
  56. *
  57. * @param {StyleSheet} styles - An object which represents a stylesheet.
  58. * @private
  59. * @returns {StyleSheet}
  60. */
  61. function _shimStyles<T: StyleSheet>(styles: T): T {
  62. // Certain style properties may not be numbers on Web but must be numbers on
  63. // React Native. For example, height and width may be expressed in percent
  64. // on Web but React Native will not understand them and we will get errors
  65. // (at least during development). Convert such well-known properties to
  66. // numbers if possible; otherwise, remove them to avoid runtime errors.
  67. for (const k of _WELL_KNOWN_NUMBER_PROPERTIES) {
  68. const v = styles[k];
  69. const typeofV = typeof v;
  70. if (typeofV !== 'undefined' && typeofV !== 'number') {
  71. const numberV = Number(v);
  72. if (Number.isNaN(numberV)) {
  73. delete styles[k];
  74. } else {
  75. styles[k] = numberV;
  76. }
  77. }
  78. }
  79. return styles;
  80. }