Class ShareAction

All Implemented Interfaces:
Serializable

public class ShareAction extends PromiseAction<Void>
Invokes the browser's native share sheet via navigator.share when the bound trigger fires. Supports any combination of title, text, and url payload slots; at least one slot must be set ? the Web Share API rejects a call with an empty payload.

The Web Share API requires the call to happen inside a short-lived user gesture (click, key press, ...). Bind this action to a trigger that fires during such a gesture, typically a ClickTrigger. The share sheet itself acts as the user-facing confirmation; the browser also rejects calls made outside a gesture.

Outcome handling extends PromiseAction: use the no-callbacks constructor for fire-and-forget, or the overload taking onShared/onError. onShared fires after the user dismisses the sheet by sharing; onError fires for both true failures (no gesture, permissions policy block) and for the AbortError the browser reports when the user dismisses the sheet without picking a target.

For internal use only. May be renamed or removed in a future release.

Since:
25.2
See Also:
  • Constructor Details

    • ShareAction

      public ShareAction(@Nullable Action.Input<String> titleInput, @Nullable Action.Input<String> textInput, @Nullable Action.Input<String> urlInput)
      Creates a fire-and-forget share action.
      Parameters:
      titleInput - input producing the title field, or null to omit
      textInput - input producing the text field, or null to omit
      urlInput - input producing the url field, or null to omit
      Throws:
      IllegalArgumentException - if all three inputs are null
    • ShareAction

      public ShareAction(@Nullable Action.Input<String> titleInput, @Nullable Action.Input<String> textInput, @Nullable Action.Input<String> urlInput, SerializableRunnable onShared, SerializableConsumer<PromiseAction.Error> onError)
      Creates a share action whose outcome is reported back to the server.
      Parameters:
      titleInput - input producing the title field, or null to omit
      textInput - input producing the text field, or null to omit
      urlInput - input producing the url field, or null to omit
      onShared - invoked on the UI thread after the client reports the share resolved, not null
      onError - invoked on the UI thread with the browser's error after the client reports the share rejected ? typically AbortError when the user dismissed the sheet, not null
      Throws:
      IllegalArgumentException - if all three inputs are null
  • Method Details

    • toPromiseJs

      protected JsFunction toPromiseJs(Trigger trigger)
      Description copied from class: PromiseAction
      Subclasses return a JsFunction that, when invoked with the trigger's event, evaluates to a Promise. The value the promise resolves with is decoded to T on the server and handed to onSuccess. To deliver a typed value, subclasses can wrap their API call in an IIFE inside the function body ? for example, copying a string and resolving with it:
      
       return JsFunction.of(
               "return ((v) => navigator.clipboard.writeText(v).then(() => v))($0(event))",
               textInput.toJs(trigger)).withArguments("event");
       
      Specified by:
      toPromiseJs in class PromiseAction<Void>
      Parameters:
      trigger - the surrounding trigger this render is for, not null
      Returns:
      the promise-yielding JS function, not null