Class WebShareBinding

java.lang.Object
com.vaadin.flow.component.webshare.WebShareBinding
All Implemented Interfaces:
Serializable

public final class WebShareBinding extends Object implements Serializable
Fluent surface returned from WebShare.onClick(T), used to declare what a click should hand to the browser's native share sheet. Call share(com.vaadin.flow.component.webshare.ShareContent) with the title, text, and/or URL to share. The share runs in the browser at click time, while the user gesture required by the Web Share API is still valid.

Actions come in two flavours: fire-and-forget (one argument) and observed (with onShared/onError callbacks). onShared fires after the user picks a share target; onError fires when the user dismisses the sheet (the browser reports AbortError) or when the call is rejected for other reasons (no gesture, permissions policy block, unsupported browser). Both consumers are required in the observed form ? pass () -> {} or err -> {} to opt out of one.


 Button share = new Button("Share");
 WebShare.onClick(share).share(
         ShareContent.create().title("Hello").url("https://vaadin.com"));

 WebShare.onClick(share).share(
         ShareContent.create().url("https://vaadin.com"),
         () -> Notification.show("Shared!"),
         err -> Notification.show("Cancelled: " + err.name()));
 
Since:
25.2
See Also:
  • Method Details

    • share

      public void share(ShareContent content)
      Invokes the browser's native share sheet with the given content when the underlying trigger fires.
      Parameters:
      content - the content, not null; must have at least one slot set
      Throws:
      IllegalArgumentException - if content has no slots set
    • share

      public void share(ShareContent content, SerializableRunnable onShared, SerializableConsumer<PromiseAction.Error> onError)
      Like share(ShareContent) but reports the outcome back to the server.
      Parameters:
      content - the content, not null; must have at least one slot set
      onShared - UI-thread callback invoked after the user picked a share target, not null
      onError - UI-thread callback receiving the browser's error (including AbortError when the user dismissed the sheet), not null
      Throws:
      IllegalArgumentException - if content has no slots set