|
| 1 | +# Contribution guidelines |
| 2 | + |
| 3 | +## Tools we use |
| 4 | + |
| 5 | +### Components |
| 6 | +1. We use [emotion] to style our components, and [emotion-theming] as the theme provider. |
| 7 | +1. We use style functions from [styled-system] whenever possible, and styled-systems' `style()` function to create new ones. |
| 8 | +1. We use [system-components] to reduce the amount of boilerplate needed to implement styled-system functions. |
| 9 | + |
| 10 | +## Component patterns |
| 11 | +With a couple of exceptions, all components should be created by the `withSystemProps()` function from `src/system-props.js`. This function takes a "component-ish" value as its first argument, and an array of [system props](#system-props) as the second: |
| 12 | + |
| 13 | +```jsx |
| 14 | +import {withSystemProps, POSITION} from './system-props' |
| 15 | + |
| 16 | +function Component(props) { |
| 17 | + /* implementation */ |
| 18 | +} |
| 19 | + |
| 20 | +export default withSystemProps(Component, POSITION) |
| 21 | + |
| 22 | +// equivalent: |
| 23 | +export default withSystemProps({is: Component}, POSITION) |
| 24 | + |
| 25 | +// with more default props: |
| 26 | +export default withSystemProps( |
| 27 | + { |
| 28 | + is: Component, |
| 29 | + m: 2 |
| 30 | + }, |
| 31 | + POSITION |
| 32 | +) |
| 33 | +``` |
| 34 | + |
| 35 | +Categories of system props are exported from `src/system-props`: |
| 36 | + |
| 37 | +* `COMMON` includes color and spacing (margin and padding) props |
| 38 | +* `TYPOGRAPHY` includes `COMMON` and font family, font weight, and line-height |
| 39 | +* `POSITION` includes `COMMON` and positioning props |
| 40 | +* `FLEX_CONTAINER` includes `COMMON` and flexbox props for containers |
| 41 | +* `FLEX_ITEM` includes `COMMON` and flexbox props for items in a flex container |
| 42 | + |
| 43 | +### Components with only system props |
| 44 | +Components with only system props should be created by passing the default tag to `withSystemProps()`: |
| 45 | + |
| 46 | +```jsx |
| 47 | +import {withSystemProps, LAYOUT} from './system-props' |
| 48 | + |
| 49 | +const Block = withSystemProps('div', LAYOUT) |
| 50 | +``` |
| 51 | + |
| 52 | +### Primer CSS components |
| 53 | +If you're just adding Primer CSS class names, you can pass the `className` prop to another component created with `withSystemProps()`, and it will be combined with any generated classes automatically: |
| 54 | + |
| 55 | +```jsx |
| 56 | +import Box from './Box' |
| 57 | + |
| 58 | +function FancyBox({flashing, ...rest}) { |
| 59 | + return <Box className={flashing && 'Box--flashing'} {...rest} /> |
| 60 | +} |
| 61 | + |
| 62 | +FancyBox.propTypes = { |
| 63 | + flashing: PropTypes.bool, |
| 64 | + // be sure to spread Box's prop-types |
| 65 | + ...Box.propTypes |
| 66 | +} |
| 67 | + |
| 68 | +FancyBox.defaultProps = { |
| 69 | + ...Box.defaultProps |
| 70 | +} |
| 71 | + |
| 72 | +// if you don't spread defaultProps from another system component, |
| 73 | +// you will need to wrap the export in withDefaultTheme() |
| 74 | +export default FancyBox |
| 75 | +``` |
| 76 | + |
| 77 | +> **⚠️ If you use this pattern, passing the component to `withSystemProps()` should throw an error because system-components has an issue (_TODO: ref_) with calling the underlying component render function twice.** |
| 78 | +
|
| 79 | +With the above pattern, it's possible to control whether users may pass additional class names to your component. If you want to allow this, the `className` prop should be listed in `propTypes` and combined with your own classes using the [classnames] function: |
| 80 | + |
| 81 | +```jsx |
| 82 | +import classnames from 'classnames' |
| 83 | +import Box from './Box' |
| 84 | + |
| 85 | +export default function FancyBox({flashing, className, ...rest}) { |
| 86 | + const classes = classnames(className, flashing && 'Box--flashing') |
| 87 | + return <Box className={classes} {...rest} /> |
| 88 | +} |
| 89 | + |
| 90 | +FancyBox.propTypes = { |
| 91 | + className: PropTypes.string, |
| 92 | + flashing: PropTypes.bool, |
| 93 | + ...Box.propTypes |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Alternatively, you can create the component from scratch using `withSystemProps()`, and pass it the same system props: |
| 98 | + |
| 99 | +```jsx |
| 100 | +import classnames from 'classnames' |
| 101 | +import {withSystemProps, LAYOUT} from './system-props' |
| 102 | + |
| 103 | +function FancyBox({flashing, className, is: Tag, ...rest}) { |
| 104 | + return ( |
| 105 | + <Tag |
| 106 | + className={classnames(className, flashing && 'Box--flashing')} |
| 107 | + {...rest} |
| 108 | + /> |
| 109 | + ) |
| 110 | +} |
| 111 | + |
| 112 | +FancyBox.propTypes = { |
| 113 | + flashing: PropTypes.bool |
| 114 | +} |
| 115 | + |
| 116 | +export default withSystemProps(FancyBox, LAYOUT) |
| 117 | +``` |
| 118 | + |
| 119 | +In this case, you will need to deal explicitly with two props passed down from [emotion] and [system-components], respectively: |
| 120 | + |
| 121 | + * `className`: You _must_ render this prop, otherwise **your component will not be styled.** |
| 122 | + * `is`: This is what allows your component to render with arbitrary elements, and even other components. If you don't respect this prop, you should `delete Component.propTypes.is` to signal that it's not available. |
| 123 | + |
| 124 | +## Glossary |
| 125 | + |
| 126 | +### System props |
| 127 | +System props are style functions that provide on or more props, and can be passed directly the return value of [emotion]'s `styled()` function: |
| 128 | + |
| 129 | +```jsx |
| 130 | +import {styled} from 'react-emotion' |
| 131 | +import {space} from 'styled-system' |
| 132 | +const SpaceDiv = styled('div')(space) |
| 133 | +``` |
| 134 | + |
| 135 | +System props come with `propTypes` that can be mixed into your own with ES6 [spread syntax]: |
| 136 | + |
| 137 | +```jsx |
| 138 | +SpaceDiv.propTypes = { |
| 139 | + stellar: PropTypes.bool, |
| 140 | + ...space.propTypes |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +[classnames]: https://www.npmjs.com/package/classnames |
| 145 | +[emotion]: https://emotion.sh/ |
| 146 | +[emotion-theming]: https://github.com/emotion-js/emotion/tree/master/packages/emotion-theming |
| 147 | +[spread syntax]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax |
| 148 | +[styled-system]: https://jxnblk.com/styled-system/getting-started |
| 149 | +[system-components]: https://jxnblk.com/styled-system/system-components |
| 150 | +[table]: https://jxnblk.com/styled-system/table |
0 commit comments