Skip to content

Commit

Permalink
web resize changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jxmoose committed May 14, 2024
1 parent 7fcdd1a commit 6a30121
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 25 deletions.
16 changes: 4 additions & 12 deletions src/app/exhibitsPage/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { CategoryRow } from '../../types/types';
import { fetchAllCategories } from '../../supabase/category/queries';
import Exhibit from '../../components/userComponents/Exhibit/Exhibit';
import BackButton from '../../components/userComponents/BackButton/page';
import { useWebDeviceDetection } from '../../context/WindowWidthContext/WindowWidthContext';

/**
* @returns exhibit page
*/
function App() {
const isWebDevice = useWebDeviceDetection();
const [exhibits, setExhibits] = useState<CategoryRow[]>([]);
// fetches all the exhibits to display on the page
useEffect(() => {
Expand All @@ -24,16 +26,6 @@ function App() {
// Detect the hash in URL and scroll to the element with the corresponding ID
}, [exhibits]);

// for web rendering
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
useEffect(() => {
const handleResize = () => setWindowWidth(window.innerWidth);
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);

// activates whenever the page opens.
// checks if there's a "hash" which is an id of one of the exhibits to scroll to.
// scrolls down to corresponding exhibit with slight offset
Expand All @@ -54,7 +46,7 @@ function App() {
}, []);
return (
<div>
{windowWidth <= 1024 && (
{!isWebDevice && (
<div className="bg-ivory">
<NavBar />
<div className="p-4 m-auto">
Expand Down Expand Up @@ -95,7 +87,7 @@ function App() {
</div>
</div>
)}
{windowWidth > 1024 && (
{isWebDevice && (
<div className="bg-ivory">
<NavBar />
<div className="pl-64 pr-64 pt-24 m-auto">
Expand Down
20 changes: 9 additions & 11 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import './globals.css';
import type { Metadata } from 'next';
import { Inter, Lato, Manrope } from 'next/font/google';
import { Inter } from 'next/font/google';
import React from 'react';
import { WindowWidthProvider } from '../context/WindowWidthContext/WindowWidthContext';

const inter = Inter({ subsets: ['latin'] });

const manrope = Manrope({ weight: ['400', '700'], subsets: ['latin'] });

const lato = Lato({
weight: ['400', '700'],
subsets: ['latin'],
});
// const lato = Lato({ weight: ['300', '400', '500', '700'], subsets: ['latin'] });

export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
};

/**
* @param root0 - Destructured object containing the properties.
* @param root0.children - The child elements to be rendered within the RootLayout component.
Expand All @@ -37,9 +32,10 @@ export default function RootLayout({
crossOrigin=""
/>
<link
href="https://fonts.googleapis.com/css?family=Lato:200,300,400,700"
href="https://fonts.googleapis.com/css?family=Lato:400,700"
rel="stylesheet"
integrity="sha384-S/0sPWyivHb0nXEN1JbIZoAHSDK4PQv0Zdl6399sbd0mYRatVZsKWdfflTIgipPy"
integrity="sha384-DelNu+PL/74bL4pHGH8gPG8J5Q6wrLpZiiVttBSvpOibBVQf3EOXerhZlmHcnZsI"
type="text/css"
crossOrigin=""
/>
<script
Expand All @@ -50,7 +46,9 @@ export default function RootLayout({
<script src="html5-qrcode.min.js" />
</head>

<body className={inter.className}>{children}</body>
<body className={inter.className}>
<WindowWidthProvider>{children}</WindowWidthProvider>
</body>
</html>
);
}
6 changes: 4 additions & 2 deletions src/components/userComponents/Exhibit/Exhibit.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import Image from 'next/image';
import { useWebDeviceDetection } from '../../../context/WindowWidthContext/WindowWidthContext';

/**
*
Expand All @@ -24,9 +25,10 @@ export default function Exhibit({
id: number;
web: boolean;
}) {
const isWebDevice = useWebDeviceDetection();
return (
<div>
{!web && (
{!isWebDevice && (
<li key={id} id={`a${id}`}>
<div className="w-full px-4 py-8 bg-mint-cream rounded-lg flex-col justify-start items-start gap-2.5 inline-flex mt-6">
<div className="flex-col justify-start items-start gap-5 flex">
Expand All @@ -44,7 +46,7 @@ export default function Exhibit({
</div>
</li>
)}
{web && (
{isWebDevice && (
<div className="flex flex-col w-full px-8 py-16 bg-mint-cream rounded-lg flex-col justify-start items-start gap-2.5 mt-6">
<div className="justify-start items-start gap-5">
<div className="justify-start items-center gap-2">
Expand Down
58 changes: 58 additions & 0 deletions src/context/WindowWidthContext/WindowWidthContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use client';

import React, {
createContext,
useState,
useEffect,
useContext,
useMemo,
} from 'react';

interface WindowWidthContextProps {
isWeb: boolean;
}

const WindowWidthContext = createContext<WindowWidthContextProps | undefined>(
undefined,
);
/**
*
* @param root0 - Provider that checks window width and calculates if this is a web or mobile screen
* @param root0.children - Provider will give context to its children components.
* @returns The WindowWidthProvider component, should be wrapped across the whole project for
* responsive design, per the Progressive Web Application functionality.
* This provider is used in the root layout of the project such that we have global access to the window width
* and can conditionally render accordingly.
*/
export function WindowWidthProvider({
children,
}: {
children: React.ReactNode;
}) {
const [isWeb, setIsWeb] = useState(window.innerWidth >= 1024);

useEffect(() => {
const handleResize = () => setIsWeb(window.innerWidth >= 1024);
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);

const windowWidthValue = useMemo(() => ({ isWeb }), [isWeb]);

return (
<WindowWidthContext.Provider value={windowWidthValue}>
{children}
</WindowWidthContext.Provider>
);
}

export const useWebDeviceDetection = () => {
const context = useContext(WindowWidthContext);
if (context === undefined) {
throw new Error('useIsWeb must be used within a WindowWidthProvider');
}

return context.isWeb;
};

0 comments on commit 6a30121

Please sign in to comment.