Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. import React, {
  2. PropTypes,
  3. } from 'react';
  4.  
  5. import {
  6. StyleSheet,
  7. Text,
  8. TouchableHighlight,
  9. TouchableOpacity,
  10. View,
  11. } from 'react-native';
  12.  
  13. import Color from 'color';
  14.  
  15. const colors = {
  16. light: '#fff',
  17. stable: '#f8f8f8',
  18. primary: '#337AF9',
  19. calm: '#11c1f3',
  20. secondary: '#22DD5E',
  21. energized: '#FFC600',
  22. danger: '#F83B36',
  23. royal: '#7E59FF',
  24. dark: '#222',
  25. };
  26.  
  27. const sizes = {
  28. xs: 4,
  29. sm: 8,
  30. md: 12,
  31. lg: 16,
  32. xl: 20,
  33. };
  34.  
  35. const propTypes = {
  36. activeOpacity: PropTypes.number,
  37. block: PropTypes.bool,
  38. children: PropTypes.node,
  39. clear: PropTypes.bool,
  40. color: PropTypes.oneOfType([
  41. PropTypes.number,
  42. PropTypes.string,
  43. ]),
  44. onPress: PropTypes.func,
  45. outline: PropTypes.bool,
  46. round: PropTypes.bool,
  47. shadow: PropTypes.bool,
  48. size: PropTypes.oneOfType([
  49. PropTypes.number,
  50. PropTypes.string,
  51. ]),
  52. style: PropTypes.oneOfType([
  53. PropTypes.number,
  54. PropTypes.object,
  55. ]),
  56. text: PropTypes.string,
  57. underlayColor: PropTypes.oneOfType([
  58. PropTypes.number,
  59. PropTypes.string,
  60. ]),
  61. };
  62.  
  63. const defaultProps = {
  64. activeOpacity: 1,
  65. block: false,
  66. children: null,
  67. clear: false,
  68. color: colors.stable,
  69. onPress: () => alert('Attach an onPress prop'),
  70. outline: false,
  71. round: false,
  72. shadow: true,
  73. size: sizes.md,
  74. style: null,
  75. text: 'Press Me',
  76. underlayColor: null,
  77. };
  78.  
  79. const cs = StyleSheet.create({
  80. button: {
  81. alignItems: 'center',
  82. borderWidth: 2,
  83. flex: 0,
  84. justifyContent: 'center',
  85. },
  86. });
  87.  
  88. export default function Button(props) {
  89. const {
  90. activeOpacity,
  91. block: $block,
  92. children,
  93. clear,
  94. color: $color,
  95. onPress,
  96. outline,
  97. round,
  98. shadow: $shadow,
  99. size: $size,
  100. style,
  101. text,
  102. underlayColor,
  103. ...passProps,
  104. } = props;
  105.  
  106. const size = sizes[$size] || $size;
  107. const color = colors[$color] ? Color(colors[$color]) : Color($color);
  108. const colorDark = color.darken(0.2);
  109. const luminosTextColor = color.luminosity() < 0.5 ? '#fff' : '#000';
  110.  
  111. const block = $block && { alignSelf: 'stretch' };
  112. const backgroundColor = clear || outline ? 'transparent' : color
  113. const borderColor = outline ? color : 'transparent';
  114. const borderRadius = round ? 50 : 2;
  115. const padding = size > 15
  116. ? { padding: 16 }
  117. : { paddingHorizontal: 4, paddingVertical: 8 };
  118. const shadow = $shadow && {
  119. elevation: 2,
  120. shadowColor: '#000',
  121. shadowOffset: { width: 0, height: 2 },
  122. shadowOpacity: 0.2,
  123. shadowRadius: 2,
  124. };
  125. const textColor = clear || outline ? color : luminosTextColor;
  126.  
  127. const buttonStyle = [
  128. cs.button,
  129. block,
  130. { backgroundColor },
  131. { borderColor },
  132. { borderRadius },
  133. padding,
  134. shadow,
  135. style,
  136. ];
  137.  
  138. const textStyle = {
  139. color: textColor,
  140. fontSize: size,
  141. };
  142.  
  143. const content = children || (
  144. <Text style={textStyle}>
  145. {text}
  146. </Text>
  147. );
  148.  
  149. if (clear) {
  150. return (
  151. <TouchableOpacity
  152. onPress={onPress}
  153. style={buttonStyle}
  154. {...passProps}
  155. >
  156. {content}
  157. </TouchableOpacity>
  158. );
  159. }
  160.  
  161. return (
  162. <TouchableHighlight
  163. activeOpacity={activeOpacity}
  164. onPress={onPress}
  165. style={buttonStyle}
  166. underlayColor={underlayColor || colorDark}
  167. {...passProps}
  168. >
  169. {content}
  170. </TouchableHighlight>
  171. );
  172. }
  173.  
  174. Button.propTypes = propTypes;
  175. Button.defaultProps = defaultProps;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement