@targetprocess/popover 12.19.2-alpha.3 → 12.19.2-alpha.6

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
@@ -1,4 +1,4 @@
1
- import React from 'react'
1
+ import React, {createRef} from 'react'
2
2
  import PropTypes from 'prop-types'
3
3
  import {PopoverPosition} from './PopoverPosition'
4
4
  import {PopoverBody} from './PopoverBody'
@@ -148,21 +148,35 @@ export class Popover extends React.Component<PopoverProps, PopoverState> {
148
148
  }
149
149
  }
150
150
 
151
+ onEscapeAction = (event: Event) => {
152
+ this.setOpened(false)
153
+ if (this.props.onOuterAction) {
154
+ this.props.onOuterAction(event)
155
+ }
156
+ this.childrenRef.current?.focus()
157
+ }
158
+
159
+ childrenRef = createRef<HTMLElement>()
160
+
151
161
  render() {
152
162
  const isOpen = typeof this.props.isOpen === 'undefined' ? this.state.isOpen : this.props.isOpen
153
163
  const children =
154
164
  typeof this.props.children === 'function'
155
- ? this.props.children({isOpen, setOpened: this.setOpened})
165
+ ? this.props.children({isOpen, setOpened: this.setOpened, ref: this.childrenRef})
156
166
  : this.props.children
167
+ const childrenWithRef = React.Children.map(children, child =>
168
+ React.cloneElement(child, {ref: this.childrenRef})
169
+ )
157
170
 
158
171
  return (
159
172
  <StatelessPopover
160
173
  {...this.props}
161
174
  onOuterAction={this.onOuterAction}
175
+ onEscapeAction={this.onEscapeAction}
162
176
  isOpen={isOpen}
163
177
  role={this.props.role}
164
178
  >
165
- {children}
179
+ {childrenWithRef}
166
180
  </StatelessPopover>
167
181
  )
168
182
  }
@@ -7,6 +7,7 @@ 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
11
 
