statelet
Version:
Dead simple state management built on kvkit - composable, reactive, shareable. Supports Standard Schema (Valibot, Zod, ArkType).
89 lines (88 loc) • 2.3 kB
TypeScript
import { type Codec } from '@kvkit/codecs';
import type { Enhancer } from './index.js';
/**
* Sync state with URL search parameters (?key=value)
*
* @example
* const searchState = compose(
* state({ query: '', page: 1 }),
* withUrlParams()
* );
*/
export declare function withUrlParams<T extends Record<string, any>>(codec?: Codec<T>): Enhancer<T>;
/**
* Sync state with URL hash parameters (#key=value)
*
* @example
* const tabState = compose(
* state({ tab: 'home', modal: false }),
* withHashParams()
* );
*/
export declare function withHashParams<T extends Record<string, any>>(codec?: Codec<T>): Enhancer<T>;
/**
* Persist state to localStorage
*
* @example
* const userPrefs = compose(
* state({ theme: 'light', fontSize: 14 }),
* withStorage('user-preferences')
* );
*/
export declare function withStorage<T>(key: string, codec?: Codec<T>): Enhancer<T>;
/**
* Run side effects when state changes
*
* @example
* const analytics = compose(
* state({ page: 'home' }),
* withEffect((newValue, prevValue) => {
* gtag('event', 'page_view', { page: newValue.page });
* })
* );
*/
export declare function withEffect<T>(effect: (value: T, prev: T) => void, options?: {
immediate?: boolean;
}): Enhancer<T>;
/**
* Validate state changes with Standard Schema
*
* Supports Valibot, Zod, ArkType, and any other Standard Schema-compliant library
*
* @example
* // With Valibot
* const userForm = compose(
* state({ name: '', email: '', age: 0 }),
* withValidation(v.object({
* name: v.string(),
* email: v.pipe(v.string(), v.email()),
* age: v.number()
* }))
* );
*
* @example
* // With Zod
* const userForm = compose(
* state({ name: '', email: '', age: 0 }),
* withValidation(z.object({
* name: z.string(),
* email: z.string().email(),
* age: z.number()
* }))
* );
*/
export declare function withValidation<T>(schema: {
'~standard': any;
}): Enhancer<T>;
/**
* Debounce state updates
*
* @example
* const searchState = compose(
* state({ query: '' }),
* withDebounce(300), // Wait 300ms before updating
* withUrlParams()
* );
*/
export declare function withDebounce<T>(ms: number): Enhancer<T>;
export { flatCodec, jsonCodec, type Codec } from '@kvkit/codecs';