-
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.
Add useIsIntersecting and useIsIntersectingState hooks #270
- Loading branch information
1 parent
8c7973d
commit 294ad43
Showing
9 changed files
with
204 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Meta } from '@storybook/blocks'; | ||
|
||
<Meta title="Hooks / useIsIntersecting" /> | ||
|
||
# useIsIntersecting | ||
|
||
Please add a description about the `useIsIntersecting` hook. | ||
|
||
## Reference | ||
|
||
```ts | ||
function useIsIntersecting(): void; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `TODO` - Describe the parameters here. | ||
|
||
### Returns | ||
|
||
- `TODO` - Define the return value here. | ||
|
||
## Usage | ||
|
||
```tsx | ||
function DemoComponent() { | ||
useIsIntersecting(); | ||
return <div></div>; | ||
} | ||
``` |
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,37 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import type { ReactElement } from 'react'; | ||
import { useIsIntersecting } from './useIsIntersecting.js'; | ||
|
||
const meta = { | ||
title: 'Hooks / useIsIntersecting', | ||
} satisfies Meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
function DemoComponent(): ReactElement { | ||
useIsIntersecting(); | ||
|
||
return ( | ||
<div> | ||
<div className="alert alert-primary"> | ||
<h4 className="alert-heading">Instructions!</h4> | ||
<p className="mb-0">Add instructions about the hook here.</p> | ||
</div> | ||
<div> | ||
Value: <span className="badge rounded-pill bg-primary">{/* value */}</span> | ||
</div> | ||
<div className="card border-dark" data-ref="test-area"> | ||
<div className="card-header">Test Area</div> | ||
<div className="card-body"> | ||
<p>TODO: Implement a test case for the hook.</p> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export const Demo: Story = { | ||
render() { | ||
return <DemoComponent />; | ||
}, | ||
}; |
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 { renderHook } from '@testing-library/react'; | ||
import { describe, it } from 'vitest'; | ||
import { useIsIntersecting } from './useIsIntersecting.js'; | ||
|
||
describe('useIsIntersecting', () => { | ||
it('should not crash', async () => { | ||
renderHook(() => { | ||
return useIsIntersecting(); | ||
}); | ||
}); | ||
}); |
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 { RefObject, useRef, useState } from 'react'; | ||
import { useIntersectionObserver } from '../useIntersectionObserver/useIntersectionObserver.js'; | ||
|
||
/** | ||
* @param targetOrTargets | ||
* @param options | ||
*/ | ||
export function useIsIntersecting( | ||
targetOrTargets: RefObject<Element | ReadonlyArray<Element | null> | null>, | ||
options?: IntersectionObserverInit, | ||
): RefObject<boolean> { | ||
const isIntersectingRef = useRef(false); | ||
|
||
useIntersectionObserver( | ||
targetOrTargets, | ||
(entries) => { | ||
isIntersectingRef.current = entries.every((entry) => entry.isIntersecting); | ||
}, | ||
options, | ||
); | ||
|
||
return isIntersectingRef; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/hooks/useIsIntersectingState/useIsIntersectingState.mdx
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,30 @@ | ||
import { Meta } from '@storybook/blocks'; | ||
|
||
<Meta title="Hooks / useIsIntersectingState" /> | ||
|
||
# useIsIntersectingState | ||
|
||
Please add a description about the `useIsIntersectingState` hook. | ||
|
||
## Reference | ||
|
||
```ts | ||
function useIsIntersectingState(): void; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `TODO` - Describe the parameters here. | ||
|
||
### Returns | ||
|
||
- `TODO` - Define the return value here. | ||
|
||
## Usage | ||
|
||
```tsx | ||
function DemoComponent() { | ||
useIsIntersectingState(); | ||
return <div></div>; | ||
} | ||
``` |
37 changes: 37 additions & 0 deletions
37
src/hooks/useIsIntersectingState/useIsIntersectingState.stories.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,37 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import type { ReactElement } from 'react'; | ||
import { useIsIntersectingState } from './useIsIntersectingState.js'; | ||
|
||
const meta = { | ||
title: 'Hooks / useIsIntersectingState', | ||
} satisfies Meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
function DemoComponent(): ReactElement { | ||
useIsIntersectingState(); | ||
|
||
return ( | ||
<div> | ||
<div className="alert alert-primary"> | ||
<h4 className="alert-heading">Instructions!</h4> | ||
<p className="mb-0">Add instructions about the hook here.</p> | ||
</div> | ||
<div> | ||
Value: <span className="badge rounded-pill bg-primary">{/* value */}</span> | ||
</div> | ||
<div className="card border-dark" data-ref="test-area"> | ||
<div className="card-header">Test Area</div> | ||
<div className="card-body"> | ||
<p>TODO: Implement a test case for the hook.</p> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export const Demo: Story = { | ||
render() { | ||
return <DemoComponent />; | ||
}, | ||
}; |
11 changes: 11 additions & 0 deletions
11
src/hooks/useIsIntersectingState/useIsIntersectingState.test.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,11 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { describe, it } from 'vitest'; | ||
import { useIsIntersectingState } from './useIsIntersectingState.js'; | ||
|
||
describe('useIsIntersectingState', () => { | ||
it('should not crash', async () => { | ||
renderHook(() => { | ||
return useIsIntersectingState(); | ||
}); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
src/hooks/useIsIntersectingState/useIsIntersectingState.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 { RefObject, useState } from 'react'; | ||
import { useIntersectionObserver } from '../useIntersectionObserver/useIntersectionObserver.js'; | ||
|
||
/** | ||
* @param targetOrTargets | ||
* @param options | ||
*/ | ||
export function useIsIntersectingState( | ||
targetOrTargets: RefObject<Element | ReadonlyArray<Element | null> | null>, | ||
options?: IntersectionObserverInit, | ||
): boolean { | ||
const [isIntersecting, setIsIntersecting] = useState(false); | ||
|
||
useIntersectionObserver( | ||
targetOrTargets, | ||
(entries) => { | ||
setIsIntersecting(() => entries.every((entry) => entry.isIntersecting)); | ||
}, | ||
options, | ||
); | ||
|
||
return isIntersecting; | ||
} |
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