11
12
  type PopoverContainerProps = {
12
13
  containerElement?: HTMLElement
@@ -19,6 +20,7 @@ type PopoverContainerProps = {
19
20
  showTip?: boolean
20
21
  zIndex: number
21
22
  role?: 'tooltip' | 'dialog'
23
+ onEscapeAction: (event: Event) => void
22
24
  }
23
25
 
24
26
  type PopoverContainerState = {
@@ -88,7 +90,8 @@ export class PopoverContainer extends React.Component<
88
90
  containerElement: PropTypes.object,
89
91
  className: PropTypes.string,
90
92
  trackScroll: PropTypes.bool,
91
- role: PropTypes.oneOf(['tooltip', 'dialog'])
93
+ role: PropTypes.oneOf(['tooltip', 'dialog']),
94
+ onEscapeAction: PropTypes.func
92
95
  }
93
96
 
94
97
  static defaultProps = {
@@ -151,8 +154,11 @@ export class PopoverContainer extends React.Component<
151
154
  this.mounted = true
152
155
  this.adjustPosition()
153
156
 
154
- if (this.popoverContentRef) {
155
- this.popoverContentRef.current.focus()
157
+ if (this.popoverContentRef.current) {
158
+ if (!focusFirstDescendant(this.popoverContentRef.current)) {
159
+ this.popoverContentRef.current.tabIndex = 0
160
+ this.popoverContentRef.current.focus()
161
+ }
156
162
  }
157
163
 
158
164
  if (this.props.trackScroll) {
@@ -201,6 +207,12 @@ export class PopoverContainer extends React.Component<
201
207
  return null
202
208
  }
203
209
 
210
+ handleKeyDown = event => {
211
+ if (event.key === 'Escape') {
212
+ this.props.onEscapeAction(event)
213
+ }
214
+ }
215
+
204
216
  render() {
205
217
  const getPopoverPosition =
206
218
  typeof this.props.getPopoverPosition === 'function'
@@ -262,7 +274,7 @@ export class PopoverContainer extends React.Component<
262
274
  }}
263
275
  className={this.props.className}
264
276
  role={this.props.role}
265
- tabIndex={0}
277
+ onKeyDown={this.handleKeyDown}
266
278
  >
267
279
  {this.props.children}
268
280
  </div>
@@ -16,6 +16,7 @@ export type StatelessPopoverProps = {
16
16
  className?: string
17
17
  outerActionEvents?: string[]
18
18
  onOuterAction?: (evt: Event) => void
19
+ onEscapeAction: (evt: Event) => void
19
20
  trackScroll?: boolean
20
21
  role?: 'tooltip' | 'dialog'
21
22
  }
@@ -42,6 +43,7 @@ export class StatelessPopover extends React.Component<StatelessPopoverProps> {
42
43
  zIndex={props.zIndex}
43
44
  trackScroll={props.trackScroll}
44
45
  role={this.props.role}
46
+ onEscapeAction={this.props.onEscapeAction}
45
47
  >
46
48
  {props.body}
47
49
  </PopoverContainer>
package/dist/Popover.d.ts CHANGED
@@ -124,5 +124,7 @@ export declare class Popover extends React.Component<PopoverProps, PopoverState>
124
124
  };
125
125
  setOpened: (isOpen: boolean) => void;
126
126
  onOuterAction: (event: Event) => void;
127
+ onEscapeAction: (event: Event) => void;
128
+ childrenRef: React.RefObject<HTMLElement>;
127
129
  render(): JSX.Element;
128
130
  }
@@ -12,6 +12,7 @@ declare type PopoverContainerProps = {
12
12
  showTip?: boolean;
13
13
  zIndex: number;
14
14
  role?: 'tooltip' | 'dialog';
15
+ onEscapeAction: (event: Event) => void;
15
16
  };
16
17
  declare type PopoverContainerState = {
17
18
  adjusted: boolean;
@@ -28,6 +29,7 @@ export declare class PopoverContainer extends React.Component<PopoverContainerPr
28
29
  className: PropTypes.Requireable<string>;
29
30
  trackScroll: PropTypes.Requireable<boolean>;
30
31
  role: PropTypes.Requireable<string>;
32
+ onEscapeAction: PropTypes.Requireable<(...args: any[]) => any>;
31
33
  };
32
34
  static defaultProps: {
33
35
  trackScroll: boolean;
@@ -42,6 +44,7 @@ export declare class PopoverContainer extends React.Component<PopoverContainerPr
42
44
  componentDidUpdate(previousProps: any): void;
43
45
  componentWillUnmount(): void;
44
46
  getPopoverTipPosition: (popoverPosition: any, containerPosition: any) => "top" | "bottom" | "right" | "left";
47
+ handleKeyDown: (event: any) => void;
45
48
  render(): JSX.Element;
46
49
  }
47
50
  export {};
@@ -11,6 +11,7 @@ export declare type StatelessPopoverProps = {
11
11
  className?: string;
12
12
  outerActionEvents?: string[];
13
13
  onOuterAction?: (evt: Event) => void;
14
+ onEscapeAction: (evt: Event) => void;
14
15
  trackScroll?: boolean;
15
16
  role?: 'tooltip' | 'dialog';
16
17
  };
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ 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';
@@ -205,8 +205,8 @@ function styleInject(css, ref) {
205
205
  }
206
206
  }
207
207
 
