actions | ||
css | ||
docs | ||
examples | ||
preprocess | ||
scripts | ||
src | ||
tests | ||
types | ||
.gitignore | ||
.prettierignore | ||
.travis.yml | ||
CHANGELOG.md | ||
CODEOWNERS | ||
COMPONENT_INDEX.md | ||
CONTRIBUTING.md | ||
LICENSE | ||
MAINTAINERS.md | ||
package.json | ||
README.md | ||
rollup.config.js | ||
tsconfig.json | ||
yarn.lock |
carbon-components-svelte
Carbon Components Svelte is a Svelte component library that implements the Carbon Design System, an open source design system by IBM.
Design systems facilitate design and development through reuse, consistency, and extensibility.
The Carbon Svelte portfolio also includes:
- Carbon Icons Svelte: 7000+ Carbon icons as Svelte components
- Carbon Pictograms Svelte: 700+ Carbon pictograms as Svelte components
- Carbon Charts Svelte: 20+ charts, powered by d3
- Carbon Preprocess Svelte: Collection of Svelte preprocessors for Carbon
Documentation
The documentation website contains live demos and examples.
Other forms of documentation are auto-generated:
- TypeScript definitions: Component TypeScript definitions
- Component Index: Component API in Markdown format
- Component API: Component API in JSON format
Installation
Install carbon-components-svelte
as a development dependency.
Yarn
yarn add -D carbon-components-svelte
NPM
npm i -D carbon-components-svelte
Usage
Importing components
Import components from carbon-components-svelte
in the script
tag of your Svelte file.
<!-- App.svelte -->
<script>
import { Accordion, AccordionItem } from "carbon-components-svelte";
</script>
<Accordion>
<AccordionItem title="Section 1" open> Content 1 </AccordionItem>
<AccordionItem title="Section 2"> Content 2 </AccordionItem>
<AccordionItem title="Section 3"> Content 3 </AccordionItem>
</Accordion>
Refer to COMPONENT_INDEX.md for component API documentation.
Pre-compiled CSS StyleSheets
carbon-components-svelte
includes pre-compiled CSS StyleSheets for each Carbon theme:
- white.css: Default Carbon theme (light)
- g10.css: Gray 10 theme (light)
- g80.css: Gray 80 theme (dark)
- g90.css: Gray 90 theme (dark)
- g100.css: Gray 100 theme (dark)
- all.css: All themes (White, Gray 10, Gray 90, Gray 100) using CSS variables
Each StyleSheet is generated from the flagship carbon-components library.
The compiled CSS is generated from the following .scss
files:
Usage
svelte-preprocess
The easiest way to import a StyleSheet is with svelte-preprocess.
const svelteOptions = {
preprocess: require("svelte-preprocess")(),
};
<!-- App.svelte -->
<style lang="scss" global>
/** Gray 10 theme **/
@import "carbon-components-svelte/css/g10";
</style>
JavaScript import
Importing a CSS file in a JavaScript file will require the appropriate file loader(s).
import "carbon-components-svelte/css/all.css";
import App from "./App.svelte";
const app = new App({ target: document.body });
export default app;
See webpack.config.js in examples/webpack.
Dynamic theming
Use carbon-components-svelte/css/all.css
for dynamic, client-side styling.
Update the theme by setting the theme
attribute on the html
element. The default theme
is "white"
.
<!DOCTYPE html>
<html lang="en" theme="g10">
<body>
...
</body>
</html>
Using JavaScript:
<script>
/** @type {"white" | "g10" | "g80" | "g90" | "g100"} */
let theme = "white";
$: document.documentElement.setAttribute("theme", theme);
</script>
<button on:click="{() => (theme = 'g90')}">Update theme</button>
Preprocessors
optimizeCarbonImports
optimizeCarbonImports
is a Svelte preprocessor that optimizes base imports inside the script
block of a Svelte file from the following libraries:
- carbon-components-svelte
- carbon-icons-svelte
- carbon-pictograms-svelte
The preprocessor rewrites base imports to directly import the source Svelte file. This may lead to faster complile times during development.
Example:
Before
import { Button, Header } from "carbon-components-svelte";
import { Notification20 } from "carbon-icons-svelte";
import { Airplane } from "carbon-pictograms-svelte";
After
import Button from "carbon-components-svelte/Button/Button.svelte";
import Header from "carbon-components-svelte/UIShell/GlobalHeader/Header.svelte";
import Notification20 from "carbon-icons-svelte/lib/Notification20/Notification20.svelte";
import Airplane from "carbon-pictograms-svelte/lib/Airplane/Airplane.svelte";
svelte.config.js
// svelte.config.js
const {
optimizeCarbonImports,
} = require("carbon-components-svelte/preprocess");
module.exports = {
preprocess: [optimizeCarbonImports()],
};
svelte-loader
// webpack.config.js
const {
optimizeCarbonImports,
} = require("carbon-components-svelte/preprocess");
module.exports = {
// ...
module: {
rules: [
{
test: /\.svelte$/,
use: {
loader: "svelte-loader",
options: {
hotReload: true,
preprocess: [optimizeCarbonImports()],
},
},
},
],
},
};
Examples
TypeScript support
TypeScript definitions are generated by sveld.
Contributing
Refer to the Contributing guidelines.