Understanding modes #
Modes allow you to control common visual states that allow for both accessibility and comfort. For now, Hydrogen supports dark mode, but there are plans to also introduce a high contrast mode down the road.
Modes vs. themes #
Modes and themes are both ways of applying specific styles to a context. The biggest difference between the two is that modes are applied on a per-theme basis. This means that each theme you create in your configuration will have access to its own unique light and dark modes.
Applying modes to your project #
Hydrogen offers two ways to take advantage of mode-specific styles. By default, your configuration will have the method
option set to preference
, because this method doesn't require any intervention on your end to work. Alternatively, you can choose to take advantage of the more complex, but more useful toggle
method. This option does require extra work to apply, but provides a much better user experience.
1
2
3
4
5
6
"modes": {
"method": "preference"
"dark": {
...
}
}
The "preference" mode #
The preference-based approach relies on the prefers-color-scheme
media query to enable styles when the user has set their browser to request a particular mode. These styles are automatic and will instantly apply when the end user sets their browser configuration. This option is great because it's low maintenance and doesn't require custom scripting or interactivity to work. The downside is that your application won't be able to be customized independently of the browser's setting, which can sometimes be frustrating for users who prefer a specific setting on individual applications.
The "toggle" mode #
The toggle-based approach is more complex and relies on data attributes to trigger styles. In order for it to work, you'll need to build a toggle UI element on your project that allows the user to make their theme choice. A basic working toggle can be found below.
The advantage to using toggle
is that it allows your end user to choose between 3 options:
- Their browser setting
- Manual light mode
- Manual dark mode
Dark mode #
Because Hydrogen only supports dark mode for now, the dark
object is the only available mode you can configure.
auto_apply_styles
tells Hydrogen whether or not you want dark mode styles to apply automatically in your code. This setting allows you to define dark mode values for your theme and have them apply without any extra work. Disabling this setting allows you to apply your dark mode styles by hand, preventing unexpected results.swap_default_modifiers
will tell Hydrogen if you want it to automatically swap its default color modifiers for their opposite in dark mode. For example, if you setprimary.darker
as a color in light mode, this setting will automatically apply theprimary.lighter
value in dark mode.
1
2
3
4
5
6
"modes": {
"dark": {
"auto_apply_styles": true,
"swap_default_modifiers": true,
}
}
Dark mode styles are applied to elements using the :dark
modifier on the base
or a custom media query.
1
<span data-h2-color="base:dark(primary)"></span>
Automatic vs. manual styling #
Hydrogen provides the option to specify mode-specific values for theme configurations in your hydrogen.config.json
file. Using these values, Hydrogen can handle the heavy-lifting of applying dark mode styles to your project for you. This functionality is further broken down into two control settings you can enable or disable to meet your needs.
Automatic dark mode #
The auto_apply_styles
option will tell Hydrogen to automatically apply your dark mode specific values for you. This is a blanket application, so if you choose to tell the theme to swap out white
for black
in dark mode, every instance of white
will automatically be black.
In a majority of cases, this is extremely helpful, because it will allow you to focus your effort on the handful of instances where the color swap doesn't make sense. This is particularly important when performing accessibility audits to ensure that your swapped color hasn't created contrast problems.
To extend this even further, a second control is provided through the swap_default_modifiers
option that will automatically swap Hydrogen's default color modifiers for their opposite when dark mode is enabled. For example, primary.dark
in light mode will swap to the value generated for primary.light
when dark mode is toggled on. This allows for intuitive theme building by ensuring that a majority of contrast instances are accounted for when dark mode is automatically applied.
Manual overrides #
For times when the automatic swap isn't working as you'd expect, you can override automatic dark mode styles using the :dark
modifier on your query. This is especially useful for accessibility corrections.
For objects that need to retain their styles across both light and dark modes, you can use the :all
modifier on your query to force the styles to stay the same.
Styling dark mode manually #
If you'd rather not use Hydrogen's automated mode settings, you can still use the :dark
query modifier in your attributes to apply styles that are unique to dark mode. This gives you full control over how dark mode styles work, but requires much more manual work to ensure that all of your elements and interfaces have been considered.