generated from zayne-labs/create-ts-library
-
-
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(toolkit-react): ✨ create core version of useScrollObserver hook …
…for scroll detection
- Loading branch information
1 parent
878752d
commit e1ea64d
Showing
7 changed files
with
87 additions
and
171 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 |
---|---|---|
@@ -1,28 +1,37 @@ | ||
import { useResource } from "@zayne-labs/toolkit-react"; | ||
import { useScrollObserver } from "@zayne-labs/toolkit-react"; | ||
|
||
function AnotherApp() { | ||
const resource = useResource({ | ||
fn: () => fetch("https://jsonplaceholder.typicode.com/todos/1").then((res) => res.json()), | ||
}); | ||
const { isScrolled, observedElementRef } = useScrollObserver(); | ||
Check failure on line 4 in apps/dev/src/client/AnotherApp.tsx
|
||
|
||
console.info(resource); | ||
console.info({ isScrolled }); | ||
|
||
return ( | ||
<> | ||
<div> | ||
<style> | ||
{` | ||
.btn { | ||
background-color: red; | ||
margin-top: 10px; | ||
@scope { | ||
button { | ||
background-color: red; | ||
margin-top: 10px; | ||
} | ||
header { | ||
position: sticky; | ||
top: 0; | ||
height: 100px; | ||
background-color: blue; | ||
width: 100%; | ||
margin-bottom: 10000px; | ||
} | ||
} | ||
`} | ||
</style> | ||
<div> | ||
<button className="btn" type="button"> | ||
Force Render | ||
</button> | ||
</div> | ||
</> | ||
<header ref={observedElementRef}>Header</header> | ||
<div /> | ||
<button className="btn" type="button"> | ||
Force Render | ||
</button> | ||
</div> | ||
); | ||
} | ||
export default AnotherApp; |
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 |
---|---|---|
|
@@ -5,9 +5,9 @@ | |
"packageManager": "[email protected]", | ||
"author": "Ryan Zayne", | ||
"scripts": { | ||
"build": "pnpm --filter ./packages/* build", | ||
"build": "pnpm --filter \"./packages/*\" build", | ||
"build:dev": "pnpm --filter ./packages/* build:dev", | ||
"build:test": "pnpm --filter ./packages/* build:test", | ||
"build:test": "pnpm --filter \"./packages/*\" build:test", | ||
"bump": "bumpp", | ||
"dev": "pnpm --filter ./packages/* dev", | ||
"inspect:eslint-config": "pnpx @eslint/config-inspector@latest", | ||
|
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 |
---|---|---|
|
@@ -73,7 +73,7 @@ | |
"size-limit": [ | ||
{ | ||
"path": "./src/index.ts", | ||
"limit": "3.6 kb" | ||
"limit": "3.7 kb" | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { isBrowser } from "./constants"; | ||
|
||
export type ScrollObserverOptions = IntersectionObserverInit & { | ||
onIntersection?: (entry: IntersectionObserverEntry, observer: IntersectionObserver) => void; | ||
}; | ||
|
||
export const createScrollObserver = <TElement extends HTMLElement>( | ||
options: ScrollObserverOptions = {} | ||
) => { | ||
const { rootMargin = "10px 0px 0px 0px", ...restOfOptions } = options; | ||
|
||
const elementObserver = isBrowser() | ||
? new IntersectionObserver( | ||
(entries, observer) => entries.forEach((entry) => options.onIntersection?.(entry, observer)), | ||
{ rootMargin, ...restOfOptions } | ||
) | ||
: null; | ||
|
||
const handleObservation = (element: TElement | null) => { | ||
const scrollWatcher = document.createElement("span"); | ||
scrollWatcher.dataset.scrollWatcher = ""; | ||
|
||
element?.before(scrollWatcher); | ||
|
||
if (!elementObserver) return; | ||
|
||
elementObserver.observe(scrollWatcher); | ||
|
||
const cleanupFn = () => { | ||
scrollWatcher.remove(); | ||
elementObserver.disconnect(); | ||
}; | ||
|
||
return cleanupFn; | ||
}; | ||
|
||
return { elementObserver, handleObservation }; | ||
}; |
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