Home >Web Front-end >JS Tutorial >Accessible Components: Pagination
Today we are going to see how to create a pagination from scratch and make it accessible and reusable. I hope it helps you and leave me your comments at the end of the post!
Github: https://github.com/micaavigliano/accessible-pagination
Project: https://accessible-pagination.vercel.app/
const useFetch = <T,>(url: string, currentPage: number = 0, pageSize: number = 20) => { const [data, setData] = useState<T | null>(null); const [loading, setLoading] = useState<boolean>(true); const [error, setError] = useState<boolean>(false); useEffect(() => { const fetchData = async() => { setLoading(true); setError(false); try { const response = await fetch(url); if (!response.ok) { throw new Error('network response failed') } const result: T = await response.json() as T; setData(result) } catch (error) { setError(true) } finally { setLoading(false); } }; fetchData() }, [url, currentPage, pageSize]); return { data, loading, error, } };
For a page to be accessible we must take into account the following points:
aria-pointset is used to calculate the position of the item within all the items on the page. The screen reader will announce it as follows:
In order to get to this we are going to code it as follows:
const useFetch = <T,>(url: string, currentPage: number = 0, pageSize: number = 20) => { const [data, setData] = useState<T | null>(null); const [loading, setLoading] = useState<boolean>(true); const [error, setError] = useState<boolean>(false); useEffect(() => { const fetchData = async() => { setLoading(true); setError(false); try { const response = await fetch(url); if (!response.ok) { throw new Error('network response failed') } const result: T = await response.json() as T; setData(result) } catch (error) { setError(true) } finally { setLoading(false); } }; fetchData() }, [url, currentPage, pageSize]); return { data, loading, error, } };
When the page stops loading, we will set a new message with our currentPage and the length of the new array we are loading.
Now yes! Let's see how the code is structured in the file pagination.tsx
The component will require five props
const [statusMessage, setStatusMessage] = useState<string>(""); useEffect(() => { window.scrollTo({ top: 0, behavior: 'smooth' }); if (!loading) { setStatusMessage(`Page ${currentPage} loaded. Displaying ${data?.near_earth_objects.length || 0} items.`); } }, [currentPage, loading]);
interface PaginationProps { currentPage: number; totalPages: number; nextPage: () => void; prevPage: () => void; goToPage: (page: number) => void; }
const handlePageChange = (newPage: number) => { setCurrentPage(newPage); }; const nextPage = () => { if (currentPage < totalPages) { handlePageChange(currentPage + 1); } };
const prevPage = () => { if (currentPage > 1) { handlePageChange(currentPage - 1); } };
For our pagination to come to life we need one more step, creating the array that we are going to iterate in our list! For that we must follow the following steps:
This array is the one we are going to go through to obtain the list of items in our page as follows:
const useFetch = <T,>(url: string, currentPage: number = 0, pageSize: number = 20) => { const [data, setData] = useState<T | null>(null); const [loading, setLoading] = useState<boolean>(true); const [error, setError] = useState<boolean>(false); useEffect(() => { const fetchData = async() => { setLoading(true); setError(false); try { const response = await fetch(url); if (!response.ok) { throw new Error('network response failed') } const result: T = await response.json() as T; setData(result) } catch (error) { setError(true) } finally { setLoading(false); } }; fetchData() }, [url, currentPage, pageSize]); return { data, loading, error, } };
And here's how to make a reusable and accessible pagination! Personally, I learned how to create a page from scratch because I had to implement it in live coding. I hope that my experience will be helpful for your career and that you can implement and even improve it!
Greetings,
Mica<3
The above is the detailed content of Accessible Components: Pagination. For more information, please follow other related articles on the PHP Chinese website!