Skip to content

Instantly share code, notes, and snippets.

@michaeldeboeve
Created June 3, 2023 12:46
Show Gist options
  • Save michaeldeboeve/0fa8bb27395e920801978d794991c51a to your computer and use it in GitHub Desktop.
Save michaeldeboeve/0fa8bb27395e920801978d794991c51a to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
@use 'sass:math';
@use 'sass:meta';
@use "sass:list";
@function strip-units($number) {
@if meta.type-of($number) == 'number' and not math.is-unitless($number) {
@return $number / ($number * 0 + 1);
}
@return $number;
}
@function to-fixed($val, $fractionDigits: 0) {
$power: math.pow(10, $fractionDigits);
@return (math.round($power * $val) / $power);
}
@function convert_to_rem_unitless($val, $base) {
$allowed_units: 'px', 'em', 'rem';
$unit: math.unit($val);
$is_unitless: math.is-unitless($val);
$unit_is_allowed: list.index($allowed_units, $unit);
@if $is_unitless {
@return $val;
}
@else if not $is_unitless and $unit == 'px' or not $unit_is_allowed {
$val: strip-units($val);
$val: ($val / $base); // Convert to REM
}
@else if not $is_unitless and $unit_is_allowed {
$val: strip-units($val);
}
@if not $is_unitless and not $unit_is_allowed {
@warn "Unknown unit `#{$unit}`. Converted as if it was px";
}
@return $val;
}
@function fluid($min, $max, $root: $root-fs, $min-width: $fluid-min-width, $max-width: $fluid-max-width, $force_px_output: false) {
// set preferred output unit
$output_unit: 1rem;
@if unit($min) == 'em' and unit($max) == 'em' {
$output_unit: 1em;
}
// Force root font-size to unitless
@if not unitless($root) {
$root: strip-units($root);
}
// Convert everything to unitless, based on rem
$min: convert_to_rem_unitless($min, $root);
$max: convert_to_rem_unitless($max, $root);
$min-width: convert_to_rem_unitless($min-width, $root);
$max-width: convert_to_rem_unitless($max-width, $root);
// Viewport / font-size ratio calculation
$neg-min-width: (-1 * $min-width); // Precalcuation for the -MinWidth we need
$diff-w: ($max-width - $min-width); // Precalculation for the (MaxWidth - MinWidth) we need
$btwn_1: (($neg-min-width * (($max - $min) / $diff-w) + $min) * $output_unit);
$btwn_2: ((($max - $min) / $diff-w ) * 100vw);
// Force smaller output on decimals, for filesize and cosmetic reasons
$btwn_1: to-fixed($btwn_1, 2);
$btwn_2: to-fixed($btwn_2, 2);
// Set $output_units
$min_output: $min * $output_unit;
$max_output: $max * $output_unit;
// Optional: Force output to px units if needed
@if $force_px_output {
$output_unit: 1px;
$min_output: ($min * $root) * $output_unit;
$max_output: ($max * $root) * $output_unit;
$btwn_1: (strip-units($btwn_1) * $root) * $output_unit;
}
@return clamp(#{$min_output}, calc(#{$btwn_1} + #{$btwn_2}), #{$max_output});
}
$root-fs: 10 !default;
$fluid-max-width: 1440px !default;
$fluid-min-width: 320px !default;
:root {
font-size: math.percentage($root-fs / 16);
}
.test {
clamp: fluid(70px, 120px);
example: clamp(4.38rem, calc(3.48rem + 4.46vw), 7.50rem); // based on 70px, 120px, root: 16
}
:root {
font-size: 62.5%;
}
.test {
clamp: clamp(7rem, calc(5.57rem + 4.46vw), 12rem);
example: clamp(4.38rem, calc(3.48rem + 4.46vw), 7.50rem);
}
{
"sass": {
"compiler": "dart-sass/1.32.12",
"extensions": {},
"syntax": "SCSS",
"outputStyle": "expanded"
},
"autoprefixer": false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment