UNPKG

@coveord/plasma-mantine

Version:

A Plasma flavoured Mantine theme

44 lines 2.5 kB
import { Dispatch, SetStateAction } from 'react'; /** * A search param entry defines the encoded value of a search parameter as `[key, value, alwaysEmit?]`. * The third entry is an optional boolean that defaults to `false`. * Setting `alwaysEmit` to `true` means any non-nullish value is always written to the search params, * even if it matches the initial value. It is also written on initialization. */ export type SearchParamEntry = [string, string | null | undefined, boolean?]; export interface UseUrlSyncedStateOptions<T> { /** * The initial state to use, if there would be no search params to deserialize from. * These values are also treated as defaults, and if the current state matches the initialState, * no value will be written to the search params. */ initialState: T | (() => T); /** * The serializer function is used to determine how the state is translated to url search parameters. * Called each time the state changes. * Note that the serializer should always return entries for keys it controls, also if the current value is "unset" (`null` or empty). * This ensures params get removed from the search when they are being unset. * * @param stateValue The new state value to serialize. * @returns An iterable of `[key, value]` to set as url search parameters. * @example (filterValue) => [['filter', filterValue]] // ?filter=filterValue */ serializer: (stateValue: T) => Iterable<SearchParamEntry>; /** * The deserializer function is used to determine how the url parameters influence the initial state. * May return a partial state, values that are not deserialed are taken from the `initialState`. * Called only once when initializing the state. * @param params All the search parameters of the current url. * @param initialState The initialState, can be used to take defaults from. * @returns The initial state based on the current url. * @example (params) => params.get('filter') ?? '', */ deserializer: (params: URLSearchParams, initialState: T) => T; /** * Whether the state should be synced with the url, defaults to `true`. * When set to `false`, the hook behaves just like a regular `useState` hook from react. */ sync?: boolean; } export declare const useUrlSyncedState: <T>(options: UseUrlSyncedStateOptions<T>) => readonly [T, Dispatch<SetStateAction<T>>]; //# sourceMappingURL=use-url-synced-state.d.ts.map