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: 5800+ Carbon icons as Svelte components
- Carbon Pictograms Svelte: 600+ Carbon pictograms as Svelte components
- Carbon Charts Svelte: 16 chart types, powered by d3
The documentation website contains live demos and examples. Other forms of documentation are auto-generated:
- Component Index: Markdown file documenting component props, slots, and events
- TypeScript definitions: Component TypeScript definitions
- Component API: Component API metadata in JSON format
Install carbon-components-svelte
as a development dependency.
yarn add -D carbon-components-svelte
# OR
npm i -D carbon-components-svelte
The quickest way to get started is to customize a template from the examples folder.
Example set-ups demonstrate usage with popular application bundlers and frameworks. They feature a mix of Single-page Applications (SPA), Server-side rendering (SSR) and statically exported approaches.
- examples/rollup: SPA bundled using Rollup
- examples/rollup-typescript: SPA bundled using Rollup with TypeScript support
- examples/routify: SPA + static export using Routify
- examples/sapper: SSR + static export using Sapper
- examples/svite: SPA developed with Svite, bundled with Rollup
- examples/webpack: SPA bundled with webpack
Each example is published in a dedicated branch of the same name.
Use degit to scaffold a new project:
For example, to use the svite
template, run the following commands:
npx degit ibm/carbon-components-svelte#svite svelte-app
cd svelte-app
yarn install
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.
carbon-components-svelte
includes precompiled CSS StyleSheets for each Carbon theme:
- white.css: Default Carbon theme (light)
- g10.css: Gray 10 theme (light)
- 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 examples use all.css
for dynamic theming through CSS variables.
Because the CSS is precompiled, it includes all Carbon design styles. One method to optimize the CSS is to ship only the CSS that is used. This can be accomplished using PurgeCSS with a Carbon style extractor (WIP).
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>
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.
TypeScript definitions are generated by sveld.
Refer to the Contributing guidelines.