Edit this page

up.layer up.layer.open([options])
JavaScript function

Opens a new overlay.

Opening a layer is considered navigation by default.

Opening overlays

Example

let layer = await up.layer.open({ url: '/contacts' })
console.log(layer.mode) // logs "modal"

Render options

[options]
optional

Render options to apply for opening the new overlay.

Common options are documented below, bust most options for up.render() may be used.

You may configure default layer options in up.layer.config. For example, options in up.layer.config.overlay become defaults for all overlays, while options in up.layer.config.drawer become defaults for all drawers.

[options.navigate=true]
optional

Whether to apply navigation defaults.

boolean

Placement

[options.target]
optional

The selector of root content element to place inside the overlay container.

The given { target } must be matchable within the HTML provided by either { url }, { content }, { fragment } or { document } option.

If the { target } option is emitted, Unpoly will use the first matching main target configured for the overlay mode. E.g. the default selectors for modals are configured in up.layer.config.modal.mainTargets. See main targets in overlays.

string
[options.layer="new"]
optional

Whether to stack the new overlay or replace existing overlays.

The following values are supported:

Option Description
new Stacks a new overlay over the current layer.
swap Replaces the current overlay. If no overlay is open, opens an overlay.
shatter Closes all overlays and opens a new overlay.
string
[options.origin]
optional

The link or button element that caused this overlay to open.

The origin will be re-focused when the overlay closes.

When opening a popup, the overlay will be anchored relative to the origin element.

[options.position]
optional

For popups, the position of the popup relative to the link or button that opened the overlay. See popup position.

For drawers, the screen edge on which to position the drawer (left or right). See drawer position.

string
[options.align]
optional

For popups, the alignment of the popup within its { position }. See popup position.

string

Request

[options.url]
optional

The URL to request from the server.

See loading content from a URL.

string
[options.method='get']
optional

The HTTP method to use for the request.

Common values are 'get', 'post', 'put', 'patch' and 'delete'. The value is case insensitive.

string
[options.params]
optional

Additional parameters that should be sent as the request's query string or payload.

When making a GET request to a URL with a query string, the given { params } will be added to the query parameters.

[options.headers={}]
optional

An object with additional request headers.

Unpoly will by default send a number of custom request headers. E.g. the X-Up-Target header includes the targeted CSS selector. See up.protocol for details.

[options.abort='target']
optional

Whether to abort existing requests before rendering.

By default, opening a new overlay will abort all requests within the main element of its base layer.

See aborting requests for details and a list of options.

booleanstringFunction(request): boolean
[options.abortable=true]
optional

Whether the request may be aborted by other requests.

By default, the request will be aborted:

  • By another request opening another overlay from the same base layer
  • By another request targeting the base layer's main element

See Preventing a request from being aborted.

boolean
[options.background=false]
optional

Whether this request will load in the background.

Background requests deprioritized over foreground requests. Background requests also won't emit up:network:late events and won't trigger the progress bar.

boolean
[options.lateDelay]
optional

The number of milliseconds after which this request can cause an up:network:late event and show the progress bar.

To prevent the event and progress bar, pass { lateDelay: false }.

Defaults to up.network.config.lateDelay.

numberboolean
[options.timeout]
optional

The number of milliseconds after which this request fails with a timeout.

Defaults to up.network.config.timeout.

number
[options.fail]
optional

Whether to render failed responses differently.

Failed responses will be rendered using options prefixed with fail, e.g. { failTarget }.

By default any HTTP status code other than 2xx or 304 is considered failed. Pass { fail: false } to handle any response as successful, even with a 4xx or 5xx status code.

booleanFunction(up.Response): boolean

Local content

[options.content]
optional

The new inner HTML for the targeted fragment.

See Updating an element's inner HTML from a string.

Instead of passing an HTML string you can also refer to a template.

stringElementList<Node>
[options.fragment]
optional

A string of HTML comprising only the new fragment's outer HTML.

When passing { fragment } you can omit the { target } option. The target will be derived from the root element in the given HTML.

See Rendering a string that only contains the fragment.

Instead of passing an HTML string you can also refer to a template.

stringElement
[options.document]
optional

A string of HTML containing the targeted fragment.

See Extracting an element's outer HTML from a larger HTML string.

Instead of passing an HTML string you can also refer to a template.

stringElementDocument
[options.response]
optional

An up.Response object that contains the targeted fragments in its text.

See Rendering an up.Response object.

Customization

[options.mode]
optional

The kind of overlay to open.

See available layer modes.

string
[options.size]
optional

The size of the overlay.

Supported values are 'small', 'medium', 'large' and 'grow'.
See overlay sizes for details.

string
[options.class]
optional

An optional HTML class for the overlay's container element.

See overlay classes.

string
[options.dismissable=true]
optional

How the overlay may be dismissed by the user.

Supported values are 'key', 'outside' and 'button'.
See customizing dismiss controls for details.

You may enable multiple dismiss controls by passing an array or a space-separated string.

Passing true or false will enable or disable all dismiss controls.

booleanstringArray<string>
[options.dismissLabel]
optional

The label for the dismiss icon in the top-right corner.

string
[options.dismissARIALabel]
optional

The accessibility label for the dismiss icon in the top-right corner.

string
[options.trapFocus]
optional

Whether to trap focus within the overlay while it is in front.

By default focus is trapped for all overlays except popups.

boolean

Client state

[options.data]
optional

