Skip to content

Commit e1c6fd1

Browse files
committed
chore: get a hooks
1 parent a7ff7d2 commit e1c6fd1

File tree

2 files changed

+64
-39
lines changed

2 files changed

+64
-39
lines changed

src/Popup/index.tsx

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { AlignType, ArrowPos, ArrowTypeOuter } from '../interface';
1010
import Arrow from './Arrow';
1111
import Mask from './Mask';
1212
import PopupContent from './PopupContent';
13+
import useOffsetStyle from '../hooks/useOffsetStyle';
1314

1415
export interface MobileConfig {
1516
mask?: boolean;
@@ -175,49 +176,23 @@ const Popup = React.forwardRef<HTMLDivElement, PopupProps>((props, ref) => {
175176
}
176177
}, [show, getPopupContainerNeedParams, target]);
177178

179+
// ========================= Styles =========================
180+
const offsetStyle = useOffsetStyle(
181+
isMobile,
182+
ready,
183+
open,
184+
align,
185+
offsetR,
186+
offsetB,
187+
offsetX,
188+
offsetY,
189+
);
190+
178191
// ========================= Render =========================
179192
if (!show) {
180193
return null;
181194
}
182195

183-
// TODO: Move offsetStyle logic to useOffsetStyle.ts hooks
184-
// >>>>> Offset
185-
const AUTO = 'auto' as const;
186-
187-
const offsetStyle: React.CSSProperties = isMobile
188-
? {}
189-
: {
190-
left: '-1000vw',
191-
top: '-1000vh',
192-
right: AUTO,
193-
bottom: AUTO,
194-
};
195-
196-
// Set align style
197-
if (!isMobile && (ready || !open)) {
198-
const { points } = align;
199-
const dynamicInset =
200-
align.dynamicInset || (align as any)._experimental?.dynamicInset;
201-
const alignRight = dynamicInset && points[0][1] === 'r';
202-
const alignBottom = dynamicInset && points[0][0] === 'b';
203-
204-
if (alignRight) {
205-
offsetStyle.right = offsetR;
206-
offsetStyle.left = AUTO;
207-
} else {
208-
offsetStyle.left = offsetX;
209-
offsetStyle.right = AUTO;
210-
}
211-
212-
if (alignBottom) {
213-
offsetStyle.bottom = offsetB;
214-
offsetStyle.top = AUTO;
215-
} else {
216-
offsetStyle.top = offsetY;
217-
offsetStyle.bottom = AUTO;
218-
}
219-
}
220-
221196
// >>>>> Misc
222197
const miscStyle: React.CSSProperties = {};
223198
if (stretch) {

src/hooks/useOffsetStyle.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,53 @@
11
import type * as React from 'react';
2+
import type { AlignType } from '../interface';
23

3-
export default function useAlignStyle() {}
4+
export default function useOffsetStyle(
5+
isMobile: boolean,
6+
ready: boolean,
7+
open: boolean,
8+
align: AlignType,
9+
offsetR: number,
10+
offsetB: number,
11+
offsetX: number,
12+
offsetY: number,
13+
) {
14+
// TODO: Move offsetStyle logic to useOffsetStyle.ts hooks
15+
// >>>>> Offset
16+
const AUTO = 'auto' as const;
17+
18+
const offsetStyle: React.CSSProperties = isMobile
19+
? {}
20+
: {
21+
left: '-1000vw',
22+
top: '-1000vh',
23+
right: AUTO,
24+
bottom: AUTO,
25+
};
26+
27+
// Set align style
28+
if (!isMobile && (ready || !open)) {
29+
const { points } = align;
30+
const dynamicInset =
31+
align.dynamicInset || (align as any)._experimental?.dynamicInset;
32+
const alignRight = dynamicInset && points[0][1] === 'r';
33+
const alignBottom = dynamicInset && points[0][0] === 'b';
34+
35+
if (alignRight) {
36+
offsetStyle.right = offsetR;
37+
offsetStyle.left = AUTO;
38+
} else {
39+
offsetStyle.left = offsetX;
40+
offsetStyle.right = AUTO;
41+
}
42+
43+
if (alignBottom) {
44+
offsetStyle.bottom = offsetB;
45+
offsetStyle.top = AUTO;
46+
} else {
47+
offsetStyle.top = offsetY;
48+
offsetStyle.bottom = AUTO;
49+
}
50+
}
51+
52+
return offsetStyle;
53+
}

0 commit comments

Comments
 (0)