Skip to content
Likable Aurora @likable-hair/svelte 5

ModeSwitch

Switch for the light and dark mode of the page, with three icons side by side, like a segmented TabSwitcher: light, system (follows the operating system) and dark. system={false} leaves only light and dark: while the page still follows the operating system, the icon of the mode it shows is selected, and a click on it fixes that mode. A click on an icon calls setMode, so the choice is kept across visits once the root layout calls rememberAppearance(). It is a group of native radios (role="radiogroup"): Tab enters the group and the arrow keys move the choice; every icon has an accessible name and a Tooltip. The switch shows the mode of <html> (data-mode) from the first paint, before the page is interactive, so with appearanceScript() it never flashes the wrong choice. onchange runs after the mode changed, for example to save it in the user profile. The selected option is exposed as data-selected, the size as data-size and the missing system option as data-no-system for app CSS.

import { ModeSwitch } from '@likable-hair/svelte';

src/lib/components/composed/forms/ModeSwitch.svelte · Markdown for LLMs

Light, system, dark

A click applies the mode to the whole page. "System" follows the operating system, and theme tells what the page shows now.

Mode: system · the page is light

Code
<script lang="ts">
	import { ModeSwitch, theme } from '@likable-hair/svelte';
</script>

<div class="demo">
	<ModeSwitch />
	<p>Mode: {theme.mode} · the page is {theme.dark ? 'dark' : 'light'}</p>
</div>

<style>
	.demo {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

	p {
		margin: 0;
	}
</style>

In the top bar

The usual place: on the right of the app bar, next to the user. size="sm" fits a crowded bar.

Bookings
GR
Code
<script lang="ts">
	import { Avatar, ModeSwitch } from '@likable-hair/svelte';
</script>

<header class="bar">
	<b>Bookings</b>
	<div class="end">
		<ModeSwitch size="sm" />
		<Avatar name="Giulia Rossi" size="sm" />
	</div>
</header>

<style>
	.bar {
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		gap: 16px;
		width: min(100%, 560px);
		height: 56px;
		padding: 0 16px;
		border: var(--global-border-width) solid var(--global-color-border);
		border-radius: var(--global-radius-lg);
		background: var(--global-color-surface);
	}

	.end {
		display: flex;
		align-items: center;
		gap: 12px;
	}
</style>

Custom look and saving

CSS variables give a neutral pill with round options and the selected one on a raised surface, and onchange saves the choice, for example in the user profile.

Pick a mode to save it.

Code
<script lang="ts">
	import { ModeSwitch } from '@likable-hair/svelte';

	let saved = $state<string>();
</script>

<div class="demo">
	<ModeSwitch
		onchange={({ mode }) => (saved = mode)}
		--mode-switch-background="var(--global-color-surface-2)"
		--mode-switch-border-color="transparent"
		--mode-switch-border-radius="var(--global-radius-full)"
		--mode-switch-option-border-radius="var(--global-radius-full)"
		--mode-switch-selected-background="var(--global-color-surface-pop)"
		--mode-switch-selected-color="var(--global-color-text)"
		--mode-switch-selected-box-shadow="var(--global-shadow-sm)"
	/>
	<p>{saved ? `Saved in the profile: ${saved}` : 'Pick a mode to save it.'}</p>
</div>

<style>
	.demo {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

	p {
		margin: 0;
	}
</style>

Light and dark only

system={false} removes the system option. Until the user picks one, the page follows the operating system and the icon of the mode it shows is selected; a click on it fixes that mode.

Mode: system

Code
<script lang="ts">
	import { ModeSwitch, theme } from '@likable-hair/svelte';
</script>

<div class="demo">
	<ModeSwitch system={false} />
	<p>Mode: {theme.mode}</p>
</div>

<style>
	.demo {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

	p {
		margin: 0;
	}
</style>

Props

NameTypeDefaultDescription
label stringAccessible name of the group. Defaults to "Theme", in the language of locale.
lightLabel stringName and tooltip of the light option. Defaults to "Light", in the language of locale.
darkLabel stringName and tooltip of the dark option. Defaults to "Dark", in the language of locale.
systemLabel stringName and tooltip of the option that follows the operating system. Defaults to "System", in the language of locale.
lightIcon stringLIGHT_ICONSVG path of the light icon.
darkIcon stringDARK_ICONSVG path of the dark icon.
systemIcon stringSYSTEM_ICONSVG path of the system icon.
system booleantrueShows the option that follows the operating system. Without it only light and dark are offered.
size 'sm' | 'md''md'md (the size of the prototype) or sm for a crowded bar.
disabled booleanfalseDisables the three options.
tooltipPlacement 'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end' | 'right' | 'right-start' | 'right-end''bottom'Side of the tooltips. They flip when there is no room, so the default fits a top bar; use top at the bottom of a sidebar.
locale stringBCP 47 locale of the texts: it and en are built in, other languages fall back to English. Defaults to the one set with setDefaultLocale, else en.
class { container?: string; option?: string; }{}Extra classes for each part: the group and each option.

It also accepts the native attributes and events of <div> (for example id, title, aria-label, onkeydown), which are forwarded to the element.

Snippets

NameParametersDescription
optionSnippet{ mode: 'light' | 'dark' | 'system'; label: string; icon: string; selected: boolean }Replaces the icon inside each option. The option keeps its radio, name and tooltip.

Events

NameTypeDescription
onchange(event: { mode: 'light' | 'dark' | 'system' }) => voidCalled after the user picked a mode, which is already applied.

CSS variables

For a single instance, pass them as style props: <ModeSwitch --mode-switch-background="tomato">. The --mode-switch-default-* defaults apply to the whole app: override them in :root or in a theme.

Per-instance variableDefault
--mode-switch-gap
--mode-switch-default-gap 4px
--mode-switch-padding
--mode-switch-default-padding 4px
--mode-switch-default-sm-padding 3px
--mode-switch-border-width
--mode-switch-default-border-width var(--global-border-width)
--mode-switch-border-color
--mode-switch-default-border-color var(--global-color-border)
--mode-switch-border-radius
--mode-switch-default-border-radius var(--global-radius-md)
--mode-switch-background
--mode-switch-default-background var(--global-color-surface-solid)
--mode-switch-box-shadow
--mode-switch-default-box-shadow none
--mode-switch-disabled-opacity
--mode-switch-default-disabled-opacity var(--global-input-disabled-opacity)
--mode-switch-icon-size
--mode-switch-default-icon-size 16px
--mode-switch-default-sm-icon-size 14px
--mode-switch-option-width
--mode-switch-default-option-width 34px
--mode-switch-default-sm-option-width 28px
--mode-switch-option-height
--mode-switch-default-option-height 30px
--mode-switch-default-sm-option-height 24px
--mode-switch-option-border-radius
--mode-switch-default-option-border-radius var(--global-radius-sm)
--mode-switch-color
--mode-switch-default-color var(--global-color-text-3)
--mode-switch-hover-background
--mode-switch-default-hover-background transparent
--mode-switch-hover-color
--mode-switch-default-hover-color var(--global-color-text)
--mode-switch-selected-background
--mode-switch-default-selected-background var(--global-fill-primary)
--mode-switch-selected-box-shadow
--mode-switch-default-selected-box-shadow var(--global-shadow-fill)
--mode-switch-selected-color
--mode-switch-default-selected-color var(--global-color-on-primary)
--mode-switch-focus-ring-color
--mode-switch-default-focus-ring-color var(--global-focus-ring-color)