Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useWindowSize): support includeScrollbar #2161

Merged
merged 2 commits into from Sep 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/core/useWindowSize/index.test.ts
Expand Up @@ -23,6 +23,13 @@ describe('useWindowSize', () => {
expect(height.value).toBe(window.innerHeight)
})

it('should exclude scrollbar', () => {
const { width, height } = useWindowSize({ initialWidth: 100, initialHeight: 200, includeScrollbar: false })

expect(width.value).toBe(window.document.documentElement.clientWidth)
expect(height.value).toBe(window.document.documentElement.clientHeight)
})

it('sets handler for window "resize" event', async () => {
useWindowSize({ initialWidth: 100, initialHeight: 200, listenOrientation: false })

Expand Down
17 changes: 15 additions & 2 deletions packages/core/useWindowSize/index.ts
Expand Up @@ -13,6 +13,12 @@ export interface UseWindowSizeOptions extends ConfigurableWindow {
* @default true
*/
listenOrientation?: boolean

/**
* Whether the scrollbar should be included in the width and height
* @default true
*/
includeScrollbar?: boolean
}

/**
Expand All @@ -27,15 +33,22 @@ export function useWindowSize(options: UseWindowSizeOptions = {}) {
initialWidth = Infinity,
initialHeight = Infinity,
listenOrientation = true,
includeScrollbar = true,
} = options

const width = ref(initialWidth)
const height = ref(initialHeight)

const update = () => {
if (window) {
width.value = window.innerWidth
height.value = window.innerHeight
if (includeScrollbar) {
width.value = window.innerWidth
height.value = window.innerHeight
}
else {
width.value = window.document.documentElement.clientWidth
height.value = window.document.documentElement.clientHeight
}
}
}

Expand Down