Actions

Theme Switch

A menu, toggle group or select that sets the light, dark or auto theme of a root.

Class nameTypeDescription
dropdownplf:dropdownComponentA Dropdown Menu with data-plf-theme-switch
menuplf:menuComponentThe list of themes, with dropdown-content
dropdown-contentplf:dropdown-contentPartThe floating panel
menu-itemplf:menu-itemPartA theme item with its value
menu-iconplf:menu-iconPartYour own SVG, in place of the theme's drawn icon; an icon class there doesn't need it
menu-textplf:menu-textPartAn item's text

Default

The switch sets data-plf-theme on the data-plf-root that holds it, never on the page; see Themes and cascade.

Pelatform

The switch changes the theme of the root it sits in.

Preview

Toggle group

One toggle always stays pressed.

Appearance

This root starts dark; the switch reads its current theme.

Auto follows the system light or dark setting.

Preview

Theme

A native select suits settings forms and small screens.

The select shows the root theme when the page loads.

Preview

Control outside the root

Switches for the same root stay in sync.

Workspace preview

Both switches change this root and stay in sync.

Preview

Saving the choice

Theme Switch stores nothing: apply the saved theme with an inline script right after the root’s opening tag, and save each change under a key of your own, such as plf-theme, as the site’s other scripts share localStorage.

HTML
<div data-plf-root data-plf-theme="auto" id="app">
  <script>
    try { const theme = localStorage.getItem('plf-theme'); if (theme) document.getElementById('app').dataset.plfTheme = theme; } catch {}
  </script>
</div>

<script>
  document.addEventListener('plf:theme-switch:change', event => {
    if (event.detail.root.id === 'app') try { localStorage.setItem('plf-theme', event.detail.value); } catch {}
  });
</script>

In React, onValueChange saves each change. A root that React renders on the server takes the same script, and suppressHydrationWarning keeps the theme it applies:

React
'use client';

import type { ReactNode } from 'react';
import { ThemeSwitch } from '@pelatform/react';

const restore = "try { const theme = localStorage.getItem('plf-theme'); if (theme) document.getElementById('app').dataset.plfTheme = theme; } catch {}";

export function App({ children }: { children: ReactNode }) {
  return (
    <div data-plf-root data-plf-theme="auto" id="app" suppressHydrationWarning>
      <script dangerouslySetInnerHTML={{ __html: restore }} />
      <ThemeSwitch defaultValue="auto" onValueChange={theme => { try { localStorage.setItem('plf-theme', theme); } catch {} }} />
      {children}
    </div>
  );
}

JavaScript

Theme Switch needs JavaScript. The Full script (pelatform-ui.min.js) starts every component on page load, with no code of your own; with ESM imports, call init() yourself, as in Installation. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<link rel="stylesheet" href="icons/icons.min.css">
<script src="pelatform-ui.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<link rel="stylesheet" href="icons/icons.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
Attribute Purpose
data-plf-theme-switch On a Dropdown Menu, a single Toggle Group or a native <select>, authored hidden
value light, dark or auto on each menu item, toggle or option; each value once. Items and toggles draw the theme’s icon
data-plf-target The id of the data-plf-root to change; by default, the root that holds the switch
data-plf-theme-switch-icon light, dark or auto on each icon in the menu trigger
Method Behavior
PLF.ThemeSwitch.getOrCreateInstance(element) Returns the instance, creating it for valid markup
PLF.ThemeSwitch.getInstance(element) Returns the instance or null
instance.getValue() Returns the root’s theme: light, dark or auto
instance.setValue(theme) Sets an offered theme; returns false when unchanged, not offered or canceled
instance.destroy() Removes listeners and hides the control again; the theme stays
Event When
plf:theme-switch:before-change Before the switch changes the root; cancelable
plf:theme-switch:change After the root’s data-plf-theme changes

Each event’s detail:

Property Value
instance The Theme Switch instance
value The new theme: light, dark or auto
previous The root’s theme before the change
root The data-plf-root element the switch changes
reason trigger or api
trigger The menu item, toggle or select that chose the theme, or null from a method
Control Keys
Menu As in Dropdown Menu
Toggle group As in Toggle Group
Select The native select keys

React

ThemeSwitch from @pelatform/react renders the menu, which changes the theme of the root around it; useTheme() keeps a root’s theme for a Toggle Group, a Select or another control of your own.

Prop Values Default
defaultValue light, dark or auto: the root’s data-plf-theme, which the trigger shows until it reads the root, as in server markup light
onValueChange (theme), called after a chosen item changes the root, such as to save it None
target The root to change, as a ref or the element, for a switch outside it The root around the switch
label The trigger’s name, which the current theme’s name follows Color theme
lightLabel, darkLabel, autoLabel The items’ names Light, Dark, Auto

The other props are Dropdown Menu’s, such as align, and the element’s own, className and ref included.

useTheme() takes the same defaultValue, target and onValueChange, which it calls after setTheme changes the root, and returns [theme, setTheme, ref]:

Returns Value
theme The root’s theme, which follows its data-plf-theme when another switch or the page changes it
setTheme setTheme(theme) sets the root’s theme; another value, such as a released toggle’s '', changes nothing, so one toggle stays pressed
ref On your control inside the root; without target, the hook changes the root around it

Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons