-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathKTextTruncator.vue
149 lines (139 loc) · 4.68 KB
/
KTextTruncator.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<template>
<!--
Text is wrapped in two `spans`s to allow parent components adding
padding style directly on `<TextTruncatorCss>` component no matter
of what truncating technique is used. Otherwise adding padding directly
would break when using technique (B) since text that should be truncated
would show in padding area.
Attributes are inherited by the inner `span` to emulate the same behavior
like if only one element would wrap the text to allow attributes be applied
as close as possible to the text element.
Some width information need to be provided to `<span>s` to allow `text-overflow`
calculate properly when ellipsis should be added.
-->
<span :style="{ display: 'inline-block', maxWidth: '100%' }">
<span
v-bind="$attrs"
:style="{ display: 'inline-block', maxWidth: '100%' }"
:class="$computedClass(truncate)"
>
{{ text }}
</span>
</span>
</template>
<script>
/**
* Truncates text to a certain number of lines
* and adds an ellipsis character "…"
*
* Internet Explorer note:
* Depending on length of words of the text, there might
* be a gap between the last visible word and "…"
*/
export default {
name: 'KTextTruncator',
inheritAttrs: false,
props: {
/**
* Text to be truncated
*/
text: {
type: String,
required: true,
},
/**
* Maximum number of lines to be shown
*/
maxLines: {
type: Number,
required: false,
default: 1,
},
/**
* Text line height in rem.
* Used only for Internet Explorer fallback.
*/
lineHeightIE: {
type: Number,
required: false,
default: 1.4,
},
},
computed: {
truncate() {
const nuxtServerSideRendering = process && process.server;
if (nuxtServerSideRendering) {
return;
}
/*
(A)
For one line, use standard ellipsis text overflow
that works well for such scenario
*/
if (this.maxLines === 1) {
return {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
};
}
// 54 is random number only to be able to define `supports` test condition
if ('CSS' in window && CSS.supports && CSS.supports('-webkit-line-clamp: 54')) {
/*
(B)
For multiple lines, use line clamp in browsers that support it
(https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp)
*/
return {
overflow: 'hidden',
display: '-webkit-box',
'-webkit-line-clamp': `${this.maxLines}`,
'-webkit-box-orient': 'vertical',
// needed to make line clamp work for very long word with no spaces
overflowWrap: 'break-word',
};
} else {
/*
(C)
Fallback for multiple lines in Internet Explorer and some older versions
of other browsers that don't support line clamp
(https://caniuse.com/mdn-css_properties_-webkit-line-clamp).
Calculate max height and add "..." in `::before` while covering it with
white rectangle defined in `::after` when text doesn't need to be truncated.
Adapted from https://hackingui.com/a-pure-css-solution-for-multiline-text-truncation/
and https://css-tricks.com/line-clampin/#the-hide-overflow-place-ellipsis-pure-css-way.
*/
const ellipsisWidth = '1rem';
return {
overflow: 'hidden',
position: 'relative',
lineHeight: `${this.lineHeightIE}rem`,
maxHeight: `${this.maxLines * this.lineHeightIE}rem`,
// needed to make truncation work for very long word with no spaces
// `word-wrap` is a legacy name for `overflow-wrap` that needs to be used for IE
wordWrap: 'break-word',
// create space for "..."
paddingRight: ellipsisWidth,
marginRigth: `-${ellipsisWidth}`,
'::before': {
content: "'…'",
position: 'absolute',
right: 0,
bottom: 0,
},
// cover "..." with white rectangle when text
// doesn't need to be truncated
'::after': {
content: "''",
position: 'absolute',
right: 0,
width: ellipsisWidth,
height: '100%',
background: this.$themeTokens.surface,
},
};
}
},
},
};
</script>