Skip to content

Commit 2579ec5

Browse files
committed
chore: floating
1 parent e1c6fd1 commit 2579ec5

File tree

4 files changed

+107
-7
lines changed

4 files changed

+107
-7
lines changed

assets/index.less

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@
7373
opacity: 0;
7474
}
7575
}
76+
77+
// =============== Float BG ===============
78+
&-float-bg {
79+
position: absolute;
80+
z-index: 0;
81+
box-sizing: border-box;
82+
border: 1px solid red;
83+
background: green;
84+
transition: all 0.3s;
85+
}
86+
87+
// Debug
88+
&-unique-controlled {
89+
border-color: rgba(0, 0, 0, 0.01) !important;
90+
background: transparent !important;
91+
z-index: 1;
92+
}
7693
}
7794

7895
@import './index/Mask';

src/Popup/index.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import classNames from 'classnames';
22
import type { CSSMotionProps } from '@rc-component/motion';
33
import CSSMotion from '@rc-component/motion';
4-
import ResizeObserver from '@rc-component/resize-observer';
4+
import ResizeObserver, {
5+
type ResizeObserverProps,
6+
} from '@rc-component/resize-observer';
57
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
68
import { composeRef } from '@rc-component/util/lib/ref';
79
import * as React from 'react';
@@ -11,6 +13,7 @@ import Arrow from './Arrow';
1113
import Mask from './Mask';
1214
import PopupContent from './PopupContent';
1315
import useOffsetStyle from '../hooks/useOffsetStyle';
16+
import { useEvent } from '@rc-component/util';
1417

