Edit this page

up.layer up.layer.current
JavaScript property

Returns the current layer in the layer stack.

The current layer is usually the frontmost layer. However, there are some cases where the current layer is a layer in the background:

  • While an element in a background layer is being compiled.
  • While an Unpoly event like up:request:loaded is being triggered from a background layer.
  • While an event listener bound to a background layer using up.Layer#on() is being called.

To temporarily change the current layer from your own code, use up.Layer#asCurrent().

Remembering the current layer

Most functions in the up.layer package affect the current layer. For example, up.layer.dismiss() is shorthand for up.layer.current.dismiss().

As described above, up.layer.current is set to the correct layer in compilers and most events, even if that layer is not the frontmost layer.

If you have async code, the current layer can change when your callback is called. To address this, you can retrieve the current layer for later reference:

function dismissCurrentLayerIn(seconds) {
  let savedLayer = up.layer.current // returns an up.Layer object
  let dismiss = () => savedLayer.dismiss()
  setTimeout(dismiss, seconds * 1000)
}

dismissCurrentLayerIn(10) //

Value

current