208
- var css = ".popover-body-container__popover__12-19-2-alpha-3 {\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-2-alpha-3 {\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-2-alpha-3 {\r\n padding: 0;\r\n }\r\n\r\n.popover-tip__popover__12-19-2-alpha-3 {\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-2-alpha-3: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-2-alpha-3,\r\n .popover-tip--top__popover__12-19-2-alpha-3 {\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-2-alpha-3,\r\n .popover-tip--right__popover__12-19-2-alpha-3 {\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-2-alpha-3: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-2-alpha-3: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-2-alpha-3: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-2-alpha-3: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-2-alpha-3","popover-body":"popover-body__popover__12-19-2-alpha-3","popover-body--no-padding":"popover-body--no-padding__popover__12-19-2-alpha-3","popover-tip":"popover-tip__popover__12-19-2-alpha-3","popover-tip--bottom":"popover-tip--bottom__popover__12-19-2-alpha-3","popover-tip--top":"popover-tip--top__popover__12-19-2-alpha-3","popover-tip--left":"popover-tip--left__popover__12-19-2-alpha-3","popover-tip--right":"popover-tip--right__popover__12-19-2-alpha-3"};
208
+ var css = ".popover-body-container__popover__12-19-2-alpha-6 {\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-2-alpha-6 {\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-2-alpha-6 {\r\n padding: 0;\r\n }\r\n\r\n.popover-tip__popover__12-19-2-alpha-6 {\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-2-alpha-6: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-2-alpha-6,\r\n .popover-tip--top__popover__12-19-2-alpha-6 {\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-2-alpha-6,\r\n .popover-tip--right__popover__12-19-2-alpha-6 {\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-2-alpha-6: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-2-alpha-6: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-2-alpha-6: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-2-alpha-6: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-2-alpha-6","popover-body":"popover-body__popover__12-19-2-alpha-6","popover-body--no-padding":"popover-body--no-padding__popover__12-19-2-alpha-6","popover-tip":"popover-tip__popover__12-19-2-alpha-6","popover-tip--bottom":"popover-tip--bottom__popover__12-19-2-alpha-6","popover-tip--top":"popover-tip--top__popover__12-19-2-alpha-6","popover-tip--left":"popover-tip--left__popover__12-19-2-alpha-6","popover-tip--right":"popover-tip--right__popover__12-19-2-alpha-6"};
210
210
  styleInject(css);
211
211
 
212
212
  var PopoverBody = function PopoverBody(_ref) {
@@ -243,6 +243,63 @@ var throttle = function throttle(func, limit) {
243
243
  };
244
244
  };
245
245
 
246
+ var isFocusable = function isFocusable(element) {
247
+ if (element.tabIndex < 0) {
248
+ return false;
249
+ }
250
+ if (element.disabled) {
251
+ return false;
252
+ }
253
+ switch (element.nodeName) {
254
+ case 'A':
255
+ return !!element.href && element.rel !== 'ignore';
256
+ case 'INPUT':
257
+ return element.type !== 'hidden';
258
+ case 'BUTTON':
259
+ case 'SELECT':
260
+ case 'TEXTAREA':
261
+ return true;
262
+ default:
263
+ return false;
264
+ }
265
+ };
266
+ var attemptFocus = function attemptFocus(element) {
267
+ if (!isFocusable(element)) {
268
+ return false;
269
+ }
270
+ try {
271
+ element.focus();
272
+ } catch (err) {}
273
+ return document.activeElement === element;
274
+ };
275
+ var focusFirstDescendant = function focusFirstDescendant(element) {
276
+ var _iteratorNormalCompletion = true;
277
+ var _didIteratorError = false;
278
+ var _iteratorError = undefined;
279
+ try {
280
+ for (var _iterator = element.childNodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
281
+ var child = _step.value;
282
+ if (attemptFocus(child) || focusFirstDescendant(child)) {
283
+ return true;
284
+ }
285
+ }
286
+ } catch (err) {
287
+ _didIteratorError = true;
288
+ _iteratorError = err;
289
+ } finally {
290
+ try {
291
+ if (!_iteratorNormalCompletion && _iterator["return"] != null) {
292
+ _iterator["return"]();
293
+ }
294
+ } finally {
295
+ if (_didIteratorError) {
296
+ throw _iteratorError;
297
+ }
298
+ }
299
+ }
300
+ return false;
301
+ };
302
+
246
303
  var isScrollParent = function isScrollParent(element) {
247
304
  // Firefox wants us to check `-x` and `-y` variations as well
248
305
  var _getComputedStyle = getComputedStyle(element),
@@ -323,6 +380,11 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
323
380
  }
