<div class="c-modetoggle u-inline-flex">
<label class="u-pointer c-modetoggle__label u-relative" aria-label="Modalità light">
<input type="radio" value="-1" class="u-none" data-color-scheme="-1">
<svg class="c-modetoggle__icon u-m-xsmall o-icon o-icon--animated o-icon--sm u-cursor-pointer">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-sun"></use>
</svg>
</label>
<label class="u-pointer c-modetoggle__label u-relative" aria-label="Modalità di sistema">
<input type="radio" value="0" class="u-none" data-color-scheme="0">
<svg class="c-modetoggle__icon u-m-xsmall o-icon o-icon--animated o-icon--sm u-cursor-pointer">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-monitor"></use>
</svg>
</label>
<label class="u-pointer c-modetoggle__label u-relative" aria-label="Modalità dark">
<input type="radio" value="1" class="u-none" data-color-scheme="1">
<svg class="c-modetoggle__icon u-m-xsmall o-icon o-icon--animated o-icon--sm u-cursor-pointer">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-moon"></use>
</svg>
</label>
</div>
<div class="c-modetoggle u-inline-flex">
<label class="u-pointer c-modetoggle__label u-relative" aria-label="Modalità light">
<input type="radio" value="-1" class="u-none" data-color-scheme="-1">
<svg class="c-modetoggle__icon u-m-xsmall o-icon o-icon--animated o-icon--sm u-cursor-pointer">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-sun"></use>
</svg>
</label>
<label class="u-pointer c-modetoggle__label u-relative" aria-label="Modalità di sistema">
<input type="radio" value="0" class="u-none" data-color-scheme="0">
<svg class="c-modetoggle__icon u-m-xsmall o-icon o-icon--animated o-icon--sm u-cursor-pointer">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-monitor"></use>
</svg>
</label>
<label class="u-pointer c-modetoggle__label u-relative" aria-label="Modalità dark">
<input type="radio" value="1" class="u-none" data-color-scheme="1">
<svg class="c-modetoggle__icon u-m-xsmall o-icon o-icon--animated o-icon--sm u-cursor-pointer">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-moon"></use>
</svg>
</label>
</div>
/* No context defined. */
// Name: modetoggle
// Description: Switch color scheme from three options: light, dark or system preference
//
// Component: `c-modetoggle`
//
// ========================================================================
.c-modetoggle {
// Custom properties
// ======================================================================
--modetoggle-icon: #{$ui-03};
--modetoggle-icon-active: var(--icon-color);
--modetoggle-tooltip-bg: var(--background-color);
--modetoggle-tooltip-color: var(--body-color);
--modetoggle-tooltip-width: 130px;
--modetoggle-shadow: var(--global-shadow);
// =======================================================================
.c-menu & {
--modetoggle-border: #{$menu-divider-color};
--modetoggle-icon: #{$ui-01-50};
--modetoggle-icon-active: #{$ui-01};
border: 1px solid var(--modetoggle-border);
border-radius: 1rem;
}
&__icon {
stroke: var(--modetoggle-icon);
&:hover,
input[type="radio"]:checked + & {
--modetoggle-icon: var(--modetoggle-icon-active);
}
}
&__label {
&:hover::after {
position: absolute;
top: 100%;
left: calc(50% - (var(--modetoggle-tooltip-width) / 2 ));
@include z(sky);
content: attr(aria-label);
display: block;
width: var(--modetoggle-tooltip-width);
padding: $padding-small;
background-color: var(--modetoggle-tooltip-bg);
box-shadow: var(--modetoggle-shadow);
color: var(--modetoggle-tooltip-color);
text-align: center;
white-space: nowrap;
@include type(label-08);
}
}
}
~function( )
{
if ( !document.querySelectorAll || !document.body )
return;
{
var fGetColorScheme = function( )
{
var tsColorScheme = null;
try
{
if ( window.localStorage )
{
var sColorScheme = localStorage.getItem( 'color_scheme' ) || null;
if ( sColorScheme != null )
{
tsColorScheme = +sColorScheme || 0;
if ( tsColorScheme > 0 )
tsColorScheme = 1;
else if ( tsColorScheme < 0 )
tsColorScheme = -1;
else
tsColorScheme = 0;
}
}
} catch ( _ ) { }
return tsColorScheme;
};
var fSetColorScheme = function( tsColorScheme )
{
tsColorScheme = +tsColorScheme || 0;
try
{
if ( window.localStorage )
{
if ( tsColorScheme )
localStorage.setItem( 'color_scheme', tsColorScheme > 0 ? +1 : (tsColorScheme < 0 ? -1 : 0) );
else
localStorage.removeItem( 'color_scheme' );
return true;
}
} catch ( _ ) { }
return false;
};
}
{
var bDarkMode = null;
var fUpdateColorScheme = function( )
{
var tsColorScheme = fGetColorScheme() || 0;
{
var bColorSchemeUser = !!tsColorScheme;
var bColorSchemeDarkMode = tsColorScheme ? tsColorScheme > 0 : bDarkMode;
document.body.setAttribute( 'data-color-scheme', (bColorSchemeUser ? 'user' : 'system') + '-' + (bColorSchemeDarkMode ? 'dark' : 'light') );
}
document.querySelectorAll('input[data-color-scheme]').forEach( function(oNode)
{
var tsColorSchemeNew = (oNode.getAttribute('data-color-scheme') || '').trim();
tsColorSchemeNew = +tsColorSchemeNew || 0;
if ( tsColorSchemeNew > 0 )
tsColorSchemeNew = 1;
else if ( tsColorSchemeNew < 0 )
tsColorSchemeNew = -1;
else
tsColorSchemeNew = 0;
oNode.checked = tsColorScheme == tsColorSchemeNew;
});
return true;
};
document.querySelectorAll('input[data-color-scheme]').forEach( function(oNode)
{
oNode.addEventListener( 'change', function( oEvent )
{
if ( !oEvent || !oEvent.currentTarget )
return;
var tsColorScheme = (oEvent.currentTarget.getAttribute('data-color-scheme') || '').trim();
tsColorScheme = +tsColorScheme || 0;
if ( !fSetColorScheme(tsColorScheme) )
{
oEvent.preventDefault && oEvent.preventDefault();
return false;
}
fUpdateColorScheme( );
});
});
}
{
if ( window.matchMedia )
{
var fSystemColorScheme = function( oMatchMedia )
{
bDarkMode = oMatchMedia && !!oMatchMedia.matches;
fUpdateColorScheme( );
};
var oMatchMedia = matchMedia( '(prefers-color-scheme:dark)' );
if ( oMatchMedia.addEventListener )
oMatchMedia.addEventListener( 'change', fSystemColorScheme );
else if ( oMatchMedia.addListner )
oMatchMedia.addListner( 'change', fSystemColorScheme );
fSystemColorScheme( oMatchMedia );
}
else
fUpdateColorScheme( );
}
}();
The function in it is:
The component is visible only in desktop mode into toolbar component, while in mobile mode is rendered in menu
Use following syntax:
{% render "@modetoggle" %}