-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathread-more.js
49 lines (42 loc) · 1.27 KB
/
read-more.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
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import layout from '../templates/components/read-more';
import { setComponentTemplate } from '@ember/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
/**
* @module ReadMore
* @description
* ReadMore components are used to wrap long text that we'd like to show as one line initially with the option to expand and read.
* Text which is shorter than the surrounding div will not truncate or show the See More button.
*
* @example
* <div style="width:100px;border: solid purple 2px; margin:20px">
* <ReadMore>My <em>super</em> long text goes in here.</ReadMore>
* </div>
*
* <div style="width:100px;border: solid purple 2px; margin:20px">
* <ReadMore>This text fits!</ReadMore>
* </div>
*/
class ReadMoreComponent extends Component {
@action
calculateOverflow(e) {
const spanText = e.querySelector('.description-block');
if (spanText.offsetWidth > e.offsetWidth) {
this.hasOverflow = true;
}
}
@tracked
isOpen = false;
@tracked
hasOverflow = false;
@action
toggleOpen() {
this.isOpen = !this.isOpen;
}
}
export default setComponentTemplate(layout, ReadMoreComponent);