API Reference

grunfeld.add()#

The grunfeld.add() method is the core API for creating new dialogs. It provides two overloads to support various use cases.

Alert Overload#

Using a factory function without parameters creates a simple alert:

This overload returns void and executes immediately. You can return a React element directly or return a GrunfeldProps object.

Response Overload#

Using a factory function that receives the removeWith parameter allows you to receive user responses:

This overload returns Promise<T | undefined>. When the user calls removeWith(value), the Promise resolves with that value.

GrunfeldProps#

An object specifying dialog options:

  • element: The React element to display in the dialog
  • position: The position of the dialog (default: 'center')
  • lightDismiss: Whether to close on backdrop click (default: true)
  • renderMode: Rendering mode ('inline' | 'top-layer', default: 'inline')
  • backdropStyle: CSS styles for the backdrop
  • dismissCallback: Callback executed when the dialog closes

Promise Interruption#

When a dialog is forcibly closed by grunfeld.remove() or grunfeld.clear(), the Promise resolves with undefined:

You can use this to handle interruptions due to timeouts or external events.

remove & clear#

Grunfeld provides two methods for removing dialogs.

grunfeld.remove()#

Removes the most recently added dialog. Dialogs are removed in LIFO (Last In First Out) order:

This method resolves the dialog's Promise with undefined. When multiple dialogs are stacked, only the topmost dialog is removed.

grunfeld.clear()#

Removes all dialogs at once:

This method resolves all dialogs' Promises with undefined. Useful in emergencies or during route changes.

ESC Key and Light Dismiss#

Users can also close dialogs using the following methods:

  • ESC Key: Closes the most recent dialog (same as grunfeld.remove())
  • Backdrop Click: Closes the dialog if lightDismiss: true

These user actions also resolve the Promise with undefined.

TypeScript Types#

Grunfeld provides full TypeScript support. All types can be imported from the package.

Core Types#

Main type definitions:

Position Type#

A type that specifies the dialog's position. You can specify exact positions using a 9-grid system:

Generic Type Parameters#

The generic parameter of grunfeld.add<T>() determines the argument type of the removeWith function and the type of the returned Promise:

For type safety, it's recommended to always explicitly specify the generic type.

API Reference | ilokesto - React Library Collection