diff --git a/STYLE.md b/STYLE.md index 3850c5463239..e3cb20ca5dee 100644 --- a/STYLE.md +++ b/STYLE.md @@ -141,6 +141,17 @@ Using arrow functions is the preferred way to write an anonymous function such a _.map(someArray, (item) => {...}); ``` +Empty functions (noop) should be declare as arrow functions with no whitespace inside. Avoid _.noop() + + ```javascript + // Bad + const callback = _.noop; + const callback = () => { }; + + // Good + const callback = () => {}; + ``` + ## `var`, `const` and `let` - Never use `var` @@ -655,10 +666,10 @@ In addition, all refs should be declared in the constructor, rather than inline. class BadRefComponent extends Component { constructor(props) { super(props); - + // Bad: Ref is declared inline instead of in the constructor } - + render() { return this.myRef = el} />; } @@ -668,7 +679,7 @@ class BadRefComponent extends Component { class GoodRefComponent extends Component { constructor(props) { super(props); - + // Good: Ref is declared in the constructor this.myRef = undefined; } diff --git a/STYLING.md b/STYLING.md new file mode 100644 index 000000000000..cbaf14efab88 --- /dev/null +++ b/STYLING.md @@ -0,0 +1,216 @@ +# React Native Styling Guidelines + +## Where to Define Styles + +All styles must be defined in the `/styles` directory and `styles.js` contains the final export after gathering all appropriate styles. Unlike some React Native applications we are not using `StyleSheet.create()` and instead store styles as plain JS objects. There are also many helper styles available for direct use in components. + +These helper styles are loosely based on the [Bootstrap system of CSS utility helper classes](https://getbootstrap.com/docs/5.0/utilities/spacing/) and are typically incremented by units of `4`. + +**Note:** Not all helpers from Bootstrap exist, so it may be necessary to create the helper style we need. + +## When to Create a New Style + +If we need some minimal set of styling rules applied to a single-use component then it's almost always better to use an array of helper styles rather than create an entirely new style if it will only be used once. Resist the urge to create a new style for every new element added to a screen. There is a very good chance the style we are adding is a "single-use" style. + +```jsx +// Bad - Since we only use this style once in this component +const TextWithPadding = props => ( + + {props.children} + +); + +// Good +const TextWithPadding = props => ( + + {props.children} + +); +``` + +On the other hand, if we are copying and pasting some chunks of JSX from one place to another then that might be a sign that we need a new reusable style. + +## Use the "Rule of Three" + +In order to resist the urge to preoptimize and have many single-use components we've adopted a main principle: + +Any array of styles associated with a single type of React element that has at least 3 identical usages should be refactored into: + +- A new resusable style that can be used in many places e.g. `styles.button` +- If that style has modifiers or style variations then those styles should follow a naming convention of `styles.elementModifer` e.g. `styles.buttonSuccess` +- If a reusable style has 3 or more modifiers it should be refactored into a component with props to modify the styles e.g. + +```jsx +