@targetprocess/popover 12.19.6-alpha.0 → 12.20.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.
package/Popover.tsx CHANGED
@@ -84,6 +84,7 @@ export type PopoverProps = {
84
84
  className?: string
85
85
  trackScroll?: boolean
86
86
  role?: 'tooltip' | 'dialog'
87
+ trapFocus?: boolean
87
88
  }
88
89
 
89
90
  export type PopoverState = {
@@ -122,13 +123,16 @@ export class Popover extends React.Component<PopoverProps, PopoverState> {
122
123
  zIndex: PropTypes.number,
123
124
  trackScroll: PropTypes.bool,
124
125
  children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
125
- role: PropTypes.oneOf(['tooltip', 'dialog'])
126
+ role: PropTypes.oneOf(['tooltip', 'dialog']),
127
+ trapFocus: PropTypes.bool
126
128
  }
127
129
 
128
130
  static defaultProps = {
129
131
  zIndex: 19999,
130
132
  outerActionEvents: ['mousedown', 'touchstart'],
131
- getPopoverPosition: 'bottomLeft'
133
+ getPopoverPosition: 'bottomLeft',
134
+ role: 'dialog',
135
+ trapFocus: 'true'
132
136
  }
133
137
 
134
138
  state = {
@@ -175,6 +179,7 @@ export class Popover extends React.Component<PopoverProps, PopoverState> {
175
179
  onEscapeAction={this.onEscapeAction}
176
180
  isOpen={isOpen}
177
181
  role={this.props.role}
182
+ trapFocus={this.props.trapFocus}
178
183
  >
179
184
  {childrenWithRef}
180
185
  </StatelessPopover>
@@ -7,7 +7,10 @@ import {PopoverPosition} from './PopoverPosition'
7
7
  import styles from './style/index.css'
8
8
  import {DimensionParams, GetPopoverPositionType, PositionParams} from './types'
9
9
  import {throttle} from '../../base/utils/throttle'
10
- import {focusFirstDescendant} from '../../base/utils/ariaUtils'
10
+ import FocusTrap from 'focus-trap-react'
11
+ import {generateId, focusFirstDescendant} from '../../base/utils/ariaUtils'
12
+
13
+ const popoverContainerPrefix = 'popover-container-'
11
14
 
12
15
  type PopoverContainerProps = {
13
16
  containerElement?: HTMLElement
@@ -20,6 +23,7 @@ type PopoverContainerProps = {
20
23
  showTip?: boolean
21
24
  zIndex: number
22
25
  role?: 'tooltip' | 'dialog'
26
+ trapFocus?: boolean
23
27
  onEscapeAction: (event: Event) => void
24
28
  }
25
29
 
@@ -91,6 +95,7 @@ export class PopoverContainer extends React.Component<
91
95
  className: PropTypes.string,
92
96
  trackScroll: PropTypes.bool,
93
97
  role: PropTypes.oneOf(['tooltip', 'dialog']),
98
+ trapFocus: PropTypes.bool,
94
99
  onEscapeAction: PropTypes.func
95
100
  }
96
101
 
@@ -107,6 +112,8 @@ export class PopoverContainer extends React.Component<
107
112
  positionY: 0
108
113
  }
109
114
 
115
+ popoverContainerId = generateId(popoverContainerPrefix)
116
+
110
117
  adjustPosition() {
111
118
  if (!this.mounted) {
112
119
  return
@@ -154,13 +161,6 @@ export class PopoverContainer extends React.Component<
154
161
  this.mounted = true
155
162
  this.adjustPosition()
156
163
 
157
- if (this.popoverContentRef.current) {
158
- if (!focusFirstDescendant(this.popoverContentRef.current)) {
159
- this.popoverContentRef.current.tabIndex = 0
160
- this.popoverContentRef.current.focus()
161
- }
162
- }
163
-
164
164
  if (this.props.trackScroll) {
165
165
  document.addEventListener('scroll', this.onScroll)
166
166
  window.addEventListener('resize', this.onResize)
@@ -168,6 +168,13 @@ export class PopoverContainer extends React.Component<
168
168
  this.props.containerElement.addEventListener('scroll', this.onScroll)
169
169
  }
170
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
+ }
171
178
  }
172
179
 
173
180
  componentDidUpdate(previousProps) {
@@ -209,6 +216,7 @@ export class PopoverContainer extends React.Component<
209
216
 
210
217
  handleKeyDown = event => {
211
218
  if (event.key === 'Escape') {
219
+ event.stopPropagation()
212
220
  this.props.onEscapeAction(event)
213
221
  }
214
222
  }
@@ -264,20 +272,50 @@ export class PopoverContainer extends React.Component<
264
272
  }}
265
273
  />
266
274
  )}
267
- <div
268
- ref={this.popoverContentRef}
269
- aria-modal="true"
270
- style={{
271
- position: 'absolute',
272
- pointerEvents: 'all',
273
- ...position
274
- }}
275
- className={this.props.className}
276
- role={this.props.role}
277
- onKeyDown={this.handleKeyDown}
278
- >
279
- {this.props.children}
280
- </div>
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
+ )}
281
319
  </div>
282
320
  )
283
321
  }
