# Installation

`material` is a Material Design 3 component library for the web, with no dependencies. The same
components work as plain JavaScript factories, as web components, and as React, Vue, Svelte and
Solid components. This page takes you from install to a first component; the chips at the
top show every step and example in the way you use it.

For coverage, measured sizes, browser support and the current limits, see [Why material](https://md3.io/why.md).

## Install

```install
material
```

React, Vue, Svelte and Solid are optional peer dependencies: `material` uses the one your app already
has, and installs none of them itself.

## Choose how to use it

| Way | Import from | Good for |
|-----|-------------|----------|
| [Vanilla](https://md3.io/develop/factory.md) | `material` | The smallest bundles, full control, any stack |
| [Web Components](https://md3.io/develop/web-components.md) | `material/elements` | Plain HTML, server templates, any framework |
| [React](https://md3.io/develop/frameworks/react.md) | `material/react` | React 18 and 19, Next.js |
| [Vue](https://md3.io/develop/frameworks/vue.md) | `material/vue` | Vue 3, Nuxt |
| [Svelte](https://md3.io/develop/frameworks/svelte.md) | `material/svelte` | Svelte 5, SvelteKit |
| [Solid](https://md3.io/develop/frameworks/solid.md) | `material/solid` | Solid, SolidStart |

The web components are built on the factories, and the framework components render the web
components, so they all look and behave alike. Pick yours with the chips at the top: the steps
below follow it, and [Frameworks](https://md3.io/develop/frameworks.md) has its guide.

Each web component styles its own shadow root, which costs more on a first render than the same
component as a factory: 1,000 text fields mount in about 290 ms as elements against about
150 ms as factories, and with the CPU slowed four times in about 1.4 s against about 0.63 s
(`material` 3.0.0-next.0, Chromium 153, medians of 15 runs). A form or a page has far fewer. For hundreds of instances at once, such as a long editable table, use the Vanilla
factories. See [Web Components](https://md3.io/develop/web-components.md#many-instances).

## Add the styles

Every app loads one base stylesheet. For factories, use the full base; add
`material/styles/typography` for the type scale:

```typescript
import 'material/styles/base';
import 'material/styles/typography';
```

For element and framework applications, choose the base that fits the page:

| Entry | Includes |
|-------|----------|
| `material/styles/base` | Theme, tokens, reset, system contrast and global ripple rules; needed for factories and page-level `data-ripple` |
| `material/styles/base-elements` | Baseline light/dark theme, colour/shape/typeface tokens, reset, cascade order and system contrast (`prefers-contrast: more`); no global ripple rules |
| `material/styles/base-lite` | Theme, tokens, body theme and reduced-motion switch; no reset, ripple or contrast modes |

Elements carry ripple CSS in their shadow roots, so bundled `base-elements` CSS saves 293 B gzip (970 B raw)
against `base`. Explicit `data-theme-contrast` still needs `material/styles/contrast`.
The typography JavaScript entry brings in the full base; [Theming](https://md3.io/styles/theming.md#the-baseline)
explains loading typography CSS separately when keeping the smaller base.

<div data-for-framework="vanilla">

With the **Vanilla** factories, also import each component's stylesheet:

```typescript
import 'material/styles/button';
```

</div>
<div data-for-framework="html">

The **web components** carry their own styles in their shadow roots, so no page-level
component stylesheets are needed. Register them once:

```typescript
import 'material/elements/css';
import { defineAll } from 'material/elements';

defineAll();
```

</div>
<div data-for-framework="react">

The **React** components carry their own styles in their shadow roots, so no page-level
component stylesheets are needed. Import them from `material/react`; each registers the element
it renders the first time it mounts.

</div>
<div data-for-framework="vue">

The **Vue** components carry their own styles in their shadow roots, so no page-level
component stylesheets are needed. Import them from `material/vue`; each registers the element it
renders the first time it mounts.

</div>
<div data-for-framework="svelte">

The **Svelte** components carry their own styles in their shadow roots, so no page-level
component stylesheets are needed. Import them from `material/svelte`; each registers the element
it renders the first time it mounts.

</div>
<div data-for-framework="solid">

The **Solid** components carry their own styles in their shadow roots, so no page-level
component stylesheets are needed. Import them from `material/solid`; each registers the element
it renders the first time it mounts.

</div>

## A first component

A filled button that saves when it is clicked:

Vanilla:

```js
import { createButton } from 'material';

const button = createButton({ text: 'Save', variant: 'filled' });
button.on('click', () => save());
document.body.append(button.element);
```

Web Components:

```html
<m-button variant="filled">Save</m-button>

<script type="module">
  const button = document.querySelector('m-button');
  button.addEventListener('click', () => save());
</script>
```

React:

```tsx
import { Button } from 'material/react';
import { save } from './app';

export function Example() {
  return (
    <Button onClick={() => save()} variant="filled">Save</Button>
  );
}
```

Vue:

```vue
<script setup lang="ts">
import { MButton } from 'material/vue';
import { save } from './app';
</script>

<template>
  <MButton @click="save()" variant="filled">Save</MButton>
</template>
```

Svelte:

```svelte
<script lang="ts">
  import { Button } from 'material/svelte';
  import { save } from './app';
</script>

<Button onclick={() => save()} variant="filled">Save</Button>
```

Solid:

```tsx
import { Button } from 'material/solid';
import { save } from './app';

export function Example() {
  return (
    <Button onClick={() => save()} variant="filled">Save</Button>
  );
}
```

Every component page has the same kind of example, and a playground to try the options.

## Themes and dark mode

The base stylesheet has the baseline theme. Another theme is one more import, and it applies to
the element that names it, usually the whole page:

```typescript
import 'material/themes/vibrant';

document.documentElement.dataset.theme = 'vibrant';
document.documentElement.dataset.themeMode = 'dark';
```

Browse the themes and every colour role in [Styles › Color](/styles/color/), and see
[Theming](https://md3.io/styles/theming.md) to make your own.

## Next steps

- [Components](/components/): every component in a playground, with the code for your
  framework.
- [Frameworks](https://md3.io/develop/frameworks.md): the guide for your framework, for its events, forms and
  server rendering.
- [Architecture](https://md3.io/develop/architecture.md): how `material` is built.
