mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-18 00:54:43 -04:00
feat(frontend): show indicator in document title for background changes
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
336e621bc4
commit
9497726a7c
5 changed files with 142 additions and 2 deletions
24
frontend/src/hooks/common/use-is-document-visible.spec.tsx
Normal file
24
frontend/src/hooks/common/use-is-document-visible.spec.tsx
Normal file
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useIsDocumentVisible } from './use-is-document-visible'
|
||||
import { fireEvent, render } from '@testing-library/react'
|
||||
import React, { Fragment } from 'react'
|
||||
|
||||
describe('use is document visible', () => {
|
||||
const TestComponent: React.FC = () => {
|
||||
const visible = useIsDocumentVisible()
|
||||
return <Fragment>{String(visible)}</Fragment>
|
||||
}
|
||||
|
||||
it('returns the correct value', () => {
|
||||
const view = render(<TestComponent />)
|
||||
expect(view.container.textContent).toBe('true')
|
||||
fireEvent(window, new Event('blur'))
|
||||
expect(view.container.textContent).toBe('false')
|
||||
fireEvent(window, new Event('focus'))
|
||||
expect(view.container.textContent).toBe('true')
|
||||
})
|
||||
})
|
28
frontend/src/hooks/common/use-is-document-visible.ts
Normal file
28
frontend/src/hooks/common/use-is-document-visible.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
/**
|
||||
* Uses the browsers visiblity API to determine if the tab is active or now.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
|
||||
*/
|
||||
export const useIsDocumentVisible = (): boolean => {
|
||||
const [documentVisible, setDocumentVisible] = useState(true)
|
||||
|
||||
useEffect(() => {
|
||||
const onFocus = () => setDocumentVisible(true)
|
||||
const onBlur = () => setDocumentVisible(false)
|
||||
window.addEventListener('focus', onFocus)
|
||||
window.addEventListener('blur', onBlur)
|
||||
return () => {
|
||||
document.removeEventListener('focus', onFocus)
|
||||
document.removeEventListener('blur', onBlur)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return documentVisible
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue