@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 1.19 kB
Source Map (JSON)
{"version":3,"file":"use-toggle.cjs","names":[],"sources":["../../src/use-toggle/use-toggle.ts"],"sourcesContent":["import { useReducer } from 'react';\n\ntype UseToggleAction<T> = (value?: React.SetStateAction<T>) => void;\nexport type UseToggleReturnValue<T> = [T, UseToggleAction<T>];\n\nexport function useToggle<T = boolean>(\n options: readonly T[] = [false, true] as any\n): UseToggleReturnValue<T> {\n const [[option], toggle] = useReducer((state: T[], action: React.SetStateAction<T>) => {\n const value = action instanceof Function ? action(state[0]) : action;\n const index = Math.abs(state.indexOf(value));\n\n return state.slice(index).concat(state.slice(0, index));\n }, options as T[]);\n\n return [option, toggle as UseToggleAction<T>];\n}\n\nexport namespace useToggle {\n export type ReturnValue<T> = UseToggleReturnValue<T>;\n}\n"],"mappings":";;;AAKA,SAAgB,UACd,UAAwB,CAAC,OAAO,IAAI,GACX;CACzB,MAAM,CAAC,CAAC,SAAS,WAAA,GAAA,MAAA,aAAsB,OAAY,WAAoC;EACrF,MAAM,QAAQ,kBAAkB,WAAW,OAAO,MAAM,EAAE,IAAI;EAC9D,MAAM,QAAQ,KAAK,IAAI,MAAM,QAAQ,KAAK,CAAC;EAE3C,OAAO,MAAM,MAAM,KAAK,EAAE,OAAO,MAAM,MAAM,GAAG,KAAK,CAAC;CACxD,GAAG,OAAc;CAEjB,OAAO,CAAC,QAAQ,MAA4B;AAC9C"}