@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 1.16 kB
Source Map (JSON)
{"version":3,"file":"use-toggle.mjs","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,KAAK,EACZ;CACzB,MAAM,CAAC,CAAC,SAAS,UAAU,YAAY,OAAY,WAAoC;EACrF,MAAM,QAAQ,kBAAkB,WAAW,OAAO,MAAM,GAAG,GAAG;EAC9D,MAAM,QAAQ,KAAK,IAAI,MAAM,QAAQ,MAAM,CAAC;AAE5C,SAAO,MAAM,MAAM,MAAM,CAAC,OAAO,MAAM,MAAM,GAAG,MAAM,CAAC;IACtD,QAAe;AAElB,QAAO,CAAC,QAAQ,OAA6B"}