324
381
  return null;
325
382
  };
383
+ _this.handleKeyDown = function (event) {
384
+ if (event.key === 'Escape') {
385
+ _this.props.onEscapeAction(event);
386
+ }
387
+ };
326
388
  return _this;
327
389
  }
328
390
  _createClass(PopoverContainer, [{
@@ -365,8 +427,11 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
365
427
  value: function componentDidMount() {
366
428
  this.mounted = true;
367
429
  this.adjustPosition();
368
- if (this.popoverContentRef) {
369
- this.popoverContentRef.current.focus();
430
+ if (this.popoverContentRef.current) {
431
+ if (!focusFirstDescendant(this.popoverContentRef.current)) {
432
+ this.popoverContentRef.current.tabIndex = 0;
433
+ this.popoverContentRef.current.focus();
434
+ }
370
435
  }
371
436
  if (this.props.trackScroll) {
372
437
  document.addEventListener('scroll', this.onScroll);
@@ -435,7 +500,7 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
435
500
  }, position),
436
501
  className: this.props.className,
437
502
  role: this.props.role,
438
- tabIndex: 0
503
+ onKeyDown: this.handleKeyDown
439
504
  }, this.props.children));
440
505
  }
441
506
  }]);
@@ -570,7 +635,8 @@ var StatelessPopover = /*#__PURE__*/function (_React$Component) {
570
635
  showTip: props.showTip,
571
636
  zIndex: props.zIndex,
572
637
  trackScroll: props.trackScroll,
573
- role: this.props.role
638
+ role: this.props.role,
639
+ onEscapeAction: this.props.onEscapeAction
574
640
  }, props.body);
575
641
  if (!props.inline) {
576
642
  return ReactDOM.createPortal(popoverContent, container);
@@ -634,21 +700,38 @@ var Popover = /*#__PURE__*/function (_React$Component) {
634
700
  _this.props.onOuterAction(event);
635
701
  }
636
702
  };
703
+ _this.onEscapeAction = function (event) {
704
+ var _a;
705
+ _this.setOpened(false);
706
+ if (_this.props.onOuterAction) {
707
+ _this.props.onOuterAction(event);
708
+ }
709
+ (_a = _this.childrenRef.current) === null || _a === void 0 ? void 0 : _a.focus();
710
+ };
711
+ _this.childrenRef = createRef();
637
712
  return _this;
638
713
  }
639
714
  _createClass(Popover, [{
640
715
  key: "render",
641
716
  value: function render() {
717
+ var _this2 = this;
642
718
  var isOpen = typeof this.props.isOpen === 'undefined' ? this.state.isOpen : this.props.isOpen;
643
719
  var children = typeof this.props.children === 'function' ? this.props.children({
644
720
  isOpen: isOpen,
645
- setOpened: this.setOpened
721
+ setOpened: this.setOpened,
722
+ ref: this.childrenRef
646
723
  }) : this.props.children;
724
+ var childrenWithRef = React.Children.map(children, function (child) {
725
+ return React.cloneElement(child, {
726
+ ref: _this2.childrenRef
727
+ });
728
+ });
647
729
  return React.createElement(StatelessPopover, Object.assign({}, this.props, {
648
730
  onOuterAction: this.onOuterAction,
731
+ onEscapeAction: this.onEscapeAction,
649
732
  isOpen: isOpen,
650
733
  role: this.props.role
651
- }), children);
734
+ }), childrenWithRef);
652
735
  }
653
736
  }]);
654
737
  return Popover;
package/dist/popover.js CHANGED
@@ -4,7 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
6
 