The data object object for the overlay's root content element ({ target }).

If the content element already has an [up-data] attribute, this option will override individual properties from that attribute.

[options.context={}]
optional

The initial context object for the new overlay.

History

[options.history]
optional

Whether the overlay has visible history.

If set to true the overlay location, title and meta tags will be shown while the overlay is open. When the overlay is closed, the parent layer's history is restored.

If set to 'auto' history will be visible if the initial overlay content matches a main target.

If set to false, fragments changes within the overlay will never update the address bar. You can still access the overlay's current location using up.layer.location.

See History in overlays.

booleanstring
[options.failHistory]
optional

Whether to update history when the server responds with an error code.

See Rendering failed responses differently.

booleanstring
[options.title]
optional

An explicit document title to set before rendering.

By default the title is extracted from the response's <title> tag. To prevent the title from being updated, pass { title: false }

This option is only used when updating history.

booleanstring
[options.location]
optional

An explicit browser location URL to set before rendering.

By default Unpoly will use the { url } or the final URL after the server redirected. To prevent the URL from being updated, pass { location: false }.

This option is only used when updating history.

booleanstring
[options.metaTags]
optional

Whether to update meta tags in the <head>.

By default Unpoly will extract meta tags from the response's <head>. To prevent meta tags from being updated, pass { metaTags: false }.

This option is only used when updating history.

booleanArray<Element>
[options.lang]
optional

An explicit language code to set as the html[lang] attribute.

By default Unpoly will extract the language from the response and update the html[lang] attribute in the current page. To prevent the attrribute from being changed, pass { lang: false }.

This option is only used when updating history.

booleanArray<Element>

Animation

[options.animation='fade-in']
optional

The opening animation.

stringFunctionboolean
[options.easing='ease']
optional

The timing function that controls the opening animation's acceleration.

string
[options.duration=175]
optional

The duration of the opening animation in milliseconds.

number
[options.closeAnimation='fade-out']
optional

The closing animation.

stringFunctionboolean
[options.closeEasing='ease']
optional

The timing function that controls the closing animation's acceleration.

string
[options.closeDuration=175]
optional

The duration of the closing animation in milliseconds.

number

Close conditions

[options.acceptEvent]
optional

One or more event types that will cause this overlay to automatically be accepted when a matching event occurs within the overlay.

The overlay result value is the event object that caused the overlay to close.

See Closing when an event is emitted.

stringArray<string>
[options.dismissEvent]
optional

One or more event types that will cause this overlay to automatically be dismissed when a matching event occurs within the overlay.

The overlay result value is the event object that caused the overlay to close.

See Closing when an event is emitted.

stringArray<string>
[options.acceptLocation]
optional

One or more URL patterns that will cause this overlay to automatically be accepted when the overlay reaches a matching location.

The overlay result value is an object of named segments matches captured by the URL pattern.

See Closing when a location is reached.

stringArray<string>
[options.dismissLocation]
optional

One or more URL patterns that will cause this overlay to automatically be dismissed when the overlay reaches a matching location.

The overlay result value is an object of named segments matches captured by the URL pattern.

See Closing when a location is reached.

stringArray<string>

Callbacks

[options.onOpened]
optional

A function that is called when the overlay was inserted into the DOM.

The function argument is an up:layer:opened event.

The overlay may still play an opening animation when this function is called. To be called when the opening animation is done, pass an { onFinished } option.

Function(Event)
[options.onAccepted]
optional

A function that is called when the overlay was accepted.

The function argument is an up:layer:accepted event.

Function(Event)
[options.onDismissed]
optional

A function that is called when the overlay was dismissed.

The function argument is an up:layer:dismissed event.

Function(Event)
[options.confirm]
optional

A message the user needs to confirm before any request or changes are made.

The message will be shown as a native browser prompt.

If the user does not confirm, no request is sent and no fragments are updated. In that case the render promise rejects with an up.AbortError.

string
[options.onLoaded]
optional

A callback that will be run when the server responds with new HTML, but before the HTML is rendered.

The callback argument is a preventable up:fragment:loaded event.

This callback will also run for failed responses.

Function(Event)
[options.onOffline]
optional

A callback that will be run when the fragment could not be loaded due to a disconnect or timeout.

The callback argument is a preventable up:fragment:offline event.

Function(Event)
[options.onRendered]
optional

A function to call when Unpoly has updated fragments.

This callback may be called zero, one or two times:

  • When the server rendered an empty response, no fragments are updated. { onRendered } is not called.
  • When the server rendered a matching fragment, it will be updated on the page. { onRendered } is called with the result.
  • When revalidation renders a second time, { onRendered } is called again with the final result.

Also see Running code after rendering.

Function(up.RenderResult)
[options.onFinished]
optional

A function to call when no further DOM changes will be caused by this render pass.

In particular:

The callback argument is the last up.RenderResult that updated a fragment. If revalidation re-rendered the fragment, it is the result from the second render pass. If no revalidation was performed, or if revalidation yielded an empty response, it is the result from the initial render pass.

Also see Awaiting postprocessing.

Function(up.RenderResult)
[options.onError]
optional

A callback that will be run when any error is thrown during the rendering process.

The callback is also called when the render pass fails due to network issues, or aborts.

Also see Handling errors.

Function(Error)

Return value

A promise for the up.Layer object that describes the new overlay.

The promise is fulfilled once the overlay was placed into the DOM. At this time an overlay's opening animation will still be playing.