{"componentChunkName":"component---src-pages-get-started-develop-vanilla-mdx","path":"/get-started/develop/vanilla/","webpackCompilationHash":"9ffd1cb8ca3b610e9ad3","result":{"pageContext":{"isCreatedByStatefulCreatePages":true,"frontmatter":{"title":"Develop","description":"The Carbon Design System supports vanilla JS, React and Angular as core parts of the product. But you can still build components even if you're using a different framework.","tabs":["React","Vanilla","Angular","Vue","Other frameworks"]},"relativePagePath":"/get-started/develop/vanilla.mdx","titleType":"prepend","MdxNode":{"id":"9fc6a42b-498d-569e-bf18-4f31d7324e3e","children":[],"parent":"b00b63de-242c-5f4c-8468-e7da1487da14","internal":{"content":"---\ntitle: Develop\ndescription: The Carbon Design System supports vanilla JS, React and Angular as core parts of the product. But you can still build components even if you're using a different framework.\ntabs: ['React', 'Vanilla', 'Angular', 'Vue', 'Other frameworks']\n---\n\n<AnchorLinks>\n\n<AnchorLink>Carbon Vanilla library</AnchorLink>\n<AnchorLink>Install</AnchorLink>\n<AnchorLink>Getting started</AnchorLink>\n<AnchorLink>Troubleshooting</AnchorLink>\n\n</AnchorLinks>\n\n## Carbon Vanilla library\n\nThe library provides front-end developers & engineers a collection of reusable HTML and SCSS partials to build websites and user interfaces. Adopting the library enables developers to use consistent markup, styles, and behavior in prototype and production work.\n\n## Install\n\n#### Using npm:\n\n<br />\n\n```bash\n$ npm install --save carbon-components\n```\n\n#### If you prefer [Yarn](https://yarnpkg.com/en/):\n\n<br />\n\n```bash\n$ yarn add carbon-components\n```\n\n_NB: March 29, 2019 - Please use the @next tag to install Carbon v10 components. e.g. `npm i carbon-components@next`._\n\n## Getting started\n\n<AnchorLinks small>\n\n<AnchorLink>What's included</AnchorLink>\n<AnchorLink>CDN</AnchorLink>\n<AnchorLink>CodePen</AnchorLink>\n<AnchorLink>SCSS</AnchorLink>\n<AnchorLink>JavaScript</AnchorLink>\n<AnchorLink>Polyfills for older browsers</AnchorLink>\n\n</AnchorLinks>\n\n### What's included\n\n```bash\ncarbon-components/\n├── html\n├── css\n│   ├── carbon-components.css\n│   └── carbon-components.min.css\n├── scripts\n│   ├── carbon-components.js\n│   └── carbon-components.min.js\n├── scss\n│   └── components\n│       └── modal\n│           └── _modal.scss\n├── umd\n│   └── index.js\n├── es\n│   └── index.js\n└── src (Deprecated and subject to breaking changes, please use es/umd instead)\n```\n\n### CDN\n\nUse unpkg for easy access to our built static files. This is great for prototyping and trying `carbon-components` without installing anything. See all files from `carbon-components` available on [unpkg CDN](https://unpkg.com/carbon-components/).\n\n| Scripts | URL                                                                  |\n| ------- | -------------------------------------------------------------------- |\n| CSS     | https://unpkg.com/carbon-components/css/carbon-components.min.css    |\n| ES5     | https://unpkg.com/carbon-components/scripts/carbon-components.min.js |\n\n### CodePen\n\nWe have individual [CodePens](https://codepen.io/collection/XqRbEz/) for all of our components which you can easily fork and play around with.\n\n### SCSS\n\nUsing the Carbon Sass files infers usage of the SCSS pre-processor.\nAll Sass files use the `*.scss` file extension.\n\nFor transpiling Sass code, use `node-sass` based Sass compilers, for example, WebPack `sass-loader` or `gulp-sass`.\n\nIf you're starting a new project without a boilerplate,\nyou need to know about a few things to get started.\n\n#### Autoprefixer\n\nMake sure your build process uses [autoprefixer](https://github.com/postcss/autoprefixer) to ensure vendor prefixes are automatically added to your output CSS.\n\n#### Default body styles\n\nCSS is automatically applied to `<body>` element, which comes from [\\_css--body.scss](https://github.com/carbon-design-system/carbon/blob/master/src/globals/scss/_css--body.scss). These styles are meant to cascade down to everything in `<body>` to set common styles shared across all components.\n\n```scss\nbody {\n  @include reset;\n  @include type-style('body-short-01');\n  color: $text-01;\n  background-color: $ui-background;\n  line-height: 1;\n}\n```\n\n#### Icons\n\nA lot of components depend on SVG icons from [carbon-icons](https://github.com/carbon-design-system/carbon-icons). Read the [docs](https://github.com/carbon-design-system/carbon-icons/blob/master/docs/usage.md) for details on how to use them.\n\n#### Global SCSS variables\n\nThese variables are used to configure which parts of the SCSS get compiled, where each variable controls a SCSS file of the same name. All variables are set to `true` by default.\n\nFor example:\n\n- When you set `$css--reset: true`, then the contents of [\\_css--reset.scss](https://github.com/carbon-design-system/carbon/blob/master/src/globals/scss/_css--reset.scss) will be part of your output CSS.\n- When you set `$css--reset: false`, then nothing gets included from that SCSS file.\n- When the variable is not declared at all, then nothing gets included from that SCSS file.\n\nThe same rules apply to all the following variables:\n\n```scss\n// In styles.scss:\n// These are the default settings.\n$css--font-face: true !default;\n$css--helpers: true !default;\n$css--body: true !default;\n$css--use-layer: true !default;\n$css--reset: true !default;\n$css--typography: true !default;\n$css--plex: true !default;\n```\n\nThese flags are set for you by default when you `@import` the `styles.scss` file.\nYou can override these default settings by redeclaring the variables.\n\n#### Importing SCSS files\n\nTo add a component style to your build, simply import the component directly. Importing a component this way will bring in any dependencies that component has as well. The import system removes duplicate dependencies, so shared dependencies between components will not create extra CSS.\n\n```js\n@import 'node_modules/carbon-components/scss/components/card/card';\n```\n\n#### Namespacing\n\nCarbon Components are built to be included individually and not clobber global styles - all `class` attributes are prefixed by the `bx--` moniker. You can specify your own prefix by changing the SCSS variable `$prefix`.\n\n### JavaScript\n\nCarbon Component has component JavaScript classes, each of which correspond to a component found in [our components page](../../components/overview). The first steps to work with component JavaScript classes are the following:\n\n#### 1. Getting component JavaScript class reference\n\n<br />\n\n**Using a module bundler:** We recommend using ECMAScript module along with your module bundler toolchain to do so. Using a module bundler will bring in only the component code your application needs, creating an optimized build for production. Carbon Components ships with a ECMAScript module build as well as UMD build for each component, for use with webpack or Rollup.\n\nAfter you've installed the components through `npm`, you can grab a component JavaScript class reference by something like this:\n\n```js\nimport { Modal } from 'carbon-components';\n```\n\n**Using pre-built bundle:** Users can also opt to use the pre-built `carbon-components.js` file directly, like below. We recommend that most users do _not_ use this file, as it includes components your application may or may not actually be using.\n\n```html\n<html>\n  <body>\n    <!-- Put HTML snippets of components here... -->\n    <script src=\"node_modules/carbon-components/scripts/carbon-components.min.js\"></script>\n  </body>\n</html>\n```\n\nOnce you load `carbon-components.js` via `<script>` tag, you can grab a component JavaScript class reference by something like this:\n\n```js\nvar Modal = CarbonComponents.Modal;\n```\n\n_Note: By default, pre-built bundle automatically instantiates all components on the page when_ `DOMContentLoaded` _event on the page fires. In case you don't want that behavior, you can set_ `CarbonComponents.settings.disableAutoInit = true` _right after_ `carbon-components.js` _is loaded._\n\n_Caveat: Don't use pre-built_ `carbon-components.js` _if you are importing components via ECMAScript module syntax. Doing so will cause Carbon code loaded twice, often competing each other._\n\n#### 2. Instantiating component class on DOM nodes\n\nOnce you have a [component JavaScript class reference](#1-getting-component-javascript-class-reference), you can instantiate it on a DOM node with the `.create()` API.\n\nFor example, if you have the following HTML for modal:\n\n```html\n<div data-modal id=\"modal-nofooter\" class=\"bx--modal\" tabindex=\"-1\">\n  <div class=\"bx--modal-container\">...</div>\n</div>\n```\n\nYou can create and get the instance by:\n\n```js\nconst modalElement = document.getElementById('modal-nofooter');\nconst modalInstance = Modal.create(modalElement);\n```\n\n_Note: The DOM element to instantiate components on typically has a `data-componentname` attribute, e.g. `data-modal` for modal._\n\nInstantiating a component basically does two things:\n\n- Hooks several event handlers on some DOM elements inside (in above example, ones in `modalElement`, e.g. close button)\n- Allows you to access public methods (found in [our components page](../../components/overview), e.g. [here](/components/modal/code#public-methods) for modal) via the instance reference (`modalInstance.show()`, etc. in above example)\n\n#### Higher-level component instantiation API\n\nWhile `.create()` API gives you the full control of component instantiation, there is a higher-level API for instantiating components, which is, `.init(rootElement)`. It instantiates all components with `data-componentname` attribute (e.g. `data-modal` for modal) inside the given `rootElement`. If `rootElement` is omitted, `document` is used.\n\n_Note:_ `.init()` _API does not take care of DOM elements that hasn't been available at the time it's called.\nIf the JavaScript framework you are using creates DOM elements dynamically,\nfollow the instructions in the next section instead of using_ `.init()`.\n\n#### Wrapping a component with JavaScript framework of your choice\n\nMany JavaScript frameworks have a mechanism to dynamically create/destroy DOM elements, for example, upon change in array.\nThis often makes it unclear when the DOM element to instantiate Carbon component on is available, which often depends on the JavaScript framework you use.\n\nAlso when DOM elements that Carbon components have been instantiated on are being destroyed, the Carbon component instances should be released so that e.g. there are no zombie event handlers.\n\nThe easiest way to hook on the creation/destroy of DOM elements is defining a \"wrapping component\", with the JavaScript framework of your choice. Here's an example using Web Components' [Custom Elements v1 spec](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Custom_Elements), but the notion of components, along with the lifecycle callbacks, are commonly found in many other JavaScript frameworks.\n\n```javascript\nclass BXLoading extends HTMLElement {\n  // Called when this custom element gets into render tree\n  connectedCallback() {\n    // \"this\" here is \"<bx-loading>\" element\n    this.innerHTML =\n      '(e.g. snippet from http://carbondesignsystem.com/components/loading/code)';\n    this.loading = CarbonComponents.Loading.create(\n      this.querySelector('[data-loading]')\n    );\n  }\n  // Called when this custom element gets out of render tree\n  disconnectedCallback() {\n    this.loading.release();\n  }\n}\ncustomElements.define('bx-loading', BXLoading);\n```\n\n### Polyfills for older browsers\n\ncarbon-components requires some polyfills for older browsers, in addition to carbon-components.js (or carbon-components.min.js).\nThe latest list of polyfills is maintained in [carbon-components/blog/master/packages/components/demo/polyfills/index.js](https://github.com/carbon-design-system/carbon/blob/master/packages/components/demo/polyfills/index.js).\nYou can easily find the polyfills in NPM, etc. The current list is below:\n\n- [Array.from()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)\n- [Math.sign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign)\n- [Object.assign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)\n- [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent)\n- [Element#closest()](https://developer.mozilla.org/es/docs/Web/API/Element/closest)\n- [Element#matches()](https://developer.mozilla.org/en-US/docs/Web/API/Element/matches)\n- [2nd arg of Element.classList.toggle()](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Methods)\n\n## Troubleshooting\n\nIf you experience any issues while getting set up with Carbon Components, please head over to the [GitHub repo](https://github.com/carbon-design-system/carbon-components) for more guidelines and support. Please [create an issue](https://github.com/carbon-design-system/carbon/issues) if your issue does not already exist.\n","type":"Mdx","contentDigest":"20cab3d852753046d12c7a1860bfa2bb","counter":1451,"owner":"gatsby-plugin-mdx"},"frontmatter":{"title":"Develop","description":"The Carbon Design System supports vanilla JS, React and Angular as core parts of the product. But you can still build components even if you're using a different framework.","tabs":["React","Vanilla","Angular","Vue","Other frameworks"]},"exports":{},"rawBody":"---\ntitle: Develop\ndescription: The Carbon Design System supports vanilla JS, React and Angular as core parts of the product. But you can still build components even if you're using a different framework.\ntabs: ['React', 'Vanilla', 'Angular', 'Vue', 'Other frameworks']\n---\n\n<AnchorLinks>\n\n<AnchorLink>Carbon Vanilla library</AnchorLink>\n<AnchorLink>Install</AnchorLink>\n<AnchorLink>Getting started</AnchorLink>\n<AnchorLink>Troubleshooting</AnchorLink>\n\n</AnchorLinks>\n\n## Carbon Vanilla library\n\nThe library provides front-end developers & engineers a collection of reusable HTML and SCSS partials to build websites and user interfaces. Adopting the library enables developers to use consistent markup, styles, and behavior in prototype and production work.\n\n## Install\n\n#### Using npm:\n\n<br />\n\n```bash\n$ npm install --save carbon-components\n```\n\n#### If you prefer [Yarn](https://yarnpkg.com/en/):\n\n<br />\n\n```bash\n$ yarn add carbon-components\n```\n\n_NB: March 29, 2019 - Please use the @next tag to install Carbon v10 components. e.g. `npm i carbon-components@next`._\n\n## Getting started\n\n<AnchorLinks small>\n\n<AnchorLink>What's included</AnchorLink>\n<AnchorLink>CDN</AnchorLink>\n<AnchorLink>CodePen</AnchorLink>\n<AnchorLink>SCSS</AnchorLink>\n<AnchorLink>JavaScript</AnchorLink>\n<AnchorLink>Polyfills for older browsers</AnchorLink>\n\n</AnchorLinks>\n\n### What's included\n\n```bash\ncarbon-components/\n├── html\n├── css\n│   ├── carbon-components.css\n│   └── carbon-components.min.css\n├── scripts\n│   ├── carbon-components.js\n│   └── carbon-components.min.js\n├── scss\n│   └── components\n│       └── modal\n│           └── _modal.scss\n├── umd\n│   └── index.js\n├── es\n│   └── index.js\n└── src (Deprecated and subject to breaking changes, please use es/umd instead)\n```\n\n### CDN\n\nUse unpkg for easy access to our built static files. This is great for prototyping and trying `carbon-components` without installing anything. See all files from `carbon-components` available on [unpkg CDN](https://unpkg.com/carbon-components/).\n\n| Scripts | URL                                                                  |\n| ------- | -------------------------------------------------------------------- |\n| CSS     | https://unpkg.com/carbon-components/css/carbon-components.min.css    |\n| ES5     | https://unpkg.com/carbon-components/scripts/carbon-components.min.js |\n\n### CodePen\n\nWe have individual [CodePens](https://codepen.io/collection/XqRbEz/) for all of our components which you can easily fork and play around with.\n\n### SCSS\n\nUsing the Carbon Sass files infers usage of the SCSS pre-processor.\nAll Sass files use the `*.scss` file extension.\n\nFor transpiling Sass code, use `node-sass` based Sass compilers, for example, WebPack `sass-loader` or `gulp-sass`.\n\nIf you're starting a new project without a boilerplate,\nyou need to know about a few things to get started.\n\n#### Autoprefixer\n\nMake sure your build process uses [autoprefixer](https://github.com/postcss/autoprefixer) to ensure vendor prefixes are automatically added to your output CSS.\n\n#### Default body styles\n\nCSS is automatically applied to `<body>` element, which comes from [\\_css--body.scss](https://github.com/carbon-design-system/carbon/blob/master/src/globals/scss/_css--body.scss). These styles are meant to cascade down to everything in `<body>` to set common styles shared across all components.\n\n```scss\nbody {\n  @include reset;\n  @include type-style('body-short-01');\n  color: $text-01;\n  background-color: $ui-background;\n  line-height: 1;\n}\n```\n\n#### Icons\n\nA lot of components depend on SVG icons from [carbon-icons](https://github.com/carbon-design-system/carbon-icons). Read the [docs](https://github.com/carbon-design-system/carbon-icons/blob/master/docs/usage.md) for details on how to use them.\n\n#### Global SCSS variables\n\nThese variables are used to configure which parts of the SCSS get compiled, where each variable controls a SCSS file of the same name. All variables are set to `true` by default.\n\nFor example:\n\n- When you set `$css--reset: true`, then the contents of [\\_css--reset.scss](https://github.com/carbon-design-system/carbon/blob/master/src/globals/scss/_css--reset.scss) will be part of your output CSS.\n- When you set `$css--reset: false`, then nothing gets included from that SCSS file.\n- When the variable is not declared at all, then nothing gets included from that SCSS file.\n\nThe same rules apply to all the following variables:\n\n```scss\n// In styles.scss:\n// These are the default settings.\n$css--font-face: true !default;\n$css--helpers: true !default;\n$css--body: true !default;\n$css--use-layer: true !default;\n$css--reset: true !default;\n$css--typography: true !default;\n$css--plex: true !default;\n```\n\nThese flags are set for you by default when you `@import` the `styles.scss` file.\nYou can override these default settings by redeclaring the variables.\n\n#### Importing SCSS files\n\nTo add a component style to your build, simply import the component directly. Importing a component this way will bring in any dependencies that component has as well. The import system removes duplicate dependencies, so shared dependencies between components will not create extra CSS.\n\n```js\n@import 'node_modules/carbon-components/scss/components/card/card';\n```\n\n#### Namespacing\n\nCarbon Components are built to be included individually and not clobber global styles - all `class` attributes are prefixed by the `bx--` moniker. You can specify your own prefix by changing the SCSS variable `$prefix`.\n\n### JavaScript\n\nCarbon Component has component JavaScript classes, each of which correspond to a component found in [our components page](../../components/overview). The first steps to work with component JavaScript classes are the following:\n\n#### 1. Getting component JavaScript class reference\n\n<br />\n\n**Using a module bundler:** We recommend using ECMAScript module along with your module bundler toolchain to do so. Using a module bundler will bring in only the component code your application needs, creating an optimized build for production. Carbon Components ships with a ECMAScript module build as well as UMD build for each component, for use with webpack or Rollup.\n\nAfter you've installed the components through `npm`, you can grab a component JavaScript class reference by something like this:\n\n```js\nimport { Modal } from 'carbon-components';\n```\n\n**Using pre-built bundle:** Users can also opt to use the pre-built `carbon-components.js` file directly, like below. We recommend that most users do _not_ use this file, as it includes components your application may or may not actually be using.\n\n```html\n<html>\n  <body>\n    <!-- Put HTML snippets of components here... -->\n    <script src=\"node_modules/carbon-components/scripts/carbon-components.min.js\"></script>\n  </body>\n</html>\n```\n\nOnce you load `carbon-components.js` via `<script>` tag, you can grab a component JavaScript class reference by something like this:\n\n```js\nvar Modal = CarbonComponents.Modal;\n```\n\n_Note: By default, pre-built bundle automatically instantiates all components on the page when_ `DOMContentLoaded` _event on the page fires. In case you don't want that behavior, you can set_ `CarbonComponents.settings.disableAutoInit = true` _right after_ `carbon-components.js` _is loaded._\n\n_Caveat: Don't use pre-built_ `carbon-components.js` _if you are importing components via ECMAScript module syntax. Doing so will cause Carbon code loaded twice, often competing each other._\n\n#### 2. Instantiating component class on DOM nodes\n\nOnce you have a [component JavaScript class reference](#1-getting-component-javascript-class-reference), you can instantiate it on a DOM node with the `.create()` API.\n\nFor example, if you have the following HTML for modal:\n\n```html\n<div data-modal id=\"modal-nofooter\" class=\"bx--modal\" tabindex=\"-1\">\n  <div class=\"bx--modal-container\">...</div>\n</div>\n```\n\nYou can create and get the instance by:\n\n```js\nconst modalElement = document.getElementById('modal-nofooter');\nconst modalInstance = Modal.create(modalElement);\n```\n\n_Note: The DOM element to instantiate components on typically has a `data-componentname` attribute, e.g. `data-modal` for modal._\n\nInstantiating a component basically does two things:\n\n- Hooks several event handlers on some DOM elements inside (in above example, ones in `modalElement`, e.g. close button)\n- Allows you to access public methods (found in [our components page](../../components/overview), e.g. [here](/components/modal/code#public-methods) for modal) via the instance reference (`modalInstance.show()`, etc. in above example)\n\n#### Higher-level component instantiation API\n\nWhile `.create()` API gives you the full control of component instantiation, there is a higher-level API for instantiating components, which is, `.init(rootElement)`. It instantiates all components with `data-componentname` attribute (e.g. `data-modal` for modal) inside the given `rootElement`. If `rootElement` is omitted, `document` is used.\n\n_Note:_ `.init()` _API does not take care of DOM elements that hasn't been available at the time it's called.\nIf the JavaScript framework you are using creates DOM elements dynamically,\nfollow the instructions in the next section instead of using_ `.init()`.\n\n#### Wrapping a component with JavaScript framework of your choice\n\nMany JavaScript frameworks have a mechanism to dynamically create/destroy DOM elements, for example, upon change in array.\nThis often makes it unclear when the DOM element to instantiate Carbon component on is available, which often depends on the JavaScript framework you use.\n\nAlso when DOM elements that Carbon components have been instantiated on are being destroyed, the Carbon component instances should be released so that e.g. there are no zombie event handlers.\n\nThe easiest way to hook on the creation/destroy of DOM elements is defining a \"wrapping component\", with the JavaScript framework of your choice. Here's an example using Web Components' [Custom Elements v1 spec](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Custom_Elements), but the notion of components, along with the lifecycle callbacks, are commonly found in many other JavaScript frameworks.\n\n```javascript\nclass BXLoading extends HTMLElement {\n  // Called when this custom element gets into render tree\n  connectedCallback() {\n    // \"this\" here is \"<bx-loading>\" element\n    this.innerHTML =\n      '(e.g. snippet from http://carbondesignsystem.com/components/loading/code)';\n    this.loading = CarbonComponents.Loading.create(\n      this.querySelector('[data-loading]')\n    );\n  }\n  // Called when this custom element gets out of render tree\n  disconnectedCallback() {\n    this.loading.release();\n  }\n}\ncustomElements.define('bx-loading', BXLoading);\n```\n\n### Polyfills for older browsers\n\ncarbon-components requires some polyfills for older browsers, in addition to carbon-components.js (or carbon-components.min.js).\nThe latest list of polyfills is maintained in [carbon-components/blog/master/packages/components/demo/polyfills/index.js](https://github.com/carbon-design-system/carbon/blob/master/packages/components/demo/polyfills/index.js).\nYou can easily find the polyfills in NPM, etc. The current list is below:\n\n- [Array.from()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)\n- [Math.sign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign)\n- [Object.assign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)\n- [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent)\n- [Element#closest()](https://developer.mozilla.org/es/docs/Web/API/Element/closest)\n- [Element#matches()](https://developer.mozilla.org/en-US/docs/Web/API/Element/matches)\n- [2nd arg of Element.classList.toggle()](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Methods)\n\n## Troubleshooting\n\nIf you experience any issues while getting set up with Carbon Components, please head over to the [GitHub repo](https://github.com/carbon-design-system/carbon-components) for more guidelines and support. Please [create an issue](https://github.com/carbon-design-system/carbon/issues) if your issue does not already exist.\n","fileAbsolutePath":"/fargate/7b444b50/src/pages/get-started/develop/vanilla.mdx"}}}}