Skip to content
This repository was archived by the owner on Nov 4, 2025. It is now read-only.

Commit 6ecc42b

Browse files
shaodahongzombieJ
authored andcommitted
use monitorResize (#35)
1 parent fc51002 commit 6ecc42b

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

examples/simple.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Test extends Component {
1010
align: {
1111
points: ['cc', 'cc'],
1212
},
13+
sourceWidth: 50,
1314
};
1415

1516
componentDidMount() {
@@ -65,6 +66,13 @@ class Test extends Component {
6566
this.$align.forceAlign();
6667
};
6768

69+
toggleSourceSize = () => {
70+
this.setState({
71+
// eslint-disable-next-line react/no-access-state-in-setstate
72+
sourceWidth: this.state.sourceWidth + 10,
73+
});
74+
};
75+
6876
render() {
6977
const { random, randomWidth } = this.state;
7078

@@ -79,6 +87,10 @@ class Test extends Component {
7987
Force align
8088
</button>
8189
&nbsp;&nbsp;&nbsp;
90+
<button type="button" onClick={this.toggleSourceSize}>
91+
Resize Source
92+
</button>
93+
&nbsp;&nbsp;&nbsp;
8294
<label>
8395
<input type="checkbox" checked={this.state.monitor} onChange={this.toggleMonitor} />
8496
&nbsp; Monitor window resize
@@ -117,7 +129,7 @@ class Test extends Component {
117129
<div
118130
style={{
119131
position: 'absolute',
120-
width: 50,
132+
width: this.state.sourceWidth,
121133
height: 50,
122134
background: 'yellow',
123135
}}

src/Align.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import React from 'react';
77
import { composeRef } from 'rc-util/lib/ref';
8-
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
98
import { alignElement, alignPoint } from 'dom-align';
109
import addEventListener from 'rc-util/lib/Dom/addEventListener';
1110

@@ -66,7 +65,7 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
6665
const [forceAlign, cancelForceAlign] = useBuffer(() => {
6766
const { disabled: latestDisabled, target: latestTarget } = forceAlignPropsRef.current;
6867
if (!latestDisabled && latestTarget) {
69-
const source = findDOMNode<HTMLElement>(nodeRef.current);
68+
const source = nodeRef.current;
7069

7170
let result: AlignResult;
7271
const element = getElement(latestTarget);
@@ -102,10 +101,20 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
102101
const resizeMonitor = React.useRef<MonitorRef>({
103102
cancel: () => {},
104103
});
104+
// Listen for source updated
105+
const sourceResizeMonitor = React.useRef<MonitorRef>({
106+
cancel: () => {},
107+
});
105108
React.useEffect(() => {
106109
const element = getElement(target);
107110
const point = getPoint(target);
108111

112+
if (nodeRef.current !== sourceResizeMonitor.current.element) {
113+
sourceResizeMonitor.current.cancel();
114+
sourceResizeMonitor.current.element = nodeRef.current;
115+
sourceResizeMonitor.current.cancel = monitorResize(nodeRef.current, forceAlign);
116+
}
117+
109118
if (cacheRef.current.element !== element || !isSamePoint(cacheRef.current.point, point)) {
110119
forceAlign();
111120

@@ -144,6 +153,7 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
144153
React.useEffect(
145154
() => () => {
146155
resizeMonitor.current.cancel();
156+
sourceResizeMonitor.current.cancel();
147157
if (winResizeRef.current) winResizeRef.current.remove();
148158
cancelForceAlign();
149159
},

0 commit comments

Comments
 (0)