Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
CSS in JS documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jkup committed Oct 27, 2016
1 parent 463c53d commit 77868c2
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
106 changes: 106 additions & 0 deletions docs/style.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Overview

All applicable styles should be colocated with their corresponding JavaScript component using [Aphrodite](https://github.com/Khan/aphrodite).

## Legacy

All legacy styles are processed with [Less](http://lesscss.org/) and can be found in the /less directory. These should still be maintained but all future CSS should be written in JavaScript and kept inside their component file.

## Example

Here is an example of a React component styled with Aphrodite.

Aphrodite will automatically create a `<style>` tag in the document `<head>` to put its generated styles in.

```jsx
const React = require('react')
const ImmutableComponent = require('./immutableComponent')
const Aphrodite = require('aphrodite')
const StyleSheet = Aphrodite.StyleSheet
const css = Aphrodite.css

class Button extends ImmutableComponent {
render () {
return <span className={css(styles.browserButton)}
disabled={this.props.disabled}
data-l10n-id={this.props.l10nId}
data-button-value={this.props.dataButtonValue}
onClick={this.props.onClick} />
}
}

const styles = StyleSheet.create({
browserButton: {
cursor: 'default',
display: 'inline-block',
lineHeight: '25px',
width: '25px',
height: '25px',

':hover': {
color: 'black'
},

'@media (min-width: 500px)': {
width: '100px'
}
}
})

module.exports = Button
```

A few things to note:

1. All multi-word properties become camel cased (lineHeight instead of line-height).
2. Pseudo selectors and media queries can be given their own class or be nested within a selector.

## Passing Styles to Child Components

With Aphrodite, styles can be treated as props and passed down from parent to child. Here is an example:

```jsx
// addEditBookmarkHanger.js

const React = require('react')
const ImmutableComponent = require('../../../js/components/immutableComponent')
const Button = require('../../../js/components/button')
const Aphrodite = require('aphrodite')
const StyleSheet = Aphrodite.StyleSheet
const css = Aphrodite.css

class AddEditBookmarkHanger extends ImmutableComponent {
render () {
<Button styles={[styles.hangerButton, styles.hangerText]} />
}
}

const styles = StyleSheet.create({
hangerButton: {
width: '50px',
height: '25px',

':hover': {
color: orange
}
},

hangerText: {
fontSize: '20px'
}
})

// button.js

const React = require('react')
const ImmutableComponent = require('./immutableComponent')
const Aphrodite = require('aphrodite')
const StyleSheet = Aphrodite.StyleSheet
const css = Aphrodite.css

class Button extends ImmutableComponent {
render () {
return <span className={css(this.props.styles)} />
}
}
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"dependencies": {
"abp-filter-parser-cpp": "1.2.x",
"acorn": "3.2.0",
"aphrodite": "^1.0.0",
"async": "^2.0.1",
"electron-localshortcut": "^0.6.0",
"electron-prebuilt": "brave/electron-prebuilt",
Expand Down

0 comments on commit 77868c2

Please sign in to comment.