-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for mobile viewport innerheight (#57)
* separate actions and state in main store * next config paths only in prod * override and use css vars to calc min-h-screen * hook for window resize to update css var for height (mobile fix) * little refactor and use min-h-screen custom util class * execute window resize hook in root app
- Loading branch information
1 parent
fa9e38b
commit 66d6266
Showing
11 changed files
with
86 additions
and
13 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
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useEffect } from 'react'; | ||
import { useMainStoreActions } from '../../store/store'; | ||
|
||
/** | ||
* This hook is used to observe the `window` object. | ||
* and update the window dimensions in the store. | ||
*/ | ||
export const useResizeWindow = () => { | ||
const { setWindowDimensions } = useMainStoreActions(); | ||
|
||
useEffect(() => { | ||
let timer = null; | ||
window.onresize = () => { | ||
// debounce the resize event to prevent too many re-renders | ||
clearTimeout(timer); | ||
timer = setTimeout(() => { | ||
const innerHeight = window.innerHeight; | ||
setWindowDimensions({ | ||
width: window.innerWidth, | ||
height: innerHeight, | ||
}); | ||
|
||
// set css variable for mobile browsers | ||
let vh = innerHeight * 0.01; | ||
document.documentElement.style.setProperty('--vh', `${vh}px`); | ||
}, 100); | ||
}; | ||
// cleanup listener | ||
return () => { | ||
window.onresize = null; | ||
}; | ||
}, [setWindowDimensions]); | ||
}; |
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,7 +1,9 @@ | ||
import 'leaflet/dist/leaflet.css'; | ||
import { useResizeWindow } from '@/lib/hooks/useResizeWindow'; | ||
import '@/styles/globals.css'; | ||
import 'leaflet/dist/leaflet.css'; | ||
import type { AppProps } from 'next/app'; | ||
|
||
export default function App({ Component, pageProps }: AppProps) { | ||
useResizeWindow(); | ||
return <Component {...pageProps} />; | ||
} |
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,15 +1,32 @@ | ||
//@ts-check | ||
import { create } from 'zustand'; | ||
|
||
const useMainStore = create((set) => ({ | ||
/** | ||
* main global zustand store | ||
* - stores the state | ||
* - actions separated into the `useMainStoreActions` object | ||
*/ | ||
const useMainStore = create(() => ({ | ||
geoData: { lat: -8.7445, lng: 115.182 }, | ||
markers: [], | ||
setMarkers: (object) => | ||
set((state) => ({ markers: [...state.markers, object] })), | ||
rootDimensions: { width: 0, height: 0 }, | ||
windowDimensions: { width: 0, height: 0 }, | ||
})); | ||
|
||
/** | ||
* state updater funcs for the main zustand store | ||
*/ | ||
export const useMainStoreActions = () => ({ | ||
setGeoData: ({ lat, lng }) => { | ||
console.info('setGeoData', lat, lng); | ||
set({ geoData: { lat, lng } }); | ||
useMainStore.setState({ geoData: { lat, lng } }); | ||
}, | ||
})); | ||
setMarkers: (object) => | ||
useMainStore.setState((state) => ({ markers: [...state.markers, object] })), | ||
setRootDimensions: ({ width, height }) => | ||
useMainStore.setState({ rootDimensions: { width, height } }), | ||
setWindowDimensions: ({ width, height }) => | ||
useMainStore.setState({ windowDimensions: { width, height } }), | ||
}); | ||
|
||
export default useMainStore; |
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