Le creazioni di Responsive Web Design spesso esistono su diversi punti di interruzione. Gestire questi punti di interruzione non è sempre facile. Usarli e aggiornarli a volte può essere noioso. Da qui la necessità di un mixin per gestire la configurazione e l'utilizzo dei breakpoint.
Versione semplice
Per prima cosa hai bisogno di una mappa dei punti di interruzione, associati ai nomi.
$breakpoints: ( 'small': 767px, 'medium': 992px, 'large': 1200px ) !default;
Quindi, il mixin utilizzerà questa mappa.
/// Mixin to manage responsive breakpoints /// @author Hugo Giraudel /// @param (String) $breakpoint - Breakpoint name /// @require $breakpoints @mixin respond-to($breakpoint) ( // If the key exists in the map @if map-has-key($breakpoints, $breakpoint) ( // Prints a media query based on the value @media (min-width: map-get($breakpoints, $breakpoint)) ( @content; ) ) // If the key doesn't exist in the map @else ( @warn "Unfortunately, no value could be retrieved from `#($breakpoint)`. " + "Available breakpoints are: #(map-keys($breakpoints))."; ) )
Utilizzo:
.selector ( color: red; @include respond-to('small') ( color: blue; ) )
Risultato:
.selector ( color: red; ) @media (min-width: 767px) ( .selector ( color: blue; ) )
Versione avanzata
La versione semplice consente solo di utilizzare le min-width
media query. Per utilizzare query più avanzate, possiamo modificare la nostra mappa iniziale e mescolare un po '.
$breakpoints: ( 'small': ( min-width: 767px ), 'medium': ( min-width: 992px ), 'large': ( min-width: 1200px ) ) !default;
/// Mixin to manage responsive breakpoints /// @author Hugo Giraudel /// @param (String) $breakpoint - Breakpoint name /// @require $breakpoints @mixin respond-to($breakpoint) ( // If the key exists in the map @if map-has-key($breakpoints, $breakpoint) ( // Prints a media query based on the value @media #(inspect(map-get($breakpoints, $breakpoint))) ( @content; ) ) // If the key doesn't exist in the map @else ( @warn "Unfortunately, no value could be retrieved from `#($breakpoint)`. " + "Available breakpoints are: #(map-keys($breakpoints))."; ) )
Utilizzo:
.selector ( color: red; @include respond-to('small') ( color: blue; ) )
Risultato:
.selector ( color: red; ) @media (min-width: 767px) ( .selector ( color: blue; ) )