iimport classNames from 'classnames'; 1 import React, { Dispatch, RefObject,…
- User
iimport classNames from 'classnames';
1 import React, { Dispatch, RefObject, SetStateAction, useState } from 'react';
2
3 import { useDevice } from '@/helpers/deviceContext';
4
5 type PropsType = {
6 │ src?: string;
7 │ fullscreenSrc?: string;
8 │ alt?: string;
9 │ loading?: 'lazy' | 'eager';
10 │ className?: string;
11 │ fullScreen?: RefObject;
12 │ setDocFS?: Dispatch<SetStateAction>;
13 │ imageClassName?: string;
14 };
15
16 const Img: React.FC = ({
17 │ src = '',
18 │ fullscreenSrc = '',
19 │ alt = '',
20 │ loading = 'lazy',
21 │ className = '',
22 │ fullScreen = null,
23 │ imageClassName = '',
24 │ setDocFS = () => { },
25 }) => {
26 │ const { isDesktop } = useDevice();
27 │ const [imgSrc, setImgSrc] = useState(src);
28 │
29 │ const imgContainerClasses = classNames('relative bg-gray-200 overflow-hidden', className, {
30 │ │ ['aspect-[16/9]']: isDesktop,
31 │ });
32 │
33 │ const mainImageClasses = classNames('w-full h-full', imageClassName, {
34 │ │ ['absolute left-0 top-0 object-center object-contain']: isDesktop,
35 │ });
36 │
37 │ const handleClick = (e: React.MouseEvent) => {
38 │ │ const el = e.target as HTMLImageElement;
39 │ │ if (!fullscreenSrc || !('requestFullscreen' in el) || !fullScreen || !fullScreen.current) {
40 │ │ │ return;
41 │ │ }
42 │ │
43 │ │ setImgSrc(fullscreenSrc);
44 │ │ if (document.fullscreenElement) {
45 │ │ │ setDocFS(false);
46 │ │ │ void document.exitFullscreen();
47 │ │ } else {
48 │ │ │ setDocFS(true);
49 │ │ │ void fullScreen.current.requestFullscreen();
50 │ │ }
51 │ };
52 │
53 │ return (
54 │ │
55 │ │ │ <img
56 │ │ │ │ className={classNames(mainImageClasses)}
57 │ │ │ │ src={imgSrc}
58 │ │ │ │ alt={alt}
59 │ │ │ │ loading={loading}
60 │ │ │ │ onClickCapture={handleClick}
61 │ │ │ />
62 │ │
63 │ );
64 };
65
66 export default Img;
~this code works in dev tools, with mobile emulation also. but not work on mobile device. why?
- Assistant