@targetprocess/popover 12.23.0 → 12.23.1-alpha.2

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,7 +1,7 @@
1
1
  /*********************************************************************
2
2
  * © Copyright IBM Corp. 2024
3
3
  *********************************************************************/
4
- import React, {createRef} from 'react'
4
+ import React from 'react'
5
5
  import PropTypes from 'prop-types'
6
6
  import {PopoverPosition} from './PopoverPosition'
7
7
  import {PopoverBody} from './PopoverBody'
@@ -136,7 +136,7 @@ export class Popover extends React.Component<PopoverProps, PopoverState> {
136
136
  outerActionEvents: ['mousedown', 'touchstart'],
137
137
  getPopoverPosition: 'bottomLeft',
138
138
  role: 'dialog',
139
- trapFocus: 'true'
139
+ trapFocus: true
140
140
  }
141
141
 
142
142
  state = {
@@ -161,10 +161,10 @@ export class Popover extends React.Component<PopoverProps, PopoverState> {
161
161
  if (this.props.onOuterAction) {
162
162
  this.props.onOuterAction(event)
163
163
  }
164
- this.childrenRef.current?.focus()
164
+ this.childrenRef?.focus()
165
165
  }
166
166
 
167
- childrenRef = createRef<HTMLElement>()
167
+ childrenRef?: HTMLElement
168
168
 
169
169
  render() {
170
170
  const isOpen = typeof this.props.isOpen === 'undefined' ? this.state.isOpen : this.props.isOpen
@@ -173,8 +173,18 @@ export class Popover extends React.Component<PopoverProps, PopoverState> {
173
173
  ? this.props.children({isOpen, setOpened: this.setOpened, ref: this.childrenRef})
174
174
  : this.props.children
175
175
  const childrenWithRef = React.Children.map(children, child =>
176
- React.cloneElement(child, {ref: this.childrenRef})
176
+ React.cloneElement(child, {
177
+ ref: (current?: HTMLElement) => {
178
+ this.childrenRef = current
179
+ if (typeof child.ref === 'function') {
180
+ child.ref(current)
181
+ } else if (child.ref && child.ref.hasOwnProperty('current')) {
182
+ child.ref.current = current
183
+ }
184
+ }
185
+ })
177
186
  )
187
+ this.childrenRef = null
178
188
 
179
189
  return (
180
190
  <StatelessPopover
@@ -131,10 +131,22 @@ export class PopoverContainer extends React.Component<
131
131
 
132
132
  this.setState({
133
133
  hostRect: {
134
- top: this.props.relativeToParent ? 0 : positioningBoundingRect.top,
135
- left: this.props.relativeToParent ? 0 : positioningBoundingRect.left,
136
- right: this.props.relativeToParent ? 0 : positioningBoundingRect.right,
137
- bottom: this.props.relativeToParent ? 0 : positioningBoundingRect.bottom,
134
+ top:
135
+ this.props.relativeToParent || isNaN(positioningBoundingRect.top)
136
+ ? 0
137
+ : positioningBoundingRect.top,
138
+ left:
139
+ this.props.relativeToParent || isNaN(positioningBoundingRect.left)
140
+ ? 0
141
+ : positioningBoundingRect.left,
142
+ right:
143
+ this.props.relativeToParent || isNaN(positioningBoundingRect.right)
144
+ ? 0
145
+ : positioningBoundingRect.right,
146
+ bottom:
147
+ this.props.relativeToParent || isNaN(positioningBoundingRect.bottom)
148
+ ? 0
149
+ : positioningBoundingRect.bottom,
138
150
  width: positioningBoundingRect.width,
139
151
  height: positioningBoundingRect.height
140
152
  },
@@ -18,6 +18,12 @@ jest.mock('focus-trap', () => {
18
18
  return () => trap
19
19
  })
20
20
 
21
+ const ComponentWithRef = React.forwardRef<HTMLElement>((prop, ref) => (
22
+ <div>
23
+ <span ref={ref}>Ref</span>
24
+ </div>
25
+ ))
26
+
21
27
  describe('Popover', () => {
22
28
  beforeEach(() => {
23
29
  cleanDocumentBody()
@@ -63,4 +69,19 @@ describe('Popover', () => {
63
69
  expect(onOuterAction.mock.calls.length).toBe(1)
64
70
  expect(onOuterAction.mock.calls[0][0].target).toBeDefined()
65
71
  })
72
+
73
+ it('should keep ref in children', () => {
74
+ const ref1 = jest.fn()
75
+ const ref2 = jest.fn()
76
+
77
+ mount(
78
+ <Popover getPopoverPosition="bottomLeft">
79
+ <ComponentWithRef ref={ref1} />
80
+ <ComponentWithRef ref={ref2} />
81
+ </Popover>
82
+ )
83
+
84
+ expect(ref1).toBeCalled()
85
+ expect(ref2).toBeCalled()
86
+ })
66
87
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@targetprocess/popover",
3
- "version": "12.23.0",
3
+ "version": "12.23.1-alpha.2",
4
4
  "description": "",
5
5
  "main": "dist/popover.js",
6
6
  "module": "dist/popover.es.js",
@@ -22,5 +22,5 @@
22
22
  "publishConfig": {
23
23
  "access": "public"
24
24
  },
25
- "gitHead": "32f86f62bef29fb1cfb18aaf42f72411a8f7cc48"
25
+ "gitHead": "3d12d498f8e567a89ed1bee47f9dace942695590"
26
26
  }
@@ -1,36 +0,0 @@
1
- /*********************************************************************
2
- * © Copyright IBM Corp. 2024
3
- *********************************************************************/
4
- import React from 'react';
5
- import PropTypes from 'prop-types';
6
- export declare type BodyDomNodeProps = {
7
- children: (node: HTMLElement) => React.ReactNode;
8
- onOuterAction?: (event: Event) => void;
9
- outerActionEvents?: string[];
10
- positioningTarget: HTMLElement;
11
- containerElement: HTMLElement;
12
- };
13
- export declare class BodyDomNode extends React.Component<BodyDomNodeProps, {
14
- node: HTMLElement;
15
- }> {
16
- static propTypes: {
17
- children: PropTypes.Validator<(...args: any[]) => any>;
18
- onOuterAction: PropTypes.Requireable<(...args: any[]) => any>;
19
- outerActionEvents: PropTypes.Requireable<string[]>;
20
- positioningTarget: PropTypes.Requireable<HTMLElement>;
21
- containerElement: PropTypes.Requireable<HTMLElement>;
22
- };
23
- static defaultProps: {
24
- containerElement: HTMLElement;
25
- };
26
- state: {
27
- node: any;
28
- };
29
- _onEvent: (event: any) => void;
30
- _addDocumentEventListeners: () => void;
31
- _removeDocumentEventListeners: (outerActionEvents?: string[]) => void;
32
- componentDidMount(): void;
33
- componentDidUpdate(prevProps: BodyDomNodeProps): void;
34
- componentWillUnmount(): void;
35
- render(): React.ReactNode;
36
- }
package/dist/Popover.d.ts DELETED
@@ -1,138 +0,0 @@
1
- /*********************************************************************
2
- * © Copyright IBM Corp. 2024
3
- *********************************************************************/
4
- import React from 'react';
5
- import PropTypes from 'prop-types';
6
- import { GetPopoverPositionType } from './types';
7
- /**
8
- * Popover is react component for displaying some toggleable content near
9
- * other elements.
10
- *
11
- * By itself, `<Popover>` component is pass-through, i.e. it only renders
12
- * its `children` without any other side effects. Its `body` prop accepts any
13
- * react element, and when `isOpen` is set to `true`, it renders `body` in a
14
- * portal and appends it to the element returned by `getContainerElement`
15
- * (or to `document.body`, if it's not provided).
16
- *
17
- * By default, `body` doesn't add any additional styling. You can use
18
- * `Popover.Body` component if you need a container with commonly styled
19
- * box shadow and rounded corners.
20
- *
21
- * Popover accepts render props children, in which case it can use its
22
- * own internal state to work in uncontrolled mode. In this mode, however,
23
- * `isOpen` prop on `Popover` takes preceedence over internal state.
24
- * Render prop function signature is
25
- * `({isOpen: boolean, setOpened: (isOpen: boolean) => void}) => React.Element`;
26
- * children can use `isOpen` to adjust their own view and call setOpened to open
27
- * or close the popover:
28
- * ```jsx
29
- * <Popover body={<Popover.Body>hello world!</Popover.Body>}>
30
- * {({isOpen, setOpened}) => (
31
- * <button onClick={() => setOpened(!isOpen)}>
32
- * Toggle popover
33
- * </button>
34
- * )}
35
- * </Popover>
36
- * ```
37
- *
38
- * Popover positioning can be configured using `getPopoverPosition` function.
39
- * It also accepts any of the 12 positions as strings:
40
- * `'bottomLeft', 'bottom', 'bottomRight', 'topLeft', 'top', 'topRight',
41
- * 'leftTop', 'left', 'leftBottom', 'rightTop', 'right', 'rightBottom'`.
42
- *
43
- * `getPopoverPosition` accepts 3 arguments: trigger dimensions (x, y,
44
- * wdith and height), body dimensions as DOMRect (x, y, width and height)
45
- * and container element scroll offsets. It is expected to return any CSS
46
- * style modifiers that will be then applied to the popover itself.
47
- *
48
- * For example, default `'bottom'` position is implemented as follows:
49
- * ```js
50
- * bottom: (hostRect, popoverRect, containerScroll) => ({
51
- * left: '50%',
52
- * marginLeft: -popoverRect.width / 2,
53
- * top: hostRect.height + parseInt(SpaceXS) - containerScroll.top
54
- * }),
55
- * ```
56
- *
57
- * Default positioning functions are also avaliable from `Popover.Position`
58
- * object, so you can reuse them in certain situations. For example, if you
59
- * want to use `bottom` positioning but move it 5 pixels higher, you can do
60
- * it like this:
61
- * ```js
62
- * const getPopoverPosition = (...args) => {
63
- * const defaultBottomPosition = Popover.Position.bottom(...args);
64
- * return {...defaultBottomPosition, top: defaultBottomPosition - 5}
65
- * }
66
- * ```
67
- *
68
- * You can also use `showTip` to display a triangle near popover.
69
- * Triangle position is calculated automatically depending on relative popover
70
- * position; it is always placed at the center of the trigger from the according side.
71
- */
72
- export declare type PopoverProps = {
73
- closeOnOuterAction?: boolean;
74
- isOpen?: boolean;
75
- getPopoverPosition: GetPopoverPositionType;
76
- onOuterAction?: (evt: Event) => void;
77
- outerActionEvents?: string[];
78
- getContainerElement?: () => HTMLElement;
79
- inline?: boolean;
80
- body?: React.ReactNode;
81
- zIndex: number;
82
- showTip?: boolean;
83
- classNameTip?: string;
84
- className?: string;
85
- trackScroll?: boolean;
86
- role?: 'tooltip' | 'dialog';
87
- trapFocus?: boolean;
88
- };
89
- export declare type PopoverState = {
90
- isOpen: boolean;
91
- };
92
- export declare class Popover extends React.Component<PopoverProps, PopoverState> {
93
- static Body: React.FunctionComponent<import("./PopoverBody").PopoverBodyProps>;
94
- static Position: import("./types").DefaultPopoverPositions;
95
- static propTypes: {
96
- /** Class name of toplevel popover container */
97
- className: PropTypes.Requireable<string>;
98
- /** Wether popover is opened or closed */
99
- isOpen: PropTypes.Requireable<boolean>;
100
- /** Element that will be mounted within the popover */
101
- body: PropTypes.Requireable<PropTypes.ReactElementLike>;
102
- /** Called when one of the `outerActionEvents` occurs */
103
- onOuterAction: PropTypes.Requireable<(...args: any[]) => any>;
104
- /** Must return DOM element to append popover to. */
105
- getContainerElement: PropTypes.Requireable<(...args: any[]) => any>;
106
- /** Wether to show the triangle. */
107
- showTip: PropTypes.Requireable<boolean>;
108
- /** Position string or positioning function. */
109
- getPopoverPosition: PropTypes.Requireable<string | ((...args: any[]) => any)>;
110
- /**
111
- * Events that trigger `onOuterAction`.
112
- */
113
- outerActionEvents: PropTypes.Requireable<string[]>;
114
- /** Whether uncontrolled (render props) popover should be closed on outer action */
115
- closeOnOuterAction: PropTypes.Requireable<boolean>;
116
- noPadding: PropTypes.Requireable<boolean>;
117
- zIndex: PropTypes.Requireable<number>;
118
- trackScroll: PropTypes.Requireable<boolean>;
119
- children: PropTypes.Requireable<PropTypes.ReactNodeLike>;
120
- role: PropTypes.Requireable<string>;
121
- trapFocus: PropTypes.Requireable<boolean>;
122
- };
123
- static defaultProps: {
124
- zIndex: number;
125
- outerActionEvents: string[];
126
- getPopoverPosition: string;
127
- role: string;
128
- trapFocus: string;
129
- };
130
- state: {
131
- isOpen: boolean;
132
- };
133
- setOpened: (isOpen: boolean) => void;
134
- onOuterAction: (event: Event) => void;
135
- onEscapeAction: (event: Event) => void;
136
- childrenRef: React.RefObject<HTMLElement>;
137
- render(): JSX.Element;
138
- }
@@ -1,11 +0,0 @@
1
- /*********************************************************************
2
- * © Copyright IBM Corp. 2024
3
- *********************************************************************/
4
- import React from 'react';
5
- export declare type PopoverBodyProps = {
6
- children?: React.ReactNode;
7
- className?: string;
8
- classNameBody?: string;
9
- noPadding?: boolean;
10
- };
11
- export declare const PopoverBody: React.FunctionComponent<PopoverBodyProps>;
@@ -1,57 +0,0 @@
1
- /*********************************************************************
2
- * © Copyright IBM Corp. 2024
3
- *********************************************************************/
4
- import React from 'react';
5
- import PropTypes from 'prop-types';
6
- import { DimensionParams, GetPopoverPositionType, PositionParams } from './types';
7
- declare type PopoverContainerProps = {
8
- containerElement?: HTMLElement;
9
- positioningTarget: HTMLElement;
10
- className?: string;
11
- relativeToParent?: boolean;
12
- trackScroll?: boolean;
13
- scrollThrottleIntervalMs?: number;
14
- getPopoverPosition: GetPopoverPositionType;
15
- showTip?: boolean;
16
- classNameTip?: string;
17
- zIndex: number;
18
- role?: 'tooltip' | 'dialog';
19
- trapFocus?: boolean;
20
- onEscapeAction: (event: Event) => void;
21
- };
22
- declare type PopoverContainerState = {
23
- adjusted: boolean;
24
- positionX: number;
25
- positionY: number;
26
- hostRect?: PositionParams & DimensionParams;
27
- popoverRect?: DimensionParams;
28
- scroll?: PositionParams;
29
- };
30
- export declare class PopoverContainer extends React.Component<PopoverContainerProps, PopoverContainerState> {
31
- static propTypes: {
32
- positioningTarget: PropTypes.Requireable<object>;
33
- containerElement: PropTypes.Requireable<object>;
34
- className: PropTypes.Requireable<string>;
35
- trackScroll: PropTypes.Requireable<boolean>;
36
- role: PropTypes.Requireable<string>;
37
- trapFocus: PropTypes.Requireable<boolean>;
38
- onEscapeAction: PropTypes.Requireable<(...args: any[]) => any>;
39
- };
40
- static defaultProps: {
41
- trackScroll: boolean;
42
- };
43
- mounted: boolean;
44
- popoverContentRef: React.RefObject<HTMLDivElement>;
45
- state: PopoverContainerState;
46
- popoverContainerId: string;
47
- adjustPosition(): void;
48
- onResize: () => void;
49
- onScroll: (...args: any[]) => void;
50
- componentDidMount(): void;
51
- componentDidUpdate(previousProps: any): void;
52
- componentWillUnmount(): void;
53
- getPopoverTipPosition: (popoverPosition: any, containerPosition: any) => "top" | "bottom" | "right" | "left";
54
- handleKeyDown: (event: any) => void;
55
- render(): JSX.Element;
56
- }
57
- export {};
@@ -1,2 +0,0 @@
1
- import { DefaultPopoverPositions } from './types';
2
- export declare const PopoverPosition: DefaultPopoverPositions;
@@ -1,20 +0,0 @@
1
- /*********************************************************************
2
- * © Copyright IBM Corp. 2024
3
- *********************************************************************/
4
- import React from 'react';
5
- import PropTypes from 'prop-types';
6
- export declare class PositioningTarget extends React.Component<{
7
- children: (target: HTMLElement) => React.ReactNode;
8
- }, {
9
- target: HTMLElement;
10
- }> {
11
- static propTypes: {
12
- children: PropTypes.Validator<(...args: any[]) => any>;
13
- };
14
- state: {
15
- target: any;
16
- };
17
- targetRef: React.RefObject<HTMLDivElement>;
18
- componentDidMount(): void;
19
- render(): JSX.Element;
20
- }
@@ -1,32 +0,0 @@
1
- /*********************************************************************
2
- * © Copyright IBM Corp. 2024
3
- *********************************************************************/
4
- import React from 'react';
5
- import { GetPopoverPositionType } from './types';
6
- export declare type StatelessPopoverProps = {
7
- isOpen: boolean;
8
- getPopoverPosition: GetPopoverPositionType;
9
- getContainerElement?: () => HTMLElement;
10
- inline?: boolean;
11
- body?: React.ReactNode;
12
- zIndex: number;
13
- showTip?: boolean;
14
- classNameTip?: string;
15
- className?: string;
16
- outerActionEvents?: string[];
17
- onOuterAction?: (evt: Event) => void;
18
- onEscapeAction: (evt: Event) => void;
19
- trackScroll?: boolean;
20
- role?: 'tooltip' | 'dialog';
21
- trapFocus?: boolean;
22
- };
23
- export declare class StatelessPopover extends React.Component<StatelessPopoverProps> {
24
- renderPortalContent({ props, container, positioningTarget }: {
25
- props: StatelessPopoverProps;
26
- container?: HTMLElement;
27
- positioningTarget: HTMLElement;
28
- }): React.ReactPortal;
29
- getContainer: () => HTMLElement;
30
- renderPortal(positioningTarget: HTMLElement): JSX.Element;
31
- render(): JSX.Element;
32
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
package/dist/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- /*********************************************************************
2
- * © Copyright IBM Corp. 2024
3
- *********************************************************************/
4
- import { Popover, PopoverProps } from './Popover';
5
- import { PopoverBody, PopoverBodyProps } from './PopoverBody';
6
- import { GetPopoverPositionType } from './types';
7
- import { PopoverPosition } from './PopoverPosition';
8
- export { Popover, PopoverBody, PopoverProps, PopoverBodyProps, GetPopoverPositionType, PopoverPosition };
9
- export default Popover;