Class WebShare
java.lang.Object
com.vaadin.flow.component.webshare.WebShare
- All Implemented Interfaces:
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 exposesnavigator.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 Summary
Modifier and TypeMethodDescriptionstatic <T extends Component & ClickNotifier<?>>
WebShareBindingonClick(T component) Starts a share action bound to clicks on the given component ? the entry point for "Share" buttons.static Signal<WebShareSupport> Returns a read-only signal hinting at whether the Web Share API is available in the current browser for the current UI.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.
-
Method Details
-
onClick
Starts a share action bound to clicks on the given component ? the entry point for "Share" buttons. Chain the content to share onto the returnedWebShareBinding:
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 throughButton share = new Button("Share"); WebShare.onClick(share).share( ShareContent.create().title("Vaadin").url("https://vaadin.com"));onClickrather than an ordinaryaddClickListener.- Type Parameters:
T- the component type, must implementClickNotifier- Parameters:
component- the component whose clicks trigger the share action, notnull- Returns:
- a binding for chaining the content to share on click
- See Also:
-
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/UNSUPPORTEDonce 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.UNSUPPORTEDsilently rejects in the browser.- Returns:
- the read-only support signal
- Throws:
IllegalStateException- if there is no current UI
-
supportSignal
Returns a read-only signal hinting at whether the Web Share API is available in the current browser for the given UI. Same semantics assupportSignal(); use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
ui- the UI to read the hint from, notnull- Returns:
- the read-only support signal
- Throws:
NullPointerException- ifuiisnull
-