-
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.
Implement smooth scrolling and category status
- Loading branch information
Showing
3 changed files
with
62 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,69 @@ | ||
import Head from 'next/head' | ||
import {useEffect, useState} from "react"; | ||
import ItemList from "@/components/item_list"; | ||
import {Config} from "@/lib/types"; | ||
import {Collection, Config} from "@/lib/types"; | ||
|
||
export default function Home() { | ||
|
||
const [collection, setCollection] = useState({} as Config); | ||
const [collections, setCollections] = useState([] as string[]); | ||
const [config, setConfig] = useState({} as Config); | ||
const [collections, setCollections] = useState([] as string[]); | ||
|
||
useEffect(() => { | ||
fetch("https://api.wapuugotchi.com/collection") | ||
.then(value => value.json()).then(value => { | ||
setCollection(value) | ||
let collections = []; | ||
for(let index in collection.collections) { | ||
collections.push(collection.collections[index].caption) | ||
} | ||
setCollections(collections) | ||
}); | ||
}) | ||
useEffect(() => { | ||
fetch("https://api.wapuugotchi.com/collection") | ||
.then(value => value.json()).then(value => { | ||
setConfig(value as Config) | ||
console.log(config) | ||
let collections = []; | ||
for (let index in config.collections) { | ||
collections.push(config.collections[index].caption) | ||
} | ||
setCollections(collections) | ||
}); | ||
}) | ||
|
||
function getCollection( type: string ) { | ||
return collection.collections.filter((value) => value.caption === type)[0] | ||
} | ||
function getCollection(type: string) { | ||
return config.collections.filter((value) => value.caption === type)[0] | ||
} | ||
|
||
function isInView( collection: string ) { | ||
let element = document.getElementById(collection.toLowerCase()); | ||
if(element) { | ||
let rect = element.getBoundingClientRect(); | ||
function scrollTo(collection: Collection) { | ||
let element = document.getElementById(collection.caption.toLowerCase() + "_header"); | ||
if (element) { | ||
element.scrollIntoView({behavior: "smooth", inline: "center", block: "center"}); | ||
} | ||
} | ||
|
||
return ( | ||
rect.top >= 0 && | ||
rect.left >= 0 && | ||
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /* or $(window).height() */ | ||
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /* or $(window).width() */ | ||
); | ||
// Check if the collection is currently in view | ||
function isInView(collection: Collection) { | ||
let element = document.getElementById(collection.caption.toLowerCase()); | ||
if (element) { | ||
let rect = element.getBoundingClientRect(); | ||
return rect.top >= 0 && rect.bottom <= window.innerHeight; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
return ( | ||
<div className="w-full h-full bg-gray-100" key={ "home_page" }> | ||
<div className="w-full h-12 bg-gray-800 flex flex-row justify-between py-1 px-5 z-20 fixed"> | ||
<h1 className="text-2xl font-bold text-white">Wapuugotchi Collection</h1> | ||
<ul className="flex flex-row gap-3 pr-5"> | ||
{ | ||
collections.map((value) => { | ||
return ( | ||
<li key={ getCollection(value).caption }> | ||
<a className={ isInView(getCollection(value).caption) ? "nav-item active" : "nav-item"} href={ "#" + getCollection(value).caption.toLowerCase() }>{ getCollection(value).caption }</a> | ||
</li> | ||
) | ||
}) | ||
} | ||
</ul> | ||
</div> | ||
<div className="py-12"> | ||
{collections.map((value) => { | ||
return ItemList(getCollection(value)) | ||
})} | ||
</div> | ||
</div> | ||
) | ||
return ( | ||
<div className="w-full h-full bg-gray-100" key={"home_page"}> | ||
<div className="w-full h-12 bg-gray-800 flex flex-row justify-between py-1 px-5 z-20 fixed"> | ||
<h1 className="text-2xl font-bold text-white">WapuuGotchi Collection</h1> | ||
<ul className="flex flex-row gap-3 pr-5 sm:hidden md:flex"> | ||
{ | ||
collections.map((collection) => { | ||
return ( | ||
<li key={getCollection(collection).caption}> | ||
<a className={isInView(getCollection(collection)) ? "nav-item active" : "nav-item"} | ||
onClick={event => scrollTo(getCollection(collection))}>{getCollection(collection).caption}</a> | ||
</li> | ||
) | ||
}) | ||
} | ||
</ul> | ||
</div> | ||
<div className="py-12"> | ||
{ | ||
collections.map((value) => { | ||
return ItemList(getCollection(value)) | ||
}) | ||
} | ||
</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