bootstrap-less-port
Version:
A Less port of Bootstrap v4
201 lines (169 loc) • 6.47 kB
text/less
// LESS RFS mixin
//
// (This is a custom Less port of the
// [Sass RFS Mixin](https://github.com/twbs/bootstrap/blob/master/scss/vendor/_rfs.scss).)
//
// Automated font-resizing
//
// See https://github.com/twbs/rfs
// Configuration
// Base font size
-base-font-size: 1.25rem;
-font-size-unit: rem;
// Breakpoint at where font-size starts decreasing if screen width is smaller
-breakpoint: 1200px;
-breakpoint-unit: px;
// Resize font-size based on screen height and width
-two-dimensional: false;
// Factor of decrease
-factor: 10;
& when not (isnumber( -factor)) or ( -factor <= 1) {
error("`@{rfs-factor}` is not a valid @rfs-factor, it must be greater than 1.");
}
// Generate enable or disable classes. Possibilities: false, "enable" or "disable"
-class: false;
// 1 rem = @rfs-rem-value px
-rem-value: 16;
// Safari iframe resize bug: https://github.com/twbs/rfs/issues/14
-safari-iframe-resize-bug-fix: false;
// Disable RFS by setting @enable-responsive-font-sizes to false
// LESS PORT: This variable definition overrides the definition of the same variable in
// `_variables.less`, so commenting here since it’s redundant anyway.
//@enable-responsive-font-sizes: true;
// Cache @rfs-base-font-size unit
-base-font-size-unit: get-unit( -base-font-size);
// Remove px-unit from @rfs-base-font-size for calculations
// LESS PORT: Use `@_rfs-base-font-size` here instead of `@rfs-base-font-size` since Less doesn’t
// allow recursive variable definitions.
-base-font-size: if(( -base-font-size-unit = rem),
unit(( -base-font-size * -rem-value)),
unit( -base-font-size)
);
// Cache @rfs-breakpoint unit to prevent multiple calls
-breakpoint-unit-cache: get-unit( -breakpoint);
// Remove unit from @rfs-breakpoint for calculations
// LESS PORT: Use `@_rfs-breakpoint` here instead of `@rfs-breakpoint` since Less doesn’t
// allow recursive variable definitions.
-breakpoint: if(( -breakpoint-unit = rem),
unit(( -breakpoint * -rem-value)),
unit( -breakpoint)
);
// Responsive font-size mixin
#rfs(false) {
, : // Cache @fs unit
-unit: get-unit( );
// Add !important suffix if needed
-suffix: if( , ~" !important", ~"");
// If @fs isn't a number (like inherit) or @fs has a unit (not px or rem, like 1.5em) or @ is 0, just print the value
& when
not (isnumber( ))
or (not ( -unit = px) and not ( -unit = rem))
or ( = 0) {
font-size: ~"@{fs}@{rfs-suffix}";
}
& when
(isnumber( ))
and (( -unit = px) or ( -unit = rem))
and not ( = 0) {
// Remove px-unit from @fs for calculations
// LESS PORT: Use `@_fs` here instead of `@fs` since Less doesn’t allow recursive variable
// definitions.
: if(( -unit = rem), unit(( * -rem-value)), unit( ));
// Set default font-size
-static: if(( -font-size-unit = rem),
%(~"%srem%s", ( / -rem-value), -suffix),
%(~"%spx%s", , -suffix)
);
& when (not ( -font-size-unit = px) and not ( -font-size-unit = rem)) {
error("`@{rfs-font-size-unit}` is not a valid unit for @rfs-font-size-unit. Use `px` or `rem`.")
}
// Calculate minimum font-size for given font-size
-min: ( -base-font-size + ( - -base-font-size) / -factor);
// Calculate difference between given font-size and minimum font-size for given font-size
-diff: ( - -min);
// Base font-size formatting
// No need to check if the unit is valid, because we did that before
-width: if(( -font-size-unit = rem), unit(( -min / -rem-value), rem), ~"@{fs-min}px");
// If two-dimensional, use smallest of screen width and height
-unit: if( -two-dimensional, vmin, vw);
// Calculate the variable width between 0 and @_rfs-breakpoint
-width: unit(( -diff * 100 / -breakpoint), -unit);
// Only add media query if font-size is bigger as the minimum font-size
// If @rfs-factor == 1, no rescaling will take place
-fluid: if(( > -base-font-size) and ( -responsive-font-sizes),
// Set the calculated font-size.
calc( -width + -width) -suffix,
null
);
// Rendering
& when ( -fluid = null) {
// Only render static font-size if no fluid font-size is available
font-size: -static;
}
& when not ( -fluid = null) {
// RFS breakpoint formatting
#mq-value(em) { : unit(( -breakpoint / -rem-value), -breakpoint-unit); }
#mq-value(rem) { : unit(( -breakpoint / -rem-value), -breakpoint-unit); }
#mq-value(px) { : unit( -breakpoint, px); }
#mq-value( ) when (default()) {
error("`@{rfs-breakpoint-unit}` is not a valid unit for @rfs-breakpoint-unit. Use `px`, `em` or `rem`.");
: ~"";
}
-value: #mq-value( -breakpoint-unit)[];
& when ( -class = "disable") {
// Adding an extra class increases specificity,
// which prevents the media query to override the font size
&,
.disable-responsive-font-size &,
&.disable-responsive-font-size {
font-size: -static;
}
}
& when not ( -class = "disable") {
font-size: -static;
}
& when ( -two-dimensional) {
(max-width: -value), (max-height: -value) {
& when ( -class = "enable") {
.enable-responsive-font-size &,
&.enable-responsive-font-size {
font-size: -fluid;
}
}
& when not ( -class = "enable") {
font-size: -fluid;
}
& when ( -safari-iframe-resize-bug-fix) {
min-width: 0vw;
}
}
}
& when not ( -two-dimensional) {
(max-width: -value) {
& when ( -class = "enable") {
.enable-responsive-font-size &,
&.enable-responsive-font-size {
font-size: -fluid;
}
}
& when not ( -class = "enable") {
font-size: -fluid;
}
& when ( -safari-iframe-resize-bug-fix) {
min-width: 0vw;
}
}
}
}
}
}
// The font-size & responsive-font-size mixin uses RFS to rescale font sizes
#font-size(false) {
, : // LESS PORT: Less doesn’t strip “empty” property values so we have to check for a value first.
& when not ( = ~"") {
#rfs( , );
}
}
#responsive-font-size(false) {
# , : rfs( , );
}