-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
d6703f7
commit 17a46cb
Showing
8 changed files
with
337 additions
and
442 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export * from './usePositionStickyObserver' |
57 changes: 57 additions & 0 deletions
57
src/usePositionStickyObserver/usePositionStickyObserver.ts
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,57 @@ | ||
import { Ref, computed, onMounted, ref, MaybeRefOrGetter, toValue } from 'vue' | ||
import { useIntersectionObserver } from '@/useIntersectionObserver' | ||
|
||
type UsePositionStickyObserver = { | ||
stuck: Ref<boolean>, | ||
} | ||
|
||
export type UsePositionStickyObserverOptions = { | ||
rootMargin?: string, | ||
boundingElement?: MaybeRefOrGetter<HTMLElement | undefined>, | ||
} | ||
|
||
const usePositionStickyObserverDefaultOptions = { | ||
rootMargin: '-1px 0px 0px 0px', | ||
boundingElement: document.body, | ||
} | ||
|
||
export function usePositionStickyObserver( | ||
element: MaybeRefOrGetter<HTMLElement | undefined>, | ||
options?: MaybeRefOrGetter<UsePositionStickyObserverOptions>, | ||
): UsePositionStickyObserver { | ||
const elementRef = computed(() => toValue(element)) | ||
const optionsRef = computed(() => { | ||
if (!options) { | ||
return usePositionStickyObserverDefaultOptions | ||
} | ||
|
||
const { rootMargin, boundingElement } = toValue(options) | ||
console.log('in deal', boundingElement) | ||
const boundingElementRef = toValue(boundingElement) | ||
|
||
return { | ||
rootMargin: rootMargin ?? usePositionStickyObserverDefaultOptions.rootMargin, | ||
boundingElement: boundingElementRef ?? usePositionStickyObserverDefaultOptions.boundingElement, | ||
} | ||
}) | ||
|
||
const stuck = ref(false) | ||
|
||
function intersect(entries: IntersectionObserverEntry[]): void { | ||
entries.forEach(entry => { | ||
stuck.value = entry.intersectionRatio < 1 | ||
}) | ||
} | ||
|
||
const { observe } = useIntersectionObserver(intersect, computed(() => ({ | ||
threshold: [1], | ||
rootMargin: optionsRef.value.rootMargin, | ||
root: optionsRef.value.boundingElement, | ||
}))) | ||
|
||
onMounted(() => observe(elementRef)) | ||
|
||
return { | ||
stuck, | ||
} | ||
} |