-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
8fe3907
commit ce5f7f4
Showing
4 changed files
with
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
# Changelog | ||
|
||
## v1.0.1 | ||
|
||
- Added the `InterObs` island for abstracting the native Intersection Observer | ||
js functionality. | ||
- Added the `effects.transparent` global style for animations that start from | ||
`opacity: 0;`. | ||
|
||
## v1.0.0 | ||
|
||
- This updates marks the beginning of Lunchbox as a fully released product. Everything is official from now on. | ||
- This updates marks the beginning of Lunchbox as a fully released product. | ||
Everything is official from now on. |
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,60 @@ | ||
// ___ _ ___ _ | ||
// |_ _|_ _| |_ ___ _ _ / _ \| |__ ___ | ||
// | || ' \ _/ -_) '_| (_) | '_ (_-< | ||
// |___|_||_\__\___|_| \___/|_.__/__/ | ||
// | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
* ### InterObs | ||
* *Organism* | ||
* | ||
* This module contains the render function for the `<InterObs />` island. | ||
* | ||
* @module | ||
*/ | ||
import { useEffect, useRef, useState } from 'preact/hooks'; | ||
import setup, { iInterObs } from './setup.ts'; | ||
import { cn } from '../../src/utils.ts'; | ||
import { effects } from '../../src/styles.ts'; | ||
|
||
export default function (props: Partial<iInterObs>) { | ||
const { animation, isIntersectingCb, observerOptions, ...p } = setup(props); | ||
|
||
const ref = useRef<HTMLDivElement>(null); | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!ref.current) return; | ||
|
||
const observerCallback: IntersectionObserverCallback = ( | ||
entries, | ||
observer, | ||
) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
setIsVisible(true); | ||
isIntersectingCb(); | ||
observer.unobserve(entry.target); | ||
} | ||
}); | ||
}; | ||
|
||
const observer = new IntersectionObserver( | ||
observerCallback, | ||
observerOptions, | ||
); | ||
observer.observe(ref.current); | ||
|
||
return () => { | ||
if (ref.current) observer.unobserve(ref.current); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
{...p} | ||
class={cn(p.class, isVisible ? animation : effects.transparent)} | ||
/> | ||
); | ||
} |
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,44 @@ | ||
// ___ _ ___ _ | ||
// |_ _|_ _| |_ ___ _ _ / _ \| |__ ___ | ||
// | || ' \ _/ -_) '_| (_) | '_ (_-< | ||
// |___|_||_\__\___|_| \___/|_.__/__/ | ||
// | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
* This module contains the prop type, default values, and styles for the `<InterObs />` island. | ||
* | ||
* @module | ||
*/ | ||
import { apDef, o } from '../../src/utils.ts'; | ||
import { iComponent } from '../../src/types.ts'; | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
/** Properties of the `<InterObs />` island. */ | ||
export type iInterObs = iComponent<HTMLDivElement> & { | ||
/** Class that makes a CSS animation. */ | ||
animation: string; | ||
/** Function that will run whenever the island is intersecting. */ | ||
isIntersectingCb: () => void; | ||
observerOptions: IntersectionObserverInit; | ||
}; | ||
|
||
/** These are the default values of the `<Menu />` island's props. */ | ||
const defaults: iInterObs = { | ||
animation: '', | ||
isIntersectingCb: () => null, | ||
observerOptions: { | ||
root: null, | ||
rootMargin: '0px', | ||
threshold: 1, | ||
}, | ||
}; | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
/** Setup function of the `<Menu />` island. */ | ||
export default (props: Partial<iInterObs>) => { | ||
const p = apDef<iInterObs>(defaults, props); | ||
|
||
p.class = o('interObs', { ...p }); | ||
|
||
return p; | ||
}; |
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