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

Commit a785b98

Browse files
committed
fix: Align should support svg
1 parent cf110a4 commit a785b98

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

examples/follow.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ const Demo = () => {
77
const [left, setLeft] = React.useState(100);
88
const [top, setTop] = React.useState(100);
99
const [visible, setVisible] = React.useState(true);
10+
const [svg, setSvg] = React.useState(false);
11+
12+
const sharedStyle: React.CSSProperties = {
13+
width,
14+
height,
15+
position: 'absolute',
16+
left,
17+
top,
18+
display: visible ? 'flex' : 'none',
19+
};
1020

1121
return (
1222
<div>
@@ -29,6 +39,14 @@ const Demo = () => {
2939
>
3040
Trigger Visible
3141
</button>
42+
<button
43+
type="button"
44+
onClick={() => {
45+
setSvg(!svg);
46+
}}
47+
>
48+
Follow SVG ({String(svg)})
49+
</button>
3250

3351
<div
3452
style={{
@@ -38,27 +56,26 @@ const Demo = () => {
3856
position: 'relative',
3957
}}
4058
>
41-
<div
42-
style={{
43-
width,
44-
height,
45-
border: '1px solid green',
46-
alignItems: 'center',
47-
justifyContent: 'center',
48-
position: 'absolute',
49-
left,
50-
top,
51-
display: visible ? 'flex' : 'none',
52-
}}
53-
id="content"
54-
>
55-
Content
56-
</div>
59+
{svg ? (
60+
<svg id="content" width={width} height={height} style={sharedStyle}>
61+
<rect x="0" y="0" width={width} height={height} />
62+
</svg>
63+
) : (
64+
<div
65+
style={{
66+
border: '1px solid green',
67+
boxSizing: 'border-box',
68+
alignItems: 'center',
69+
justifyContent: 'center',
70+
...sharedStyle,
71+
}}
72+
id="content"
73+
>
74+
Content
75+
</div>
76+
)}
5777

58-
<Align
59-
target={() => document.getElementById('content')}
60-
align={{ points: ['tc', 'bc'] }}
61-
>
78+
<Align target={() => document.getElementById('content')} align={{ points: ['tc', 'bc'] }}>
6279
<div
6380
style={{
6481
border: '1px solid blue',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"@babel/runtime": "^7.10.1",
4343
"classnames": "2.x",
4444
"dom-align": "^1.7.0",
45-
"rc-util": "^5.0.1",
45+
"rc-util": "^5.3.0",
4646
"resize-observer-polyfill": "^1.5.1"
4747
},
4848
"devDependencies": {

src/Align.tsx

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

66
import React from 'react';
77
import { composeRef } from 'rc-util/lib/ref';
8+
import isVisible from 'rc-util/lib/Dom/isVisible';
89
import { alignElement, alignPoint } from 'dom-align';
910
import addEventListener from 'rc-util/lib/Dom/addEventListener';
1011

@@ -79,7 +80,7 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
7980
const { activeElement } = document;
8081

8182
// We only align when element is visible
82-
if (element && element.offsetParent) {
83+
if (element && isVisible(element)) {
8384
result = alignElement(source, element, align);
8485
} else if (point) {
8586
result = alignPoint(source, point, align);

0 commit comments

Comments
 (0)