forked from raybrownco/rem
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move unit conversions in to own partial
- Loading branch information
Showing
6 changed files
with
109 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
// Base font size in pixels, if not already defined. | ||
// Should be the same as the font-size of the html element. | ||
$base-font-size: 16px !default; | ||
|
||
@function convert-unit($value, $to-unit, $context) { | ||
|
||
// Return any unitless value as-is. | ||
@if unitless($value) { | ||
@return $value; | ||
} | ||
|
||
// Prep the units to convert from and to. | ||
$from-unit: unit($value); | ||
$to-unit: quote($to-unit); | ||
|
||
// @debug "Output unit is #{$to-unit}"; | ||
|
||
// Relative length units | ||
// http://dev.w3.org/csswg/css3-values/#relative-lengths | ||
$px-per-rem: $base-font-size / 1rem; | ||
$px-per-em: $context / 1em; | ||
$px-per-ex: ($context / 2) / 1ex; | ||
$px-per-cent: $context / 100%; | ||
|
||
// Absolute length units | ||
// http://dev.w3.org/csswg/css3-values/#absolute-lengths | ||
$px-per-inch: 96px / 1in; | ||
$px-per-mm: 96px / 25.4mm; | ||
$px-per-cm: 96px / 2.54cm; | ||
$px-per-pt: 4px / 3pt; | ||
$px-per-pc: 16px / 1pc; | ||
|
||
// Create a variable to store intermediate values. | ||
$value-px: ''; | ||
|
||
// @debug "Attempting to convert #{$value} from #{$from-unit} to intermediate value."; | ||
|
||
// Convert the supplied font size to pixels. | ||
@if $from-unit == 'px' { $value-px: $value; } | ||
@else if $from-unit == 'rem' { $value-px: $value * $px-per-rem; } | ||
@else if $from-unit == 'em' { $value-px: $value * $px-per-em; } | ||
@else if $from-unit == 'ex' { $value-px: $value * $px-per-ex; } | ||
@else if $from-unit == '%' { $value-px: $value * $px-per-cent; } | ||
@else if $from-unit == 'in' { $value-px: $value * $px-per-in; } | ||
@else if $from-unit == 'mm' { $value-px: $value * $px-per-mm; } | ||
@else if $from-unit == 'cm' { $value-px: $value * $px-per-cm; } | ||
@else if $from-unit == 'pt' { $value-px: $value * $px-per-pt; } | ||
@else if $from-unit == 'pc' { $value-px: $value * $px-per-pc; } | ||
@else if $from-unit == 'ch' { | ||
@warn "#{$from-unit} units can't be reliably converted; Returning original value."; | ||
@return $value; | ||
} | ||
@else { | ||
@warn "#{$from-unit} is an unknown font-sizing unit. Returning original value."; | ||
@return $value; | ||
} | ||
|
||
// @debug "Successfully converted #{$value} to intermediate value of #{$value-px}."; | ||
|
||
// @debug "Attempting to convert #{$value-px} to final value in #{$to-unit}."; | ||
|
||
// Return the length value in the desired unit. | ||
@if $to-unit == 'px' { @return $value-px; } | ||
@else if $to-unit == 'rem' { @return $value-px / $px-per-rem; } | ||
@else if $to-unit == 'em' { @return $value-px / $px-per-em; } | ||
@else if $to-unit == 'ex' { @return $value-px / $px-per-ex; } | ||
@else if $to-unit == '%' { @return $value-px / $px-per-cent; } | ||
@else if $to-unit == 'in' { @return $value-px / $px-per-in; } | ||
@else if $to-unit == 'mm' { @return $value-px / $px-per-mm; } | ||
@else if $to-unit == 'cm' { @return $value-px / $px-per-cm; } | ||
@else if $to-unit == 'pt' { @return $value-px / $px-per-pt; } | ||
@else if $to-unit == 'pc' { @return $value-px / $px-per-pc; } | ||
@else if $to-unit == 'ch' { | ||
@warn "#{$to-unit} units can't be reliably converted. Returning original value."; | ||
@return $value; | ||
} | ||
@else { | ||
@warn "#{$to-unit} is an unknown font-sizing unit. Returning original value."; | ||
@return $value; | ||
} | ||
} | ||
|
||
@function convert-font-unit($font-value, $to-unit, $context-font-size: $base-font-size) { | ||
@return convert-unit($font-value, $to-unit, $context-font-size); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* Test */ | ||
.rem { | ||
font-size: 10px; | ||
margin-top: 0.75rem; | ||
margin-right: 1.25rem; | ||
margin-bottom: 0.313rem; | ||
margin-left: 12px; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@import "../../stylesheets/units" | ||
|
||
/* Test */ | ||
.rem | ||
font-size: 10px | ||
margin-top: convert-length(12px, rem) | ||
margin-right: convert-length(2em, rem, 10px) | ||
margin-bottom: convert-length(50%, rem, 10px) | ||
margin-left: convert-length(12px, px) | ||
|