Skip to content

Commit 6784d0c

Browse files
committed
1.2.0
1 parent 281e6a1 commit 6784d0c

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.0
2+
- Reposition Popover for resize and scroll events on viewport
3+
- Add scrollRef prop to give control to user for which scroll events to listen to
4+
15
## 1.0.2
26
- Popover opens even when selectionWidth is 0
37

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,34 @@ You might still want to use selection events to control whether the Popover is s
8888
>Hey there</Popover>
8989
```
9090

91+
### Using Popover inside a scrollable element
92+
93+
Some applications have scrollable elements inside them other than `<body>`. By default Popover repositions itself when the viewport scrolls (`window` that is). If you're using the `Popover` inside a scrollable element you need to define the `scrollRef` prop like so:
94+
95+
```
96+
class MyApp extends Component {
97+
constructor() {
98+
this.scrollRef = React.createRef()
99+
}
100+
101+
render() {
102+
return <div>
103+
<main ref={this.scrollRef}>
104+
</main>
105+
<Popover scrollRef={this.scrollRef}>My popover</Popover>
106+
</div>
107+
}
108+
}
109+
```
110+
91111
For more info on how to use the `Popover` component, please see below :)
92112

93113
## `<Popover />` Props
94114

95115
| Property | Type | required? | Description |
96116
| - | - | - | - |
97117
| `selectionRef` | `React.ref` | optional | Set this to constrain selection events to a dom element and its children. You need this especially if you use more than one Popover component __(defaults to `document`)__ |
118+
| `scrollRef` | `React.ref` | optional | By default Popover repositions itself when there's a scroll event for document.body. Set this to reposition the Popover upon scrolling of a given element |
98119
| `isOpen` | `Boolean` | optional | Is the Popover visible or not __(defaults to `true`)__ |
99120
| `onTextSelect` | `Function` | optional | Callback for when text is selected (typically used for setting state that opens the modal) |
100121
| `onTextUnSelect` | `Function` | optional | Callback for when selection collapses (typically used for setting state that closes the modal) |
@@ -120,4 +141,6 @@ write test for all states:
120141
- Test isOpen overrides anything internal
121142
- Test placement strategies
122143
- Test refs
144+
- Test scroll events
145+
- Test resize events
123146
- Test events being only inside selection ref

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-text-selection-popover",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "A popover component positioned according to the current selection in a contenteditable element",
55
"author": "juliankrispel",
66
"license": "MIT",

src/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import centerAboveOrBelow from "./centerAboveOrBelow";
1111
class Popover extends Component {
1212
static defaultProps = {
1313
selectionRef: { current: document.body },
14+
scrollRef: { current: window },
1415
placementStrategy: centerAboveOrBelow,
1516
gap: 5,
1617
};
@@ -56,6 +57,7 @@ class Popover extends Component {
5657
selectionRef,
5758
measureRef,
5859
gap,
60+
scrollRef,
5961
placementStrategy,
6062
contentRect,
6163
windowHeight,
@@ -105,9 +107,19 @@ class Popover extends Component {
105107
target={document}
106108
onSelectionChange={this.updatePosition}
107109
/>,
110+
<EventListener
111+
key="on-resize-window"
112+
target={window}
113+
onResize={this.updatePosition}
114+
/>,
115+
<EventListener
116+
key="on-scroll"
117+
target={scrollRef && scrollRef.current ? scrollRef.current : window}
118+
onScroll={this.updatePosition}
119+
/>,
108120
<EventListener
109121
key="on-mouse-up"
110-
target={selectionRef && selectionRef.current ? selectionRef.current : document}
122+
target={selectionRef && selectionRef.current ? selectionRef.current : document.body}
111123
onMouseUp={() => this.setState({ mousePressed: false })}
112124
/>,
113125
<EventListener
@@ -137,6 +149,12 @@ Popover.propTypes = {
137149
selectionRef: PropTypes.shape({
138150
current: PropTypes.instanceOf(Element)
139151
}),
152+
scrollRef: PropTypes.shape({
153+
current: PropTypes.oneOfType([
154+
PropTypes.instanceOf(Element),
155+
PropTypes.instanceOf(window.constructor)
156+
])
157+
}),
140158
children: PropTypes.node.isRequired,
141159
onTextSelect: PropTypes.func,
142160
onTextUnselect: PropTypes.func,

0 commit comments

Comments
 (0)