mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
34 lines
844 B
JavaScript
34 lines
844 B
JavaScript
|
import { useEffect, useState } from 'react'
|
||
|
import { useLocation } from './use-location'
|
||
|
|
||
|
function useBookmarkableTabSet(defaultState) {
|
||
|
const location = useLocation()
|
||
|
|
||
|
const [activeTabState, setActiveTabState] = useState(() => {
|
||
|
const url = new URL(window.location.href)
|
||
|
return url.hash.slice(1) || defaultState
|
||
|
})
|
||
|
|
||
|
function setActiveTab(eventKey) {
|
||
|
setActiveTabState(eventKey)
|
||
|
location.assign(`#${eventKey}`)
|
||
|
}
|
||
|
|
||
|
useEffect(() => {
|
||
|
const handlePopstate = () => {
|
||
|
const newUrl = new URL(window.location.href)
|
||
|
setActiveTabState(newUrl.hash.slice(1) || defaultState)
|
||
|
}
|
||
|
|
||
|
window.addEventListener('popstate', handlePopstate)
|
||
|
|
||
|
return () => {
|
||
|
window.removeEventListener('popstate', handlePopstate)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
return [activeTabState, setActiveTab]
|
||
|
}
|
||
|
|
||
|
export default useBookmarkableTabSet
|