@targetprocess/popover 12.20.1-alpha.2 → 12.20.2-alpha.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/BodyDomNode.tsx CHANGED
@@ -1,76 +1,79 @@
1
- import React from 'react'
2
- import PropTypes from 'prop-types'
3
-
4
- export type BodyDomNodeProps = {
5
- children: (node: HTMLElement) => React.ReactNode
6
- onOuterAction?: (event: Event) => void
7
- outerActionEvents?: string[]
8
- positioningTarget: HTMLElement
9
- containerElement: HTMLElement
10
- }
11
-
12
- export class BodyDomNode extends React.Component<BodyDomNodeProps, {node: HTMLElement}> {
13
- static propTypes = {
14
- children: PropTypes.func.isRequired,
15
- onOuterAction: PropTypes.func,
16
- outerActionEvents: PropTypes.arrayOf(PropTypes.string),
17
- positioningTarget: PropTypes.instanceOf(HTMLElement),
18
- containerElement: PropTypes.instanceOf(HTMLElement)
19
- }
20
-
21
- static defaultProps = {
22
- containerElement: document.body
23
- }
24
-
25
- state = {node: null}
26
-
27
- _onEvent = event => {
28
- if (
29
- !this.state.node.contains(event.target) &&
30
- !this.props.positioningTarget.contains(event.target)
31
- ) {
32
- this.props.onOuterAction(event)
33
- }
34
- }
35
-
36
- _addDocumentEventListeners = () => {
37
- if (this.props.outerActionEvents && this.props.onOuterAction) {
38
- this.props.outerActionEvents.forEach(event => document.addEventListener(event, this._onEvent))
39
- }
40
- }
41
-
42
- _removeDocumentEventListeners = (outerActionEvents = this.props.outerActionEvents) => {
43
- if (outerActionEvents) {
44
- outerActionEvents.forEach(event => document.removeEventListener(event, this._onEvent))
45
- }
46
- }
47
-
48
- componentDidMount() {
49
- const node = document.createElement('div')
50
-
51
- this.props.containerElement.appendChild(node)
52
-
53
- this.setState({node}, this._addDocumentEventListeners)
54
- }
55
-
56
- componentDidUpdate(prevProps: BodyDomNodeProps) {
57
- if (
58
- prevProps.outerActionEvents !== this.props.outerActionEvents ||
59
- prevProps.onOuterAction !== this.props.onOuterAction
60
- ) {
61
- this._removeDocumentEventListeners(prevProps.outerActionEvents)
62
- this._addDocumentEventListeners()
63
- }
64
- }
65
-
66
- componentWillUnmount() {
67
- if (this.state.node) {
68
- this.props.containerElement.removeChild(this.state.node)
69
- this._removeDocumentEventListeners()
70
- }
71
- }
72
-
73
- render() {
74
- return this.props.children(this.state.node)
75
- }
76
- }
1
+ /*********************************************************************
2
+ * © Copyright IBM Corp. 2024
3
+ *********************************************************************/
4
+ import React from 'react'
5
+ import PropTypes from 'prop-types'
6
+
7
+ export type BodyDomNodeProps = {
8
+ children: (node: HTMLElement) => React.ReactNode
9
+ onOuterAction?: (event: Event) => void
10
+ outerActionEvents?: string[]
11
+ positioningTarget: HTMLElement
12
+ containerElement: HTMLElement
13
+ }
14
+
15
+ export class BodyDomNode extends React.Component<BodyDomNodeProps, {node: HTMLElement}> {
16
+ static propTypes = {
17
+ children: PropTypes.func.isRequired,
18
+ onOuterAction: PropTypes.func,
19
+ outerActionEvents: PropTypes.arrayOf(PropTypes.string),
20
+ positioningTarget: PropTypes.instanceOf(HTMLElement),
21
+ containerElement: PropTypes.instanceOf(HTMLElement)
22
+ }
23
+
24
+ static defaultProps = {
25
+ containerElement: document.body
26
+ }
27
+
28
+ state = {node: null}
29
+
30
+ _onEvent = event => {
31
+ if (
32
+ !this.state.node.contains(event.target) &&
33
+ !this.props.positioningTarget.contains(event.target)
34
+ ) {
35
+ this.props.onOuterAction(event)
36
+ }
37
+ }
38
+
39
+ _addDocumentEventListeners = () => {
40
+ if (this.props.outerActionEvents && this.props.onOuterAction) {
41
+ this.props.outerActionEvents.forEach(event => document.addEventListener(event, this._onEvent))
42
+ }
43
+ }
44
+
45
+ _removeDocumentEventListeners = (outerActionEvents = this.props.outerActionEvents) => {
46
+ if (outerActionEvents) {
47
+ outerActionEvents.forEach(event => document.removeEventListener(event, this._onEvent))
48
+ }
49
+ }
50
+
51
+ componentDidMount() {
52
+ const node = document.createElement('div')
53
+
54
+ this.props.containerElement.appendChild(node)
55
+
56
+ this.setState({node}, this._addDocumentEventListeners)
57
+ }
58
+
59
+ componentDidUpdate(prevProps: BodyDomNodeProps) {
60
+ if (
61
+ prevProps.outerActionEvents !== this.props.outerActionEvents ||
62
+ prevProps.onOuterAction !== this.props.onOuterAction
63
+ ) {
64
+ this._removeDocumentEventListeners(prevProps.outerActionEvents)
65
+ this._addDocumentEventListeners()
66
+ }
67
+ }
68
+
69
+ componentWillUnmount() {
70
+ if (this.state.node) {
71
+ this.props.containerElement.removeChild(this.state.node)
72
+ this._removeDocumentEventListeners()
73
+ }
74
+ }
75
+
76
+ render() {
77
+ return this.props.children(this.state.node)
78
+ }
79
+ }
package/CHANGELOG.md CHANGED
@@ -1,22 +1,22 @@
1
- #### v7.5.1
2
-
3
- - Add position re-calculation on `resize` event.
4
- - Use `React.createPortal` instead of `ReactDOM.unstable_renderSubtreeIntoContainer` for correcte context propogation to popover's content.
5
- - Remove deprecated `ReactDOM.findDOMNode`.
6
-
7
- #### v7.1.0
8
-
9
- - Fixed incorrect throttle function implementation
10
- - (breaking change) Now uses automatic style injection - no style import/`@global-import` is needed
11
-
12
- #### v3.0.0
13
-
14
- - Removed dependency on `lodash-es`.
15
-
16
- #### 2.0.4
17
-
18
- - Update box shadow colors to use transparency in `@targetprocess/global-constants`.
19
-
20
- #### v2.0.0
21
-
22
- - Fixed missing `module` field in `package.json`, which could affect some builds that rely on using CommonJS build of Popover.
1
+ #### v7.5.1
2
+
3
+ - Add position re-calculation on `resize` event.
4
+ - Use `React.createPortal` instead of `ReactDOM.unstable_renderSubtreeIntoContainer` for correcte context propogation to popover's content.
5
+ - Remove deprecated `ReactDOM.findDOMNode`.
6
+
7
+ #### v7.1.0
8
+
9
+ - Fixed incorrect throttle function implementation
10
+ - (breaking change) Now uses automatic style injection - no style import/`@global-import` is needed
11
+
12
+ #### v3.0.0
13
+
14
+ - Removed dependency on `lodash-es`.
15
+
16
+ #### 2.0.4
17
+
18
+ - Update box shadow colors to use transparency in `@targetprocess/global-constants`.
19
+
20
+ #### v2.0.0
21
+
22
+ - Fixed missing `module` field in `package.json`, which could affect some builds that rely on using CommonJS build of Popover.
package/Popover.tsx CHANGED
@@ -1,188 +1,191 @@
1
- import React, {createRef} from 'react'
2
- import PropTypes from 'prop-types'
3
- import {PopoverPosition} from './PopoverPosition'
4
- import {PopoverBody} from './PopoverBody'
5
- import {StatelessPopover} from './StatlessPopover'
6
- import {GetPopoverPositionType} from './types'
7
-
8
- /**
9
- * Popover is react component for displaying some toggleable content near
10
- * other elements.
11
- *
12
- * By itself, `<Popover>` component is pass-through, i.e. it only renders
13
- * its `children` without any other side effects. Its `body` prop accepts any
14
- * react element, and when `isOpen` is set to `true`, it renders `body` in a
15
- * portal and appends it to the element returned by `getContainerElement`
16
- * (or to `document.body`, if it's not provided).
17
- *
18
- * By default, `body` doesn't add any additional styling. You can use
19
- * `Popover.Body` component if you need a container with commonly styled
20
- * box shadow and rounded corners.
21
- *
22
- * Popover accepts render props children, in which case it can use its
23
- * own internal state to work in uncontrolled mode. In this mode, however,
24
- * `isOpen` prop on `Popover` takes preceedence over internal state.
25
- * Render prop function signature is
26
- * `({isOpen: boolean, setOpened: (isOpen: boolean) => void}) => React.Element`;
27
- * children can use `isOpen` to adjust their own view and call setOpened to open
28
- * or close the popover:
29
- * ```jsx
30
- * <Popover body={<Popover.Body>hello world!</Popover.Body>}>
31
- * {({isOpen, setOpened}) => (
32
- * <button onClick={() => setOpened(!isOpen)}>
33
- * Toggle popover
34
- * </button>
35
- * )}
36
- * </Popover>
37
- * ```
38
- *
39
- * Popover positioning can be configured using `getPopoverPosition` function.
40
- * It also accepts any of the 12 positions as strings:
41
- * `'bottomLeft', 'bottom', 'bottomRight', 'topLeft', 'top', 'topRight',
42
- * 'leftTop', 'left', 'leftBottom', 'rightTop', 'right', 'rightBottom'`.
43
- *
44
- * `getPopoverPosition` accepts 3 arguments: trigger dimensions (x, y,
45
- * wdith and height), body dimensions as DOMRect (x, y, width and height)
46
- * and container element scroll offsets. It is expected to return any CSS
47
- * style modifiers that will be then applied to the popover itself.
48
- *
49
- * For example, default `'bottom'` position is implemented as follows:
50
- * ```js
51
- * bottom: (hostRect, popoverRect, containerScroll) => ({
52
- * left: '50%',
53
- * marginLeft: -popoverRect.width / 2,
54
- * top: hostRect.height + parseInt(SpaceXS) - containerScroll.top
55
- * }),
56
- * ```
57
- *
58
- * Default positioning functions are also avaliable from `Popover.Position`
59
- * object, so you can reuse them in certain situations. For example, if you
60
- * want to use `bottom` positioning but move it 5 pixels higher, you can do
61
- * it like this:
62
- * ```js
63
- * const getPopoverPosition = (...args) => {
64
- * const defaultBottomPosition = Popover.Position.bottom(...args);
65
- * return {...defaultBottomPosition, top: defaultBottomPosition - 5}
66
- * }
67
- * ```
68
- *
69
- * You can also use `showTip` to display a triangle near popover.
70
- * Triangle position is calculated automatically depending on relative popover
71
- * position; it is always placed at the center of the trigger from the according side.
72
- */
73
- export type PopoverProps = {
74
- closeOnOuterAction?: boolean
75
- isOpen?: boolean
76
- getPopoverPosition: GetPopoverPositionType
77
- onOuterAction?: (evt: Event) => void
78
- outerActionEvents?: string[]
79
- getContainerElement?: () => HTMLElement
80
- inline?: boolean
81
- body?: React.ReactNode
82
- zIndex: number
83
- showTip?: boolean
84
- className?: string
85
- trackScroll?: boolean
86
- role?: 'tooltip' | 'dialog'
87
- trapFocus?: boolean
88
- }
89
-
90
- export type PopoverState = {
91
- isOpen: boolean
92
- }
93
-
94
- export class Popover extends React.Component<PopoverProps, PopoverState> {
95
- static Body = PopoverBody
96
- static Position = PopoverPosition
97
-
98
- static propTypes = {
99
- /** Class name of toplevel popover container */
100
- className: PropTypes.string,
101
- /** Wether popover is opened or closed */
102
- isOpen: PropTypes.bool,
103
- /** Element that will be mounted within the popover */
104
- body: PropTypes.element,
105
- /** Called when one of the `outerActionEvents` occurs */
106
- onOuterAction: PropTypes.func,
107
- /** Must return DOM element to append popover to. */
108
- getContainerElement: PropTypes.func,
109
- /** Wether to show the triangle. */
110
- showTip: PropTypes.bool,
111
- /** Position string or positioning function. */
112
- getPopoverPosition: PropTypes.oneOfType([
113
- PropTypes.oneOf(Object.keys(PopoverPosition)),
114
- PropTypes.func
115
- ]),
116
- /**
117
- * Events that trigger `onOuterAction`.
118
- */
119
- outerActionEvents: PropTypes.arrayOf(PropTypes.string),
120
- /** Whether uncontrolled (render props) popover should be closed on outer action */
121
- closeOnOuterAction: PropTypes.bool,
122
- noPadding: PropTypes.bool,
123
- zIndex: PropTypes.number,
124
- trackScroll: PropTypes.bool,
125
- children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
126
- role: PropTypes.oneOf(['tooltip', 'dialog']),
127
- trapFocus: PropTypes.bool
128
- }
129
-
130
- static defaultProps = {
131
- zIndex: 19999,
132
- outerActionEvents: ['mousedown', 'touchstart'],
133
- getPopoverPosition: 'bottomLeft',
134
- role: 'dialog',
135
- trapFocus: 'true'
136
- }
137
-
138
- state = {
139
- isOpen: false
140
- }
141
-
142
- setOpened = (isOpen: boolean) => {
143
- this.setState({isOpen})
144
- }
145
-
146
- onOuterAction = (event: Event) => {
147
- if (this.props.closeOnOuterAction) {
148
- this.setOpened(false)
149
- }
150
- if (this.props.onOuterAction) {
151
- this.props.onOuterAction(event)
152
- }
153
- }
154
-
155
- onEscapeAction = (event: Event) => {
156
- this.setOpened(false)
157
- if (this.props.onOuterAction) {
158
- this.props.onOuterAction(event)
159
- }
160
- this.childrenRef.current?.focus()
161
- }
162
-
163
- childrenRef = createRef<HTMLElement>()
164
-
165
- render() {
166
- const isOpen = typeof this.props.isOpen === 'undefined' ? this.state.isOpen : this.props.isOpen
167
- const children =
168
- typeof this.props.children === 'function'
169
- ? this.props.children({isOpen, setOpened: this.setOpened, ref: this.childrenRef})
170
- : this.props.children
171
- const childrenWithRef = React.Children.map(children, child =>
172
- React.cloneElement(child, {ref: this.childrenRef})
173
- )
174
-
175
- return (
176
- <StatelessPopover
177
- {...this.props}
178
- onOuterAction={this.onOuterAction}
179
- onEscapeAction={this.onEscapeAction}
180
- isOpen={isOpen}
181
- role={this.props.role}
182
- trapFocus={this.props.trapFocus}
183
- >
184
- {childrenWithRef}
185
- </StatelessPopover>
186
- )
187
- }
188
- }
1
+ /*********************************************************************
2
+ * © Copyright IBM Corp. 2024
3
+ *********************************************************************/
4
+ import React, {createRef} from 'react'
5
+ import PropTypes from 'prop-types'
6
+ import {PopoverPosition} from './PopoverPosition'
7
+ import {PopoverBody} from './PopoverBody'
8
+ import {StatelessPopover} from './StatlessPopover'
9
+ import {GetPopoverPositionType} from './types'
10
+
11
+ /**
12
+ * Popover is react component for displaying some toggleable content near
13
+ * other elements.
14
+ *
15
+ * By itself, `<Popover>` component is pass-through, i.e. it only renders
16
+ * its `children` without any other side effects. Its `body` prop accepts any
17
+ * react element, and when `isOpen` is set to `true`, it renders `body` in a
18
+ * portal and appends it to the element returned by `getContainerElement`
19
+ * (or to `document.body`, if it's not provided).
20
+ *
21
+ * By default, `body` doesn't add any additional styling. You can use
22
+ * `Popover.Body` component if you need a container with commonly styled
23
+ * box shadow and rounded corners.
24
+ *
25
+ * Popover accepts render props children, in which case it can use its
26
+ * own internal state to work in uncontrolled mode. In this mode, however,
27
+ * `isOpen` prop on `Popover` takes preceedence over internal state.
28
+ * Render prop function signature is
29
+ * `({isOpen: boolean, setOpened: (isOpen: boolean) => void}) => React.Element`;
30
+ * children can use `isOpen` to adjust their own view and call setOpened to open
31
+ * or close the popover:
32
+ * ```jsx
33
+ * <Popover body={<Popover.Body>hello world!</Popover.Body>}>
34
+ * {({isOpen, setOpened}) => (
35
+ * <button onClick={() => setOpened(!isOpen)}>
36
+ * Toggle popover
37
+ * </button>
38
+ * )}
39
+ * </Popover>
40
+ * ```
41
+ *
42
+ * Popover positioning can be configured using `getPopoverPosition` function.
43
+ * It also accepts any of the 12 positions as strings:
44
+ * `'bottomLeft', 'bottom', 'bottomRight', 'topLeft', 'top', 'topRight',
45
+ * 'leftTop', 'left', 'leftBottom', 'rightTop', 'right', 'rightBottom'`.
46
+ *
47
+ * `getPopoverPosition` accepts 3 arguments: trigger dimensions (x, y,
48
+ * wdith and height), body dimensions as DOMRect (x, y, width and height)
49
+ * and container element scroll offsets. It is expected to return any CSS
50
+ * style modifiers that will be then applied to the popover itself.
51
+ *
52
+ * For example, default `'bottom'` position is implemented as follows:
53
+ * ```js
54
+ * bottom: (hostRect, popoverRect, containerScroll) => ({
55
+ * left: '50%',
56
+ * marginLeft: -popoverRect.width / 2,
57
+ * top: hostRect.height + parseInt(SpaceXS) - containerScroll.top
58
+ * }),
59
+ * ```
60
+ *
61
+ * Default positioning functions are also avaliable from `Popover.Position`
62
+ * object, so you can reuse them in certain situations. For example, if you
63
+ * want to use `bottom` positioning but move it 5 pixels higher, you can do
64
+ * it like this:
65
+ * ```js
66
+ * const getPopoverPosition = (...args) => {
67
+ * const defaultBottomPosition = Popover.Position.bottom(...args);
68
+ * return {...defaultBottomPosition, top: defaultBottomPosition - 5}
69
+ * }
70
+ * ```
71
+ *
72
+ * You can also use `showTip` to display a triangle near popover.
73
+ * Triangle position is calculated automatically depending on relative popover
74
+ * position; it is always placed at the center of the trigger from the according side.
75
+ */
76
+ export type PopoverProps = {
77
+ closeOnOuterAction?: boolean
78
+ isOpen?: boolean
79
+ getPopoverPosition: GetPopoverPositionType
80
+ onOuterAction?: (evt: Event) => void
81
+ outerActionEvents?: string[]
82
+ getContainerElement?: () => HTMLElement
83
+ inline?: boolean
84
+ body?: React.ReactNode
85
+ zIndex: number
86
+ showTip?: boolean
87
+ className?: string
88
+ trackScroll?: boolean
89
+ role?: 'tooltip' | 'dialog'
90
+ trapFocus?: boolean
91
+ }
92
+
93
+ export type PopoverState = {
94
+ isOpen: boolean
95
+ }
96
+
97
+ export class Popover extends React.Component<PopoverProps, PopoverState> {
98
+ static Body = PopoverBody
99
+ static Position = PopoverPosition
100
+
101
+ static propTypes = {
102
+ /** Class name of toplevel popover container */
103
+ className: PropTypes.string,
104
+ /** Wether popover is opened or closed */
105
+ isOpen: PropTypes.bool,
106
+ /** Element that will be mounted within the popover */
107
+ body: PropTypes.element,
108
+ /** Called when one of the `outerActionEvents` occurs */
109
+ onOuterAction: PropTypes.func,
110
+ /** Must return DOM element to append popover to. */
111
+ getContainerElement: PropTypes.func,
112
+ /** Wether to show the triangle. */
113
+ showTip: PropTypes.bool,
114
+ /** Position string or positioning function. */
115
+ getPopoverPosition: PropTypes.oneOfType([
116
+ PropTypes.oneOf(Object.keys(PopoverPosition)),
117
+ PropTypes.func
118
+ ]),
119
+ /**
120
+ * Events that trigger `onOuterAction`.
121
+ */
122
+ outerActionEvents: PropTypes.arrayOf(PropTypes.string),
123
+ /** Whether uncontrolled (render props) popover should be closed on outer action */
124
+ closeOnOuterAction: PropTypes.bool,
125
+ noPadding: PropTypes.bool,
126
+ zIndex: PropTypes.number,
127
+ trackScroll: PropTypes.bool,
128
+ children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
129
+ role: PropTypes.oneOf(['tooltip', 'dialog']),
130
+ trapFocus: PropTypes.bool
131
+ }
132
+
133
+ static defaultProps = {
134
+ zIndex: 19999,
135
+ outerActionEvents: ['mousedown', 'touchstart'],
136
+ getPopoverPosition: 'bottomLeft',
137
+ role: 'dialog',
138
+ trapFocus: 'true'
139
+ }
140
+
141
+ state = {
142
+ isOpen: false
143
+ }
144
+
145
+ setOpened = (isOpen: boolean) => {
146
+ this.setState({isOpen})
147
+ }
148
+
149
+ onOuterAction = (event: Event) => {
150
+ if (this.props.closeOnOuterAction) {
151
+ this.setOpened(false)
152
+ }
153
+ if (this.props.onOuterAction) {
154
+ this.props.onOuterAction(event)
155
+ }
156
+ }
157
+
158
+ onEscapeAction = (event: Event) => {
159
+ this.setOpened(false)
160
+ if (this.props.onOuterAction) {
161
+ this.props.onOuterAction(event)
162
+ }
163
+ this.childrenRef.current?.focus()
164
+ }
165
+
166
+ childrenRef = createRef<HTMLElement>()
167
+
168
+ render() {
169
+ const isOpen = typeof this.props.isOpen === 'undefined' ? this.state.isOpen : this.props.isOpen
170
+ const children =
171
+ typeof this.props.children === 'function'
172
+ ? this.props.children({isOpen, setOpened: this.setOpened, ref: this.childrenRef})
173
+ : this.props.children
174
+ const childrenWithRef = React.Children.map(children, child =>
175
+ React.cloneElement(child, {ref: this.childrenRef})
176
+ )
177
+
178
+ return (
179
+ <StatelessPopover
180
+ {...this.props}
181
+ onOuterAction={this.onOuterAction}
182
+ onEscapeAction={this.onEscapeAction}
183
+ isOpen={isOpen}
184
+ role={this.props.role}
185
+ trapFocus={this.props.trapFocus}
186
+ >
187
+ {childrenWithRef}
188
+ </StatelessPopover>
189
+ )
190
+ }
191
+ }