Skip to content

Commit

Permalink
fix(ui): add useBrowser hook and update image-block component
Browse files Browse the repository at this point in the history
A new useBrowser hook is implemented to determine the user's browser. The image-block component from the web application has been updated to utilize this hook. A conditional transition feature is added to the image-block component, which allows image zooming transition only on Chrome desktop to prevent image flickering.
  • Loading branch information
kkhys committed Jun 9, 2024
1 parent cfee806 commit eae03a7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions apps/web/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './use-browser';
export * from './use-media-query';
export * from './use-time-distance';
23 changes: 23 additions & 0 deletions apps/web/src/hooks/use-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react';

type Browser = 'internet explorer' | 'edge' | 'chrome' | 'safari' | 'firefox' | 'opera' | 'other';

export const useBrowser = () => {
const [browser, setBrowser] = React.useState<Browser | null>(null);

React.useEffect(() => {
const userAgent = typeof window.navigator === 'undefined' ? '' : navigator.userAgent;
const isOpera = !!window.opr;
const isEdge = window.navigator.userAgent.indexOf('Edge') > -1;

if (/Trident|MSIE/.test(userAgent)) setBrowser('internet explorer');
else if (isEdge) setBrowser('edge');
else if (/Chrome/.test(userAgent) && !isOpera && !isEdge) setBrowser('chrome');
else if (/Safari/.test(userAgent) && !isOpera && !isEdge) setBrowser('safari');
else if (/Firefox/.test(userAgent)) setBrowser('firefox');
else if (isOpera || /OPR/.test(userAgent)) setBrowser('opera');
else setBrowser('other');
}, []);

return { browser };
};
15 changes: 9 additions & 6 deletions apps/web/src/ui/post/blocks/image-block/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { motion } from 'framer-motion';

import { cn } from '@kkhys/ui';

import { useBrowser, useMediaQuery } from '#/hooks';

const NextImage = ({
src,
alt,
width,
height,
blurDataURL,
loading,
onClick,
layoutId,
className,
Expand All @@ -31,7 +32,6 @@ const NextImage = ({
sizes='(min-width: 768px) 42rem, 100vw'
placeholder={blurDataURL ? 'blur' : 'empty'}
blurDataURL={blurDataURL}
loading={loading}
quality={90}
className={cn('w-full rounded-2xl border-[#474747] shadow dark:border dark:shadow-none', className)}
onClick={onClick}
Expand All @@ -55,6 +55,10 @@ export const ImageBlock = ({
}) => {
const [isOpen, setOpen] = React.useState(false);

const { browser } = useBrowser();
const isDesktop = useMediaQuery('(min-width: 768px)');
const allowTransition = browser === 'chrome' && isDesktop;

React.useEffect(() => {
const handleScroll = () => {
if (isOpen) setOpen(false);
Expand All @@ -76,16 +80,15 @@ export const ImageBlock = ({
height={height as number}
blurDataURL={blurDataURL}
onClick={() => setOpen(!isOpen)}
// I don't like to use it too much, but it causes image flickering, so I specify it.
loading='eager'
layoutId={src}
layoutId={allowTransition ? src : undefined}
className={cn(!isOpen && 'cursor-zoom-in')}
/>
{isOpen && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.4 }}
onClick={() => setOpen(false)}
className='fixed inset-0 z-20 bg-background/80 backdrop-blur-[2px]'
>
Expand All @@ -95,7 +98,7 @@ export const ImageBlock = ({
width={width as number}
height={height as number}
onClick={() => setOpen(!isOpen)}
layoutId={src}
layoutId={allowTransition ? src : undefined}
className={cn('fixed inset-0 z-30 m-auto w-[1100px]', isOpen ? 'cursor-zoom-out' : 'cursor-zoom-in')}
/>
</motion.div>
Expand Down

0 comments on commit eae03a7

Please sign in to comment.