@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 1.65 kB
Source Map (JSON)
{"version":3,"file":"use-hash.mjs","names":[],"sources":["../../src/use-hash/use-hash.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nexport interface UseHashInput {\n getInitialValueInEffect?: boolean;\n}\n\nexport type UseHashReturnValue = [string, (value: string) => void];\n\nexport function useHash({ getInitialValueInEffect = true }: UseHashInput = {}): UseHashReturnValue {\n const [hash, setHash] = useState<string>(\n getInitialValueInEffect ? '' : window.location.hash || ''\n );\n\n const setHashHandler = (value: string) => {\n const valueWithHash = value.startsWith('#') ? value : `#${value}`;\n window.location.hash = valueWithHash;\n setHash(valueWithHash);\n };\n\n useWindowEvent('hashchange', () => {\n const newHash = window.location.hash;\n if (hash !== newHash) {\n setHash(newHash);\n }\n });\n\n useEffect(() => {\n if (getInitialValueInEffect) {\n setHash(window.location.hash);\n }\n }, []);\n\n return [hash, setHashHandler];\n}\n\nexport namespace useHash {\n export type Options = UseHashInput;\n export type ReturnValue = UseHashReturnValue;\n}\n"],"mappings":";;;;AASA,SAAgB,QAAQ,EAAE,0BAA0B,SAAuB,CAAC,GAAuB;CACjG,MAAM,CAAC,MAAM,WAAW,SACtB,0BAA0B,KAAK,OAAO,SAAS,QAAQ,EACzD;CAEA,MAAM,kBAAkB,UAAkB;EACxC,MAAM,gBAAgB,MAAM,WAAW,GAAG,IAAI,QAAQ,IAAI;EAC1D,OAAO,SAAS,OAAO;EACvB,QAAQ,aAAa;CACvB;CAEA,eAAe,oBAAoB;EACjC,MAAM,UAAU,OAAO,SAAS;EAChC,IAAI,SAAS,SACX,QAAQ,OAAO;CAEnB,CAAC;CAED,gBAAgB;EACd,IAAI,yBACF,QAAQ,OAAO,SAAS,IAAI;CAEhC,GAAG,CAAC,CAAC;CAEL,OAAO,CAAC,MAAM,cAAc;AAC9B"}