@salesforce/design-system-react
Version:
Salesforce Lightning Design System for React
143 lines (108 loc) • 4.12 kB
Markdown
Bootstrap React App with create-react-app 2x and Salesforce Lightning Design System (SLDS) without ejecting the webpack config
## Resources:
- https://reactjs.org/docs/create-a-new-react-app.html
- https://react.lightningdesignsystem.com/
- https://www.lightningdesignsystem.com/
## Sample Repo
Follow this repo at https://github.com/synle/create-react-app-salesforce-lightning to view a fully functional create-react-app 2x with Salesforce Lightning Design System
## Requirements
Make sure you have node js version >=8. You can have it installed using `nvm`, `n` or just binary from your OS of choice
```
npm install
npm start
```
```
npx create-react-app my-app
cd my-app
npm start
```
By default, create-react-app hides the webpack config from us, we need to use a package called `rewire` to modify the webpack config to properly install salesforce lightning
More on that here: https://github.com/timarney/react-app-rewired
```
npm install react-app-rewired --save-dev
```
Then changes the package.json scripts to use `react-app-rewired`
```
/* package.json */
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test --env=jsdom",
}
```
Create a new file called `config-overrides.js` with the following content
```
const path = require('path');
module.exports = function override(config, env) {
config.module.rules = [
// salesforce dependencies
// this will compile salesforce lightning as src, not as package
{
test: /\.jsx?$/,
include: [
'node_modules/@salesforce/design-system-react',
].map(
someDir => path.resolve(
process.cwd(),
someDir
)
),
loader: require.resolve('babel-loader'),
options: {
presets: [
"react-app"
],
},
},
].concat(config.module.rules);
return config;
}
```
```
npm install --save @salesforce-ux/design-system @salesforce/design-system-react
```
Follow the instruction from Salesforce lightning website at https://github.com/salesforce/design-system-react/blob/master/docs/create-react-app.md
Overall, you need to copy the following items: CSS, Fonts, and icon SVGs
```
cp node_modules/@salesforce-ux/design-system/assets/styles/salesforce-lightning-design-system.min.css public/
```
```
cp -r node_modules/@salesforce-ux/design-system/assets/fonts public/
```
Note that here I copy the icons into `public/assets` instead of `public` as in the original guide from Salesforce Repo. The reason for this is that most code samples from the SLDS page reference the icon path from `assets/icons`.
```
mkdir -p public/assets
cp -r node_modules/@salesforce-ux/design-system/assets/icons public/assets
```
This is optional. But if you are using Salesforce Illustrations, make sure you copy over the illustrations files from Salesforce module
```
mkdir -p public/assets
cp -r node_modules/\@salesforce/design-system-react/assets/images public/assets/
```
Add the following to your `head` tag
```
<link rel="stylesheet" type="text/css" href="/salesforce-lightning-design-system.min.css">
```
In your index.js (root / entry point file) to set the path to Salesforce Icon. Just wrap it where you would normally mount your app.
```
import IconSettings from '@salesforce/design-system-react/components/icon-settings';
<IconSettings iconPath="/icons">
...
</IconSettings>
```
Visit this link to start using Salesforce Lightning Design Components https://react.lightningdesignsystem.com/components