obolus/www/utils/hooks.js

36 lines
1014 B
JavaScript
Raw Normal View History

2019-05-27 18:18:26 +00:00
import React, { useEffect, useState } from 'react'
2019-05-22 18:34:58 +00:00
export const useLocalStorageWatcher = (fn) => {
useEffect(() => {
window.addEventListener('storage', fn)
return () => {
window.removeEventListener('storage', fn)
}
}, [])
2019-05-27 18:18:26 +00:00
}
export const editableWhenOnline = () => {
const [online, setOnline] = useState(window.navigator.onLine)
useEffect(() => {
const onlineHandler = () => setOnline(true)
const offlineHandler = () => setOnline(false)
window.addEventListener('online', onlineHandler)
window.addEventListener('offline', offlineHandler)
return () => {
window.removeEventListener('online', onlineHandler)
window.removeEventListener('offline', offlineHandler)
}
}, [])
useEffect(() => {
const elements = window.document.querySelectorAll('input, button')
for (const element of elements)
element.disabled = !online
}, [online])
return online
2019-05-22 18:34:58 +00:00
}