-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
228 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
export type HSVAColor = { | ||
h: number | ||
s: number | ||
v: number | ||
a: number | ||
} | ||
|
||
export default {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { watchEffect, Ref, onBeforeUnmount } from 'vue' | ||
|
||
function useEventListener( | ||
target: Ref<HTMLElement | Window | null>, | ||
type: string, | ||
listener: EventListenerOrEventListenerObject, | ||
options?: boolean | AddEventListenerOptions | ||
): () => void { | ||
let prevEle: HTMLElement | Window | null = null | ||
const destroyWatcher = watchEffect( | ||
() => { | ||
target.value?.addEventListener(type, listener, options) | ||
if (prevEle) { | ||
prevEle.removeEventListener(type, listener) | ||
} | ||
prevEle = target?.value | ||
}, | ||
{ flush: 'post' } | ||
) | ||
const removeListener = (isDestroyWatcher = true) => { | ||
target.value?.removeEventListener(type, listener) | ||
if (isDestroyWatcher) { | ||
destroyWatcher() | ||
} | ||
} | ||
onBeforeUnmount(() => { | ||
removeListener(true) | ||
}) | ||
return removeListener | ||
} | ||
export default useEventListener |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ref, Ref } from 'vue' | ||
import useEventListener from './useEventListener' | ||
|
||
type Size = { width: Ref<number>; height: Ref<number> } | ||
|
||
const useSize = (target: Ref<HTMLElement | Window | null>): Size => { | ||
const width = ref(0) | ||
const height = ref(0) | ||
|
||
useEventListener( | ||
target, | ||
'resize', | ||
() => { | ||
width.value = window.innerWidth | ||
height.value = window.innerHeight | ||
}, | ||
{ passive: true } | ||
) | ||
|
||
return { width, height } | ||
} | ||
|
||
export default useSize |
Oops, something went wrong.