-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
ellipsis.js
50 lines (48 loc) · 1.01 KB
/
ellipsis.js
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
// @flow
import type { Styles } from '../types/style'
/**
* CSS to represent truncated text with an ellipsis. You can optionally pass a max-width and number of lines before truncating.
*
* @example
* // Styles as object usage
* const styles = {
* ...ellipsis('250px')
* }
*
* // styled-components usage
* const div = styled.div`
* ${ellipsis('250px')}
* `
*
* // CSS as JS Output
*
* div: {
* 'display': 'inline-block',
* 'maxWidth': '250px',
* 'overflow': 'hidden',
* 'textOverflow': 'ellipsis',
* 'whiteSpace': 'nowrap',
* 'wordWrap': 'normal'
* }
*/
export default function ellipsis(
width?: ?string | ?number,
lines?: number = 1,
): Styles {
const styles = {
display: 'inline-block',
maxWidth: width || '100%',
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
wordWrap: 'normal',
}
return lines > 1
? {
...styles,
display: '-webkit-box',
webkitLineClamp: lines,
webkitBoxOrient: 'vertical',
}
: styles
}