react-unique-id-generator
Version:
A lightweight, zero-dependency library for generating unique, sequential IDs in React — with hooks, custom prefix/suffix, SSR support, and stable CSS selector generation
100 lines • 2.76 kB
TypeScript
declare const OVERFLOW_THRESHOLD = 1000000;
/**
* Generates the next unique ID. Accepts an optional prefix that overrides the
* global prefix for that call only.
*
* @param localPrefix - Optional prefix string. When provided (non-null/undefined),
* it replaces the global prefix for this call. Pass `null` or `undefined` to
* fall back to the global prefix.
* @returns A unique string ID in the format `{prefix}{counter}{suffix}`
*
* @example
* ```ts
* nextId() // "1"
* nextId('input-') // "input-2"
* ```
*/
export default function nextId(localPrefix?: string | null): string;
/**
* Resets the ID counter back to 0. The next call to {@link nextId} or
* {@link generateId} will produce an ID with counter value 1.
*
* @example
* ```ts
* nextId(); // "1"
* nextId(); // "2"
* resetId();
* nextId(); // "1"
* ```
*/
export declare const resetId: () => void;
/**
* Sets a global prefix applied to every subsequent {@link nextId} call,
* unless overridden by a local prefix argument.
*
* @param newPrefix - The prefix string. Falsy values are normalized to `""`.
*
* @example
* ```ts
* setGlobalPrefix('app-');
* nextId(); // "app-1"
* nextId(); // "app-2"
* ```
*/
export declare const setGlobalPrefix: (newPrefix: string) => void;
/**
* Sets a global suffix appended to every subsequent {@link nextId} call.
*
* @param newSuffix - The suffix string. Falsy values are normalized to `""`.
*
* @example
* ```ts
* setGlobalSuffix('-id');
* nextId(); // "1-id"
* nextId(); // "2-id"
* ```
*/
export declare const setGlobalSuffix: (newSuffix: string) => void;
/**
* Returns the current counter value without incrementing it.
*
* @returns The current counter value
*
* @example
* ```ts
* nextId(); // "1"
* getCurrentId(); // 1
* ```
*/
export declare const getCurrentId: () => number;
/**
* Sets the counter to a specific value. Negative values are clamped to 0;
* decimals are floored.
*
* @param id - The counter value to set
*
* @example
* ```ts
* setId(10);
* nextId(); // "11"
* ```
*/
export declare const setId: (id: number) => void;
/**
* Generates a unique ID with an explicit prefix and suffix, ignoring the
* global prefix/suffix configuration.
*
* @param prefix - The prefix for this ID (default: `""`)
* @param suffix - The suffix for this ID (default: `""`)
* @returns A unique string ID in the format `{prefix}{counter}{suffix}`
*
* @example
* ```ts
* generateId('btn-', '-primary') // "btn-1-primary"
* generateId('icon-') // "icon-2"
* generateId() // "3"
* ```
*/
export declare const generateId: (prefix?: string, suffix?: string) => string;
export { OVERFLOW_THRESHOLD };
//# sourceMappingURL=nextId.d.ts.map