Skip to content

Commit

Permalink
fix(Sticky): initial sticky position (youzan#12601)
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-shen authored and CatsAndMice committed Apr 8, 2024
1 parent 2c1d82e commit 792ac7d
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 47 deletions.
3 changes: 1 addition & 2 deletions packages/vant/src/sticky/Sticky.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ export default defineComponent({
if (container) {
const containerRect = useRect(container);
const difference = containerRect.bottom - offset.value - state.height;
state.fixed =
offset.value >= rootRect.top && containerRect.bottom > 0;
state.fixed = offset.value > rootRect.top && containerRect.bottom > 0;
state.transform = difference < 0 ? difference : 0;
} else {
state.fixed = offset.value > rootRect.top;
Expand Down
45 changes: 0 additions & 45 deletions packages/vant/src/tab/test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -437,51 +437,6 @@ test('should not render header when showHeader is false', async () => {
expect(tabs.length).toEqual(0);
});

test('should fixed when sticky offset is equal top', async () => {
const wrapper = mount({
render() {
return (
<Tabs sticky>
<Tab title="title1">Text</Tab>
<Tab title="title2">Text</Tab>
</Tabs>
);
},
});

const mockContainerRect = vi
.spyOn(wrapper.element, 'getBoundingClientRect')
.mockReturnValue({
bottom: 10,
} as DOMRect);
const container = wrapper.element.children[0];
const mockStickyRect = vi
.spyOn(container, 'getBoundingClientRect')
.mockReturnValue({
top: -10,
} as DOMRect);

expect(wrapper.find('.van-sticky').classes()).toStrictEqual(['van-sticky']);

await mockScrollTop(100);
expect(wrapper.find('.van-sticky').classes()).toStrictEqual([
'van-sticky',
'van-sticky--fixed',
]);

mockStickyRect.mockReturnValue({
top: 0,
} as DOMRect);
await mockScrollTop(100);
expect(wrapper.find('.van-sticky').classes()).toStrictEqual([
'van-sticky',
'van-sticky--fixed',
]);

mockStickyRect.mockRestore();
mockContainerRect.mockRestore();
});

test('should call before-change prop before changing', async () => {
const onChange = vi.fn();
const beforeChange = (name: number) => {
Expand Down

0 comments on commit 792ac7d

Please sign in to comment.