-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathbal-hint.tsx
More file actions
252 lines (228 loc) · 6.08 KB
/
bal-hint.tsx
File metadata and controls
252 lines (228 loc) · 6.08 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import {
Component,
Host,
h,
Method,
State,
Prop,
Element,
FunctionalComponent,
ComponentInterface,
} from '@stencil/core'
import { ListenToConfig, BalConfigObserver, BalConfigState } from '../../utils/config'
import { BEM } from '../../utils/bem'
import { preventDefault } from '../bal-select/utils/utils'
import { BalScrollHandler } from '../../utils/scroll'
import { ListenToBreakpoints, BalBreakpointObserver, BalBreakpoints, balBreakpoints } from '../../utils/breakpoints'
import { isEnterKey, isSpaceKey } from '../../utils/keyboard'
@Component({
tag: 'bal-hint',
styleUrl: 'bal-hint.sass',
})
export class Hint implements ComponentInterface, BalConfigObserver, BalBreakpointObserver {
@Element() element!: HTMLElement
private popoverElement!: HTMLBalPopoverElement
private slotWrapperEl?: HTMLDivElement
private hintContentEl?: HTMLDivElement
private bodyScrollHandler = new BalScrollHandler()
@State() isActive = false
@State() innerCloseLabel = 'Close'
@State() isMobile = balBreakpoints.isMobile
/**
* Text for the close button.
*/
@Prop() closeLabel?: string
/**
* Disables the close button for tablet and desktop
*/
@Prop() small = false
connectedCallback() {
this.bodyScrollHandler.connect()
}
componentDidRender() {
this.updateContent()
}
disconnectedCallback() {
this.bodyScrollHandler.disconnect()
}
@ListenToBreakpoints()
breakpointListener(breakpoints: BalBreakpoints): void {
const isCurrentMobile = breakpoints.mobile
if (isCurrentMobile !== this.isMobile) {
this.isActive = false
}
this.isMobile = isCurrentMobile
}
/**
* @internal define config for the component
*/
@Method()
@ListenToConfig()
async configChanged(state: BalConfigState): Promise<void> {
if (!this.closeLabel) {
switch (state.language) {
case 'de':
this.innerCloseLabel = 'Schliessen'
break
case 'fr':
this.innerCloseLabel = 'Fermer'
break
case 'it':
this.innerCloseLabel = 'Chiudere'
break
case 'nl':
this.innerCloseLabel = 'Sluiten'
break
default:
this.innerCloseLabel = 'Close'
break
}
}
}
/**
* Toggles the hint box.
*/
@Method()
async toggle(): Promise<void> {
if (this.isActive) {
this.dismiss()
} else {
this.present()
}
}
/**
* Opens the hint box.
*/
@Method()
async present(): Promise<void> {
if (this.popoverElement) {
this.popoverElement.present()
}
if (this.isMobile) {
this.bodyScrollHandler.disable()
}
this.isActive = true
}
/**
* Closes the hint box.
*/
@Method()
async dismiss(): Promise<void> {
if (this.popoverElement) {
this.popoverElement.dismiss()
}
if (this.isMobile) {
this.bodyScrollHandler.enable()
}
this.isActive = false
}
private onPopoverChange = (ev: CustomEvent<boolean>) => {
this.isActive = ev.detail
preventDefault(ev)
}
private updateContent() {
if (this.hintContentEl && this.slotWrapperEl) {
this.hintContentEl.innerHTML = this.slotWrapperEl.innerHTML
}
}
render() {
const block = BEM.block('hint')
const elIcon = block.element('icon')
const elContent = block.element('content')
const elButtons = elContent.element('buttons')
const padding = this.isMobile ? 0 : 8
const offsetY = this.isMobile ? 0 : 16
const TriggerIcon: FunctionalComponent = () => {
return (
<bal-icon
class={{
...elIcon.class(),
}}
data-testid="bal-hint-trigger"
bal-popover-trigger
aria-haspopup="true"
role="button"
name="info-circle"
tabindex={0}
onClick={() => this.toggle()}
onKeyDown={event => (isEnterKey(event) || isSpaceKey(event)) && this.toggle()}
></bal-icon>
)
}
const HintContent: FunctionalComponent = () => {
return (
<div
class={{
...elContent.class(),
}}
data-testid="bal-hint-content"
>
<div ref={el => (this.hintContentEl = el)}></div>
<bal-button-group
class={{
...elButtons.class(),
...elButtons.modifier('is-hidden-desktop').class(this.small),
}}
>
<bal-button data-testid="bal-hint-close" color="info" onClick={() => this.dismiss()}>
{this.closeLabel ? this.closeLabel : this.innerCloseLabel}
</bal-button>
</bal-button-group>
</div>
)
}
const MobileOverlay: FunctionalComponent = () => {
const elOverlay = block.element('overlay')
return (
<div
class={{
...elOverlay.class(),
}}
>
<TriggerIcon></TriggerIcon>
<div
class={{
...elOverlay.element('content').class(),
...elOverlay.element('content').modifier('active').class(this.isActive),
}}
>
<HintContent></HintContent>
</div>
</div>
)
}
const Popover: FunctionalComponent = () => {
return (
<div class={{ ...block.element('popover').class() }}>
<bal-popover
hint
position="right"
offsetX={0}
offsetY={offsetY}
padding={padding}
ref={el => (this.popoverElement = el as HTMLBalPopoverElement)}
onBalChange={this.onPopoverChange}
>
<TriggerIcon></TriggerIcon>
<bal-popover-content color="grey">
<HintContent></HintContent>
</bal-popover-content>
</bal-popover>
</div>
)
}
const HintElement = this.isMobile ? MobileOverlay : Popover
return (
<Host
class={{
...block.class(),
}}
>
<HintElement></HintElement>
<div ref={el => (this.slotWrapperEl = el)} style={{ display: 'none' }}>
<slot></slot>
</div>
</Host>
)
}
}