Creates a new Webview.
the window to add this webview to.
The unique webview label. Must be alphanumeric: a-zA-Z-/:_
.
The Webview instance to communicate with the webview.
import { Window } from '@tauri-apps/api/window'
import { Webview } from '@tauri-apps/api/webview'
const appWindow = new Window('my-label')
appWindow.once('tauri://created', async function() {
const webview = new Webview(appWindow, 'my-label', {
url: 'https://github.com/tauri-apps/tauri',
// create a webview with specific logical position and size
x: 0,
y: 0,
width: 800,
height: 600,
});
webview.once('tauri://created', function () {
// webview successfully created
});
webview.once('tauri://error', function (e) {
// an error happened creating the webview
});
});
The webview label. It is a unique identifier for the webview, can be used to reference it later.
The window hosting this webview.
Local event listeners.
Static
getStatic
getGet an instance of Webview
for the current webview.
Static
getListen to an emitted event on this webview.
Event name. Must include only alphanumeric characters, -
, /
, :
and _
.
Event handler.
A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
import { getCurrentWebview } from '@tauri-apps/api/webview';
const unlisten = await getCurrentWebview().listen<string>('state-changed', (event) => {
console.log(`Got error: ${payload}`);
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();
Listen to an emitted event on this webview only once.
Event name. Must include only alphanumeric characters, -
, /
, :
and _
.
Event handler.
A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
Emits an event to all targets matching the given target.
Label of the target Window/Webview/WebviewWindow or raw EventTarget object.
Event name. Must include only alphanumeric characters, -
, /
, :
and _
.
Optional
payload: TEvent payload.
The position of the top-left hand corner of the webview's client area relative to the top-left hand corner of the desktop.
The webview's position.
The physical size of the webview's client area. The client area is the content of the webview, excluding the title bar and borders.
The webview's size.
Closes the webview.
A promise indicating the success or failure of the operation.
Resizes the webview.
The logical or physical size.
A promise indicating the success or failure of the operation.
Sets the webview position.
The new position, in logical or physical pixels.
A promise indicating the success or failure of the operation.
Bring the webview to front and focus.
A promise indicating the success or failure of the operation.
Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes.
A promise indicating the success or failure of the operation.
Hide the webview.
A promise indicating the success or failure of the operation.
Show the webview.
A promise indicating the success or failure of the operation.
Set webview zoom level.
A promise indicating the success or failure of the operation.
Moves this webview to the given label.
A promise indicating the success or failure of the operation.
Clears all browsing data for this webview.
A promise indicating the success or failure of the operation.
Specify the webview background color.
0
will be replaced by 255
A promise indicating the success or failure of the operation.
Listen to a file drop event. The listener is triggered when the user hovers the selected files on the webview, drops the files or cancels the operation.
A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
import { getCurrentWebview } from "@tauri-apps/api/webview";
const unlisten = await getCurrentWebview().onDragDropEvent((event) => {
if (event.payload.type === 'over') {
console.log('User hovering', event.payload.position);
} else if (event.payload.type === 'drop') {
console.log('User dropped', event.payload.paths);
} else {
console.log('File drop cancelled');
}
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();
When the debugger panel is open, the drop position of this event may be inaccurate due to a known limitation. To retrieve the correct drop position, please detach the debugger.
Create new webview or get a handle to an existing one.
Webviews are identified by a label a unique identifier that can be used to reference it later. It may only contain alphanumeric characters
a-zA-Z
plus the following special characters-
,/
,:
and_
.Example
Since
2.0.0