Class OpenInNewTab

java.lang.Object
com.vaadin.flow.component.newtab.OpenInNewTab
All Implemented Interfaces:
Serializable

public final class OpenInNewTab extends Object implements Serializable
Entry point for opening content in a new browser tab from a click on a button or any other clickable component:

 Button view = new Button("View invoice");
 OpenInNewTab.onClick(view, DownloadHandler.fromInputStream(event -> {
     byte[] pdf = invoiceService.createPdf();
     return new DownloadResponse(new ByteArrayInputStream(pdf), "invoice.pdf",
             "application/pdf", pdf.length);
 }));
 
The tab opens in the browser's own click handler, without waiting for a server round trip, so popup blockers let it through. Opening a tab from a server-side click listener, for example with Page.open, happens only after the round trip and may be blocked.

The new tab is opened without access to the opening page (noopener) and without a Referer header (noreferrer).

To save content as a file instead of showing it, use Download.

See Also:
  • Method Details

    • onClick

      public static <T extends Component & ClickNotifier<?>> Registration onClick(T component, DownloadHandler handler)
      Opens the content of a handler in a new tab when the given component is clicked. The version that takes a handler is for content produced on the server; the version that takes a URL is for content the browser can already address.

      The handler runs only when the new tab requests the content, once per click. The browser shows the content when it can, for example a PDF, an image or plain text. The built-in handlers, such as DownloadHandler.forFile(java.io.File), are switched to serve the content inline. This changes the given handler instance, so it also serves inline wherever else it is used. A custom handler should call DownloadEvent.inline itself, since content served as an attachment is downloaded instead of shown.

      The handler is registered for the clicked component and follows its lifecycle: it is only served while the component is attached, visible and enabled. A button that disables itself on click can be disabled before the new tab requests the content; pass handler.allowDisabled() to serve the content in that case. The handler that allowDisabled() returns can no longer be switched to inline, so call inline() on a built-in handler first, for example DownloadHandler.forFile(file).inline().allowDisabled().

      Type Parameters:
      T - the component type, must implement ClickNotifier
      Parameters:
      component - the component whose clicks open the new tab, not null
      handler - produces the content when the new tab requests it, not null
      Returns:
      a registration for removing the behavior from the component; after removal the handler is no longer served
    • onClick

      public static <T extends Component & ClickNotifier<?>> Registration onClick(T component, String url)
      Opens a URL in a new tab when the given component is clicked. The version that takes a URL is for content the browser can already address, such as a static file, a route of the application or an external page; the version that takes a handler is for content produced on the server.
      Type Parameters:
      T - the component type, must implement ClickNotifier
      Parameters:
      component - the component whose clicks open the new tab, not null
      url - the URL to open, not null
      Returns:
      a registration for removing the behavior from the component
      Throws:
      IllegalArgumentException - if url is a javascript: URL