Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 4338327

Browse files
committed
move back to controlling DOM updates through React
1 parent df55899 commit 4338327

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

src/Popper.jsx

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,26 @@ class Popper extends Component {
6363
return this.context.popperManager.getTargetNode()
6464
}
6565

66+
_getOffsets = data => {
67+
return Object.keys(data.offsets).map(key => data.offsets[key])
68+
}
69+
70+
_isDataDirty = data => {
71+
if (this.state.data) {
72+
return (
73+
JSON.stringify(this._getOffsets(this.state.data)) !==
74+
JSON.stringify(this._getOffsets(data))
75+
)
76+
} else {
77+
return true
78+
}
79+
}
80+
6681
_updateStateModifier = {
6782
enabled: true,
6883
order: 900,
6984
fn: data => {
70-
if (
71-
data.placement !== this.state.placement ||
72-
data.hide !== this.state.hide
73-
) {
85+
if (this._isDataDirty(data)) {
7486
this.setState({ data })
7587
}
7688
return data
@@ -81,6 +93,7 @@ class Popper extends Component {
8193
const { placement, eventsEnabled } = this.props
8294
const modifiers = {
8395
...this.props.modifiers,
96+
applyStyle: { enabled: false },
8497
updateState: this._updateStateModifier,
8598
}
8699
if (this._arrowNode) {
@@ -93,7 +106,9 @@ class Popper extends Component {
93106
eventsEnabled,
94107
modifiers,
95108
})
96-
this._scheduleUpdate()
109+
110+
// TODO: look into setTimeout scheduleUpdate call, without it, the popper will not position properly on creation
111+
setTimeout(() => this._scheduleUpdate())
97112
}
98113

99114
_destroyPopper() {
@@ -102,6 +117,23 @@ class Popper extends Component {
102117
}
103118
}
104119

120+
_getPopperStyle = () => {
121+
const { data } = this.state
122+
123+
if (!this._popper || !data) {
124+
return {
125+
position: 'absolute',
126+
pointerEvents: 'none',
127+
opacity: 0,
128+
}
129+
}
130+
131+
return {
132+
position: data.offsets.popper.position,
133+
...data.styles,
134+
}
135+
}
136+
105137
_getPopperPlacement = () => {
106138
return this.state.data ? this.state.data.placement : undefined
107139
}
@@ -145,12 +177,14 @@ class Popper extends Component {
145177
children,
146178
...restProps
147179
} = this.props
180+
const popperStyle = this._getPopperStyle()
148181
const popperPlacement = this._getPopperPlacement()
149182
const popperHide = this._getPopperHide()
150183

151184
if (typeof children === 'function') {
152185
const popperProps = {
153186
ref: this._handlePopperRef,
187+
style: popperStyle,
154188
'data-placement': popperPlacement,
155189
'data-x-out-of-boundaries': popperHide,
156190
}
@@ -163,6 +197,10 @@ class Popper extends Component {
163197

164198
const componentProps = {
165199
...restProps,
200+
style: {
201+
...restProps.style,
202+
...popperStyle,
203+
},
166204
'data-placement': popperPlacement,
167205
'data-x-out-of-boundaries': popperHide,
168206
}

0 commit comments

Comments
 (0)