use-keybinds
Version:
Lightweight keybind library for React
1 lines • 2.22 kB
Source Map (JSON)
{"version":3,"sources":["../src/useKeybindsList.tsx","../src/KeybindsProvider.tsx"],"sourcesContent":["import {useContext} from \"react\";\nimport {KeybindsContext} from \"./KeybindsProvider\";\n\nconst useKeybindsList = <Slug extends string>() => {\n const { keybinds } = useContext(KeybindsContext);\n\n const getKeybind = (slug: Slug) => keybinds[slug]\n\n return {\n getKeybind,\n keybinds\n }\n};\n\nexport default useKeybindsList;","import React, { createContext, useMemo } from \"react\";\nimport { createKeybindCombinationString } from \"./utils\";\nimport type { ReactNode } from \"react\";\nimport type { Keybind, Keybinds } from \"./types\";\n\nexport const KeybindsContext = createContext<{\n keybinds: Keybinds<string>;\n combinationsToKeybindKey: Record<string, string>;\n}>({\n keybinds: {},\n combinationsToKeybindKey: {},\n});\n\ninterface KeybindsProviderProps<Slug extends string> {\n children: ReactNode;\n keybinds: Keybinds<Slug>;\n}\n\nconst KeybindsProvider = <Slug extends string>(props: KeybindsProviderProps<Slug>) => {\n const { children, keybinds = {} } = props;\n\n const combinationsToKeybindKey = useMemo<Record<string, Slug>>(() => {\n return Object.entries(keybinds).reduce((result, entry) => {\n const key = entry[0] as string;\n const keybind: Keybind = entry[1] as Keybind;\n const combinationString = createKeybindCombinationString(\n keybind.keybind,\n keybind.isSequential\n );\n return { ...result, [combinationString]: key };\n }, {});\n }, [keybinds]);\n\n return (\n <KeybindsContext.Provider value={{ keybinds, combinationsToKeybindKey }}>\n {children}\n </KeybindsContext.Provider>\n );\n};\n\nexport default KeybindsProvider;\n"],"mappings":";AAAA,OAAQ,cAAAA,MAAiB,QCAzB,OAAOC,GAAS,iBAAAC,EAAe,WAAAC,MAAe,QAKvC,IAAMC,EAAkBC,EAG5B,CACD,SAAU,CAAC,EACX,yBAA0B,CAAC,CAC7B,CAAC,EDRD,IAAMC,EAAkB,IAA2B,CACjD,GAAM,CAAE,SAAAC,CAAS,EAAIC,EAAWC,CAAe,EAI/C,MAAO,CACL,WAHkBC,GAAeH,EAASG,CAAI,EAI9C,SAAAH,CACF,CACF,EAEOI,EAAQL","names":["useContext","React","createContext","useMemo","KeybindsContext","createContext","useKeybindsList","keybinds","useContext","KeybindsContext","slug","useKeybindsList_default"]}