Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add truncate scss mixin #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions scss/tools/_truncate.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
$font-size-line-height-difference: 4px !default;

// Returns the computed line-height the caller should have.
//
// @param {Number | String} $difference - Difference from line-height to font-size. Should be positive.
//
// @return {String}
//
@function line-height($difference: $font-size-line-height-difference) {
@return calc(1em + #{$difference});
}

// Truncates the element based on amount of lines and line-height.
// Do not override the CSS properties that this mixin adds!
//
// Usage:
// p.truncate-3 {
// font-size: 16px;
// @include truncate(3);
// }
//
// @param {Number} $lines - Amount of lines that should be shown.
// @param {Number | String} $lineHeight - Line-height that will be used for the calculation of max-height.
// Should be greater than or equal font-size.
//
// @throws 'Parameter "lines" needs to be zero or a positive integer!'
//
@mixin truncate($lines: 1, $lineHeight: line-height()) {
@if type-of($lines) != 'number'
or not unitless($lines)
or $lines < 0
or round($lines) != $lines {
@error 'Parameter "lines" needs to be zero or a positive integer!';
}
display: block;
overflow: hidden;
box-sizing: content-box;
padding-top: 0;
padding-bottom: 0;
line-height: $lineHeight;
max-height: calc(#{$lineHeight} * #{$lines});
}