Use Salesforce Lightning Design System with LWR
You can use the Salesforce Lightning Design System (SLDS) to style your LWR site. Setting up SLDS for an LWR app lets you add base Lightning web components to your app. You have to enable SLDS before you can pull over base components.
Follow these steps to make SLDS available to your app.
In package.json
, add a dependency on @salesforce-ux/design-system
to the devDependencies
section. Check npm: @salesforce-ux/design-system for the latest stable version of the package.
To ensure your project uses the latest compatible version of the SLDS package, we recommend using a script to copy SLDS resources into your app every time you run npm run build
.
-
Make sure you have a
scripts
folder at the top level of your project. -
In the
scripts
folder, create a new file calledcopy-slds.mjs
and add your build script. Here's an example script: -
In
package.json
, add a dependency on thecpx
package todevDependencies
and update thebuild
script to runcopy-slds.mjs
. To ensure that SLDS resources copy over before your site gets built, make sure you run thecopy-slds
script beforelwr build
.
In your app's static layout template, add or change the link
to point to the SLDS stylesheets. The layout template can be an .html
or a .njk
file.
For more information about the context object properties body
and lwr_resources
, see Compile-Time Data: LWR
-
In the
routes
section of the file, configure thebootstrap
property to set thesyntheticShadow
property totrue
. EnablingsyntheticShadow
lets the SLDS stylesheet function as global styles. -
If they don't already exist, add the
/assets
and/utility-sprite
paths to SLDS stylesheets and images, respectively, to allow browser access.
If you initialized your project as a git repo, this step applies to you!
The LWR build
command generates a few very large folders in your repo. For example, LWR creates two assets
folders (that each contain over 4,000 files) in your project directory at build time.
To easily avoid adding unnecessary content to your repo, we recommend revising your .gitignore
to exclude certain folders.
Run the following terminal commands from the root of your project:
Read Get Started with LWR for details on LWR's NPM commands.
Open the site at http://localhost:3000. If you're already using port 3000 for something else, review your terminal output for the alternative port being used.
Now that you've set up SLDS for your LWR site, you can configure your app to use base Lightning web components (LWC). Base components run on SLDS, so you have to enable SLDS before you can add components to your site.
To start adding LWC to your LWR app, take a look at Base Lightning Web Components.