|
| 1 | +--- |
| 2 | +title: ThemeToggle |
| 3 | +description: "One icon button that flips the site between its light and dark themes." |
| 4 | +template: doc |
| 5 | +layout: |
| 6 | + showNav: true |
| 7 | + showToc: true |
| 8 | + showFooter: false |
| 9 | +nav: |
| 10 | + order: 85 |
| 11 | + icon: tabler:components |
| 12 | +--- |
| 13 | + |
| 14 | +# Theme<wbr/>Toggle |
| 15 | + |
| 16 | +One icon button that flips the site between its light and dark themes. |
| 17 | + |
| 18 | +<Badge tone="accent" variant="surface">ThemeToggle</Badge> |
| 19 | + |
| 20 | +[View source](https://github.com/PureStackStudio/PureStack/blob/main/packages/ts-components/src/standard/themeToggle/themeToggle.ts) · [API reference](#api-reference) |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## Flip the theme |
| 25 | + |
| 26 | +The sun shows in the light theme and the moon in the dark one. Press it and the glyphs turn into each other. |
| 27 | + |
| 28 | +<Tabs group="theme-toggle-sample-1" selectedTab="theme-toggle-sample-1-preview" ariaLabel="Flip the theme example" tone="neutral" variant="surface" tabVariant="underline"> |
| 29 | + <TabPane id="theme-toggle-sample-1-preview" label="Preview"> |
| 30 | + <Flex align="center" wrap="true"> |
| 31 | + <ThemeToggle /> |
| 32 | + <span>Switch between the light and dark studio skins.</span> |
| 33 | + </Flex> |
| 34 | + </TabPane> |
| 35 | + <TabPane id="theme-toggle-sample-1-source-0" label="theme-control.mdx"> |
| 36 | +```html |
| 37 | +<Flex align="center" wrap="true"> |
| 38 | + <ThemeToggle /> |
| 39 | + <span>Switch between the light and dark studio skins.</span> |
| 40 | +</Flex> |
| 41 | +``` |
| 42 | + </TabPane> |
| 43 | +</Tabs> |
| 44 | + |
| 45 | +## In a settings row |
| 46 | + |
| 47 | +The toggle sits beside a visible setting label as easily as it sits in a header. |
| 48 | + |
| 49 | +<Tabs group="theme-toggle-sample-2" selectedTab="theme-toggle-sample-2-preview" ariaLabel="In a settings row example" tone="neutral" variant="surface" tabVariant="underline"> |
| 50 | + <TabPane id="theme-toggle-sample-2-preview" label="Preview"> |
| 51 | + <Panel bodyClass="p-3"> |
| 52 | + <Flex align="center" justify="between"> |
| 53 | + <div> |
| 54 | + <strong>Appearance</strong> |
| 55 | + <p class="mb-0">Choose the skin that is comfortable for you.</p> |
| 56 | + </div> |
| 57 | + <ThemeToggle /> |
| 58 | + </Flex> |
| 59 | + </Panel> |
| 60 | + </TabPane> |
| 61 | + <TabPane id="theme-toggle-sample-2-source-0" label="appearance-setting.mdx"> |
| 62 | +```html |
| 63 | +<Panel bodyClass="p-3"> |
| 64 | + <Flex align="center" justify="between"> |
| 65 | + <div> |
| 66 | + <strong>Appearance</strong> |
| 67 | + <p class="mb-0">Choose the skin that is comfortable for you.</p> |
| 68 | + </div> |
| 69 | + <ThemeToggle /> |
| 70 | + </Flex> |
| 71 | +</Panel> |
| 72 | +``` |
| 73 | + </TabPane> |
| 74 | +</Tabs> |
| 75 | + |
| 76 | +## Behavior and accessibility |
| 77 | + |
| 78 | +ThemeToggle is a native button labelled “Dark theme”. The theme runtime keeps `aria-pressed` in step with the active theme, so assistive technology announces whether dark is on. The glyphs are decorative and hidden from the accessibility tree. |
| 79 | + |
| 80 | +Each theme stylesheet decides which glyph is visible, so the right one shows on first paint without waiting for script. The turn animates only after the runtime settles the first theme, and it is switched off for visitors who prefer reduced motion. |
| 81 | + |
| 82 | +## API reference |
| 83 | + |
| 84 | +<div class="api-overview"> |
| 85 | + <p class="text-eyebrow">ThemeToggle contract</p> |
| 86 | + <p>ThemeToggle has no public props. Available themes and stylesheet URLs come from the site’s theme setup.</p> |
| 87 | +</div> |
| 88 | + |
| 89 | +This component has no public props. |
| 90 | + |
| 91 | +### Composition |
| 92 | + |
| 93 | +<div class="api-contract"> |
| 94 | +<p>No slots. Register defineThemeToggleComponents and the Icon family. SSG includes the theme runtime, which binds every element marked data-theme-toggle; outside SSG initialize buildThemeSwitchScript with the configured theme names and matching data-theme stylesheets.</p> |
| 95 | +</div> |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +## Related components |
| 100 | + |
| 101 | +- [TopBar](/components/site/top-bar/) |
| 102 | +- [SignIn](/components/site/sign-in/) |
0 commit comments