@gfazioli/mantine-scheme-transition
The light and dark switch of a Mantine app as a view transition that starts from the button you pressed: circle, wipe, curtain or dissolve, with a ready-made SchemeToggle and a headless hook.
Installation
After installation import package styles at the root of your application:
You can import styles within a layer @layer mantine-scheme-transition by importing @gfazioli/mantine-scheme-transition/styles.layer.css file.
The stylesheet draws the transitions: without it the switch is still animated, but as the browser's plain cross-fade.
Usage
SchemeToggle switches Mantine's colour scheme with a view transition: the new scheme spreads across the page from the point you clicked. Every demo on this page switches the scheme of the whole page, the header included: that is the point. The header toggle of this site is a SchemeToggle too, and ⌘ J / Ctrl J reveals from it.
SchemeToggle is an ActionIcon: every ActionIcon prop applies, variant, color, radius, gradient, loading and disabled included, and so does its theme. The props tab lists only the props SchemeToggle adds.
Video tutorial
06 · Add an animated dark mode toggle
Install Scheme Transition, put SchemeToggle in your header, pick a transition (circle, wipe, curtain, dissolve or none), switch from any button with the useColorSchemeTransition hook, share one controller with a hotkey and choose what reduced motion gets.
Transitions
Four transitions ship with the package. In each one the new scheme is the layer that moves, on the way to dark and on the way back to light; dissolve is the exception, where the old scheme fades out over the new one. Click each to compare them.
- circle (default): a circle grows from the pointer until it passes the farthest corner of the viewport.
- wipe: a slanted edge sweeps across the page, entering from the side the pointer is on.
- curtain: a band opens at the pointer's column and reaches both edges at the same moment.
- dissolve: a plain cross-fade, 300 ms by default; the pointer does not matter.
circlewipecurtaindissolvetransition="none" switches at once, as Mantine does: useful when a SchemeToggle should not animate in some part of an app.
The hook
useColorSchemeTransition holds the engine. It returns Mantine's colour scheme functions wrapped in the transition, so any trigger can use them: pass the click event and the reveal starts at the pointer.
The hook takes the same transition options as SchemeToggle:
It returns a controller. toggle, setColorScheme, clearColorScheme and ref keep their identity across renders, so they are safe in effect dependencies and in useHotkeys.
Where the reveal starts
toggle, setColorScheme and clearColorScheme take an optional origin as their last argument:
- an event: its pointer position. A click made with the keyboard, a screen reader or
element.click()has no position, so the reveal starts from the centre of the element that received it; - an element: the centre of its box;
- a point
{ x, y }, in CSS pixels from the top left of the viewport; - nothing: the centre of the element holding the controller's
ref, else the centre of the viewport.
One engine, several triggers
Pass the controller to SchemeToggle with the controller prop and the button registers itself as the controller's ref. A menu item, a hotkey or any code that calls the same toggle without a pointer then reveals from that button. With a controller, the transition props of the SchemeToggle itself are not used.
or press ⌘ / Ctrl + Shift + U
Icons
The toggle shows the icon of the scheme you are in: a sun in light, a moon in dark. Swap lightIcon and darkIcon to show the scheme you would get instead, as Mantine's own docs do. children replaces the pair altogether, and SchemeToggle.Icon is the pair on its own, for a Button, a Menu.Item or a NavLink.
Both icons are always rendered and CSS shows the right one, so the server never has to know the scheme and nothing changes on hydration. While a reveal runs, the two icons swap with a short turn; under reduced motion they fade.
Responsive size
size takes the sizes of ActionIcon (xs to xl, input-xs to input-xl), a number or any CSS length, and an object of breakpoints. The responsive value is written as CSS media queries, with no re-render.
Resize the window: md on small screens, xl from the sm breakpoint
Custom transitions
A transition the package does not know is written to the page as it is, and your stylesheet draws it. While a reveal runs, these attributes and variables are on the html element, and nowhere else at any other time:
| On the html element | Value |
|---|---|
data-scheme-transition | The transition name |
data-scheme-transition-to | light or dark, the scheme being revealed, once the change is applied |
data-scheme-transition-side | left or right, the half of the viewport the origin is in |
--scheme-transition-x, --scheme-transition-y | The origin, in px from the top left of the viewport |
--scheme-transition-radius | The distance in px from the origin to the farthest corner of the viewport |
--scheme-transition-duration, --scheme-transition-easing | The duration and the easing of this reveal |
The old page is ::view-transition-old(root) and the live new page is ::view-transition-new(root), on top of it; the package has already switched off the browser's default cross-fade on both. Animate one of them, as this diamond does with the new page. In a CSS module, wrap the whole selector in :global(...), pseudo-element included: some compilers would otherwise rename root as if it were a local name, and the rule would match nothing.
A transition the package does not know, drawn by the CSS in Demo.module.css
Styles API
SchemeToggle and SchemeToggle.Icon support Styles API: you can add styles to any inner element with the classNames prop. SchemeToggle has a single selector, root, which is the ActionIcon root; hover the selectors below to see the three parts of SchemeToggle.Icon. Style the icon of a SchemeToggle through its iconProps.
Component Styles API
Hover over selectors to highlight corresponding elements
Browser support
The reveal uses same-document view transitions (document.startViewTransition). Where the browser does not have them, the toggle calls Mantine's setColorScheme and nothing more: the same result and the same stored value, without the animation, a polyfill or an error.
| Browser | Animated from | Before that |
|---|---|---|
| Chrome, Edge | 111 | Instant switch |
| Safari (macOS, iOS) | 18.0 | Instant switch |
| Firefox | 144 | Instant switch |
| Opera | 97 | Instant switch |
| Samsung Internet | 23 | Instant switch |
Sources: MDN browser compatibility data and Can I use.
Reduced motion
When the user asks the operating system for reduced motion, reducedMotion decides what happens:
- instant (default): Mantine's switch, with no animation at all.
- dissolve: whatever the transition, a cross-fade of 200 ms at most, with no shape and no movement. The gentle middle, when the change should still be felt.
- animate: the transition runs anyway. A reveal covers the whole screen, so keep this for a transition that does not move, such as your own fade.
The setting is read at each click, so a change in the system settings applies at once. It does not depend on theme.respectReducedMotion, which is about Mantine's small component transitions.
Good to know
- During a reveal the page does not react to the pointer: clicks land on the transition layer, never on the page below. This is how the browser runs a view transition of the whole page; keep durations short. The keyboard still works, and a second toggle during a reveal cuts the first one short and starts again, so the scheme always matches the number of presses.
- forceColorScheme: where the
MantineProviderforces the scheme, the toggle changes nothing and shows no animation. Hide or disable it there. - keepTransitions: Mantine switches the page's CSS transitions off for an instant during every scheme change, and so does this package by default, so the new scheme appears in its final colours with a crisp edge. With
keepTransitions, the page's own colour transitions play inside the revealed area. - Your own view transitions are untouched: the package's styles apply only while one of its reveals runs. For the length of a reveal, elements you named with
view-transition-namejoin the single snapshot of the page. - Server rendering works: the components are client components, and the icons need no scheme on the server.
- Import the stylesheet, or the reveal is the browser's plain cross-fade.
