mirror of
https://github.com/System-End/site.git
synced 2026-04-19 16:28:21 +00:00
Move useMedia to own file
This commit is contained in:
parent
88604df654
commit
33327f7c0a
2 changed files with 18 additions and 16 deletions
|
|
@ -1,20 +1,6 @@
|
||||||
import React, { useEffect, useRef, useState } from 'react'
|
import React, { useEffect, useRef } from 'react'
|
||||||
import VanillaTilt from 'vanilla-tilt'
|
import VanillaTilt from 'vanilla-tilt'
|
||||||
|
import useMedia from '../lib/use-media'
|
||||||
function useMedia(query) {
|
|
||||||
const [matches, setMatches] = useState(false)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const onChange = e => setMatches(e.matches)
|
|
||||||
const mq = window.matchMedia(query)
|
|
||||||
setMatches(mq.matches)
|
|
||||||
mq.addEventListener('change', onChange)
|
|
||||||
|
|
||||||
return () => mq.removeEventListener('change', onChange)
|
|
||||||
}, [query])
|
|
||||||
|
|
||||||
return { matches }
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(@lachlanjc): only pass one child!
|
// NOTE(@lachlanjc): only pass one child!
|
||||||
const Tilt = ({ options = {}, children, ...props }) => {
|
const Tilt = ({ options = {}, children, ...props }) => {
|
||||||
|
|
|
||||||
16
lib/use-media.js
Normal file
16
lib/use-media.js
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { useState, useEffect } from 'react'
|
||||||
|
|
||||||
|
export default function useMedia(query) {
|
||||||
|
const [matches, setMatches] = useState(false)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onChange = e => setMatches(e.matches)
|
||||||
|
const mq = window.matchMedia(query)
|
||||||
|
setMatches(mq.matches)
|
||||||
|
mq.addEventListener('change', onChange)
|
||||||
|
|
||||||
|
return () => mq.removeEventListener('change', onChange)
|
||||||
|
}, [query])
|
||||||
|
|
||||||
|
return { matches }
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue