Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Add TS to useWindowScroll #1590

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,45 @@ import { useEventListener } from '~/composables/use-event-listener'
*
* This global ref is SSR safe because it will only
* change internal value based on client side interaction.
*
* @type {import('@nuxtjs/composition-api').Ref<boolean>}
*/
const isScrolled = ref(false)

/**
*
* @param {object} options
* @param {Window} [options.window]
* @param {number} [options.throttleMs] - time to throttle the scroll handler.
* Set to 0 to remove throttling
*/
interface UseWindowScrollOptions {
/**
* Window from which to read and track scroll position
*/
window?: typeof defaultWindow | undefined
/**
* Time to throttle the scroll handler.
* Set to 0 to remove throttling.
*/
throttleMs?: number
}

export function useWindowScroll({
window = defaultWindow,
throttleMs = 200,
} = {}) {
}: UseWindowScrollOptions = {}) {
if (!window) {
return {
// In SSR, no need to track anything.
return Object.freeze({
x: ref(0),
y: ref(0),
isScrolled,
}
})
}

const x = ref(window.pageXOffset)
const y = ref(window.pageYOffset)
const x = ref(0)
const y = ref(0)

const scrollHandler = () => {
x.value = window.pageXOffset
y.value = window.pageYOffset
x.value = window.scrollX
y.value = window.scrollY
isScrolled.value = y.value > 0
}

scrollHandler()

const handler = throttleMs
? throttle(throttleMs, scrollHandler)
: scrollHandler
Expand All @@ -52,5 +58,5 @@ export function useWindowScroll({
passive: true,
})

return { x, y, isScrolled }
return Object.freeze({ x, y, isScrolled })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this object is frozen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To encode the immutability of the structure? Changing the structure of this object is meaningless so why allow it? Maybe I am over thinking this though.

}
1 change: 1 addition & 0 deletions test/unit/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = {
env: { jest: true },
rules: {
'import/no-named-as-default-member': ['off'],
'@intlify/vue-i18n/no-raw-text': ['off'],
},
}
65 changes: 65 additions & 0 deletions test/unit/specs/composables/use-window-scroll.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Vue from 'vue'
import { ref } from '@nuxtjs/composition-api'
import { render } from '@testing-library/vue'

import { useWindowScroll } from '~/composables/use-window-scroll'

const getMockWindow = <T>(props: T) =>
({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
...props,
} as unknown as typeof window)

const UseWindowScrollTestContainer = Vue.component(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very interesting setup!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be possible to create a "generic" component for this to test composables, but I'm not sure 😅

'UseWindowScrollTestContainer',
{
props: ['initX', 'initY', 'throttleMs'],
setup(props) {
return useWindowScroll({
window: getMockWindow({
scrollX: props.initX,
scrollY: props.initY,
}),
throttleMs: props.throttleMs as number | undefined,
})
},
template: '<div>x={{x}} y={{y}} isScrolled={{isScrolled}}</div>',
}
)

describe('useWindowScroll', () => {
it('should return [0, 0] and false when no window', () => {
expect(useWindowScroll({})).toMatchObject({
x: ref(0),
y: ref(0),
isScrolled: ref(false),
})
})

it("should return the window's scroll position and not scrolled when y == 0", () => {
const { container } = render(UseWindowScrollTestContainer, {
props: {
initX: 10,
initY: 0,
},
})

expect(container.firstChild?.textContent).toEqual(
'x=10 y=0 isScrolled=false'
)
})

it("should return the window's scroll position and scrolled when y != 0", () => {
const { container } = render(UseWindowScrollTestContainer, {
props: {
initX: 31,
initY: 1,
},
})

expect(container.firstChild?.textContent).toEqual(
'x=31 y=1 isScrolled=true'
)
})
})