@targetprocess/popover 12.20.1-alpha.4 → 12.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,322 +1,325 @@
1
- /* eslint-disable id-length,wrap-regex */
2
-
3
- import React from 'react'
4
- import PropTypes from 'prop-types'
5
- import classNames from 'classnames'
6
- import {PopoverPosition} from './PopoverPosition'
7
- import styles from './style/index.css'
8
- import {DimensionParams, GetPopoverPositionType, PositionParams} from './types'
9
- import {throttle} from '../../base/utils/throttle'
10
- import FocusTrap from 'focus-trap-react'
11
- import {generateId, focusFirstDescendant} from '../../base/utils/ariaUtils'
12
-
13
- const popoverContainerPrefix = 'popover-container-'
14
-
15
- type PopoverContainerProps = {
16
- containerElement?: HTMLElement
17
- positioningTarget: HTMLElement
18
- className?: string
19
- relativeToParent?: boolean
20
- trackScroll?: boolean
21
- scrollThrottleIntervalMs?: number
22
- getPopoverPosition: GetPopoverPositionType
23
- showTip?: boolean
24
- zIndex: number
25
- role?: 'tooltip' | 'dialog'
26
- trapFocus?: boolean
27
- onEscapeAction: (event: Event) => void
28
- }
29
-
30
- type PopoverContainerState = {
31
- adjusted: boolean
32
- positionX: number
33
- positionY: number
34
- hostRect?: PositionParams & DimensionParams
35
- popoverRect?: DimensionParams
36
- scroll?: PositionParams
37
- }
38
-
39
- const isScrollParent = (element: HTMLElement): boolean => {
40
- // Firefox wants us to check `-x` and `-y` variations as well
41
- const {overflow, overflowX, overflowY} = getComputedStyle(element)
42
- return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX)
43
- }
44
-
45
- function getNodeName(element): string | null | undefined {
46
- return element ? (element.nodeName || '').toLowerCase() : null
47
- }
48
-
49
- function getNodeScroll(node) {
50
- if (node === window) {
51
- return {
52
- scrollLeft: window.pageXOffset,
53
- scrollTop: window.pageYOffset
54
- }
55
- }
56
- return {
57
- scrollLeft: node.scrollLeft,
58
- scrollTop: node.scrollTop
59
- }
60
- }
61
-
62
- const getCompositeRect = (elementOrVirtualElement, offsetParent) => {
63
- const rect = elementOrVirtualElement.getBoundingClientRect()
64
- let scroll = {scrollLeft: 0, scrollTop: 0}
65
-
66
- if (
67
- // https://github.com/popperjs/popper-core/issues/1078
68
- getNodeName(offsetParent) !== 'body' ||
69
- isScrollParent(document.documentElement)
70
- ) {
71
- scroll = getNodeScroll(offsetParent)
72
- }
73
-
74
- const offsets = offsetParent.getBoundingClientRect()
75
- offsets.x += offsetParent.clientLeft
76
- offsets.y += offsetParent.clientTop
77
-
78
- return {
79
- left: rect.left + scroll.scrollLeft - offsets.x,
80
- top: rect.top + scroll.scrollTop - offsets.y,
81
- right: rect.right + scroll.scrollLeft - offsets.x,
82
- bottom: rect.bottom + scroll.scrollTop - offsets.y,
83
- width: rect.width,
84
- height: rect.height
85
- }
86
- }
87
-
88
- export class PopoverContainer extends React.Component<
89
- PopoverContainerProps,
90
- PopoverContainerState
91
- > {
92
- static propTypes = {
93
- positioningTarget: PropTypes.object,
94
- containerElement: PropTypes.object,
95
- className: PropTypes.string,
96
- trackScroll: PropTypes.bool,
97
- role: PropTypes.oneOf(['tooltip', 'dialog']),
98
- trapFocus: PropTypes.bool,
99
- onEscapeAction: PropTypes.func
100
- }
101
-
102
- static defaultProps = {
103
- trackScroll: true
104
- }
105
-
106
- mounted = false
107
- popoverContentRef = React.createRef<HTMLDivElement>()
108
-
109
- state: PopoverContainerState = {
110
- adjusted: false,
111
- positionX: 0,
112
- positionY: 0
113
- }
114
-
115
- popoverContainerId = generateId(popoverContainerPrefix)
116
-
117
- adjustPosition() {
118
- if (!this.mounted) {
119
- return
120
- }
121
-
122
- const containerElement = this.props.containerElement || document.body
123
- const positioningBoundingRect = getCompositeRect(
124
- this.props.positioningTarget,
125
- this.props.containerElement
126
- )
127
-
128
- this.setState({
129
- hostRect: {
130
- top: this.props.relativeToParent ? 0 : positioningBoundingRect.top,
131
- left: this.props.relativeToParent ? 0 : positioningBoundingRect.left,
132
- right: this.props.relativeToParent ? 0 : positioningBoundingRect.right,
133
- bottom: this.props.relativeToParent ? 0 : positioningBoundingRect.bottom,
134
- width: positioningBoundingRect.width,
135
- height: positioningBoundingRect.height
136
- },
137
- popoverRect: this.popoverContentRef.current.getBoundingClientRect(),
138
- scroll:
139
- containerElement === document.body || !this.props.trackScroll
140
- ? {left: 0, top: 0}
141
- : {
142
- left: containerElement.scrollLeft,
143
- top: containerElement.scrollTop
144
- }
145
- })
146
-
147
- window.requestAnimationFrame(() => {
148
- if (this.mounted) {
149
- this.setState({adjusted: true})
150
- }
151
- })
152
- }
153
-
154
- onResize = () => this.adjustPosition()
155
-
156
- onScroll = throttle(() => {
157
- this.adjustPosition()
158
- }, this.props.scrollThrottleIntervalMs || 200)
159
-
160
- componentDidMount() {
161
- this.mounted = true
162
- this.adjustPosition()
163
-
164
- if (this.props.trackScroll) {
165
- document.addEventListener('scroll', this.onScroll)
166
- window.addEventListener('resize', this.onResize)
167
- if (this.props.containerElement && this.props.containerElement !== document.body) {
168
- this.props.containerElement.addEventListener('scroll', this.onScroll)
169
- }
170
- }
171
-
172
- if (this.popoverContentRef.current && !this.props.trapFocus) {
173
- if (!focusFirstDescendant(this.popoverContentRef.current)) {
174
- this.popoverContentRef.current.tabIndex = -1
175
- this.popoverContentRef.current.focus()
176
- }
177
- }
178
- }
179
-
180
- componentDidUpdate(previousProps) {
181
- if (
182
- this.props.containerElement !== previousProps.containerElement ||
183
- this.props.getPopoverPosition !== previousProps.getPopoverPosition
184
- ) {
185
- this.adjustPosition()
186
- }
187
- }
188
-
189
- componentWillUnmount() {
190
- this.mounted = false
191
- if (this.onScroll && this.props.trackScroll) {
192
- document.body.removeEventListener('scroll', this.onScroll)
193
- window.removeEventListener('resize', this.onResize)
194
- if (this.props.containerElement && this.props.containerElement !== document.body) {
195
- this.props.containerElement.removeEventListener('scroll', this.onScroll)
196
- }
197
- }
198
- }
199
-
200
- getPopoverTipPosition = (popoverPosition, containerPosition) => {
201
- if (popoverPosition.bottom && popoverPosition.bottom >= containerPosition.height) {
202
- return 'top'
203
- }
204
- if (popoverPosition.top && popoverPosition.top >= containerPosition.height) {
205
- return 'bottom'
206
- }
207
- if (popoverPosition.left && popoverPosition.left >= containerPosition.width) {
208
- return 'right'
209
- }
210
- if (popoverPosition.right && popoverPosition.right >= containerPosition.width) {
211
- return 'left'
212
- }
213
-
214
- return null
215
- }
216
-
217
- handleKeyDown = event => {
218
- if (event.key === 'Escape') {
219
- event.stopPropagation()
220
- this.props.onEscapeAction(event)
221
- }
222
- }
223
-
224
- render() {
225
- const getPopoverPosition =
226
- typeof this.props.getPopoverPosition === 'function'
227
- ? this.props.getPopoverPosition
228
- : PopoverPosition[this.props.getPopoverPosition] || PopoverPosition.bottomLeft
229
- const position = this.state.hostRect
230
- ? getPopoverPosition(
231
- this.state.hostRect,
232
- this.state.popoverRect,
233
- this.state.scroll,
234
- this.props.containerElement
235
- )
236
- : {}
237
-
238
- const containerPosition = {
239
- left: this.state.hostRect ? this.state.hostRect.left : 0,
240
- top: this.state.hostRect ? this.state.hostRect.top : 0,
241
- width: this.state.hostRect && this.state.hostRect.width,
242
- height: this.state.hostRect && this.state.hostRect.height
243
- }
244
-
245
- const tipPosition = this.getPopoverTipPosition(position, containerPosition)
246
-
247
- // FIXME: position.top cast to number, because value may be not a number
248
- return (
249
- <div
250
- style={{
251
- position: 'absolute',
252
- ...containerPosition,
253
- pointerEvents: 'none',
254
- zIndex: this.props.zIndex,
255
- transition: '0.1s',
256
- transitionProperty: 'opacity',
257
- opacity: this.state.adjusted ? 1 : 0
258
- }}
259
- >
260
- {this.props.showTip && tipPosition && (
261
- <div
262
- className={classNames(
263
- styles['popover-tip'],
264
- styles[`popover-tip--${tipPosition}`],
265
- 'popover-tip'
266
- )}
267
- style={{
268
- top: tipPosition === 'bottom' && (position.top as number) - 8,
269
- bottom: tipPosition === 'top' && (position.bottom as number) - 8,
270
- left: tipPosition === 'right' && (position.left as number) - 8,
271
- right: tipPosition === 'left' && (position.right as number) - 8
272
- }}
273
- />
274
- )}
275
- {this.props.trapFocus && (
276
- <FocusTrap
277
- focusTrapOptions={{
278
- fallbackFocus: `#${this.popoverContainerId}`,
279
- clickOutsideDeactivates: true
280
- }}
281
- >
282
- <div
283
- id={this.popoverContainerId}
284
- ref={this.popoverContentRef}
285
- style={{
286
- position: 'absolute',
287
- pointerEvents: 'all',
288
- ...position
289
- }}
290
- tabIndex={-1}
291
- className={this.props.className}
292
- role={this.props.role}
293
- aria-modal={this.props.trapFocus ? 'true' : 'false'}
294
- onKeyDown={this.handleKeyDown}
295
- >
296
- {this.props.children}
297
- </div>
298
- </FocusTrap>
299
- )}
300
-
301
- {!this.props.trapFocus && (
302
- <div
303
- id={this.popoverContainerId}
304
- ref={this.popoverContentRef}
305
- style={{
306
- position: 'absolute',
307
- pointerEvents: 'all',
308
- ...position
309
- }}
310
- tabIndex={-1}
311
- className={this.props.className}
312
- role={this.props.role}
313
- aria-modal={this.props.trapFocus ? 'true' : 'false'}
314
- onKeyDown={this.handleKeyDown}
315
- >
316
- {this.props.children}
317
- </div>
318
- )}
319
- </div>
320
- )
321
- }
322
- }
1
+ /*********************************************************************
2
+ * © Copyright IBM Corp. 2024
3
+ *********************************************************************/
4
+ /* eslint-disable id-length,wrap-regex */
5
+
6
+ import React from 'react'
7
+ import PropTypes from 'prop-types'
8
+ import classNames from 'classnames'
9
+ import {PopoverPosition} from './PopoverPosition'
10
+ import styles from './style/index.css'
11
+ import {DimensionParams, GetPopoverPositionType, PositionParams} from './types'
12
+ import {throttle} from '../../base/utils/throttle'
13
+ import FocusTrap from 'focus-trap-react'
14
+ import {generateId, focusFirstDescendant} from '../../base/utils/ariaUtils'
15
+
16
+ const popoverContainerPrefix = 'popover-container-'
17
+
18
+ type PopoverContainerProps = {
19
+ containerElement?: HTMLElement
20
+ positioningTarget: HTMLElement
21
+ className?: string
22
+ relativeToParent?: boolean
23
+ trackScroll?: boolean
24
+ scrollThrottleIntervalMs?: number
25
+ getPopoverPosition: GetPopoverPositionType
26
+ showTip?: boolean
27
+ zIndex: number
28
+ role?: 'tooltip' | 'dialog'
29
+ trapFocus?: boolean
30
+ onEscapeAction: (event: Event) => void
31
+ }
32
+
33
+ type PopoverContainerState = {
34
+ adjusted: boolean
35
+ positionX: number
36
+ positionY: number
37
+ hostRect?: PositionParams & DimensionParams
38
+ popoverRect?: DimensionParams
39
+ scroll?: PositionParams
40
+ }
41
+
42
+ const isScrollParent = (element: HTMLElement): boolean => {
43
+ // Firefox wants us to check `-x` and `-y` variations as well
44
+ const {overflow, overflowX, overflowY} = getComputedStyle(element)
45
+ return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX)
46
+ }
47
+
48
+ function getNodeName(element): string | null | undefined {
49
+ return element ? (element.nodeName || '').toLowerCase() : null
50
+ }
51
+
52
+ function getNodeScroll(node) {
53
+ if (node === window) {
54
+ return {
55
+ scrollLeft: window.pageXOffset,
56
+ scrollTop: window.pageYOffset
57
+ }
58
+ }
59
+ return {
60
+ scrollLeft: node.scrollLeft,
61
+ scrollTop: node.scrollTop
62
+ }
63
+ }
64
+
65
+ const getCompositeRect = (elementOrVirtualElement, offsetParent) => {
66
+ const rect = elementOrVirtualElement.getBoundingClientRect()
67
+ let scroll = {scrollLeft: 0, scrollTop: 0}
68
+
69
+ if (
70
+ // https://github.com/popperjs/popper-core/issues/1078
71
+ getNodeName(offsetParent) !== 'body' ||
72
+ isScrollParent(document.documentElement)
73
+ ) {
74
+ scroll = getNodeScroll(offsetParent)
75
+ }
76
+
77
+ const offsets = offsetParent.getBoundingClientRect()
78
+ offsets.x += offsetParent.clientLeft
79
+ offsets.y += offsetParent.clientTop
80
+
81
+ return {
82
+ left: rect.left + scroll.scrollLeft - offsets.x,
83
+ top: rect.top + scroll.scrollTop - offsets.y,
84
+ right: rect.right + scroll.scrollLeft - offsets.x,
85
+ bottom: rect.bottom + scroll.scrollTop - offsets.y,
86
+ width: rect.width,
87
+ height: rect.height
88
+ }
89
+ }
90
+
91
+ export class PopoverContainer extends React.Component<
92
+ PopoverContainerProps,
93
+ PopoverContainerState
94
+ > {
95
+ static propTypes = {
96
+ positioningTarget: PropTypes.object,
97
+ containerElement: PropTypes.object,
98
+ className: PropTypes.string,
99
+ trackScroll: PropTypes.bool,
100
+ role: PropTypes.oneOf(['tooltip', 'dialog']),
101
+ trapFocus: PropTypes.bool,
102
+ onEscapeAction: PropTypes.func
103
+ }
104
+
105
+ static defaultProps = {
106
+ trackScroll: true
107
+ }
108
+
109
+ mounted = false
110
+ popoverContentRef = React.createRef<HTMLDivElement>()
111
+
112
+ state: PopoverContainerState = {
113
+ adjusted: false,
114
+ positionX: 0,
115
+ positionY: 0
116
+ }
117
+
118
+ popoverContainerId = generateId(popoverContainerPrefix)
119
+
120
+ adjustPosition() {
121
+ if (!this.mounted) {
122
+ return
123
+ }
124
+
125
+ const containerElement = this.props.containerElement || document.body
126
+ const positioningBoundingRect = getCompositeRect(
127
+ this.props.positioningTarget,
128
+ this.props.containerElement
129
+ )
130
+
131
+ this.setState({
132
+ hostRect: {
133
+ top: this.props.relativeToParent ? 0 : positioningBoundingRect.top,
134
+ left: this.props.relativeToParent ? 0 : positioningBoundingRect.left,
135
+ right: this.props.relativeToParent ? 0 : positioningBoundingRect.right,
136
+ bottom: this.props.relativeToParent ? 0 : positioningBoundingRect.bottom,
137
+ width: positioningBoundingRect.width,
138
+ height: positioningBoundingRect.height
139
+ },
140
+ popoverRect: this.popoverContentRef.current.getBoundingClientRect(),
141
+ scroll:
142
+ containerElement === document.body || !this.props.trackScroll
143
+ ? {left: 0, top: 0}
144
+ : {
145
+ left: containerElement.scrollLeft,
146
+ top: containerElement.scrollTop
147
+ }
148
+ })
149
+
150
+ window.requestAnimationFrame(() => {
151
+ if (this.mounted) {
152
+ this.setState({adjusted: true})
153
+ }
154
+ })
155
+ }
156
+
157
+ onResize = () => this.adjustPosition()
158
+
159
+ onScroll = throttle(() => {
160
+ this.adjustPosition()
161
+ }, this.props.scrollThrottleIntervalMs || 200)
162
+
163
+ componentDidMount() {
164
+ this.mounted = true
165
+ this.adjustPosition()
166
+
167
+ if (this.props.trackScroll) {
168
+ document.addEventListener('scroll', this.onScroll)
169
+ window.addEventListener('resize', this.onResize)
170
+ if (this.props.containerElement && this.props.containerElement !== document.body) {
171
+ this.props.containerElement.addEventListener('scroll', this.onScroll)
172
+ }
173
+ }
174
+
175
+ if (this.popoverContentRef.current && !this.props.trapFocus) {
176
+ if (!focusFirstDescendant(this.popoverContentRef.current)) {
177
+ this.popoverContentRef.current.tabIndex = -1
178
+ this.popoverContentRef.current.focus()
179
+ }
180
+ }
181
+ }
182
+
183
+ componentDidUpdate(previousProps) {
184
+ if (
185
+ this.props.containerElement !== previousProps.containerElement ||
186
+ this.props.getPopoverPosition !== previousProps.getPopoverPosition
187
+ ) {
188
+ this.adjustPosition()
189
+ }
190
+ }
191
+
192
+ componentWillUnmount() {
193
+ this.mounted = false
194
+ if (this.onScroll && this.props.trackScroll) {
195
+ document.body.removeEventListener('scroll', this.onScroll)
196
+ window.removeEventListener('resize', this.onResize)
197
+ if (this.props.containerElement && this.props.containerElement !== document.body) {
198
+ this.props.containerElement.removeEventListener('scroll', this.onScroll)
199
+ }
200
+ }
201
+ }
202
+
203
+ getPopoverTipPosition = (popoverPosition, containerPosition) => {
204
+ if (popoverPosition.bottom && popoverPosition.bottom >= containerPosition.height) {
205
+ return 'top'
206
+ }
207
+ if (popoverPosition.top && popoverPosition.top >= containerPosition.height) {
208
+ return 'bottom'
209
+ }
210
+ if (popoverPosition.left && popoverPosition.left >= containerPosition.width) {
211
+ return 'right'
212
+ }
213
+ if (popoverPosition.right && popoverPosition.right >= containerPosition.width) {
214
+ return 'left'
215
+ }
216
+
217
+ return null
218
+ }
219
+
220
+ handleKeyDown = event => {
221
+ if (event.key === 'Escape') {
222
+ event.stopPropagation()
223
+ this.props.onEscapeAction(event)
224
+ }
225
+ }
226
+
227
+ render() {
228
+ const getPopoverPosition =
229
+ typeof this.props.getPopoverPosition === 'function'
230
+ ? this.props.getPopoverPosition
231
+ : PopoverPosition[this.props.getPopoverPosition] || PopoverPosition.bottomLeft
232
+ const position = this.state.hostRect
233
+ ? getPopoverPosition(
234
+ this.state.hostRect,
235
+ this.state.popoverRect,
236
+ this.state.scroll,
237
+ this.props.containerElement
238
+ )
239
+ : {}
240
+
241
+ const containerPosition = {
242
+ left: this.state.hostRect ? this.state.hostRect.left : 0,
243
+ top: this.state.hostRect ? this.state.hostRect.top : 0,
244
+ width: this.state.hostRect && this.state.hostRect.width,
245
+ height: this.state.hostRect && this.state.hostRect.height
246
+ }
247
+
248
+ const tipPosition = this.getPopoverTipPosition(position, containerPosition)
249
+
250
+ // FIXME: position.top cast to number, because value may be not a number
251
+ return (
252
+ <div
253
+ style={{
254
+ position: 'absolute',
255
+ ...containerPosition,
256
+ pointerEvents: 'none',
257
+ zIndex: this.props.zIndex,
258
+ transition: '0.1s',
259
+ transitionProperty: 'opacity',
260
+ opacity: this.state.adjusted ? 1 : 0
261
+ }}
262
+ >
263
+ {this.props.showTip && tipPosition && (
264
+ <div
265
+ className={classNames(
266
+ styles['popover-tip'],
267
+ styles[`popover-tip--${tipPosition}`],
268
+ 'popover-tip'
269
+ )}
270
+ style={{
271
+ top: tipPosition === 'bottom' && (position.top as number) - 8,
272
+ bottom: tipPosition === 'top' && (position.bottom as number) - 8,
273
+ left: tipPosition === 'right' && (position.left as number) - 8,
274
+ right: tipPosition === 'left' && (position.right as number) - 8
275
+ }}
276
+ />
277
+ )}
278
+ {this.props.trapFocus && (
279
+ <FocusTrap
280
+ focusTrapOptions={{
281
+ fallbackFocus: `#${this.popoverContainerId}`,
282
+ clickOutsideDeactivates: true
283
+ }}
284
+ >
285
+ <div
286
+ id={this.popoverContainerId}
287
+ ref={this.popoverContentRef}
288
+ style={{
289
+ position: 'absolute',
290
+ pointerEvents: 'all',
291
+ ...position
292
+ }}
293
+ tabIndex={-1}
294
+ className={this.props.className}
295
+ role={this.props.role}
296
+ aria-modal={this.props.trapFocus ? 'true' : 'false'}
297
+ onKeyDown={this.handleKeyDown}
298
+ >
299
+ {this.props.children}
300
+ </div>
301
+ </FocusTrap>
302
+ )}
303
+
304
+ {!this.props.trapFocus && (
305
+ <div
306
+ id={this.popoverContainerId}
307
+ ref={this.popoverContentRef}
308
+ style={{
309
+ position: 'absolute',
310
+ pointerEvents: 'all',
311
+ ...position
312
+ }}
313
+ tabIndex={-1}
314
+ className={this.props.className}
315
+ role={this.props.role}
316
+ aria-modal={this.props.trapFocus ? 'true' : 'false'}
317
+ onKeyDown={this.handleKeyDown}
318
+ >
319
+ {this.props.children}
320
+ </div>
321
+ )}
322
+ </div>
323
+ )
324
+ }
325
+ }