5180 fichiers 38.5 Mo 0 téléchargements 133 vues mis à jour il y a 4 heures ui
nitro-v36-with-renderer-v36 / src / hooks / useDevicePixelRatio.ts
useDevicePixelRatio.ts 1001 o Télécharger le fichier
import { useEffect, useState } from 'react';

export const useDevicePixelRatio = () => {
    const [devicePixelRatio, setDevicePixelRatio] = useState(() => window.devicePixelRatio);

    useEffect(() => {
        if (typeof window.matchMedia !== 'function') return;

        let disposed = false;
        let media: MediaQueryList = null;
        let onChange: () => void = null;

        const arm = () => {
            if (disposed) return;

            media = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);

            onChange = () => {
                media.removeEventListener('change', onChange);
                setDevicePixelRatio(window.devicePixelRatio);
                arm();
            };

            media.addEventListener('change', onChange);
        };

        arm();

        return () => {
            disposed = true;
            if (media && onChange) media.removeEventListener('change', onChange);
        };
    }, []);

    return devicePixelRatio;
};
15Sujets
1Dépôts
20Membres
7En ligne
Profile
Inscrit
Dernière visite
Messages
0
Votes positifs
0
Votes négatifs
0

Nous utilisons des cookies pour assurer le bon fonctionnement du site et améliorer votre expérience. en savoir plus

Autoriser