mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-27 00:38:46 +00:00
Add guides (#224)
* Add guides * Add versioning to guides * Guides: special variables and nested expressions * Add guides page to vite build * Add search and hash hooks, guide tags and headings * Improve guides list and filtering * Add 1.19 download link
This commit is contained in:
27
src/app/hooks/useHash.ts
Normal file
27
src/app/hooks/useHash.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { useCallback, useEffect, useState } from 'preact/hooks'
|
||||
import { changeUrl } from '../Utils'
|
||||
|
||||
export function useHash(): [string, (hash: string) => unknown] {
|
||||
const [hash, setHash] = useState(window.location.hash)
|
||||
|
||||
const onChange = useCallback(() => {
|
||||
setHash(window.location.hash)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener('hashchange', onChange)
|
||||
window.addEventListener('replacestate', onChange)
|
||||
return () => {
|
||||
window.removeEventListener('hashchange', onChange)
|
||||
window.removeEventListener('replacestate', onChange)
|
||||
}
|
||||
}, [])
|
||||
|
||||
const changeHash = useCallback((newHash: string) => {
|
||||
if (newHash !== hash) {
|
||||
changeUrl({ hash: newHash })
|
||||
}
|
||||
}, [hash])
|
||||
|
||||
return [hash, changeHash]
|
||||
}
|
||||
Reference in New Issue
Block a user