vscode-webview-extension-with-react is a repo to illustrate how to register a webview using react to side bar of vscode when developing vscode extension
1. Create a webview to VScode extension side panel
In package.json we need to register a view container to have a icon in side bar
Declare LeftPanelWebview class and register Webview in extension.ts
const leftPanelWebViewProvider = new LeftPanelWebview(context?.extensionUri, {});
let view = vscode.window.registerWebviewViewProvider(
EXTENSION_CONSTANT.LEFT_PANEL_WEBVIEW_ID,
leftPanelWebViewProvider,
);
context.subscriptions.push(view);
Then you will have a webview in side panel !!
2. How to render react component in vscode and handle user action
When calling Webview for the first time, resolveWebviewView will be called and expect to return the html file of the webview. We will use ReactDOM.renderToString to concat the react component into body tag of the html file. In that case, react component will be rendered.
<body>
${
ReactDOMServer.renderToString((
<LeftPanel message={"Tutorial for Left Panel Webview in VSCode extension"}></LeftPanel>
))
}
<script nonce="${nonce}" type="text/javascript" src="${constantUri}"></script>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
The weakness of this approach is the webview will only be a static html view. Because of this problem, we will need to embedded the script into html so that we can handle user action base on eventListener from DOM
We can also handle user action in script and callback to our extension by using vscode.postMessage in script file and use _view.webview.onDidReceiveMessage to receive the message in webview components
// Script file: Emit message
vscode.postMessage({
action: POST_MESSAGE_ACTION.SHOW_WARNING_LOG,
data: {
message: "You just clicked on the left panel webview button"
}});
Using this way of communication between webview and extension, we also can create a message chanel between 2 separated webview in some complicated vscode extension