Guest User

Untitled

a guest
Oct 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. /**
  2. * Box
  3. *
  4. * Usage:
  5. * <Box
  6. * margin='small|medium|large|none'
  7. * margin={{
  8. * hz: 'small|medium|large|none',
  9. * vt: 'small|medium|large|none'
  10. * }}
  11. * padding='small|medium|large|none'
  12. * padding={{
  13. * hz: 'small|medium|large|none',
  14. * vt: 'small|medium|large|none'
  15. * }}
  16. * >
  17. *
  18. * @type {React.StatelessComponent}
  19. */
  20. import React from 'react'
  21. import PropTypes from 'prop-types'
  22. import styled from 'styled-components'
  23. import { space, width, fontSize, color } from 'styled-system'
  24.  
  25. const StyledBox = styled.div`
  26. ${space}
  27. ${width}
  28. ${fontSize}
  29. ${color}
  30. `
  31.  
  32. const sizeToScaleMap = {
  33. none: 0,
  34. small: 1,
  35. medium: 2,
  36. large: 3
  37. }
  38.  
  39. const Box = props => {
  40. const extraProps = {}
  41.  
  42. if (props.margin) {
  43. if (typeof props.margin === 'string') {
  44. extraProps.m = sizeToScaleMap[props.margin]
  45. } else {
  46. if (props.margin.hz) {
  47. extraProps.mx = sizeToScaleMap[props.margin.hz]
  48. }
  49.  
  50. if (props.margin.vt) {
  51. extraProps.my = sizeToScaleMap[props.margin.vt]
  52. }
  53. }
  54. }
  55.  
  56. if (props.padding) {
  57. if (typeof props.padding === 'string') {
  58. extraProps.p = sizeToScaleMap[props.padding]
  59. } else {
  60. if (props.padding.hz) {
  61. extraProps.px = sizeToScaleMap[props.padding.hz]
  62. }
  63.  
  64. if (props.padding.vt) {
  65. extraProps.py = sizeToScaleMap[props.padding.vt]
  66. }
  67. }
  68. }
  69.  
  70. return (
  71. <StyledBox
  72. {...props}
  73. {...extraProps}
  74. >
  75. { props.children }
  76. </StyledBox>
  77. )
  78. }
  79.  
  80. const sizeTypes = PropTypes.oneOf([
  81. 'small',
  82. 'medium',
  83. 'large',
  84. 'none'
  85. ])
  86.  
  87. Box.propTypes = {
  88. children: PropTypes.node.isRequired,
  89. margin: PropTypes.oneOfType([
  90. sizeTypes,
  91. PropTypes.shape({
  92. hz: sizeTypes,
  93. vt: sizeTypes
  94. })
  95. ]),
  96. padding: PropTypes.oneOfType([
  97. sizeTypes,
  98. PropTypes.shape({
  99. hz: sizeTypes,
  100. vt: sizeTypes
  101. })
  102. ])
  103. }
  104.  
  105. export default Box
Add Comment
Please, Sign In to add comment