-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
304 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Documentation | ||
|
||
* [Getting Started](./getting-started.md) | ||
* [Dynamic Styles](./dynamic-styles.md) | ||
* [Nesting](./nesting.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Dynamic styles | ||
|
||
In this guide, we're going to be making our CSS styles dynamic, based on your React component's props. | ||
|
||
This technique is similar to the ones found in [Styled Components](https://www.styled-components.com/). | ||
|
||
You can define dynamic styles by passing a `function` into `fancy`. It will have access to a component's `props`, allowing you to define custom rules for various `prop` values. | ||
|
||
### Example | ||
|
||
```jsx | ||
const Card = props => (<div {...props} />) | ||
const css = (props) => ` | ||
div { | ||
background: ${props.title ? 'red' : 'blue'}; | ||
position: relative; | ||
border: 1px solid black; | ||
} | ||
` | ||
const StyledCard = fancy(css)(Card) | ||
``` | ||
|
||
|
||
### 🤔 How does it work… | ||
|
||
Under the hood, Fancy does a `diff` check in it's virtual Stylesheet (not unlike how React handles VDOM diffing) every time a [component updates](https://reactjs.org/docs/react-component.html#componentdidupdate). If an update occurs, it regenerates the style rules and re-injects it into the document stylesheet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Getting Started | ||
|
||
In this guide, we're going to be "fancifying" a React component with some basic CSS styles. | ||
|
||
### Step 1: Import Fancy | ||
|
||
```jsx | ||
import React from 'react' | ||
import fancy from '@helpscout/fancy' | ||
``` | ||
|
||
|
||
### Step 2: Define your styles | ||
|
||
Write your CSS styles as a string. This is default out-of-the-box CSS. Use things like `:hover`, `@media` queries, as you normally would! | ||
|
||
```jsx | ||
const css = ` | ||
.Button { | ||
background: white; | ||
border: 1px solid #eee; | ||
} | ||
` | ||
``` | ||
|
||
Note: You can of course use string interpolation. It's still JS after all! | ||
|
||
|
||
### Step 3: Create your component | ||
|
||
Create your component as you normally would. | ||
|
||
```jsx | ||
const Button = props => ( | ||
<button className='Button' {...props} /> | ||
) | ||
``` | ||
|
||
Note: The reference the CSS `className` to match the CSS you wrote. Fancy does not generated uniquely hashed classNames for you. See [CSS Modules](https://github.com/css-modules/css-modules) for that feature. | ||
|
||
|
||
### Step 4: Compose your CSS with your component | ||
|
||
When exporting your component, compose it with the `fancy` higher-order component along with your CSS styles. | ||
|
||
```jsx | ||
export default fancy(css)(Button) | ||
``` | ||
|
||
|
||
### Final code | ||
|
||
```jsx | ||
import React from 'react' | ||
import fancy from '@helpscout/fancy' | ||
|
||
const css = ` | ||
.Button { | ||
background: white; | ||
border: 1px solid #eee; | ||
} | ||
` | ||
|
||
const Button = props => ( | ||
<button className='Button' {...props} /> | ||
) | ||
|
||
export default fancy(css)(Button) | ||
``` | ||
|
||
### Results | ||
|
||
```html | ||
<html> | ||
<head> | ||
<style type='text/css'> | ||
.Button { | ||
background: white; | ||
border: 1px solid #eee; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
... | ||
<button class='Button'>Button</button> | ||
... | ||
</body> | ||
</html> | ||
``` | ||
|
||
That's it! You're done 🙌. You've created a CSS-ready component. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Nesting | ||
|
||
Fancy comes with [stylis](https://github.com/thysultan/stylis.js), a tiny CSS pre-processor. This (amazing) little library provides nifty features like nesting, similar to what you'd get with Less or Sass. | ||
|
||
|
||
### Example | ||
|
||
```jsx | ||
const css = ` | ||
.Card { | ||
background: white; | ||
position: relative; | ||
border: 1px solid black; | ||
&__block { | ||
padding: 20px; | ||
} | ||
} | ||
` | ||
|
||
const Card = props => ( | ||
<div className='Card'> | ||
<div className='Card__block'> | ||
{props.children} | ||
</div> | ||
</div> | ||
) | ||
|
||
const StyledCard = fancy(css)(Card) | ||
``` |
Oops, something went wrong.