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 3.4KB

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