Skip to content

Commit

Permalink
fix(ui/sticky): use scroll mode as sticky default mode
Browse files Browse the repository at this point in the history
affects: @varlet/ui
  • Loading branch information
haoziqaq committed Sep 17, 2021
1 parent ae28ce2 commit 3769f8b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
5 changes: 3 additions & 2 deletions packages/varlet-ui/src/sticky/Sticky.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div
class="var-sticky"
ref="stickyEl"
:class="[cssMode ? 'var-sticky--css-mode' : null]"
:style="{
zIndex: toNumber(zIndex),
top: !isFixed ? `${offsetTop}px` : null,
Expand Down Expand Up @@ -66,10 +67,10 @@ export default defineComponent({
const sticky = stickyEl.value as HTMLElement
const { top: stickyTop, left: stickyLeft } = sticky.getBoundingClientRect()
const currentOffsetTop = stickyTop - scrollerTop
const { onScroll } = props
const { onScroll, cssMode } = props
if (currentOffsetTop <= offsetTop.value) {
if (!isSupportCSSSticky) {
if (!isSupportCSSSticky || !cssMode) {
fixedWidth.value = `${sticky.offsetWidth}px`
fixedHeight.value = `${sticky.offsetHeight}px`
fixedTop.value = `${scrollerTop + offsetTop.value}px`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ exports[`test sticky example 1`] = `
`;
exports[`test sticky scrolling with css sticky position 1`] = `
"<div class=\\"var-sticky\\" style=\\"z-index: 10; top: 100px;\\">
"<div class=\\"var-sticky var-sticky--css-mode\\" style=\\"z-index: 10; top: 100px;\\">
<div class=\\"var-sticky__wrapper\\" style=\\"z-index: 10;\\">sticky content</div>
</div>"
`;
exports[`test sticky scrolling with css sticky position 2`] = `
"<div class=\\"var-sticky\\" style=\\"z-index: 10; width: 0px; height: 0px;\\">
<div class=\\"var-sticky__wrapper\\" style=\\"z-index: 10; position: fixed; width: 0px; height: 0px; top: 100px;\\">sticky content</div>
"<div class=\\"var-sticky var-sticky--css-mode\\" style=\\"z-index: 10; top: 100px;\\">
<div class=\\"var-sticky__wrapper\\" style=\\"z-index: 10;\\">sticky content</div>
</div>"
`;
Expand Down
28 changes: 19 additions & 9 deletions packages/varlet-ui/src/sticky/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,24 @@ test('test sticky onScroll', async () => {
})

test('test sticky scrolling with css sticky position', async () => {
const mockGetComputedStyle = jest
.spyOn(window, 'getComputedStyle')
.mockReturnValue({ position: 'sticky' })

const wrapper = mount(VarSticky, {
props: {
offsetTop: 100,
cssMode: true
},
slots: {
default: () => 'sticky content',
},
attachTo: document.body,
})

const mockGetComputedStyle = jest.spyOn(window, 'getComputedStyle').mockReturnValue({ position: 'sticky' })
const mockGetBoundingClientRect = jest.spyOn(wrapper.element, 'getBoundingClientRect').mockReturnValue({ top: 200 })
const mockGetBoundingClientRect = jest
.spyOn(wrapper.element, 'getBoundingClientRect')
.mockReturnValue({ top: 200 })
await trigger(window, 'scroll')
expect(wrapper.html()).toMatchSnapshot()

Expand All @@ -76,6 +82,10 @@ test('test sticky scrolling with css sticky position', async () => {
})

test('test sticky scrolling without css sticky position', async () => {
const mockGetComputedStyle = jest
.spyOn(window, 'getComputedStyle')
.mockReturnValue({ position: 'relative' })

const wrapper = mount(VarSticky, {
props: {
offsetTop: 100,
Expand All @@ -86,16 +96,16 @@ test('test sticky scrolling without css sticky position', async () => {
attachTo: document.body,
})

const mockGetComputedStyle = jest.spyOn(window, 'getComputedStyle').mockReturnValue({ position: 'relative' })
const mockGetBoundingClientRect = jest.spyOn(wrapper.element, 'getBoundingClientRect').mockReturnValue({
top: 200,
})
const mockGetBoundingClientRect = jest
.spyOn(wrapper.element, 'getBoundingClientRect')
.mockReturnValue({ top: 200 })

await trigger(window, 'scroll')
expect(wrapper.html()).toMatchSnapshot()

jest.spyOn(wrapper.element, 'getBoundingClientRect').mockReturnValue({
top: 100,
})
jest
.spyOn(wrapper.element, 'getBoundingClientRect')
.mockReturnValue({ top: 100 })
await trigger(window, 'scroll')
expect(wrapper.html()).toMatchSnapshot()

Expand Down
1 change: 1 addition & 0 deletions packages/varlet-ui/src/sticky/docs/en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ createApp().use(Sticky)
| --- | --- | --- | --- |
| `offset-top` | Sticky offset top | _string \| number_ | `0` |
| `z-index` | Sticky z-index | _string \| number_ | `0` |
| `css-mode` | Enable native `css sticky` mode | _boolean_ | `false` |

### Events

Expand Down
1 change: 1 addition & 0 deletions packages/varlet-ui/src/sticky/docs/zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ createApp().use(Sticky)
| --- | --- | --- | --- |
| `offset-top` | 吸顶距离 | _string \| number_ | `0` |
| `z-index` | 吸顶时的层级 | _string \| number_ | `0` |
| `css-mode` | 开启原生`css sticky`模式 | _boolean_ | `false` |

### 事件

Expand Down
4 changes: 4 additions & 0 deletions packages/varlet-ui/src/sticky/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const props = {
type: [String, Number],
default: 10,
},
cssMode: {
type: Boolean,
default: false,
},
onScroll: {
type: Function as PropType<(offsetTop: number, isFixed: boolean) => void>,
},
Expand Down
6 changes: 4 additions & 2 deletions packages/varlet-ui/src/sticky/sticky.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.var-sticky {
position: sticky;
position: -webkit-sticky;
&--css-mode {
position: sticky;
position: -webkit-sticky;
}
}

0 comments on commit 3769f8b

Please sign in to comment.