# M3 Svelte This is M3 Svelte, a Svelte component library that implements the Material 3 design system by @KTibow. ## Documentation pages (Svelte source) These are the actual Svelte files from the docs site. They're authoritative for getting started and building blocks: ### Quick Start page ```svelte Quick start
  1. 1
    Install M3 Svelte

    {#if useTokenShaker} {:else} {/if}
  2. 2
    Enable plugins

    {#if useVite} {#if useTokenShaker} {:else} {/if} {:else if useTokenShaker} {:else} {/if}
  3. 3
    Enable your theme snippet

    {#if useVite} {:else} import "../app.css"; `} /> {/if}
  4. 4
    Enable a font

    {#if useCustomFont} {:else} `} /> {/if}
import { Button } from "m3-svelte"; <${""}Button onclick={() => alert("Hello world")}>Click me`} stretch /> Keep learning
``` ### Detailed Walkthrough page ```svelte Walkthrough

Welcome to M3 Svelte! If you haven't already, play around with the components on the home page. It's rather fun. They even adapt to the theme you pick. You should also check out the Discord and GitHub if you want to keep track of this project.

The rest of this page covers some finer details of M3 Svelte. It isn't the place to look for component-specific info - the home page is.

Make your own components

{#if $appType == "tailwind"}

⚠️ Tailwind may not support mixins yet. You may need to stay on M3 Svelte v5 for now.

{/if}

Chances are M3 doesn't have everything you need. That's where you can make your own components while still using Material 3 elements. Here's an example.

{#if $appType == "tailwind"} Click me`} /> {:else} Click me <${""}style> button { background-color: var(--m3c-surface-container-low); color: var(--m3c-primary); box-shadow: var(--m3-elevation-1); border-radius: var(--m3-shape-full); } `} /> {/if}

If layers don't work on your own components, or even in M3 Svelte's, you may need to import "m3-svelte/etc/layer".

Apply your theme

{#if $appType == "tailwind"} `} /> {:else} {/if}

So what's going on here?

We're using M3 Svelte globals.

Some globals are defined in styles.css. If you want to change them, nothing's stopping you from overriding them in your own theme.

Other globals are defined directly in your theme - you can read and write them there.

Tokens

These live in @layer tokens. M3 Svelte defines many, like elevations (--m3-elevation-0 through --m3-elevation-5) and shapes (--m3-shape-[size] where size is none, extra-small, small, medium, large, extra-large, or full). Colors, the --m3c- variables, are also tokens.

Want these to be minified and inlined? The "Use token-shaker" option from setup does that.

Variables

These start with --m3v-, and let you send information to M3 Svelte.

  • Set --m3v-bottom-offset to shift up snackbars
  • Set --m3v-background to calibrate the color of an outlined field's box

Functions

These are shorthands for specific logic, like:

  • --translucent([color], [opacity]) makes a color semitransparent
  • --m3-density([size]) adjusts a size

Mixins

These are shorthands for specific properties, applied with @apply --[name].

M3 Svelte's built in mixins: font styles (@apply --m3-[scale]-[size]. scales: display, headline, title, body, label. sizes: large, medium, small) and focus styles (@apply --m3-focus-inward and @apply --m3-focus-none).

``` ## Tips M3 Svelte is more and less than the typical component framework. On one hand, Material 3 consists of guidelines across color, shadow, easing, animation, and spacing, and M3 Svelte is all of that. You don't have to make your own components or animations - you could use components like `Card` and animate them with the `sharedAxisTransition`, eased with `easeEmphasized`. On the other hand, M3 Svelte doesn't give you a template. There are many things you have to do yourself, like: - Generating and pasting your theme - Including your font - Resetting CSS - Setting up your own layout (ideally a responsive one) - Styling your components (if you need something M3 doesn't have) - Slotting in s - Rendering icons If you're ever unsure about something, you might want to check the [source code](https://github.com/KTibow/m3-svelte). The M3 Svelte demo website is built with M3 Svelte and might have implemented what you're trying to do. ## Troubleshooting You should avoid trying to restyle components. You can't set a `class` and due to Svelte scoped styles, even if you could, it wouldn't work. I repeat, you CANNOT set a `class` on M3 Svelte components. Always look for alternatives, like using `gap` instead of `margin`. If you _must_ do this, you would have to do something like `.my-container > :global(.m3-container)`. You must wrap Checkbox, Radio, and Switch in