Skip to content

Commit ff811dc

Browse files
authored
feat: Add targetOffset back (#327)
1 parent 74d2d53 commit ff811dc

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

docs/examples/container.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const builtinPlacements = {
1010
shiftX: 50,
1111
adjustY: true,
1212
},
13-
offset: [0, -50],
13+
offset: [0, 0],
14+
targetOffset: [10, 0],
1415
},
1516
bottomLeft: {
1617
points: ['tl', 'bl'],

src/hooks/useAlign.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,23 @@ export default function useAlign(
122122
popupElement.style.top = '0';
123123

124124
// Calculate align style, we should consider `transform` case
125-
const targetRect = Array.isArray(target)
126-
? {
127-
x: target[0],
128-
y: target[1],
129-
width: 0,
130-
height: 0,
131-
}
132-
: target.getBoundingClientRect();
125+
let targetRect: Rect;
126+
if (Array.isArray(target)) {
127+
targetRect = {
128+
x: target[0],
129+
y: target[1],
130+
width: 0,
131+
height: 0,
132+
};
133+
} else {
134+
const rect = target.getBoundingClientRect();
135+
targetRect = {
136+
x: rect.x,
137+
y: rect.y,
138+
width: rect.width,
139+
height: rect.height,
140+
};
141+
}
133142
const popupRect = popupElement.getBoundingClientRect();
134143
const { width, height } = win.getComputedStyle(popupElement);
135144
const { clientWidth, clientHeight } = doc.documentElement;
@@ -152,6 +161,16 @@ export default function useAlign(
152161
// Placement
153162
const placementInfo: AlignType =
154163
builtinPlacements[placement] || popupAlign || {};
164+
165+
// Offset
166+
const { offset, targetOffset } = placementInfo;
167+
const [popupOffsetX = 0, popupOffsetY = 0] = offset || [];
168+
const [targetOffsetX = 0, targetOffsetY = 0] = targetOffset || [];
169+
170+
targetRect.x += targetOffsetX;
171+
targetRect.y += targetOffsetY;
172+
173+
// Points
155174
const [popupPoint, targetPoint] = placementInfo.points || [];
156175
const targetPoints = splitPoints(targetPoint);
157176
const popupPoints = splitPoints(popupPoint);
@@ -164,11 +183,7 @@ export default function useAlign(
164183
...placementInfo,
165184
};
166185

167-
// Offset
168-
const { offset } = placementInfo;
169-
const [popupOffsetX = 0, popupOffsetY = 0] = offset || [];
170-
171-
// Placement
186+
// Next Offset
172187
let nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
173188
let nextOffsetY = targetAlignPoint.y - popupAlignPoint.y + popupOffsetY;
174189

src/interface.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ export interface AlignType {
3535
* offset target node by offset[0] in x and offset[1] in y.
3636
* If targetOffset contains percentage string value, it is relative to targetNode region.
3737
*/
38-
// zombieJ: removed in major version. Just keep def comment here in case we need it back.
39-
// targetOffset?: number[];
38+
targetOffset?: number[];
4039
/**
4140
* If adjustX field is true, will adjust source node in x direction if source node is invisible.
4241
* If adjustY field is true, will adjust source node in y direction if source node is invisible.

0 commit comments

Comments
 (0)