@@ -19,6 +19,7 @@ export type StatelessPopoverProps = {
19
19
  onEscapeAction: (evt: Event) => void
20
20
  trackScroll?: boolean
21
21
  role?: 'tooltip' | 'dialog'
22
+ trapFocus?: boolean
22
23
  }
23
24
 
24
25
  export class StatelessPopover extends React.Component<StatelessPopoverProps> {
@@ -43,6 +44,7 @@ export class StatelessPopover extends React.Component<StatelessPopoverProps> {
43
44
  zIndex={props.zIndex}
44
45
  trackScroll={props.trackScroll}
45
46
  role={this.props.role}
47
+ trapFocus={this.props.trapFocus}
46
48
  onEscapeAction={this.props.onEscapeAction}
47
49
  >
48
50
  {props.body}
@@ -5,6 +5,16 @@ import {Popover, PopoverProps, PopoverState} from '../Popover'
5
5
  import {PopoverBody} from '../PopoverBody'
6
6
  import {cleanDocumentBody} from '../../../testUtils/domUtils'
7
7
 
8
+ jest.mock('focus-trap', () => {
9
+ const trap = {
10
+ activate: () => trap,
11
+ deactivate: () => trap,
12
+ pause: () => {},
13
+ unpause: () => {}
14
+ }
15
+ return () => trap
16
+ })
17
+
8
18
  describe('Popover', () => {
9
19
  beforeEach(() => {
10
20
  cleanDocumentBody()
package/dist/Popover.d.ts CHANGED
@@ -80,6 +80,7 @@ export declare type PopoverProps = {
80
80
  className?: string;
81
81
  trackScroll?: boolean;
82
82
  role?: 'tooltip' | 'dialog';
83
+ trapFocus?: boolean;
83
84
  };
84
85
  export declare type PopoverState = {
85
86
  isOpen: boolean;
@@ -113,11 +114,14 @@ export declare class Popover extends React.Component<PopoverProps, PopoverState>
113
114
  trackScroll: PropTypes.Requireable<boolean>;
114
115
  children: PropTypes.Requireable<PropTypes.ReactNodeLike>;
115
116
  role: PropTypes.Requireable<string>;
117
+ trapFocus: PropTypes.Requireable<boolean>;
116
118
  };
117
119
  static defaultProps: {
118
120
  zIndex: number;
119
121
  outerActionEvents: string[];
120
122
  getPopoverPosition: string;
123
+ role: string;
124
+ trapFocus: string;
121
125
  };
122
126
  state: {
123
127
  isOpen: boolean;
@@ -12,6 +12,7 @@ declare type PopoverContainerProps = {
12
12
  showTip?: boolean;
13
13
  zIndex: number;
14
14
  role?: 'tooltip' | 'dialog';
15
+ trapFocus?: boolean;
15
16
  onEscapeAction: (event: Event) => void;
16
17
  };
17
18
  declare type PopoverContainerState = {
@@ -29,6 +30,7 @@ export declare class PopoverContainer extends React.Component<PopoverContainerPr
29
30
  className: PropTypes.Requireable<string>;
30
31
  trackScroll: PropTypes.Requireable<boolean>;
31
32
  role: PropTypes.Requireable<string>;
33
+ trapFocus: PropTypes.Requireable<boolean>;
32
34
  onEscapeAction: PropTypes.Requireable<(...args: any[]) => any>;
33
35
  };
34
36
  static defaultProps: {
@@ -37,6 +39,7 @@ export declare class PopoverContainer extends React.Component<PopoverContainerPr
37
39
  mounted: boolean;
38
40
  popoverContentRef: React.RefObject<HTMLDivElement>;
39
41
  state: PopoverContainerState;
42
+ popoverContainerId: string;
40
43
  adjustPosition(): void;
41
44
  onResize: () => void;
42
45
  onScroll: (...args: any[]) => void;
@@ -14,6 +14,7 @@ export declare type StatelessPopoverProps = {
14
14
  onEscapeAction: (evt: Event) => void;
15
15
  trackScroll?: boolean;
16
16
  role?: 'tooltip' | 'dialog';
17
+ trapFocus?: boolean;
17
18
  };
18
19
  export declare class StatelessPopover extends React.Component<StatelessPopoverProps> {
19
20
  renderPortalContent({ props, container, positioningTarget }: {
@@ -2,6 +2,7 @@ import React, { createRef } from 'react';
2
2
  import { SpaceXS } from '@targetprocess/global-constants';
3
3
  import classNames from 'classnames';
4
4
  import ReactDOM from 'react-dom';
5
+ import FocusTrap from 'focus-trap-react';
5
6
 
6
7
  function _classCallCheck(instance, Constructor) {
7
8
  if (!(instance instanceof Constructor)) {
@@ -205,8 +206,8 @@ function styleInject(css, ref) {
205
206
  }
206
207
  }
207
208
 
208
- var css = ".popover-body-container__popover__12-19-6-alpha-0 {\r\n -webkit-box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\r\n box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\r\n border-radius: var(--BorderRadiusL, 4px);\r\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\r\n background: var(--BackgroundDefault, #FFFFFF);\r\n z-index: var(--ZIndexModal, 10001);\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n}\r\n\r\n.popover-body__popover__12-19-6-alpha-0 {\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n padding: var(--SpaceSM, 12px);\r\n font-size: var(--FontSizeDefault, 13px);\r\n color: var(--FontColorDefault, #000000);\r\n}\r\n\r\n.popover-body--no-padding__popover__12-19-6-alpha-0 {\r\n padding: 0;\r\n }\r\n\r\n.popover-tip__popover__12-19-6-alpha-0 {\r\n position: absolute;\r\n pointer-events: none;\r\n z-index: calc(var(--ZIndexModal, 10001) + 1);\r\n overflow: hidden;\r\n display: -webkit-box;\r\n display: -ms-flexbox;\r\n display: flex;\r\n -webkit-box-align: center;\r\n -ms-flex-align: center;\r\n align-items: center;\r\n -webkit-box-pack: center;\r\n -ms-flex-pack: center;\r\n justify-content: center;\r\n}\r\n\r\n.popover-tip__popover__12-19-6-alpha-0:before {\r\n content: '';\r\n position: absolute;\r\n display: block;\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n background-color: var(--BackgroundDefault, #FFFFFF);\r\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\r\n /*\r\n geometry FTW\r\n\r\n 2*$popoverTipSize\r\n _______ _______\r\n | /|\\ |\r\n | / | \\ | $popoverTipSize\r\n | / | \\ |\r\n |/______|______\\|\r\n since tip is rotated 45 degrees, its width and height is a\r\n diagonal of a square with side=$popoverTipSize, thus diagonal\r\n is sqrt(2) * $popoverTipSize.\r\n sqrt(2) ~= 1.414, 1.4 used here to prevent too much rounding\r\n */\r\n width: 11.2px;\r\n height: 11.2px;\r\n -webkit-transform: rotate(45deg);\r\n transform: rotate(45deg);\r\n }\r\n\r\n.popover-tip--bottom__popover__12-19-6-alpha-0,\r\n .popover-tip--top__popover__12-19-6-alpha-0 {\r\n width: 100%;\r\n border-top: 1px var(--BackgroundDefault, #FFFFFF);\r\n border-bottom: 1px var(--BackgroundDefault, #FFFFFF);\r\n height: 9px;\r\n }\r\n\r\n.popover-tip--left__popover__12-19-6-alpha-0,\r\n .popover-tip--right__popover__12-19-6-alpha-0 {\r\n border-left: 1px var(--BackgroundDefault, #FFFFFF);\r\n border-right: 1px var(--BackgroundDefault, #FFFFFF);\r\n height: 100%;\r\n width: 9px;\r\n }\r\n\r\n/*\r\n To determine tip position, we need to rotate it\r\n along the \"tip\" point, after that, the \"tip\" point\r\n can be placed with exactly 50% offset from the origin\r\n so that it is centered.\r\n */\r\n\r\n.popover-tip--bottom__popover__12-19-6-alpha-0:before {\r\n -webkit-transform-origin: left top;\r\n transform-origin: left top;\r\n left: 50%;\r\n top: 0;\r\n }\r\n\r\n.popover-tip--top__popover__12-19-6-alpha-0:before {\r\n -webkit-transform-origin: right bottom;\r\n transform-origin: right bottom;\r\n right: 50%;\r\n bottom: 0;\r\n }\r\n\r\n.popover-tip--left__popover__12-19-6-alpha-0:before {\r\n -webkit-transform-origin: right top;\r\n transform-origin: right top;\r\n right: 0;\r\n top: 50%;\r\n }\r\n\r\n.popover-tip--right__popover__12-19-6-alpha-0:before {\r\n -webkit-transform-origin: left bottom;\r\n transform-origin: left bottom;\r\n left: 0;\r\n bottom: 50%;\r\n }\r\n";
209
- var styles = {"popover-body-container":"popover-body-container__popover__12-19-6-alpha-0","popover-body":"popover-body__popover__12-19-6-alpha-0","popover-body--no-padding":"popover-body--no-padding__popover__12-19-6-alpha-0","popover-tip":"popover-tip__popover__12-19-6-alpha-0","popover-tip--bottom":"popover-tip--bottom__popover__12-19-6-alpha-0","popover-tip--top":"popover-tip--top__popover__12-19-6-alpha-0","popover-tip--left":"popover-tip--left__popover__12-19-6-alpha-0","popover-tip--right":"popover-tip--right__popover__12-19-6-alpha-0"};
209
+ var css = ".popover-body-container__popover__12-20-0 {\r\n -webkit-box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\r\n box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\r\n border-radius: var(--BorderRadiusL, 4px);\r\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\r\n background: var(--BackgroundDefault, #FFFFFF);\r\n z-index: var(--ZIndexModal, 10001);\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n}\r\n\r\n.popover-body__popover__12-20-0 {\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n padding: var(--SpaceSM, 12px);\r\n font-size: var(--FontSizeDefault, 13px);\r\n color: var(--FontColorDefault, #000000);\r\n}\r\n\r\n.popover-body--no-padding__popover__12-20-0 {\r\n padding: 0;\r\n }\r\n\r\n.popover-tip__popover__12-20-0 {\r\n position: absolute;\r\n pointer-events: none;\r\n z-index: calc(var(--ZIndexModal, 10001) + 1);\r\n overflow: hidden;\r\n display: -webkit-box;\r\n display: -ms-flexbox;\r\n display: flex;\r\n -webkit-box-align: center;\r\n -ms-flex-align: center;\r\n align-items: center;\r\n -webkit-box-pack: center;\r\n -ms-flex-pack: center;\r\n justify-content: center;\r\n}\r\n\r\n.popover-tip__popover__12-20-0:before {\r\n content: '';\r\n position: absolute;\r\n display: block;\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n background-color: var(--BackgroundDefault, #FFFFFF);\r\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\r\n /*\r\n geometry FTW\r\n\r\n 2*$popoverTipSize\r\n _______ _______\r\n | /|\\ |\r\n | / | \\ | $popoverTipSize\r\n | / | \\ |\r\n |/______|______\\|\r\n since tip is rotated 45 degrees, its width and height is a\r\n diagonal of a square with side=$popoverTipSize, thus diagonal\r\n is sqrt(2) * $popoverTipSize.\r\n sqrt(2) ~= 1.414, 1.4 used here to prevent too much rounding\r\n */\r\n width: 11.2px;\r\n height: 11.2px;\r\n -webkit-transform: rotate(45deg);\r\n transform: rotate(45deg);\r\n }\r\n\r\n.popover-tip--bottom__popover__12-20-0,\r\n .popover-tip--top__popover__12-20-0 {\r\n width: 100%;\r\n border-top: 1px var(--BackgroundDefault, #FFFFFF);\r\n border-bottom: 1px var(--BackgroundDefault, #FFFFFF);\r\n height: 9px;\r\n }\r\n\r\n.popover-tip--left__popover__12-20-0,\r\n .popover-tip--right__popover__12-20-0 {\r\n border-left: 1px var(--BackgroundDefault, #FFFFFF);\r\n border-right: 1px var(--BackgroundDefault, #FFFFFF);\r\n height: 100%;\r\n width: 9px;\r\n }\r\n\r\n/*\r\n To determine tip position, we need to rotate it\r\n along the \"tip\" point, after that, the \"tip\" point\r\n can be placed with exactly 50% offset from the origin\r\n so that it is centered.\r\n */\r\n\r\n.popover-tip--bottom__popover__12-20-0:before {\r\n -webkit-transform-origin: left top;\r\n transform-origin: left top;\r\n left: 50%;\r\n top: 0;\r\n }\r\n\r\n.popover-tip--top__popover__12-20-0:before {\r\n -webkit-transform-origin: right bottom;\r\n transform-origin: right bottom;\r\n right: 50%;\r\n bottom: 0;\r\n }\r\n\r\n.popover-tip--left__popover__12-20-0:before {\r\n -webkit-transform-origin: right top;\r\n transform-origin: right top;\r\n right: 0;\r\n top: 50%;\r\n }\r\n\r\n.popover-tip--right__popover__12-20-0:before {\r\n -webkit-transform-origin: left bottom;\r\n transform-origin: left bottom;\r\n left: 0;\r\n bottom: 50%;\r\n }\r\n";
210
+ var styles = {"popover-body-container":"popover-body-container__popover__12-20-0","popover-body":"popover-body__popover__12-20-0","popover-body--no-padding":"popover-body--no-padding__popover__12-20-0","popover-tip":"popover-tip__popover__12-20-0","popover-tip--bottom":"popover-tip--bottom__popover__12-20-0","popover-tip--top":"popover-tip--top__popover__12-20-0","popover-tip--left":"popover-tip--left__popover__12-20-0","popover-tip--right":"popover-tip--right__popover__12-20-0"};
210
211
  styleInject(css);
211
212
 
212
213
  var PopoverBody = function PopoverBody(_ref) {
@@ -243,6 +244,9 @@ var throttle = function throttle(func, limit) {
243
244
  };
244
245
  };
245
246
 
247
+ var generateId = function generateId(prefix) {
248
+ return "".concat(prefix).concat(Math.random().toString(36).substring(7));
249
+ };
246
250
  var isFocusable = function isFocusable(element) {
247
251
  if (element.tabIndex < 0) {
248
252
  return false;
@@ -250,6 +254,9 @@ var isFocusable = function isFocusable(element) {
250
254
  if ('disabled' in element && element.disabled) {
251
255
  return false;
252
256
  }
257
+ if (element.tabIndex === 0) {
258
+ return true;
259
+ }
253
260
  switch (element.nodeName) {
254
261
  case 'A':
255
262
  return element.href;
@@ -281,6 +288,7 @@ var focusFirstDescendant = function focusFirstDescendant(element) {
281
288
  return false;
282
289
  };
283
290
 
291
+ var popoverContainerPrefix = 'popover-container-';
284
292
  var isScrollParent = function isScrollParent(element) {
285
293
  // Firefox wants us to check `-x` and `-y` variations as well
286
294
  var _getComputedStyle = getComputedStyle(element),
@@ -340,6 +348,7 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
340
348
  positionX: 0,
341
349
  positionY: 0
342
350
  };
351
+ _this.popoverContainerId = generateId(popoverContainerPrefix);
343
352
  _this.onResize = function () {
344
353
  return _this.adjustPosition();
345
354
  };
@@ -363,6 +372,7 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
363
372
  };
364
373
  _this.handleKeyDown = function (event) {
365
374
  if (event.key === 'Escape') {
375
+ event.stopPropagation();
366
376
  _this.props.onEscapeAction(event);
367
377
  }
368
378
  };
@@ -408,12 +418,6 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
408
418
  value: function componentDidMount() {
409
419
  this.mounted = true;
410
420
  this.adjustPosition();
411
- if (this.popoverContentRef.current) {
412
- if (!focusFirstDescendant(this.popoverContentRef.current)) {
413
- this.popoverContentRef.current.tabIndex = 0;
414
- this.popoverContentRef.current.focus();
415
- }
416
- }
417
421
  if (this.props.trackScroll) {
418
422
  document.addEventListener('scroll', this.onScroll);
419
423
  window.addEventListener('resize', this.onResize);
@@ -421,6 +425,12 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
421
425
  this.props.containerElement.addEventListener('scroll', this.onScroll);
422
426
  }
423
427
  }
428
+ if (this.popoverContentRef.current && !this.props.trapFocus) {
429
+ if (!focusFirstDescendant(this.popoverContentRef.current)) {
430
+ this.popoverContentRef.current.tabIndex = -1;
431
+ this.popoverContentRef.current.focus();
432
+ }
433
+ }
424
434
  }
425
435
  }, {
426
436
  key: "componentDidUpdate",
@@ -472,15 +482,34 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
472
482
  left: tipPosition === 'right' && position.left - 8,
473
483
  right: tipPosition === 'left' && position.right - 8
474
484
  }
475
- }), React.createElement("div", {
485
+ }), this.props.trapFocus && React.createElement(FocusTrap, {
486
+ focusTrapOptions: {
487
+ fallbackFocus: "#".concat(this.popoverContainerId),
488
+ clickOutsideDeactivates: true
489
+ }
490
+ }, React.createElement("div", {
491
+ id: this.popoverContainerId,
492
+ ref: this.popoverContentRef,
493
+ style: _extends({
494
+ position: 'absolute',
495
+ pointerEvents: 'all'
496
+ }, position),
497
+ tabIndex: -1,
498
+ className: this.props.className,
499
+ role: this.props.role,
500
+ "aria-modal": this.props.trapFocus ? 'true' : 'false',
501
+ onKeyDown: this.handleKeyDown
502
+ }, this.props.children)), !this.props.trapFocus && React.createElement("div", {
503
+ id: this.popoverContainerId,
476
504
  ref: this.popoverContentRef,
477
- "aria-modal": "true",
478
505
  style: _extends({
479
506
  position: 'absolute',
480
507
  pointerEvents: 'all'
481
508
  }, position),
509
+ tabIndex: -1,
482
510
  className: this.props.className,
483
511
  role: this.props.role,
512
+ "aria-modal": this.props.trapFocus ? 'true' : 'false',
484
513
  onKeyDown: this.handleKeyDown
485
514
  }, this.props.children));
486
515
  }
@@ -617,6 +646,7 @@ var StatelessPopover = /*#__PURE__*/function (_React$Component) {
617
646
  zIndex: props.zIndex,
618
647
  trackScroll: props.trackScroll,
619
648
  role: this.props.role,
649
+ trapFocus: this.props.trapFocus,
620
650
  onEscapeAction: this.props.onEscapeAction
621
651
  }, props.body);
622
652
  if (!props.inline) {
@@ -711,7 +741,8 @@ var Popover = /*#__PURE__*/function (_React$Component) {
711
741
  onOuterAction: this.onOuterAction,
712
742
  onEscapeAction: this.onEscapeAction,
713
743
  isOpen: isOpen,
714
- role: this.props.role
744
+ role: this.props.role,
745
+ trapFocus: this.props.trapFocus
715
746
  }), childrenWithRef);
716
747
  }
717
748
  }]);
@@ -722,7 +753,9 @@ Popover.Position = PopoverPosition;
722
753
  Popover.defaultProps = {
723
754
  zIndex: 19999,
724
755
  outerActionEvents: ['mousedown', 'touchstart'],
725
- getPopoverPosition: 'bottomLeft'
756
+ getPopoverPosition: 'bottomLeft',
757
+ role: 'dialog',
758
+ trapFocus: 'true'
726
759
  };
727
760
 
728
761
  export default Popover;
package/dist/popover.js CHANGED
@@ -9,6 +9,7 @@ var React__default = _interopDefault(React);
9
9
  var globalConstants = require('@targetprocess/global-constants');
10
10
  var classNames = _interopDefault(require('classnames'));
11
11
  var ReactDOM = _interopDefault(require('react-dom'));
12
+ var FocusTrap = _interopDefault(require('focus-trap-react'));
12
13
 
13
14
  function _classCallCheck(instance, Constructor) {
14
15
  if (!(instance instanceof Constructor)) {
@@ -212,8 +213,8 @@ function styleInject(css, ref) {
212
213
  }
213
214
  }
214
215
 
215
- var css = ".popover-body-container__popover__12-19-6-alpha-0 {\r\n -webkit-box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\r\n box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\r\n border-radius: var(--BorderRadiusL, 4px);\r\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\r\n background: var(--BackgroundDefault, #FFFFFF);\r\n z-index: var(--ZIndexModal, 10001);\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n}\r\n\r\n.popover-body__popover__12-19-6-alpha-0 {\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n padding: var(--SpaceSM, 12px);\r\n font-size: var(--FontSizeDefault, 13px);\r\n color: var(--FontColorDefault, #000000);\r\n}\r\n\r\n.popover-body--no-padding__popover__12-19-6-alpha-0 {\r\n padding: 0;\r\n }\r\n\r\n.popover-tip__popover__12-19-6-alpha-0 {\r\n position: absolute;\r\n pointer-events: none;\r\n z-index: calc(var(--ZIndexModal, 10001) + 1);\r\n overflow: hidden;\r\n display: -webkit-box;\r\n display: -ms-flexbox;\r\n display: flex;\r\n -webkit-box-align: center;\r\n -ms-flex-align: center;\r\n align-items: center;\r\n -webkit-box-pack: center;\r\n -ms-flex-pack: center;\r\n justify-content: center;\r\n}\r\n\r\n.popover-tip__popover__12-19-6-alpha-0:before {\r\n content: '';\r\n position: absolute;\r\n display: block;\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n background-color: var(--BackgroundDefault, #FFFFFF);\r\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\r\n /*\r\n geometry FTW\r\n\r\n 2*$popoverTipSize\r\n _______ _______\r\n | /|\\ |\r\n | / | \\ | $popoverTipSize\r\n | / | \\ |\r\n |/______|______\\|\r\n since tip is rotated 45 degrees, its width and height is a\r\n diagonal of a square with side=$popoverTipSize, thus diagonal\r\n is sqrt(2) * $popoverTipSize.\r\n sqrt(2) ~= 1.414, 1.4 used here to prevent too much rounding\r\n */\r\n width: 11.2px;\r\n height: 11.2px;\r\n -webkit-transform: rotate(45deg);\r\n transform: rotate(45deg);\r\n }\r\n\r\n.popover-tip--bottom__popover__12-19-6-alpha-0,\r\n .popover-tip--top__popover__12-19-6-alpha-0 {\r\n width: 100%;\r\n border-top: 1px var(--BackgroundDefault, #FFFFFF);\r\n border-bottom: 1px var(--BackgroundDefault, #FFFFFF);\r\n height: 9px;\r\n }\r\n\r\n.popover-tip--left__popover__12-19-6-alpha-0,\r\n .popover-tip--right__popover__12-19-6-alpha-0 {\r\n border-left: 1px var(--BackgroundDefault, #FFFFFF);\r\n border-right: 1px var(--BackgroundDefault, #FFFFFF);\r\n height: 100%;\r\n width: 9px;\r\n }\r\n\r\n/*\r\n To determine tip position, we need to rotate it\r\n along the \"tip\" point, after that, the \"tip\" point\r\n can be placed with exactly 50% offset from the origin\r\n so that it is centered.\r\n */\r\n\r\n.popover-tip--bottom__popover__12-19-6-alpha-0:before {\r\n -webkit-transform-origin: left top;\r\n transform-origin: left top;\r\n left: 50%;\r\n top: 0;\r\n }\r\n\r\n.popover-tip--top__popover__12-19-6-alpha-0:before {\r\n -webkit-transform-origin: right bottom;\r\n transform-origin: right bottom;\r\n right: 50%;\r\n bottom: 0;\r\n }\r\n\r\n.popover-tip--left__popover__12-19-6-alpha-0:before {\r\n -webkit-transform-origin: right top;\r\n transform-origin: right top;\r\n right: 0;\r\n top: 50%;\r\n }\r\n\r\n.popover-tip--right__popover__12-19-6-alpha-0:before {\r\n -webkit-transform-origin: left bottom;\r\n transform-origin: left bottom;\r\n left: 0;\r\n bottom: 50%;\r\n }\r\n";
216
- var styles = {"popover-body-container":"popover-body-container__popover__12-19-6-alpha-0","popover-body":"popover-body__popover__12-19-6-alpha-0","popover-body--no-padding":"popover-body--no-padding__popover__12-19-6-alpha-0","popover-tip":"popover-tip__popover__12-19-6-alpha-0","popover-tip--bottom":"popover-tip--bottom__popover__12-19-6-alpha-0","popover-tip--top":"popover-tip--top__popover__12-19-6-alpha-0","popover-tip--left":"popover-tip--left__popover__12-19-6-alpha-0","popover-tip--right":"popover-tip--right__popover__12-19-6-alpha-0"};
216
+ var css = ".popover-body-container__popover__12-20-0 {\r\n -webkit-box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\r\n box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\r\n border-radius: var(--BorderRadiusL, 4px);\r\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\r\n background: var(--BackgroundDefault, #FFFFFF);\r\n z-index: var(--ZIndexModal, 10001);\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n}\r\n\r\n.popover-body__popover__12-20-0 {\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n padding: var(--SpaceSM, 12px);\r\n font-size: var(--FontSizeDefault, 13px);\r\n color: var(--FontColorDefault, #000000);\r\n}\r\n\r\n.popover-body--no-padding__popover__12-20-0 {\r\n padding: 0;\r\n }\r\n\r\n.popover-tip__popover__12-20-0 {\r\n position: absolute;\r\n pointer-events: none;\r\n z-index: calc(var(--ZIndexModal, 10001) + 1);\r\n overflow: hidden;\r\n display: -webkit-box;\r\n display: -ms-flexbox;\r\n display: flex;\r\n -webkit-box-align: center;\r\n -ms-flex-align: center;\r\n align-items: center;\r\n -webkit-box-pack: center;\r\n -ms-flex-pack: center;\r\n justify-content: center;\r\n}\r\n\r\n.popover-tip__popover__12-20-0:before {\r\n content: '';\r\n position: absolute;\r\n display: block;\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n background-color: var(--BackgroundDefault, #FFFFFF);\r\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\r\n /*\r\n geometry FTW\r\n\r\n 2*$popoverTipSize\r\n _______ _______\r\n | /|\\ |\r\n | / | \\ | $popoverTipSize\r\n | / | \\ |\r\n |/______|______\\|\r\n since tip is rotated 45 degrees, its width and height is a\r\n diagonal of a square with side=$popoverTipSize, thus diagonal\r\n is sqrt(2) * $popoverTipSize.\r\n sqrt(2) ~= 1.414, 1.4 used here to prevent too much rounding\r\n */\r\n width: 11.2px;\r\n height: 11.2px;\r\n -webkit-transform: rotate(45deg);\r\n transform: rotate(45deg);\r\n }\r\n\r\n.popover-tip--bottom__popover__12-20-0,\r\n .popover-tip--top__popover__12-20-0 {\r\n width: 100%;\r\n border-top: 1px var(--BackgroundDefault, #FFFFFF);\r\n border-bottom: 1px var(--BackgroundDefault, #FFFFFF);\r\n height: 9px;\r\n }\r\n\r\n.popover-tip--left__popover__12-20-0,\r\n .popover-tip--right__popover__12-20-0 {\r\n border-left: 1px var(--BackgroundDefault, #FFFFFF);\r\n border-right: 1px var(--BackgroundDefault, #FFFFFF);\r\n height: 100%;\r\n width: 9px;\r\n }\r\n\r\n/*\r\n To determine tip position, we need to rotate it\r\n along the \"tip\" point, after that, the \"tip\" point\r\n can be placed with exactly 50% offset from the origin\r\n so that it is centered.\r\n */\r\n\r\n.popover-tip--bottom__popover__12-20-0:before {\r\n -webkit-transform-origin: left top;\r\n transform-origin: left top;\r\n left: 50%;\r\n top: 0;\r\n }\r\n\r\n.popover-tip--top__popover__12-20-0:before {\r\n -webkit-transform-origin: right bottom;\r\n transform-origin: right bottom;\r\n right: 50%;\r\n bottom: 0;\r\n }\r\n\r\n.popover-tip--left__popover__12-20-0:before {\r\n -webkit-transform-origin: right top;\r\n transform-origin: right top;\r\n right: 0;\r\n top: 50%;\r\n }\r\n\r\n.popover-tip--right__popover__12-20-0:before {\r\n -webkit-transform-origin: left bottom;\r\n transform-origin: left bottom;\r\n left: 0;\r\n bottom: 50%;\r\n }\r\n";
217
+ var styles = {"popover-body-container":"popover-body-container__popover__12-20-0","popover-body":"popover-body__popover__12-20-0","popover-body--no-padding":"popover-body--no-padding__popover__12-20-0","popover-tip":"popover-tip__popover__12-20-0","popover-tip--bottom":"popover-tip--bottom__popover__12-20-0","popover-tip--top":"popover-tip--top__popover__12-20-0","popover-tip--left":"popover-tip--left__popover__12-20-0","popover-tip--right":"popover-tip--right__popover__12-20-0"};
217
218
  styleInject(css);
218
219
 
219
220
  var PopoverBody = function PopoverBody(_ref) {
@@ -250,6 +251,9 @@ var throttle = function throttle(func, limit) {
250
251
  };
251
252
  };
252
253
 
254
+ var generateId = function generateId(prefix) {
255
+ return "".concat(prefix).concat(Math.random().toString(36).substring(7));
256
+ };
253
257
  var isFocusable = function isFocusable(element) {
254
258
  if (element.tabIndex < 0) {
255
259
  return false;
@@ -257,6 +261,9 @@ var isFocusable = function isFocusable(element) {
257
261
  if ('disabled' in element && element.disabled) {
258
262
  return false;
259
263
  }
264
+ if (element.tabIndex === 0) {
265
+ return true;
266
+ }
260
267
  switch (element.nodeName) {
261
268
  case 'A':
262
269
  return element.href;
@@ -288,6 +295,7 @@ var focusFirstDescendant = function focusFirstDescendant(element) {
288
295
  return false;
289
296
  };
290
297
 
298
+ var popoverContainerPrefix = 'popover-container-';
291
299
  var isScrollParent = function isScrollParent(element) {
292
300
  // Firefox wants us to check `-x` and `-y` variations as well
293
301
  var _getComputedStyle = getComputedStyle(element),
@@ -347,6 +355,7 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
347
355
  positionX: 0,
348
356
  positionY: 0
349
357
  };
358
+ _this.popoverContainerId = generateId(popoverContainerPrefix);
350
359
  _this.onResize = function () {
351
360
  return _this.adjustPosition();
352
361
  };
@@ -370,6 +379,7 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
370
379
  };
371
380
  _this.handleKeyDown = function (event) {
372
381
  if (event.key === 'Escape') {
382
+ event.stopPropagation();
373
383
  _this.props.onEscapeAction(event);
374
384
  }
375
385
  };
@@ -415,12 +425,6 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
415
425
  value: function componentDidMount() {
416
426
  this.mounted = true;
417
427
  this.adjustPosition();
418
- if (this.popoverContentRef.current) {
419
- if (!focusFirstDescendant(this.popoverContentRef.current)) {
420
- this.popoverContentRef.current.tabIndex = 0;
421
- this.popoverContentRef.current.focus();
422
- }
423
- }
424
428
  if (this.props.trackScroll) {
425
429
  document.addEventListener('scroll', this.onScroll);
426
430
  window.addEventListener('resize', this.onResize);
@@ -428,6 +432,12 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
428
432
  this.props.containerElement.addEventListener('scroll', this.onScroll);
429
433
  }
430
434
  }
435
+ if (this.popoverContentRef.current && !this.props.trapFocus) {
436
+ if (!focusFirstDescendant(this.popoverContentRef.current)) {
437
+ this.popoverContentRef.current.tabIndex = -1;
438
+ this.popoverContentRef.current.focus();
439
+ }
440
+ }
431
441
  }
432
442
  }, {
433
443
  key: "componentDidUpdate",
@@ -479,15 +489,34 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
479
489
  left: tipPosition === 'right' && position.left - 8,
480
490
  right: tipPosition === 'left' && position.right - 8
481
491
  }
482
- }), React__default.createElement("div", {
492
+ }), this.props.trapFocus && React__default.createElement(FocusTrap, {
493
+ focusTrapOptions: {
494
+ fallbackFocus: "#".concat(this.popoverContainerId),
495
+ clickOutsideDeactivates: true
496
+ }
497
+ }, React__default.createElement("div", {
498
+ id: this.popoverContainerId,
499
+ ref: this.popoverContentRef,
500
+ style: _extends({
501
+ position: 'absolute',
502
+ pointerEvents: 'all'
503
+ }, position),
504
+ tabIndex: -1,
505
+ className: this.props.className,
506
+ role: this.props.role,
507
+ "aria-modal": this.props.trapFocus ? 'true' : 'false',
508
+ onKeyDown: this.handleKeyDown
509
+ }, this.props.children)), !this.props.trapFocus && React__default.createElement("div", {
510
+ id: this.popoverContainerId,
483
511
  ref: this.popoverContentRef,
484
- "aria-modal": "true",
485
512
  style: _extends({
486
513
  position: 'absolute',
487
514
  pointerEvents: 'all'
488
515
  }, position),
516
+ tabIndex: -1,
489
517
  className: this.props.className,
490
518
  role: this.props.role,
519
+ "aria-modal": this.props.trapFocus ? 'true' : 'false',
491
520
  onKeyDown: this.handleKeyDown
492
521
  }, this.props.children));
493
522
  }
@@ -624,6 +653,7 @@ var StatelessPopover = /*#__PURE__*/function (_React$Component) {
624
653
  zIndex: props.zIndex,
625
654
  trackScroll: props.trackScroll,
626
655
  role: this.props.role,
656
+ trapFocus: this.props.trapFocus,
627
657
  onEscapeAction: this.props.onEscapeAction
628
658
  }, props.body);
