Class WebShare

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

public final class WebShare extends Object implements Serializable
Entry point for the browser's Web Share API (navigator.share). Two entry points:
  • onClick(Component) ? bind a share action to a click gesture. The Web Share API requires a transient user gesture, so the trigger pattern is the only reliable way to invoke it.
  • supportSignal() ? feature detection: read whether the current browser exposes navigator.share, useful for deciding whether to show a share affordance at all.

 Button share = new Button("Share");
 if (WebShare.supportSignal().peek() == WebShareSupport.SUPPORTED) {
     WebShare.onClick(share).share(
             ShareContent.create().title("Vaadin").url("https://vaadin.com"));
 }
 
The Web Share API requires a fresh user gesture for each call, so actions only run during the DOM event that fires the underlying trigger.
Since:
25.2
See Also:
  • Method Details

    • onClick

      public static <T extends Component & ClickNotifier<?>> WebShareBinding onClick(T component)
      Starts a share action bound to clicks on the given component ? the entry point for "Share" buttons. Chain the content to share onto the returned WebShareBinding:
      
       Button share = new Button("Share");
       WebShare.onClick(share).share(
               ShareContent.create().title("Vaadin").url("https://vaadin.com"));
       
      The browser only opens the native share sheet while it is handling a genuine user gesture, and that activation is gone by the time a normal server-side click listener runs. Binding the action here makes it run inside the browser's own click handler, where the gesture is still valid ? which is why sharing goes through onClick rather than an ordinary addClickListener.
      Type Parameters:
      T - the component type, must implement ClickNotifier
      Parameters:
      component - the component whose clicks trigger the share action, not null
      Returns:
      a binding for chaining the content to share on click
      See Also:
    • supportSignal

      public static Signal<WebShareSupport> supportSignal()
      Returns a read-only signal hinting at whether the Web Share API is available in the current browser for the current UI. The value is seeded from the bootstrap handshake, so user code observes a real value before any view code runs.

      Web Share support is established at page load and does not change during the session, so the signal effectively transitions UNKNOWN ? SUPPORTED/UNSUPPORTED once and then remains stable.

      Use this to decide whether to show a share affordance at all ? calling a share action when the value is WebShareSupport.UNSUPPORTED silently rejects in the browser.

      Returns:
      the read-only support signal
      Throws:
      IllegalStateException - if there is no current UI
    • supportSignal

      public static Signal<WebShareSupport> supportSignal(UI ui)
      Returns a read-only signal hinting at whether the Web Share API is available in the current browser for the given UI. Same semantics as supportSignal(); use this overload from background threads or anywhere UI.getCurrent() is unreliable.
      Parameters:
      ui - the UI to read the hint from, not null
      Returns:
      the read-only support signal
      Throws:
      NullPointerException - if ui is null