Skip to content

Commit 54fd06e

Browse files
author
Luca Rath-Heel
committed
add truncate scss component
1 parent 0eb9085 commit 54fd06e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

scss/tools/_truncate.scss

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
$font-size-line-height-difference: 4px !default;
2+
3+
// Returns the computed line-height the caller should have.
4+
//
5+
// @param {Number | String} $difference - Difference from line-height to font-size. Should be positive.
6+
//
7+
// @return {String}
8+
//
9+
@function line-height($difference: $font-size-line-height-difference) {
10+
@return calc(1em + #{$difference});
11+
}
12+
13+
// Truncates the element based on amount of lines and line-height.
14+
// Do not override the CSS properties that this mixin adds!
15+
//
16+
// Usage:
17+
// p.truncate-3 {
18+
// font-size: 16px;
19+
// @include truncate(3);
20+
// }
21+
//
22+
// @param {Number} $lines - Amount of lines that should be shown.
23+
// @param {Number | String} $lineHeight - Line-height that will be used for the calculation of max-height.
24+
// Should be greater than or equal font-size.
25+
//
26+
// @throws 'Parameter "lines" needs to be zero or a positive integer!'
27+
//
28+
@mixin truncate($lines: 1, $lineHeight: line-height()) {
29+
@if type-of($lines) != 'number'
30+
or not unitless($lines)
31+
or $lines < 0
32+
or round($lines) != $lines {
33+
@error 'Parameter "lines" needs to be zero or a positive integer!';
34+
}
35+
display: block;
36+
overflow: hidden;
37+
box-sizing: content-box;
38+
padding-top: 0;
39+
padding-bottom: 0;
40+
line-height: $lineHeight;
41+
max-height: calc(#{$lineHeight} * #{$lines});
42+
}

0 commit comments

Comments
 (0)