7
- var React = _interopDefault(require('react'));
7
+ var React = require('react');
8
+ var React__default = _interopDefault(React);
8
9
  var globalConstants = require('@targetprocess/global-constants');
9
10
  var classNames = _interopDefault(require('classnames'));
10
11
  var ReactDOM = _interopDefault(require('react-dom'));
@@ -211,8 +212,8 @@ function styleInject(css, ref) {
211
212
  }
212
213
  }
213
214
 
214
- var css = ".popover-body-container__popover__12-19-2-alpha-3 {\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-2-alpha-3 {\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-2-alpha-3 {\r\n padding: 0;\r\n }\r\n\r\n.popover-tip__popover__12-19-2-alpha-3 {\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-2-alpha-3: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-2-alpha-3,\r\n .popover-tip--top__popover__12-19-2-alpha-3 {\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-2-alpha-3,\r\n .popover-tip--right__popover__12-19-2-alpha-3 {\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-2-alpha-3: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-2-alpha-3: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-2-alpha-3: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-2-alpha-3: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";
215
- var styles = {"popover-body-container":"popover-body-container__popover__12-19-2-alpha-3","popover-body":"popover-body__popover__12-19-2-alpha-3","popover-body--no-padding":"popover-body--no-padding__popover__12-19-2-alpha-3","popover-tip":"popover-tip__popover__12-19-2-alpha-3","popover-tip--bottom":"popover-tip--bottom__popover__12-19-2-alpha-3","popover-tip--top":"popover-tip--top__popover__12-19-2-alpha-3","popover-tip--left":"popover-tip--left__popover__12-19-2-alpha-3","popover-tip--right":"popover-tip--right__popover__12-19-2-alpha-3"};
215
+ var css = ".popover-body-container__popover__12-19-2-alpha-6 {\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-2-alpha-6 {\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-2-alpha-6 {\r\n padding: 0;\r\n }\r\n\r\n.popover-tip__popover__12-19-2-alpha-6 {\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-2-alpha-6: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-2-alpha-6,\r\n .popover-tip--top__popover__12-19-2-alpha-6 {\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-2-alpha-6,\r\n .popover-tip--right__popover__12-19-2-alpha-6 {\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-2-alpha-6: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-2-alpha-6: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-2-alpha-6: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-2-alpha-6: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-2-alpha-6","popover-body":"popover-body__popover__12-19-2-alpha-6","popover-body--no-padding":"popover-body--no-padding__popover__12-19-2-alpha-6","popover-tip":"popover-tip__popover__12-19-2-alpha-6","popover-tip--bottom":"popover-tip--bottom__popover__12-19-2-alpha-6","popover-tip--top":"popover-tip--top__popover__12-19-2-alpha-6","popover-tip--left":"popover-tip--left__popover__12-19-2-alpha-6","popover-tip--right":"popover-tip--right__popover__12-19-2-alpha-6"};
216
217
  styleInject(css);
217
218
 
