Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add next/prev links and right/left arrow handling #226

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/components/IconTileModal.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@
margin-top: 2rem;
}

.nextPrevLinksWrapper {
position: absolute;
top: 20px;
left: 24px;
}

.nextPrevLinksWrapper a {
background: none;
border: none;
color: var(--link-color);
font-size: 1.5rem;
font-weight: bold;
padding-right: 0.2rem;

&:hover {
color: var(--link-color-dark);
cursor: pointer;
}
}

.nextPrevLinksWrapper span {
padding-right: 0.2rem;
font-size: 1.5rem;
font-weight: bold;
color: #ccc;
}

@media (min-width: 60rem) {
.modalWrapper {
max-height: none;
Expand Down
68 changes: 68 additions & 0 deletions src/components/IconTileModal.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
import styles from './IconTileModal.module.scss';
import ReactModal from 'react-modal';
import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { Icon } from '../lib/icons';

interface IconTileModalProps {
icon: Icon;
allIcons: Icon[];
iconType: string;
isOpen: boolean;
onClose: () => void;
}

export function IconTileModal(props: IconTileModalProps) {
const router = useRouter();

const currentIndex = props.allIcons.map((i) => i.id).indexOf(props.icon.id);
const nextIcon =
currentIndex < props.allIcons.length - 1
? props.allIcons[currentIndex + 1]
: null;

const prevIcon = currentIndex > 0 ? props.allIcons[currentIndex - 1] : null;

useEffect(() => {
const keyDownHandler = (e) => {
if (e.code === 'ArrowRight') {
if (nextIcon) {
router.replace(
`/icon/${props.iconType}/${nextIcon.category}/${nextIcon.id}`,
{ scroll: false }
);
}
} else if (e.code === 'ArrowLeft') {
if (prevIcon) {
router.replace(
`/icon/${props.iconType}/${prevIcon.category}/${prevIcon.id}`,
{ scroll: false }
);
}
}
};
document.addEventListener('keydown', keyDownHandler);

// clean up
return () => {
document.removeEventListener('keydown', keyDownHandler);
};
});

return (
<ReactModal
isOpen={props.isOpen}
Expand All @@ -21,6 +61,34 @@ export function IconTileModal(props: IconTileModalProps) {
>
<div className={styles.modalWrapper}>
<>
<div className={styles.nextPrevLinksWrapper}>
{prevIcon ? (
<Link
href={`/icon/${props.iconType}/${prevIcon.category}/${prevIcon.id}`}
title={`Previous: ${prevIcon.title}`}
scroll={false}
replace={true}
className={styles.nextPrevLink}
>
</Link>
) : (
<span className={styles.nextPrevLink}>◀</span>
)}
{nextIcon ? (
<Link
href={`/icon/${props.iconType}/${nextIcon.category}/${nextIcon.id}`}
title={`Next: ${nextIcon.title}`}
scroll={false}
replace={true}
className={styles.nextPrevLink}
>
</Link>
) : (
<span className={styles.nextPrevLink}>▶</span>
)}
</div>
<img
src={`/icons/svg/${props.iconType}/${props.icon.category}/${props.icon.id}.svg`}
className={styles.modalImage}
Expand Down
18 changes: 11 additions & 7 deletions src/components/LandingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function LandingPage({
);

const [modalIcon, setModalIcon] = useState<ModalIcon>(undefined);

const router = useRouter();

useMemo(() => {
Expand Down Expand Up @@ -102,6 +103,14 @@ export default function LandingPage({
return counter + c.icons.length;
}, 0);

const flatIcons = categoriesToRender.flatMap((c) => c.icons);
// if searching by keyword, show all icons without categories and sorted A-Z
if (searchKeywordsValue) {
flatIcons.sort((i1, i2) => {
return i1.title < i2.title ? -1 : i1.title > i2.title ? 1 : 0;
});
}

return (
<div className="container">
<TopBar />
Expand Down Expand Up @@ -184,13 +193,7 @@ export default function LandingPage({
</div>
{searchKeywordsValue ? (
<IconGrid
icons={categoriesToRender
.map((i) => i.icons)
.flat()
.sort((i1, i2) => {
// show all icons without categories and sorted A-Z
return i1.title < i2.title ? -1 : i1.title > i2.title ? 1 : 0;
})}
icons={flatIcons}
setModalIcon={setModalIcon}
style={searchStyleValue}
format={searchFormatValue}
Expand All @@ -214,6 +217,7 @@ export default function LandingPage({
icon={modalIcon.icon}
iconType={modalIcon.iconType}
isOpen={modalIcon !== undefined}
allIcons={flatIcons}
onClose={() => {
router.push('/', undefined, {
shallow: true,
Expand Down
Loading