Skip to content

Commit d739f67

Browse files
feat(FloatingBubble): allow gap xy to be set separately (#13297) (#13429)
Co-authored-by: neverland <jait.chen@foxmail.com>
1 parent 5a7042c commit d739f67

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

packages/vant/src/floating-bubble/FloatingBubble.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
addUnit,
2121
closest,
2222
createNamespace,
23-
makeNumberProp,
23+
isObject,
2424
makeStringProp,
2525
windowWidth,
2626
windowHeight,
@@ -38,11 +38,15 @@ import {
3838
FloatingBubbleAxis,
3939
FloatingBubbleMagnetic,
4040
FloatingBubbleOffset,
41+
FloatingBubbleGap,
4142
FloatingBubbleBoundary,
4243
} from './types';
4344

4445
export const floatingBubbleProps = {
45-
gap: makeNumberProp(24),
46+
gap: {
47+
type: [Number, Object] as unknown as PropType<FloatingBubbleGap>,
48+
default: 24,
49+
},
4650
icon: String,
4751
axis: makeStringProp<FloatingBubbleAxis>('y'),
4852
magnetic: String as PropType<FloatingBubbleMagnetic>,
@@ -79,11 +83,17 @@ export default defineComponent({
7983
height: 0,
8084
});
8185

86+
const gapX = computed(() =>
87+
isObject(props.gap) ? props.gap.x : props.gap,
88+
);
89+
const gapY = computed(() =>
90+
isObject(props.gap) ? props.gap.y : props.gap,
91+
);
8292
const boundary = computed<FloatingBubbleBoundary>(() => ({
83-
top: props.gap,
84-
right: windowWidth.value - state.value.width - props.gap,
85-
bottom: windowHeight.value - state.value.height - props.gap,
86-
left: props.gap,
93+
top: gapY.value,
94+
right: windowWidth.value - state.value.width - gapX.value,
95+
bottom: windowHeight.value - state.value.height - gapY.value,
96+
left: gapX.value,
8797
}));
8898

8999
const dragging = ref(false);
@@ -110,8 +120,8 @@ export default defineComponent({
110120
const { width, height } = useRect(rootRef.value!);
111121
const { offset } = props;
112122
state.value = {
113-
x: offset.x > -1 ? offset.x : windowWidth.value - width - props.gap,
114-
y: offset.y > -1 ? offset.y : windowHeight.value - height - props.gap,
123+
x: offset.x > -1 ? offset.x : windowWidth.value - width - gapX.value,
124+
y: offset.y > -1 ? offset.y : windowHeight.value - height - gapY.value,
115125
width,
116126
height,
117127
};
@@ -203,7 +213,7 @@ export default defineComponent({
203213
});
204214

205215
watch(
206-
[windowWidth, windowHeight, () => props.gap, () => props.offset],
216+
[windowWidth, windowHeight, gapX, gapY, () => props.offset],
207217
updateState,
208218
{ deep: true },
209219
);

packages/vant/src/floating-bubble/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default {
9494
| axis | Drag direction, `xy` stands for free drag, `lock` stands for disable drag | _'x' \| 'y' \| 'xy' \| 'lock'_ | `y` |
9595
| magnetic | Direction of automatic magnetic absorption | _'x' \| 'y'_ | - |
9696
| icon | Bubble icon | _string_ | - |
97-
| gap | Minimum gap between the bubble and the window, unit `px` | _number_ | `24` |
97+
| gap | Minimum gap between the bubble and the window, unit `px` | _number \| { x: number, y: number }_ | `24` |
9898
| teleport | Specifies a target element where BackTop will be mounted | _string \| Element_ | `body` |
9999

100100
### Events

packages/vant/src/floating-bubble/README.zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default {
9494
| axis | 拖拽的方向,`xy` 代表自由拖拽,`lock` 代表禁止拖拽 | _'x' \| 'y' \| 'xy' \| 'lock'_ | `y` |
9595
| magnetic | 自动磁吸的方向 | _'x' \| 'y'_ | - |
9696
| icon | 气泡图标名称或图片链接,等同于 Icon 组件的 [name 属性](#/zh-CN/icon#props) | _string_ | - |
97-
| gap | 气泡与窗口的最小间距,单位为 `px` | _number_ | `24` |
97+
| gap | 气泡与窗口的最小间距,单位为 `px` | _number \| { x: number, y: number }_ | `24` |
9898
| teleport | 指定挂载的节点,等同于 Teleport 组件的 [to 属性](https://cn.vuejs.org/api/built-in-components.html#teleport) | _string \| Element_ | `body` |
9999

100100
### Events

packages/vant/src/floating-bubble/test/index.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ test('should render correctly when all props set', async () => {
6262
restore();
6363
});
6464

65+
test('should render with xy gaps', async () => {
66+
useWindowSize();
67+
const restore = mockGetBoundingClientRect({ width: 48, height: 48 });
68+
69+
const root = document.createElement('div');
70+
mount(FloatingBubble, {
71+
props: {
72+
teleport: root,
73+
gap: { x: 50, y: 27 },
74+
},
75+
});
76+
77+
const floatingBubbleEl = root.querySelector<HTMLDivElement>(
78+
'.van-floating-bubble',
79+
)!;
80+
81+
await later();
82+
83+
expect(floatingBubbleEl.style.transform).toEqual(
84+
`translate3d(${window.innerWidth - 48 - 50}px, ${
85+
window.innerHeight - 48 - 27
86+
}px, 0)`,
87+
);
88+
89+
restore();
90+
});
91+
6592
test('should only y axis direction move when axis is default', async () => {
6693
const restore = mockGetBoundingClientRect({ width: 48, height: 48 });
6794

packages/vant/src/floating-bubble/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export type FloatingBubbleOffset = {
1616
y: number;
1717
};
1818

19+
export type FloatingBubbleGap =
20+
| number
21+
| {
22+
x: number;
23+
y: number;
24+
};
25+
1926
export type FloatingBubbleBoundary = {
2027
top: number;
2128
right: number;

0 commit comments

Comments
 (0)