-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathExpandableText.ts
More file actions
203 lines (174 loc) · 5.45 KB
/
ExpandableText.ts
File metadata and controls
203 lines (174 loc) · 5.45 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
import jsxRender from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
import { isPhone } from "@ui5/webcomponents-base/dist/Device.js";
import type { UI5CustomEvent } from "@ui5/webcomponents-base";
import type { LinkAccessibilityAttributes } from "./Link.js";
import ExpandableTextOverflowMode from "./types/ExpandableTextOverflowMode.js";
import type Button from "./Button.js";
import type TextEmptyIndicatorMode from "./types/TextEmptyIndicatorMode.js";
import {
EXPANDABLE_TEXT_SHOW_LESS,
EXPANDABLE_TEXT_SHOW_MORE,
EXPANDABLE_TEXT_CLOSE,
EXPANDABLE_TEXT_SHOW_LESS_POPOVER_ARIA_LABEL,
EXPANDABLE_TEXT_SHOW_MORE_POPOVER_ARIA_LABEL,
} from "./generated/i18n/i18n-defaults.js";
// Template
import ExpandableTextTemplate from "./ExpandableTextTemplate.js";
// Styles
import ExpandableTextCss from "./generated/themes/ExpandableText.css.js";
/**
* @class
*
* ### Overview
*
* The `ui5-expandable-text` component allows displaying a large body of text in a small space. It provides an "expand/collapse" functionality, which shows/hides potentially truncated text.
*
* ### Usage
*
* #### When to use:
* - To accommodate long texts in limited space, for example in list items, table cell texts, or forms
*
* #### When not to use:
* - The content is critical for the user. In this case use short descriptions that can fit in
* - Strive to provide short and meaningful texts to avoid excessive number of "Show More" links on the page
*
* ### Responsive Behavior
*
* On phones, if the component is configured to display the full text in a popover, the popover will appear in full screen.
*
* ### ES6 Module Import
*
* `import "@ui5/webcomponents/dist/ExpandableText";`
*
* @constructor
* @extends UI5Element
* @public
* @since 2.6.0
*/
@customElement({
tag: "ui5-expandable-text",
renderer: jsxRender,
styles: ExpandableTextCss,
template: ExpandableTextTemplate,
})
class ExpandableText extends UI5Element {
/**
* Text of the component.
*
* @default undefined
* @public
*/
@property()
text?: string;
/**
* Maximum number of characters to be displayed initially. If the text length exceeds this limit, the text will be truncated with an ellipsis, and the "More" link will be displayed.
* @default 100
* @public
*/
@property({ type: Number })
maxCharacters = 100;
/**
* Determines how the full text will be displayed.
* @default "InPlace"
* @public
*/
@property()
overflowMode: `${ExpandableTextOverflowMode}` = "InPlace"
/**
* Specifies if an empty indicator should be displayed when there is no text.
* @default "Off"
* @public
*/
@property()
emptyIndicatorMode: `${TextEmptyIndicatorMode}` = "Off";
@property({ type: Boolean })
_expanded = false;
@property({ type: Boolean })
_shouldScrollIntoView = false;
@i18n("@ui5/webcomponents")
static i18nBundle: I18nBundle;
onAfterRendering() {
if (this._shouldScrollIntoView) {
this._shouldScrollIntoView = false;
const toggleLink = this.shadowRoot?.querySelector("[ui5-link]") as HTMLElement;
if (toggleLink) {
toggleLink.scrollIntoView({
behavior: "smooth",
block: "nearest",
inline: "nearest",
});
}
}
}
getFocusDomRef(): HTMLElement | undefined {
if (this._usePopover) {
return this.shadowRoot?.querySelector("[ui5-responsive-popover]") as HTMLElement;
}
return this.shadowRoot?.querySelector("[ui5-link]") as HTMLElement;
}
get _displayedText() {
if (this._expanded && !this._usePopover) {
return this.text;
}
return this.text?.substring(0, this.maxCharacters);
}
get _maxCharactersExceeded() {
return (this.text?.length || 0) > this.maxCharacters;
}
get _usePopover() {
return this.overflowMode === ExpandableTextOverflowMode.Popover;
}
get _ellipsisText() {
if (this._expanded && !this._usePopover) {
return " ";
}
return "... ";
}
get _textForToggle() {
return this._expanded ? ExpandableText.i18nBundle.getText(EXPANDABLE_TEXT_SHOW_LESS) : ExpandableText.i18nBundle.getText(EXPANDABLE_TEXT_SHOW_MORE);
}
get _closeButtonText() {
return ExpandableText.i18nBundle.getText(EXPANDABLE_TEXT_CLOSE);
}
get _accessibilityAttributesForToggle(): LinkAccessibilityAttributes {
if (this._usePopover) {
return {
expanded: this._expanded,
hasPopup: "dialog",
};
}
return {
expanded: this._expanded,
};
}
get _accessibleNameForToggle() {
if (this._usePopover) {
return this._expanded ? ExpandableText.i18nBundle.getText(EXPANDABLE_TEXT_SHOW_LESS_POPOVER_ARIA_LABEL) : ExpandableText.i18nBundle.getText(EXPANDABLE_TEXT_SHOW_MORE_POPOVER_ARIA_LABEL);
}
return undefined;
}
_handlePopoverClose() {
if (!isPhone()) {
this._expanded = false;
}
}
_handleToggleClick() {
this._expanded = !this._expanded;
// Scroll the toggle link into view after expanding/collapsing, especially important
// when expanded content is long and pushes the "Show Less" link out of viewport
if (!this._usePopover) {
this._shouldScrollIntoView = true;
}
}
_handleCloseButtonClick(e: UI5CustomEvent<Button, "click">) {
this._expanded = false;
e.stopPropagation();
}
}
ExpandableText.define();
export default ExpandableText;