Skip to content

Modal layers ​

A modal layer renders above regular layers and takes over the keyboard. While any modal is open, keys it does not handle never reach regular layers or the screen. Use it for confirmations and dialogs that must own input.

Prerequisites ​

ChapterWhy you need it
Layer BasicsThe two APIs mirror each other
Modal Layer Keyboard EventsThe takeover and barrier rules

Method mapping ​

Regular layerModal layer
openLayeropenModalLayer
applyElementapplyElementToModalLayer
closeLayercloseModalLayer
eraseElementeraseElementInModalLayer
closeAllLayercloseAllModalLayer
activateElementactivateElementInModalLayer
deactivateElementdeactivateElementInModalLayer

Both kinds share the ID namespace. CurrentScreen renders: current screen → regular layers → modal layers.

Opening and mounting ​

ts
openModalLayer(layerId: string, zIndex: number, options?: ModalLayerOptions): void
applyElementToModalLayer(targetModalLayerId: string, layerElement: LayerElementInput<C>): void

ModalLayerOptions is the same as LayerOptions:

  • [crossPage] {boolean} Keep the layer across page switches. Default: false.
  • [automaticTakeoverKeyboard] {boolean | ComponentType[]} Limit the pages on which the modal takes over the keyboard. Default: false. See Cross-page layers and keyboard takeover.
ts
const { openModalLayer, applyElementToModalLayer } = useScreenSystem()

openModalLayer('confirm', 100)
applyElementToModalLayer('confirm', {
  element: ConfirmDialog,
  elementId: 'body',
})

openModalLayer also registers an empty container; mount elements to render anything.

Reading modal information ​

tsx
const ctx = useContext(ModalLayerElementContext)
ctx?.modalLayer.layerId

The mounted component can read its own modal layer without receiving the ID as a prop.

Closing and removing ​

MethodEffect
closeModalLayer(targetModalLayerId)Closes the modal and all its elements
eraseElementInModalLayer(modalLayerId, elementId)Removes one element
closeAllModalLayer()Closes every modal layer

Closing an unregistered ID is a no-op with a development warning.

Boundaries ​

  • layerId is shared with regular layers; reusing an ID across the two kinds is ignored with a development warning, not thrown.
  • Call openModalLayer before mounting elements. Mounting into an unopened modal layer is ignored: development warns, production is silent.
  • Only the modal with the highest zIndex receives keyboard and mouse events.
  • Unhandled keys are swallowed by the barrier; use allowModal for exceptions.
  • Modal layers close on page switches by default; crossPage: true keeps them.
  • Methods are available both from useScreenSystem() and as direct imports from ink-cartridge.

Released under the MIT License.