react-toolbox/README.md

165 lines
9.0 KiB
Markdown
Raw Normal View History

2015-11-01 20:25:29 +03:00
# React Toolbox
2015-06-19 12:21:46 +03:00
2015-11-14 14:38:00 +03:00
[![npm version](https://img.shields.io/npm/v/react-toolbox.svg?style=flat-square)](https://www.npmjs.com/package/react-toolbox)
2015-11-08 14:44:58 +03:00
[![Build Status](http://img.shields.io/travis/react-toolbox/react-toolbox/master.svg?style=flat-square)](https://travis-ci.org/react-toolbox/react-toolbox)
[![NPM Status](http://img.shields.io/npm/dm/react-toolbox.svg?style=flat-square)](https://www.npmjs.org/package/react-toolbox)
2015-11-15 18:59:11 +03:00
[![react-toolbox channel on discord](https://img.shields.io/badge/discord-%23react--toolbox%20%40%20reactiflux-61dafb.svg?style=flat-square)](https://discord.gg/0ZcbPKXt5bW9FLzM)
2015-11-03 10:32:46 +03:00
[![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/javivelasco)
2015-11-01 19:52:07 +03:00
2016-02-21 21:47:52 +03:00
React Toolbox is a set of [React](http://facebook.github.io/react/) components that implement [Google's Material Design specification](https://www.google.com/design/spec/material-design/introduction.html). It's powered by [CSS Modules](https://github.com/css-modules/css-modules) and harmoniously integrates with your [webpack](http://webpack.github.io/) workflow. You can take a tour through our documentation website and try the components live!
2015-11-01 19:52:07 +03:00
## Installation
2016-02-21 21:47:52 +03:00
React Toolbox can be installed as an [npm package](https://www.npmjs.org/package/react-toolbox):
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
```bash
2015-11-01 19:52:07 +03:00
npm install --save react-toolbox
```
2016-06-03 23:24:26 +03:00
## Prerequisites
React Toolbox uses [CSS Modules](https://github.com/css-modules/css-modules) and [SASS](http://sass-lang.com/) to provide default stylesheets. If you want to import components bundled with stylesheets, your module bundler should be able to require SASS modules. You can use whatever module bundler you want as long as it can require SASS files from `node_modules`, but we recommend [webpack](). If you are experiencing require errors, make sure your configuration satisfies the requirements.
Of course this is a set of React components so you should be familiar with [React](). If you are willing to customize your components via themes, you may want to take a look to [react-css-themr]() which is used to make styling easier.
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
## Basic usage
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
The minimal example requires a `Button` bundled with styles:
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
```
2015-11-01 19:52:07 +03:00
import React from 'react';
2016-06-03 23:24:26 +03:00
import ReactDOM from 'react-dom';
import { Button } from 'react-toolbox/lib/button';
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
ReactDOM.render(
<Button label="Hello World!" />,
document.getElementById('app')
2015-11-01 19:52:07 +03:00
);
```
2016-06-03 23:24:26 +03:00
Take into account that any required style will be bundled in the final CSS so you probably would want to require components one by one instead of requiring directly from the root index.
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
## Importing components
2015-11-29 22:13:38 +03:00
2016-06-03 23:24:26 +03:00
First let's take a look on how the components are structured in the project. The [components](https://github.com/react-toolbox/react-toolbox/tree/dev/components) folder contains a folder for each component or set of related components. For example, the `app_bar`:
2015-11-29 22:13:38 +03:00
2016-06-03 23:24:26 +03:00
```
|- /app_bar
|---- AppBar.js
|---- _config.scss
|---- index.js
|---- readme.md
|---- theme.scss
```
There you can see a component definition file, a readme, an index file, a theme stylesheet and a configuration file holding the SASS variables to configure the stylesheet. Depending on whether you want the styles to be directly bundled or not, you can import components in two different ways:
- **Bundled component**: the component requires the corresponding `theme.scss` for you so it will be included in the final bundle. Also, the local classnames will be injected in the component automatically. To import a bundled component you have to require from the `index.js` file. For example: `import {AppBar} from 'react-toolbox/lib/app_bar'`.
- **Raw component**: the component is required alone, without any CSS. In this case you are responsible from providing a theme. To import a raw component you have to require directly from the component definition. For example: `import AppBar from 'react-toolbox/lib/app_bar/AppBar'`.
2015-11-15 22:53:59 +03:00
2015-11-01 19:52:07 +03:00
## Customization
2016-06-03 23:24:26 +03:00
Since you can import raw components and then inject a theme via props or context, you can use whatever you want to provide styles. We give you some SASS stylesheets but you can configure them at import time or even port them to CSS Next or whatever. Furthermore, you can provide extra theming classes that will be mixed in the component so you can even target DOM elements in subcomponents from a parent. Let's see some examples.
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
### Customization via SASS Loader
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
Every component in React Toolbox has a `_config.scss` partial and [some parent partials](https://github.com/react-toolbox/react-toolbox/tree/dev/components) defining configuration variables that are used in each `theme.scss`. Since all variables are defined as `!default`, you can prepend variable overrides with a custom `theme.scss` file to each SASS stylesheet required in the project.
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
If you are importing bundled components, you can use something like [sass-loader](https://github.com/jtangelder/sass-loader) to prepend your custom configuration files to every sass stylesheet by using the `data` option. For example, in your webpack config:
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
```js
sassLoader: {
data: '@import "' + path.resolve(__dirname, 'theme/_theme.scss') + '";'
}
```
### Customization via Theme Property
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
Every component in React Toolbox have a **classname API** that can be browsed from the documentation. Also, they accept a `theme` property intended to provide a [CSS Module import object](https://github.com/css-modules/css-modules) that will be used by the component to assign local classnames to its DOM nodes.
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
React Toolbox uses [react-css-themr](github.com/javivelasco/react-css-themr) to make theming easier. Feel free to take a look to the documentation to learn how you can use themes for the components. For example, imagine you want to create a green success button whose icons are red:
```
// SuccessButton.js
import { Button } from 'react-toolbox/lib/button';
import successTheme from './success-theme.scss';
const SuccessButton = (props) => (
<Button theme={successButtonTheme} {...props} />
2015-11-01 19:52:07 +03:00
);
2016-06-03 23:24:26 +03:00
export default SuccessButton;
2015-11-01 19:52:07 +03:00
```
2016-06-03 23:24:26 +03:00
```
// success-theme.scss
.primary {
background: green;
}
2015-11-01 19:52:07 +03:00
2016-06-03 23:24:26 +03:00
.icon {
color: red
2015-11-01 19:52:07 +03:00
}
```
2016-06-03 23:24:26 +03:00
The given classes will be added to the component and, since they are defined *after* the original CSS they would take priority. Note that you can also boost priority by assigning a `className`. Check more examples at [react-css-themr](www.github.com/javivelasco/react-css-themr) documentation.
### Customization via Theme Context
Alternatively, you can provide CSS Modules object to React Toolbox components using context. This is useful in case you want to create a custom theme without creating wrappers for each raw component. To use this customization you need to install react-css-themr and then use the **theme keys** specified in each component documentation. For example:
```
import React from 'react';
import ReactDOM from 'react-dom';
import { ThemeProvider } from 'react-css-themr';
import Button from 'react-toolbox/lib/button/Button';
import App from './App.js';
const theme = {
RTButton: require('./theme/button-style')
};
const ThemedApp = (children) => (
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>
)
ReactDOM.render(<ThemedApp />, document.getElementById('app'));
```
## Roboto Font and Material Design Icons
React Toolbox assumes that you are importing [Roboto Font](https://www.google.com/fonts/specimen/Roboto) and [Material Design Icons](https://www.google.com/design/icons/).
In order to import the fonts for you, we'd need to include them in the CSS which is considered a bad practice. If you are not including them in your app, go to the linked sites and follow the instructions.
## TypeScript
A TypeScript definition file `react-toolbox.d.ts` is available. It is referenced in `package.json` and should be picked up by the TypeScript compiler when importing from the npm package.
Note that to comply with the typings requirement, a triple-slash reference to `react.d.ts` is *NOT included*. You will need to reference [react.d.ts](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/react/react.d.ts) somewhere in your project.
2015-11-01 19:52:07 +03:00
## Authors and Contributors
2015-11-01 19:56:04 +03:00
The project is being initially developed and maintained by [Javier Velasco](http://javivelasco.com) and [Javier Jiménez](http://soyjavi.com) and the [contribution scene](https://github.com/react-toolbox/react-toolbox/graphs/contributors) is just getting warm. We want to create reference components so any contribution is very welcome.
2015-11-01 19:52:07 +03:00
2016-02-21 21:47:52 +03:00
To work in the project you'd need a `node` version supporting ES6 syntax. Although the project is built using Babel we use some ES6 features in the development server. Also, the package has been tested with `node 4.2.1`. Consider using [nvm](https://github.com/creationix/nvm) or [n](https://github.com/tj/n) to handle different node versions!
2016-02-21 21:47:52 +03:00
To start the documentation site locally, you'll need to install the dependencies from both the main package and the docs subproject:
```
git clone https://github.com/react-toolbox/react-toolbox.git
2015-11-21 23:50:28 +03:00
npm install
cd docs/
2016-04-10 18:44:42 +03:00
npm install
npm start
```
2016-02-21 21:47:52 +03:00
Local documentation will then be available at `http://localhost:8081/`.
## License
2015-11-01 19:52:07 +03:00
This project is licensed under the terms of the [MIT license](https://github.com/react-toolbox/react-toolbox/blob/master/LICENSE).