-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sensor/useIntersection hook
- Loading branch information
Showing
5 changed files
with
171 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { storiesOf } from '@storybook/vue' | ||
import { createComponent } from '@vue/composition-api' | ||
import Doc from '../../../__stories__/components/Doc' | ||
import { useIntersection } from '../../..' | ||
|
||
const Spacer = () => ( | ||
<div | ||
style={{ | ||
width: '200px', | ||
height: '300px', | ||
backgroundColor: 'whitesmoke' | ||
}} | ||
/> | ||
) | ||
|
||
const Demo = createComponent({ | ||
setup(_, ctx) { | ||
const intersection = useIntersection( | ||
() => (ctx as any).refs.tracker as HTMLElement, | ||
{ | ||
root: null, | ||
rootMargin: '0px', | ||
threshold: 1 | ||
} | ||
) | ||
return { intersection } | ||
}, | ||
render() { | ||
const { intersection } = this | ||
return ( | ||
<div | ||
style={{ | ||
width: '400px', | ||
height: '400px', | ||
backgroundColor: 'whitesmoke', | ||
overflow: 'scroll' | ||
}} | ||
> | ||
Scroll me | ||
<Spacer /> | ||
<div | ||
ref="tracker" | ||
style={{ | ||
width: '100px', | ||
height: '100px', | ||
padding: '20px', | ||
backgroundColor: 'palegreen' | ||
}} | ||
> | ||
{intersection && intersection.intersectionRatio < 1 | ||
? 'Obscured' | ||
: 'Fully in view'} | ||
</div> | ||
<Spacer /> | ||
</div> | ||
) | ||
} | ||
}) | ||
|
||
const Docs = () => <Doc md={require('./doc.md')}></Doc> | ||
|
||
storiesOf('Sensor|useIntersection', module) | ||
.add('Docs', () => Docs as any) | ||
.add('Demo', () => Demo) |
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,72 @@ | ||
# useIntersection | ||
|
||
Vue hook that tracks the changes in the intersection of a target element with an ancestor element or with a top-level document's viewport. Uses the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) and returns a [IntersectionObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry). | ||
|
||
> Browser environment is required | ||
## Usage | ||
|
||
```jsx | ||
import { createComponent } from '@vue/composition-api' | ||
import { useIntersection } from 'vuses' | ||
|
||
const Spacer = () => ( | ||
<div | ||
style={{ | ||
width: '200px', | ||
height: '300px', | ||
backgroundColor: 'whitesmoke', | ||
}} | ||
/> | ||
) | ||
|
||
const Demo = createComponent({ | ||
setup(_, ctx) { | ||
const intersection = useIntersection(() => (ctx as any).refs.tracker as HTMLElement, { | ||
root: null, | ||
rootMargin: '0px', | ||
threshold: 1 | ||
}) | ||
return { intersection } | ||
}, | ||
render() { | ||
const { intersection } = this | ||
return ( | ||
<div | ||
style={{ | ||
width: '400px', | ||
height: '400px', | ||
backgroundColor: 'whitesmoke', | ||
overflow: 'scroll', | ||
}} | ||
> | ||
Scroll me | ||
<Spacer /> | ||
<div | ||
ref="tracker" | ||
style={{ | ||
width: '100px', | ||
height: '100px', | ||
padding: '20px', | ||
backgroundColor: 'palegreen', | ||
}} | ||
> | ||
{intersection && intersection.intersectionRatio < 1 ? 'Obscured' : 'Fully in view'} | ||
</div> | ||
<Spacer /> | ||
</div> | ||
) | ||
} | ||
}) | ||
``` | ||
|
||
## Reference | ||
|
||
```typescript {4-6} | ||
type IntersectionState = IntersectionObserverEntry | null | ||
|
||
function useIntersection( | ||
elem: HTMLElement | (() => HTMLElement), | ||
options: IntersectionObserverInit | ||
): Ref<IntersectionState> | ||
``` |
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,33 @@ | ||
import { Ref, ref, onMounted, onUnmounted } from '@vue/composition-api' | ||
import { isFunction } from '../../../utils' | ||
|
||
export type IntersectionState = IntersectionObserverEntry | null | ||
|
||
export default function useIntersection( | ||
elem: HTMLElement | (() => HTMLElement), | ||
options: IntersectionObserverInit | ||
): Ref<IntersectionState> { | ||
const getElem = isFunction(elem) ? elem : () => elem | ||
const state = ref<IntersectionState>(null) | ||
let observer: IntersectionObserver | null = null | ||
|
||
const handler = (entries: IntersectionObserverEntry[]) => { | ||
state.value = entries[0] | ||
} | ||
|
||
onMounted(() => { | ||
const el = getElem() | ||
if (!el) return | ||
|
||
observer = new IntersectionObserver(handler, options) | ||
observer.observe(el) | ||
}) | ||
|
||
onUnmounted(() => { | ||
if (observer) { | ||
observer.disconnect() | ||
} | ||
}) | ||
|
||
return state | ||
} |
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