218
219
  var PopoverBody = function PopoverBody(_ref) {
@@ -220,9 +221,9 @@ var PopoverBody = function PopoverBody(_ref) {
220
221
  className = _ref.className,
221
222
  classNameBody = _ref.classNameBody,
222
223
  noPadding = _ref.noPadding;
223
- return React.createElement("div", {
224
+ return React__default.createElement("div", {
224
225
  className: classNames(className, styles['popover-body-container'])
225
- }, React.createElement("div", {
226
+ }, React__default.createElement("div", {
226
227
  className: classNames(classNameBody, styles['popover-body'], noPadding && styles['popover-body--no-padding'])
227
228
  }, children));
228
229
  };
@@ -249,6 +250,63 @@ var throttle = function throttle(func, limit) {
249
250
  };
250
251
  };
251
252
 
253
+ var isFocusable = function isFocusable(element) {
254
+ if (element.tabIndex < 0) {
255
+ return false;
256
+ }
257
+ if (element.disabled) {
258
+ return false;
259
+ }
260
+ switch (element.nodeName) {
261
+ case 'A':
262
+ return !!element.href && element.rel !== 'ignore';
263
+ case 'INPUT':
264
+ return element.type !== 'hidden';
265
+ case 'BUTTON':
266
+ case 'SELECT':
267
+ case 'TEXTAREA':
268
+ return true;
269
+ default:
270
+ return false;
271
+ }
272
+ };
273
+ var attemptFocus = function attemptFocus(element) {
274
+ if (!isFocusable(element)) {
275
+ return false;
276
+ }
277
+ try {
278
+ element.focus();
279
+ } catch (err) {}
280
+ return document.activeElement === element;
281
+ };
282
+ var focusFirstDescendant = function focusFirstDescendant(element) {
283
+ var _iteratorNormalCompletion = true;
284
+ var _didIteratorError = false;
285
+ var _iteratorError = undefined;
286
+ try {
287
+ for (var _iterator = element.childNodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
288
+ var child = _step.value;
289
+ if (attemptFocus(child) || focusFirstDescendant(child)) {
290
+ return true;
291
+ }
292
+ }
293
+ } catch (err) {
294
+ _didIteratorError = true;
295
+ _iteratorError = err;
296
+ } finally {
297
+ try {
298
+ if (!_iteratorNormalCompletion && _iterator["return"] != null) {
299
+ _iterator["return"]();
300
+ }
301
+ } finally {
302
+ if (_didIteratorError) {
303
+ throw _iteratorError;
304
+ }
305
+ }
306
+ }
307
+ return false;
308
+ };
309
+
252
310
  var isScrollParent = function isScrollParent(element) {
253
311
  // Firefox wants us to check `-x` and `-y` variations as well
254
312
  var _getComputedStyle = getComputedStyle(element),
@@ -302,7 +360,7 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
302
360
  _classCallCheck(this, PopoverContainer);
303
361
  _this = _possibleConstructorReturn(this, _getPrototypeOf(PopoverContainer).apply(this, arguments));
304
362
  _this.mounted = false;
305
- _this.popoverContentRef = React.createRef();
363
+ _this.popoverContentRef = React__default.createRef();
306
364
  _this.state = {
307
365
  adjusted: false,
308
366
  positionX: 0,
@@ -329,6 +387,11 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
329
387
  }
330
388
  return null;
331
389
  };
390
+ _this.handleKeyDown = function (event) {
391
+ if (event.key === 'Escape') {
392
+ _this.props.onEscapeAction(event);
393
+ }
394
+ };
332
395
  return _this;
333
396
  }
334
397
  _createClass(PopoverContainer, [{
@@ -371,8 +434,11 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
371
434
  value: function componentDidMount() {
372
435
  this.mounted = true;
373
436
  this.adjustPosition();
374
- if (this.popoverContentRef) {
375
- this.popoverContentRef.current.focus();
437
+ if (this.popoverContentRef.current) {
438
+ if (!focusFirstDescendant(this.popoverContentRef.current)) {
439
+ this.popoverContentRef.current.tabIndex = 0;
440
+ this.popoverContentRef.current.focus();
441
+ }
376
442
  }
377
443
  if (this.props.trackScroll) {
378
444
  document.addEventListener('scroll', this.onScroll);
@@ -414,7 +480,7 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
414
480
  };
415
481
  var tipPosition = this.getPopoverTipPosition(position, containerPosition);
416
482
  // FIXME: position.top cast to number, because value may be not a number
417
- return React.createElement("div", {
483
+ return React__default.createElement("div", {
418
484
  style: _extends({
419
485
  position: 'absolute'
420
486
  }, containerPosition, {
@@ -424,7 +490,7 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
424
490
  transitionProperty: 'opacity',
425
491
  opacity: this.state.adjusted ? 1 : 0
426
492
  })
427
- }, this.props.showTip && tipPosition && React.createElement("div", {
493
+ }, this.props.showTip && tipPosition && React__default.createElement("div", {
428
494
  className: classNames(styles['popover-tip'], styles["popover-tip--".concat(tipPosition)], 'popover-tip'),
429
495
  style: {
430
496
  top: tipPosition === 'bottom' && position.top - 8,
@@ -432,7 +498,7 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
432
498
  left: tipPosition === 'right' && position.left - 8,
433
499
  right: tipPosition === 'left' && position.right - 8
434
500
  }
435
- }), React.createElement("div", {
501
+ }), React__default.createElement("div", {
436
502
  ref: this.popoverContentRef,
437
503
  "aria-modal": "true",
438
504
  style: _extends({
@@ -441,12 +507,12 @@ var PopoverContainer = /*#__PURE__*/function (_React$Component) {
441
507
  }, position),
442
508
  className: this.props.className,
443
509
  role: this.props.role,
444
- tabIndex: 0
510
+ onKeyDown: this.handleKeyDown
445
511
  }, this.props.children));
446
512
  }
447
513
  }]);
448
514
  return PopoverContainer;
449
- }(React.Component);
515
+ }(React__default.Component);
450
516
  PopoverContainer.defaultProps = {
451
517
  trackScroll: true
452
518
  };
@@ -460,7 +526,7 @@ var PositioningTarget = /*#__PURE__*/function (_React$Component) {
460
526
  _this.state = {
461
527
  target: null
462
528
  };
463
- _this.targetRef = React.createRef();
529
+ _this.targetRef = React__default.createRef();
464
530
  return _this;
465
531
  }
466
532
  _createClass(PositioningTarget, [{
@@ -473,13 +539,13 @@ var PositioningTarget = /*#__PURE__*/function (_React$Component) {
473
539
  }, {
474
540
  key: "render",
475
541
  value: function render() {
476
- return React.createElement("div", {
542
+ return React__default.createElement("div", {
477
543
  ref: this.targetRef
478
544
  }, this.props.children(this.state.target));
479
545
  }
480
546
  }]);
481
547
  return PositioningTarget;
482
- }(React.Component);
548
+ }(React__default.Component);
483
549
 
484
550
  var BodyDomNode = /*#__PURE__*/function (_React$Component) {
485
551
  _inherits(BodyDomNode, _React$Component);
@@ -544,7 +610,7 @@ var BodyDomNode = /*#__PURE__*/function (_React$Component) {
544
610
  }
545
611
  }]);
546
612
  return BodyDomNode;
547
- }(React.Component);
613
+ }(React__default.Component);
548
614
  BodyDomNode.defaultProps = {
549
615
  containerElement: document.body
550
616
  };
@@ -566,7 +632,7 @@ var StatelessPopover = /*#__PURE__*/function (_React$Component) {
566
632
  var props = _ref.props,
567
633
  container = _ref.container,
568
634
  positioningTarget = _ref.positioningTarget;
569
- var popoverContent = React.createElement(PopoverContainer, {
635
+ var popoverContent = React__default.createElement(PopoverContainer, {
570
636
  key: "popover-portal",
571
637
  getPopoverPosition: this.props.getPopoverPosition,
572
638
  relativeToParent: props.inline,
@@ -576,7 +642,8 @@ var StatelessPopover = /*#__PURE__*/function (_React$Component) {
576
642
  showTip: props.showTip,
577
643
  zIndex: props.zIndex,
578
644
  trackScroll: props.trackScroll,
579
- role: this.props.role
645
+ role: this.props.role,
646
+ onEscapeAction: this.props.onEscapeAction
580
647
  }, props.body);
581
648
  if (!props.inline) {
582
649
  return ReactDOM.createPortal(popoverContent, container);
@@ -593,7 +660,7 @@ var StatelessPopover = /*#__PURE__*/function (_React$Component) {
593
660
  return this.props.inline ? this.renderPortalContent({
594
661
  props: this.props,
595
662
  positioningTarget: positioningTarget
596
- }) : React.createElement(BodyDomNode, {
663
+ }) : React__default.createElement(BodyDomNode, {
597
664
  onOuterAction: this.props.onOuterAction,
598
665
  outerActionEvents: this.props.outerActionEvents,
599
666
  positioningTarget: positioningTarget,
@@ -610,13 +677,13 @@ var StatelessPopover = /*#__PURE__*/function (_React$Component) {
610
677
  key: "render",
611
678
  value: function render() {
612
679
  var _this3 = this;
613
- return React.createElement(PositioningTarget, null, function (positioningTarget) {
614
- return positioningTarget ? React.createElement(React.Fragment, null, _this3.props.children, _this3.renderPortal(positioningTarget)) : null;
680
+ return React__default.createElement(PositioningTarget, null, function (positioningTarget) {
681
+ return positioningTarget ? React__default.createElement(React__default.Fragment, null, _this3.props.children, _this3.renderPortal(positioningTarget)) : null;
615
682
  });
616
683
  }
617
684
  }]);
618
685
  return StatelessPopover;
619
- }(React.Component);
686
+ }(React__default.Component);
620
687
 
621
688
  var Popover = /*#__PURE__*/function (_React$Component) {
622
689
  _inherits(Popover, _React$Component);
@@ -640,25 +707,42 @@ var Popover = /*#__PURE__*/function (_React$Component) {
640
707
  _this.props.onOuterAction(event);
641
708
  }
642
709
  };
710
+ _this.onEscapeAction = function (event) {
711
+ var _a;
712
+ _this.setOpened(false);
713
+ if (_this.props.onOuterAction) {
714
+ _this.props.onOuterAction(event);
715
+ }
716
+ (_a = _this.childrenRef.current) === null || _a === void 0 ? void 0 : _a.focus();
717
+ };
718
+ _this.childrenRef = React.createRef();
643
719
  return _this;
644
720
  }
645
721
  _createClass(Popover, [{
646
722
  key: "render",
647
723
  value: function render() {
724
+ var _this2 = this;
648
725
  var isOpen = typeof this.props.isOpen === 'undefined' ? this.state.isOpen : this.props.isOpen;
649
726
  var children = typeof this.props.children === 'function' ? this.props.children({
650
727
  isOpen: isOpen,
651
- setOpened: this.setOpened
728
+ setOpened: this.setOpened,
729
+ ref: this.childrenRef
652
730
  }) : this.props.children;
653
- return React.createElement(StatelessPopover, Object.assign({}, this.props, {
731
+ var childrenWithRef = React__default.Children.map(children, function (child) {
732
+ return React__default.cloneElement(child, {
733
+ ref: _this2.childrenRef
734
+ });
735
+ });
736
+ return React__default.createElement(StatelessPopover, Object.assign({}, this.props, {
654
737
  onOuterAction: this.onOuterAction,
738
+ onEscapeAction: this.onEscapeAction,
655
739
  isOpen: isOpen,
656
740
  role: this.props.role
657
- }), children);
741
+ }), childrenWithRef);
658
742
  }
659
743
  }]);
660
744
  return Popover;
661
- }(React.Component);
745
+ }(React__default.Component);
662
746
  Popover.Body = PopoverBody;
663
747
  Popover.Position = PopoverPosition;
664
748
  Popover.defaultProps = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@targetprocess/popover",
3
- "version": "12.19.2-alpha.3",
3
+ "version": "12.19.2-alpha.6",
4
4
  "description": "",
5
5
  "main": "dist/popover.js",
6
6
  "module": "dist/popover.es.js",
@@ -21,5 +21,5 @@
21
21
  "publishConfig": {
22
22
  "access": "public"
23
23
  },
24
- "gitHead": "98b87b02e7b95a071030d87752048d64dd53abf4"
24
+ "gitHead": "cc4bc23e51d53edf286ab13cda5555cbcdd1c49f"
25
25
  }