Skip to content

Commit

Permalink
feat: scroll to ayah
Browse files Browse the repository at this point in the history
  • Loading branch information
aacmal committed Jul 1, 2023
1 parent 8e90083 commit 433b96d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import classNames from 'classnames';
import React, { useState, useCallback } from 'react';
import ChapterLists from './ChapterLists';
import IndexOfChapterLists from './IndexOfChapterLists';
import Link from 'next/link';
import { ChevronIcon, ArrowIcon } from '../../icons';
import { LocalChapter } from 'data/chapter/type';
import VerseList from './VerseList';

type DropdownSurahListsProps = {
chapterLists: LocalChapter[];
Expand Down Expand Up @@ -90,7 +90,7 @@ const DropdownSurahLists = ({
}
chapterActive={chapterActive}
/>
<IndexOfChapterLists
<VerseList
chapterLists={chapterLists}
chapterId={chapterActive - 1} // -1 because array start from 0
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import classNames from 'classnames';
import { LocalChapter } from 'data/chapter/type';
import { useCallback } from 'react';
import { useRouter } from 'next/navigation';

type IndexOfChapterListsProps = {
type VerseListProps = {
chapterLists: LocalChapter[];
chapterId: number;
};

const IndexOfChapterLists = ({
chapterLists,
chapterId,
}: IndexOfChapterListsProps) => {
const scrollToSpecificVerse = useCallback((verseKey: string) => {
const verseElement = document.querySelector(
`[data-verse="${verseKey}"]`
) as HTMLElement;

if (!verseElement) return;
const verseYLocation = verseElement.offsetTop;
window.scrollTo(0, verseYLocation - 120);
}, []);
const VerseList = ({ chapterLists, chapterId }: VerseListProps) => {
const router = useRouter();

return (
<ul
Expand All @@ -31,10 +20,10 @@ const IndexOfChapterLists = ({
.fill(0)
.map((key, index) => (
<li
key={index}
onClick={() =>
scrollToSpecificVerse(`${chapterId + 1}:${index + 1}`)
router.push(`/quran/surah/${chapterId + 1}?ayah=${index + 1}`)
}
key={index}
className="p-1 cursor-pointer hover:bg-emerald-100 dark:hover:bg-emerald-400 dark:hover:text-slate-100 hover:text-emerald-500 rounded flex items-center"
>
{index + 1}
Expand All @@ -44,4 +33,4 @@ const IndexOfChapterLists = ({
);
};

export default IndexOfChapterLists;
export default VerseList;
12 changes: 12 additions & 0 deletions src/components/quranReader/FetchInfiniteVerse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getVerses } from '@utils/verse';
import useQuranReader from '@stores/quranReaderStore';
import { shallow } from 'zustand/shallow';
import useSettings from '@stores/settingsStore';
import { useSearchParams } from 'next/navigation';

type Props = {
totalData: number;
Expand All @@ -21,6 +22,7 @@ const FetchInfiniteVerse = ({ totalData, id, getVerseBy }: Props) => {
const [data, setData] = useState<Verse[]>([]);
const [paginationData, setPaginationData] = useState<VersePagination>();
const [itemsRendered, setItemsRendered] = useState<ListItem<any>[]>();
const searchParams = useSearchParams();

const ref = useRef<VirtuosoHandle>(null);
const { highlightedWord, highlightedVerse, currentChapter } = useQuranReader(
Expand Down Expand Up @@ -86,6 +88,16 @@ const FetchInfiniteVerse = ({ totalData, id, getVerseBy }: Props) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [highlightedWord]);

useEffect(() => {
if (!searchParams.get('ayah')) return;
const verseNumber = Number(searchParams.get('ayah'));
if (verseNumber <= LIMIT) return; // because the first verse is already rendered on the server
ref.current.scrollToIndex({
index: verseNumber - LIMIT - 1,
align: 'center',
});
}, [searchParams]);

const renderRow = (index: number) => {
const dataIndex = index;
if (!data[dataIndex]) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/quranReader/InitialSurahVerse.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Verse } from '@utils/types/Verse';
import React from 'react';
import Verses from './Verses';
import ScrollToAyah from './ScrollToAyah';

type Props = {
versesData: Verse[];
Expand All @@ -9,6 +10,7 @@ type Props = {
const InitialSurahVerse = ({ versesData }: Props) => {
return (
<>
<ScrollToAyah />
{versesData.map((verse) => (
<Verses
key={verse.id}
Expand Down
30 changes: 30 additions & 0 deletions src/components/quranReader/ScrollToAyah.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client';

import { useParams, useSearchParams } from 'next/navigation';
import React, { useEffect } from 'react';

const ScrollToAyah = () => {
const searchParams = useSearchParams();
const params = useParams();

useEffect(() => {
if (!searchParams.has('ayah')) return;
const ayah = searchParams.get('ayah');
if (Number(ayah) > 20) return;
const verseElement = document.querySelector(
`[data-verse="${params.chapterId}:${ayah}"]`
) as HTMLElement;

if (!verseElement) return;

verseElement.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'center',
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchParams]);
return <></>;
};

export default ScrollToAyah;
2 changes: 1 addition & 1 deletion src/components/quranReader/Verses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Verses = ({
return (
<>
<div
id={verse_number.toString()}
id={id.toString()}
className="flex justify-between py-3 md:flex-row flex-col"
>
<div className="flex md:flex-col flex-row items-center mb-4">
Expand Down

1 comment on commit 433b96d

@vercel
Copy link

@vercel vercel bot commented on 433b96d Jul 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

pustaka-islam – ./

pustaka-islam-axcamz.vercel.app
pustakaislam.vercel.app
pustaka-islam-git-master-axcamz.vercel.app

Please sign in to comment.