use-keybinds
Version:
Lightweight keybind library for React
1 lines • 3.3 kB
Source Map (JSON)
{"version":3,"sources":["../src/KeybindsProvider.tsx","../src/utils.ts"],"sourcesContent":["import React, { createContext, useMemo } from \"react\";\r\nimport { createKeybindCombinationString } from \"./utils\";\r\nimport type { ReactNode } from \"react\";\r\nimport type { Keybind, KeybindCombination, Keybinds } from \"./types\";\r\n\r\nexport const KeybindsContext = createContext<{\r\n keybinds: Keybinds<string>;\r\n combinationsToKeybindKey: Record<string, string>;\r\n}>({\r\n keybinds: {},\r\n combinationsToKeybindKey: {},\r\n});\r\n\r\ninterface KeybindsProviderProps<Slug extends string> {\r\n children: ReactNode;\r\n keybinds: Keybinds<Slug>;\r\n}\r\n\r\nconst KeybindsProvider = <Slug extends string>(props: KeybindsProviderProps<Slug>) => {\r\n const { children, keybinds = {} } = props;\r\n\r\n const combinationsToKeybindKey = useMemo<Record<string, Slug>>(() => {\r\n return Object.entries(keybinds).reduce((result, entry) => {\r\n const key = entry[0] as string;\r\n const keybind: Keybind = entry[1] as Keybind;\r\n\r\n const combinations: KeybindCombination[] = Array.isArray(keybind.keybind[0])\r\n ? (keybind.keybind as KeybindCombination[])\r\n : [keybind.keybind as KeybindCombination];\r\n\r\n const combinationStrings = combinations.reduce((result, combination) => {\r\n const combinationString = createKeybindCombinationString(\r\n combination,\r\n keybind.isSequential,\r\n );\r\n return { ...result, [combinationString]: key };\r\n }, {});\r\n\r\n return { ...result, ...combinationStrings };\r\n }, {});\r\n }, [keybinds]);\r\n\r\n return (\r\n <KeybindsContext.Provider value={{ keybinds, combinationsToKeybindKey }}>\r\n {children}\r\n </KeybindsContext.Provider>\r\n );\r\n};\r\n\r\nexport default KeybindsProvider;\r\n","import type { KeybindCombination } from \"./types\";\r\n\r\nexport const createKeybindCombinationString = (combination: KeybindCombination, isSequential?: boolean) => {\r\n return (isSequential ? \"isSequential-\" : \"\") + combination.join(\"\");\r\n};\r\n"],"mappings":";AAAA,OAAOA,GAAS,iBAAAC,EAAe,WAAAC,MAAe,QCEvC,IAAMC,EAAiC,CAACC,EAAiCC,KACtEA,EAAe,gBAAkB,IAAMD,EAAY,KAAK,EAAE,EDE7D,IAAME,EAAkBC,EAG5B,CACD,SAAU,CAAC,EACX,yBAA0B,CAAC,CAC7B,CAAC,EAOKC,EAAyCC,GAAuC,CACpF,GAAM,CAAE,SAAAC,EAAU,SAAAC,EAAW,CAAC,CAAE,EAAIF,EAE9BG,EAA2BC,EAA8B,IACtD,OAAO,QAAQF,CAAQ,EAAE,OAAO,CAACG,EAAQC,IAAU,CACxD,IAAMC,EAAMD,EAAM,CAAC,EACbE,EAAmBF,EAAM,CAAC,EAM1BG,GAJqC,MAAM,QAAQD,EAAQ,QAAQ,CAAC,CAAC,EACtEA,EAAQ,QACT,CAACA,EAAQ,OAA6B,GAEF,OAAO,CAACH,EAAQK,IAAgB,CACtE,IAAMC,EAAoBC,EACxBF,EACAF,EAAQ,YACV,EACA,MAAO,CAAE,GAAGH,EAAQ,CAACM,CAAiB,EAAGJ,CAAI,CAC/C,EAAG,CAAC,CAAC,EAEL,MAAO,CAAE,GAAGF,EAAQ,GAAGI,CAAmB,CAC5C,EAAG,CAAC,CAAC,EACJ,CAACP,CAAQ,CAAC,EAEb,OACEW,EAAA,cAAChB,EAAgB,SAAhB,CAAyB,MAAO,CAAE,SAAAK,EAAU,yBAAAC,CAAyB,GACnEF,CACH,CAEJ,EAEOa,EAAQf","names":["React","createContext","useMemo","createKeybindCombinationString","combination","isSequential","KeybindsContext","createContext","KeybindsProvider","props","children","keybinds","combinationsToKeybindKey","useMemo","result","entry","key","keybind","combinationStrings","combination","combinationString","createKeybindCombinationString","React","KeybindsProvider_default"]}