From 2aae37d67bfd387802724a2a94825716746389a4 Mon Sep 17 00:00:00 2001 From: Bryan Lee <38807139+liby@users.noreply.github.com> Date: Fri, 29 Sep 2023 03:47:34 +0800 Subject: [PATCH] Improve Device Detection For Better Responsiveness (#1512) * Refactor `useOnMainScroll` function to use responsive device detection - Replace static `isDesktopWeb` with `useWebMediaQueries` hook to enable dynamic device type detection. - Create `useDeviceLimits` hook to dynamically determine `DY_LIMIT_UP` and `DY_LIMIT_DOWN` based on device type. - Update dependency arrays for the `useCallback` hooks to include new dynamic variables. * Refactor styles to be responsive to device type - Create `useStyles` hook that generates styles object based on device type detected from `useWebMediaQueries`. - Replace static styles object with dynamic styles object generated from `useStyles` hook in components. - This allows `paddingLeft` values for 'ul' and 'ol' styles to adapt to device type dynamically. - This allows `maxWidth` values for 'metaItem'' styles to adapt to device type dynamically. * Remove `isDesktopWeb` in favor of `useWebMediaQueries().isDesktop` * Refactor `SplashScreen` component for responsive design * Revision based on review results * Fix isNative check --------- Co-authored-by: Paul Frazee --- src/lib/hooks/useOnMainScroll.ts | 24 ++- src/platform/detection.ts | 1 - src/view/com/auth/SplashScreen.web.tsx | 164 ++++++++-------- .../com/composer/photos/OpenCameraBtn.tsx | 5 +- .../com/composer/photos/SelectPhotoBtn.tsx | 6 +- src/view/com/post-thread/PostThread.tsx | 21 +- src/view/com/post-thread/PostThreadItem.tsx | 180 +++++++++--------- src/view/com/util/Html.tsx | 125 ++++++------ src/view/com/util/Link.tsx | 7 +- 9 files changed, 289 insertions(+), 244 deletions(-) diff --git a/src/lib/hooks/useOnMainScroll.ts b/src/lib/hooks/useOnMainScroll.ts index 507a28ce..250ef3a3 100644 --- a/src/lib/hooks/useOnMainScroll.ts +++ b/src/lib/hooks/useOnMainScroll.ts @@ -2,12 +2,18 @@ import {useState, useCallback, useRef} from 'react' import {NativeSyntheticEvent, NativeScrollEvent} from 'react-native' import {RootStoreModel} from 'state/index' import {s} from 'lib/styles' -import {isDesktopWeb} from 'platform/detection' +import {useWebMediaQueries} from './useWebMediaQueries' -const DY_LIMIT_UP = isDesktopWeb ? 30 : 10 -const DY_LIMIT_DOWN = isDesktopWeb ? 150 : 10 const Y_LIMIT = 10 +const useDeviceLimits = () => { + const {isDesktop} = useWebMediaQueries() + return { + dyLimitUp: isDesktop ? 30 : 10, + dyLimitDown: isDesktop ? 150 : 10, + } +} + export type OnScrollCb = ( event: NativeSyntheticEvent, ) => void @@ -18,6 +24,8 @@ export function useOnMainScroll( ): [OnScrollCb, boolean, ResetCb] { let lastY = useRef(0) let [isScrolledDown, setIsScrolledDown] = useState(false) + const {dyLimitUp, dyLimitDown} = useDeviceLimits() + return [ useCallback( (event: NativeSyntheticEvent) => { @@ -25,15 +33,11 @@ export function useOnMainScroll( const dy = y - (lastY.current || 0) lastY.current = y - if ( - !store.shell.minimalShellMode && - dy > DY_LIMIT_DOWN && - y > Y_LIMIT - ) { + if (!store.shell.minimalShellMode && dy > dyLimitDown && y > Y_LIMIT) { store.shell.setMinimalShellMode(true) } else if ( store.shell.minimalShellMode && - (dy < DY_LIMIT_UP * -1 || y <= Y_LIMIT) + (dy < dyLimitUp * -1 || y <= Y_LIMIT) ) { store.shell.setMinimalShellMode(false) } @@ -50,7 +54,7 @@ export function useOnMainScroll( setIsScrolledDown(false) } }, - [store, isScrolledDown], + [store.shell, dyLimitDown, dyLimitUp, isScrolledDown], ), isScrolledDown, useCallback(() => { diff --git a/src/platform/detection.ts b/src/platform/detection.ts index d414b008..f4f7be24 100644 --- a/src/platform/detection.ts +++ b/src/platform/detection.ts @@ -12,7 +12,6 @@ export const isMobileWeb = isWeb && // @ts-ignore we know window exists -prf global.window.matchMedia(isMobileWebMediaQuery)?.matches -export const isDesktopWeb = isWeb && !isMobileWeb export const deviceLocales = dedupArray( getLocales?.().map?.(locale => locale.languageCode), diff --git a/src/view/com/auth/SplashScreen.web.tsx b/src/view/com/auth/SplashScreen.web.tsx index 3c949bb9..cef9618e 100644 --- a/src/view/com/auth/SplashScreen.web.tsx +++ b/src/view/com/auth/SplashScreen.web.tsx @@ -6,7 +6,8 @@ import {ErrorBoundary} from 'view/com/util/ErrorBoundary' import {s, colors} from 'lib/styles' import {usePalette} from 'lib/hooks/usePalette' import {CenteredView} from '../util/Views' -import {isMobileWeb} from 'platform/detection' +import {isWeb} from 'platform/detection' +import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries' export const SplashScreen = ({ onPressSignin, @@ -16,6 +17,9 @@ export const SplashScreen = ({ onPressCreateAccount: () => void }) => { const pal = usePalette('default') + const {isTabletOrMobile} = useWebMediaQueries() + const styles = useStyles() + const isMobileWeb = isWeb && isTabletOrMobile return ( @@ -55,13 +59,14 @@ export const SplashScreen = ({ -