629
659
  if (!props.inline) {
@@ -718,7 +748,8 @@ var Popover = /*#__PURE__*/function (_React$Component) {
718
748
  onOuterAction: this.onOuterAction,
719
749
  onEscapeAction: this.onEscapeAction,
720
750
  isOpen: isOpen,
721
- role: this.props.role
751
+ role: this.props.role,
752
+ trapFocus: this.props.trapFocus
722
753
  }), childrenWithRef);
723
754
  }
724
755
  }]);
@@ -729,7 +760,9 @@ Popover.Position = PopoverPosition;
729
760
  Popover.defaultProps = {
730
761
  zIndex: 19999,
731
762
  outerActionEvents: ['mousedown', 'touchstart'],
732
- getPopoverPosition: 'bottomLeft'
763
+ getPopoverPosition: 'bottomLeft',
764
+ role: 'dialog',
765
+ trapFocus: 'true'
733
766
  };
734
767
 
735
768
  exports.Popover = Popover;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@targetprocess/popover",
3
- "version": "12.19.6-alpha.0",
3
+ "version": "12.20.0",
4
4
  "description": "",
5
5
  "main": "dist/popover.js",
6
6
  "module": "dist/popover.es.js",
@@ -12,8 +12,9 @@
12
12
  "prepack": "node -r ../../.ts-node.js ../../config/rollup.config.js ."
13
13
  },
14
14
  "dependencies": {
15
- "@targetprocess/global-constants": "^12.19.6-alpha.0",
15
+ "@targetprocess/global-constants": "^12.20.0",
16
16
  "classnames": "^2.2.5",
17
+ "focus-trap-react": "^7.0.0",
17
18
  "prop-types": "^15.6.0",
18
19
  "react": "^16.7.0",
19
20
  "react-dom": "^16.7.0"
@@ -21,5 +22,5 @@
21
22
  "publishConfig": {
22
23
  "access": "public"
23
24
  },
24
- "gitHead": "09c2de637be6694aef61a9dbe734ecb9cc9e8e59"
25
+ "gitHead": "dc1b7267cd32ccd122ffd6a73ff18345d551b919"
25
26
  }