1518
export interface MobileConfig {
1619
mask?: boolean;
@@ -75,6 +78,9 @@ export interface PopupProps {
7578
targetWidth?: number;
7679
targetHeight?: number;
7780

81+
// Resize
82+
onResize?: ResizeObserverProps['onResize'];
83+
7884
// Mobile
7985
mobile?: MobileConfig;
8086
}
@@ -134,6 +140,9 @@ const Popup = React.forwardRef<HTMLDivElement, PopupProps>((props, ref) => {
134140
onAlign,
135141
onPrepare,
136142

143+
// Resize
144+
onResize,
145+
137146
stretch,
138147
targetWidth,
139148
targetHeight,
@@ -176,6 +185,12 @@ const Popup = React.forwardRef<HTMLDivElement, PopupProps>((props, ref) => {
176185
}
177186
}, [show, getPopupContainerNeedParams, target]);
178187

188+
// ========================= Resize =========================
189+
const onInternalResize: ResizeObserverProps['onResize'] = useEvent((size) => {
190+
onResize?.(size);
191+
onAlign();
192+
});
193+
179194
// ========================= Styles =========================
180195
const offsetStyle = useOffsetStyle(
181196
isMobile,
@@ -226,7 +241,7 @@ const Popup = React.forwardRef<HTMLDivElement, PopupProps>((props, ref) => {
226241
motion={mergedMaskMotion}
227242
mobile={isMobile}
228243
/>
229-
<ResizeObserver onResize={onAlign} disabled={!open}>
244+
<ResizeObserver onResize={onInternalResize} disabled={!open}>
230245
{(resizeObserverRef) => {
231246
return (
232247
<CSSMotion

src/UniqueProvider/FloatBg.tsx

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,60 @@
11
import React from 'react';
2+
import useOffsetStyle from '../hooks/useOffsetStyle';
23

34
export interface FloatBgProps {
45
prefixCls: string; // ${prefixCls}-float-bg
56
popupEle: HTMLElement;
7+
isMobile: boolean;
8+
ready: boolean;
9+
open: boolean;
10+
align: any;
11+
offsetR: number;
12+
offsetB: number;
13+
offsetX: number;
14+
offsetY: number;
15+
popupSize?: { width: number; height: number };
616
}
717

818
const FloatBg = (props: FloatBgProps) => {
9-
const { prefixCls, popupEle } = props;
19+
const {
20+
prefixCls,
21+
popupEle,
22+
isMobile,
23+
ready,
24+
open,
25+
align,
26+
offsetR,
27+
offsetB,
28+
offsetX,
29+
offsetY,
30+
popupSize,
31+
} = props;
1032

1133
// Apply className as requested in TODO
1234
const className = `${prefixCls}-float-bg`;
1335

36+
const offsetStyle = useOffsetStyle(
37+
isMobile,
38+
ready,
39+
open,
40+
align,
41+
offsetR,
42+
offsetB,
43+
offsetX,
44+
offsetY,
45+
);
46+
47+
// Apply popup size if available
48+
const sizeStyle: React.CSSProperties = {};
49+
if (popupSize) {
50+
sizeStyle.width = popupSize.width;
51+
sizeStyle.height = popupSize.height;
52+
}
53+
1454
// Remove console.log as it's for debugging only
1555
// console.log('>>>', popupEle);
1656

17-
return <div className={className} />;
57+
return <div className={className} style={{ ...offsetStyle, ...sizeStyle }} />;
1858
};
1959

2060
export default FloatBg;

src/UniqueProvider/index.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useEvent } from '@rc-component/util';
1313
import useTargetState from './useTargetState';
1414
import { isDOM } from '@rc-component/util/lib/Dom/findDOMNode';
1515
import FloatBg from './FloatBg';
16+
import classNames from 'classnames';
1617

1718
export interface UniqueProviderProps {
1819
children: React.ReactNode;
@@ -23,6 +24,10 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
2324

2425
// =========================== Popup ============================
2526
const [popupEle, setPopupEle] = React.useState<HTMLDivElement>(null);
27+
const [popupSize, setPopupSize] = React.useState<{
28+
width: number;
29+
height: number;
30+
}>(null);
2631

2732
// Used for forwardRef popup. Not use internal
2833
const externalPopupRef = React.useRef<HTMLDivElement>(null);
@@ -117,6 +122,8 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
117122
);
118123

119124
// =========================== Render ===========================
125+
const prefixCls = options?.prefixCls;
126+
120127
return (
121128
<UniqueContext.Provider value={contextValue}>
122129
{children}
@@ -125,9 +132,12 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
125132
<Popup
126133
ref={setPopupRef}
127134
portal={Portal}
128-
prefixCls={options.prefixCls}
135+
prefixCls={prefixCls}
129136
popup={options.popup}
130-
className={options.popupClassName}
137+
className={classNames(
138+
options.popupClassName,
139+
`${prefixCls}-unique-controlled`,
140+
)}
131141
style={options.popupStyle}
132142
target={options.target}
133143
open={open}
@@ -142,6 +152,12 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
142152
offsetB={offsetB}
143153
onAlign={onAlign}
144154
onPrepare={onPrepare}
155+
onResize={(size) =>
156+
setPopupSize({
157+
width: size.offsetWidth,
158+
height: size.offsetHeight,
159+
})
160+
}
145161
arrowPos={{
146162
x: arrowX,
147163
y: arrowY,
@@ -154,7 +170,19 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
154170
maskMotion={options.maskMotion}
155171
getPopupContainer={options.getPopupContainer}
156172
>
157-
<FloatBg prefixCls={options.prefixCls} popupEle={popupEle} />
173+
<FloatBg
174+
prefixCls={prefixCls}
175+
popupEle={popupEle}
176+
isMobile={false}
177+
ready={ready}
178+
open={open}
179+
align={alignInfo}
180+
offsetR={offsetR}
181+
offsetB={offsetB}
182+
offsetX={offsetX}
183+
offsetY={offsetY}
184+
popupSize={popupSize}
185+
/>
158186
</Popup>
159187
</TriggerContext.Provider>
160188
)}

0 commit comments

Comments
 (0)