-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
8 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { HooksDemos } from '@docs/demos'; | ||
import { Layout } from '@/layout'; | ||
import { MDX_DATA } from '@/mdx'; | ||
|
||
export default Layout(MDX_DATA.useIsVisible); | ||
|
||
## Usage | ||
|
||
`use-is-visible` is a hook that returns a boolean value indicating whether the target element is visible in the viewport. It makes use of the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) to determine visibility. | ||
|
||
<Demo data={HooksDemos.useIsVisibleDemo} /> |
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
48 changes: 48 additions & 0 deletions
48
packages/@docs/demos/src/demos/hooks/use-is-visible.demo.usage.tsx
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,48 @@ | ||
import React from 'react'; | ||
import { Box, Text } from '@mantine/core'; | ||
import { useIsVisible } from '@mantine/hooks'; | ||
import { MantineDemo } from '@mantinex/demo'; | ||
|
||
const code = ` | ||
import { useIsVisible } from '@mantine/hooks'; | ||
function Demo() { | ||
const { ref, isVisible } = useIsVisible(); | ||
return ( | ||
<> | ||
<Text ta="center">{isVisible ? 'Box is visible' : 'Scroll to see box'}</Text> | ||
<Box h={64} style={{ overflow: 'scroll' }}> | ||
<Box h={128}></Box> | ||
<Box ref={ref} bg="blue" h={32} p={8}> | ||
<Text ta="center" c="white"> | ||
A box | ||
</Text> | ||
</Box> | ||
</Box> | ||
</> | ||
); | ||
} | ||
`; | ||
|
||
function Demo() { | ||
const { ref, isVisible } = useIsVisible(); | ||
return ( | ||
<> | ||
<Text ta="center">{isVisible ? 'Box is visible' : 'Scroll to see box'}</Text> | ||
<Box h={64} style={{ overflow: 'scroll' }}> | ||
<Box h={128}></Box> | ||
<Box ref={ref} bg="blue" h={32} p={8}> | ||
<Text ta="center" c="white"> | ||
A box | ||
</Text> | ||
</Box> | ||
</Box> | ||
</> | ||
); | ||
} | ||
|
||
export const useIsVisibleDemo: MantineDemo = { | ||
type: 'code', | ||
code, | ||
component: 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
23 changes: 23 additions & 0 deletions
23
packages/@mantine/hooks/src/use-is-visible/use-is-visible.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,23 @@ | ||
import { useEffect, useMemo, useRef, useState } from 'react'; | ||
|
||
export function useIsVisible<T extends HTMLElement = any>() { | ||
const ref = useRef<T>(null); | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
const observer = useMemo(() => { | ||
if (typeof IntersectionObserver === 'undefined') { | ||
return null; | ||
} | ||
return new IntersectionObserver(([entry]) => setIsVisible(entry.isIntersecting)); | ||
}, [ref]); | ||
|
||
useEffect(() => { | ||
if (ref.current && observer) { | ||
observer.observe(ref.current); | ||
return () => observer.disconnect(); | ||
} | ||
return () => null; | ||
}, []); | ||
|
||
return { ref, isVisible }; | ||
} |