Skip to content

Commit 790ce50

Browse files
committed
Add more examples to the readme
1 parent e14e0bd commit 790ce50

File tree

1 file changed

+120
-16
lines changed

1 file changed

+120
-16
lines changed

Readme.md

Lines changed: 120 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -246,37 +246,141 @@ const [isInViewport, targetRef] = useIsInViewport({
246246
## Example usage
247247

248248
A CRA based example app (which is also used in the test suite) can be found under
249-
[examples/cra](examples/cra). Inline examples showcasing use-cases are below.
249+
[examples/cra](examples/cra). Inline examples showcasing some of the use-cases are below.
250250

251251
### Example 1: Element with its parent document as viewport
252252

253-
As soon as at least 1px of the child element is visible in the parent document viewport,
254-
`isInViewport` evaluates to true.
253+
As soon as at least 1px of the target `div` is visible in the parent document viewport,
254+
`isInViewport` evaluates to `true`.
255255

256256
```jsx
257257
import React from 'react'
258-
import ReactDOM from 'react-dom'
259258
import useIsInViewport from 'use-is-in-viewport'
260259

261-
export default function SimpleElement() {
262-
const [isInViewport, childElToWatch] = useIsInViewport()
260+
export default function MyElement() {
261+
const [isInViewport, targetRef] = useIsInViewport()
263262

264263
return (
265-
<div ref={childElToWatch}>
264+
<div ref={targetRef}>
266265
<p>{isInViewport ? 'In viewport' : 'Out of viewport'}</p>
267266
</div>
268267
)
269268
}
270269
```
271270

272-
More examples coming soon...
271+
### Example 2: Element with a custom viewport
273272

274-
## Tasks
273+
As soon as at least 1px of the target `div` is visible in its parent `div` (chosen as the parent by
274+
passing `viewportRef` to it), `isInViewport` evaluates to `true`.
275275

276-
- [x] Setup the hook to work with CRA, codesandbox and standalone react app
277-
- [x] Write the hook in a way that can be tested with Cypress
278-
- [x] Setup CI
279-
- [x] Increase test coverage
280-
- [x] Write awesome docs!
281-
- [x] Deploy example app to [useisinviewport.zdx.cat](https://useisinviewport.zdx.cat/)
282-
- [x] Document the motivation and API in the readme
276+
```jsx
277+
import React from 'react'
278+
import useIsInViewport from 'use-is-in-viewport'
279+
280+
export default function MyElement() {
281+
const [isInViewport, targetRef, viewportRef] = useIsInViewport()
282+
283+
return (
284+
<div ref={viewportRef}>
285+
<div ref={targetRef}>
286+
<p>{isInViewport ? 'In viewport' : 'Out of viewport'}</p>
287+
</div>
288+
</div>
289+
)
290+
}
291+
```
292+
293+
### Example 3: Using a forwarded ref for the target element
294+
295+
In some cases, the parent element might want to control the `ref` of some element inside your
296+
component. It is obviously up to your component to decide what element to assign the incoming `ref`.
297+
298+
In this example, we assign the incoming `ref` to the target element that we are watching for in the
299+
document viewport. To do so, we thread it through `useIsInViewport` hook by using the `target`
300+
option and assining the incoming `ref` to it. The hook takes care of updating the incoming `ref`
301+
such that it is completely transparent to the parent element that passed the `ref` in.
302+
303+
```jsx
304+
import React from 'react'
305+
import useIsInViewport from 'use-is-in-viewport'
306+
307+
export const RefForwardingElement = React.forwardRef(function RefForwardingElement(
308+
props,
309+
incomingTargetRef
310+
) {
311+
const [isInViewport, targetRef] = useIsInViewport({
312+
target: incomingTargetRef // thread the incoming target ref through the hook
313+
})
314+
315+
return (
316+
<div ref={targetRef}>
317+
<p>{isInViewport ? 'In viewport' : 'Out of viewport'}</p>
318+
</div>
319+
)
320+
})
321+
```
322+
323+
### Example 4: Using a forwarded ref for the viewport element
324+
325+
Same as the previous example, but here we thread the incoming `ref` into the viewport element
326+
instead of the target element.
327+
328+
```jsx
329+
import React from 'react'
330+
import useIsInViewport from 'use-is-in-viewport'
331+
332+
export const RefForwardingElement = React.forwardRef(function RefForwardingElement(
333+
props,
334+
incomingViewportRef
335+
) {
336+
const [isInViewport, targetRef, viewportRef] = useIsInViewport({
337+
viewport: incomingViewportRef // thread the incoming viewport ref through the hook
338+
})
339+
340+
return (
341+
<div ref={viewportRef}>
342+
<div ref={targetRef}>
343+
<p>{isInViewport ? 'In viewport' : 'Out of viewport'}</p>
344+
</div>
345+
</div>
346+
)
347+
})
348+
```
349+
350+
### Example 5: Tracking visibility in a custom viewport of multiple elements
351+
352+
In this example, we have a custom viewport element and we want to track whether its child `divs` are
353+
in viewport or not. We have also chosen to use different `threshold`s for the two child target
354+
`divs`.
355+
356+
As you might notice, we thread the `viewport` ref from the first call to `useIsInViewport` into the
357+
second call to `useIsInViewport`. This is because an element can only be assigned one `ref`. And
358+
here that element is the viewport element. Therefore, we use the transparent `ref` update capability
359+
of `useIsInViewport` (as shown in the previous examples) to wrap the viewport `ref` from the first
360+
call of the hook with a new one that is then passed to the viewport `div`.
361+
362+
```jsx
363+
import React from 'react'
364+
import useIsInViewport from 'use-is-in-viewport'
365+
366+
export default function MyElement() {
367+
const [isDivOneInViewport, divOneTargetRef, viewportRefToChain] = useIsInViewport({
368+
threshold: 50
369+
})
370+
const [isDivTwoInViewport, divTwoTargetRef, viewportRef] = useIsInViewport({
371+
viewport: viewportRefToChain,
372+
threshold: 75
373+
})
374+
375+
return (
376+
<div ref={viewportRef}>
377+
<div ref={divOneTargetRef}>
378+
<p>{isDivOneInViewport ? 'Div one In viewport' : 'Div one out of viewport'}</p>
379+
</div>
380+
<div ref={divTwoTargetRef}>
381+
<p>{isDivTwoInViewport ? 'Div two in viewport' : 'Div two out of viewport'}</p>
382+
</div>
383+
</div>
384+
)
385+
}
386+
```

0 commit comments

Comments
 (0)