Skip to content

Commit 66b8bbf

Browse files
Add more framework samples (microsoft#80)
Description of changes Adds hello world sample extensions for Svelte, Vue, Angular, and SolidJS-based webview extensions. Also includes some minor clean-up to the React samples and reorganizes the overall repo structure.
1 parent 2284469 commit 66b8bbf

File tree

214 files changed

+46201
-42
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+46201
-42
lines changed

README.md

+18-40
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,29 @@ Follow the instructions in the `README` file of the desired sample extension.
1414

1515
### Default samples
1616

17-
A set of sample extensions using vanilla HTML, CSS, and JavaScript to render the webview UI.
17+
A set of sample extensions using vanilla HTML, CSS, and JavaScript/TypeScript to render the webview UI and demonstrate various VS Code APIs.
1818

19-
| Sample extension | Description |
20-
| ------------------------------------------------ | --------------------------------------------------------------------------------- |
21-
| [component-gallery](./default/component-gallery) | Demonstrates every component in the Webview UI Toolkit. |
22-
| [hello-world](./default/hello-world) | A basic hello-world starter extension. |
23-
| [notepad](./default/notepad) | A simple notetaking extension that leverages the VS Code TreeView API. |
24-
| [weather-webview](./default/weather-webview) | Demonstrates the toolkit being used within a webview view – i.e., Visual Studio Code sidebar. |
25-
26-
### React samples
27-
28-
A set of sample extensions using React to render the webview UI.
29-
30-
| Sample extension | Description |
31-
| ---------------------------------------------| --------------------------------------------------------------------- |
32-
| [hello-world-cra](./react/hello-world-cra) | A basic hello-world starter extension using React + Create React App. |
33-
| [hello-world-vite](./react/hello-world-vite) | A basic hello-world starter extension using React + Vite. |
34-
35-
### Svelte samples
36-
37-
A set of sample extensions using Svelte to render the webview UI.
19+
Don't see a demonstration for your desired use case, VS Code API, etc.? Open an issue and request it.
3820

39-
| Sample extension | Description |
40-
| -----------------| ----------- |
41-
| _Coming soon_ | |
42-
43-
### Vue samples
44-
45-
A set of sample extensions using Vue to render the webview UI.
46-
47-
| Sample extension | Description |
48-
| -----------------| ----------- |
49-
| _Coming soon_ | |
50-
51-
### Angular samples
52-
53-
A set of sample extensions using Angular to render the webview UI.
21+
| Sample extension | Description |
22+
| ------------------------------------------------ | --------------------------------------------------------------------------------------------- |
23+
| [component-gallery](./default/component-gallery) | Demonstrates every component in the Webview UI Toolkit. |
24+
| [hello-world](./default/hello-world) | A basic hello-world starter extension. |
25+
| [notepad](./default/notepad) | A simple notetaking extension that leverages the VS Code TreeView API. |
26+
| [weather-webview](./default/weather-webview) | Demonstrates the toolkit being used within a webview view – i.e., Visual Studio Code sidebar. |
5427

55-
| Sample extension | Description |
56-
| -----------------| ----------- |
57-
| _Coming soon_ | |
28+
### Framework samples
5829

59-
### Other samples
30+
A set of sample extensions that demonstrate how to use various web frameworks to render the webview UI.
6031

61-
Don't see a demonstration for your desired framework, API, use case, etc.? Open an issue and request it.
32+
| Sample extension | Description |
33+
| --------------------------------------------------------------| --------------------------------------------------------------------- |
34+
| [hello-world-angular](./frameworks/hello-world-angular) | A basic hello-world starter extension using Angular. |
35+
| [hello-world-react-cra](./frameworks/hello-world-react-cra) | A basic hello-world starter extension using React + Create React App. |
36+
| [hello-world-react-vite](./frameworks/hello-world-react-vite) | A basic hello-world starter extension using React + Vite. |
37+
| [hello-world-solidjs](./frameworks/hello-world-solidjs) | A basic hello-world starter extension using SolidJS. |
38+
| [hello-world-svelte](./frameworks/hello-world-svelte) | A basic hello-world starter extension using Svelte. |
39+
| [hello-world-vue](./frameworks/hello-world-vue) | A basic hello-world starter extension using Vue. |
6240

6341
## Contributing
6442

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"printWidth": 100,
3+
"tabWidth": 2,
4+
"useTabs": false,
5+
"semi": true,
6+
"singleQuote": false,
7+
"quoteProps": "consistent",
8+
"jsxSingleQuote": false,
9+
"trailingComma": "es5",
10+
"bracketSpacing": true,
11+
"jsxBracketSameLine": true,
12+
"arrowParens": "always"
13+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Hello World (Angular)
2+
3+
This is an implementation of the default [Hello World](https://github.com/microsoft/vscode-webview-ui-toolkit-samples/tree/main/default/hello-world) sample extension that demonstrates how to set up and use a [Angular](https://angular.io/) + [Webview UI Toolkit](https://github.com/microsoft/vscode-webview-ui-toolkit) webview extension.
4+
5+
![A screenshot of the sample extension.](./assets/hello-world-screenshot.png)
6+
7+
## Documentation
8+
9+
For a deeper dive into how this sample works, read the guides below.
10+
11+
- [Extension structure](./docs/extension-structure.md)
12+
- [Extension commands](./docs/extension-commands.md)
13+
- [Extension development cycle](./docs/extension-development-cycle.md)
14+
15+
## Run The Sample
16+
17+
```bash
18+
# Copy sample extension locally
19+
npx degit microsoft/vscode-webview-ui-toolkit-samples/frameworks/hello-world-angular hello-world
20+
21+
# Navigate into sample directory
22+
cd hello-world
23+
24+
# Install dependencies for both the extension and webview UI source code
25+
npm run install:all
26+
27+
# Build webview UI source code
28+
npm run build:webview
29+
30+
# Open sample in VS Code
31+
code .
32+
```
33+
34+
Once the sample is open inside VS Code you can run the extension by doing the following:
35+
36+
1. Press `F5` to open a new Extension Development Host window
37+
2. Inside the host window, open the command palette (`Ctrl+Shift+P` or `Cmd+Shift+P` on Mac) and type `Hello World (Angular): Show`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Extension commands
2+
3+
A quick run down of some of the important commands that can be run when at the root of the project.
4+
5+
```
6+
npm run install:all Install package dependencies for both the extension and Angular webview source code.
7+
npm run start:webview Runs the Angular webview source code in development mode. Open http://localhost:4200 to view it in the browser.
8+
npm run build:webview Build Angular webview source code. Must be executed before compiling or running the extension.
9+
npm run compile Compile VS Code extension.
10+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Extension development cycle
2+
3+
The intended development cycle of this Angular-based webview extension is slightly different than that of other VS Code extensions.
4+
5+
Due to the fact that the `webview-ui` directory holds a self-contained Angular application we get to take advantage of some of the perks that that enables. In particular,
6+
7+
- UI development and iteration cycles can happen much more quickly by using the provided Angular dev server
8+
- Dependency management and project configuration is hugely simplified
9+
10+
## UI development cycle
11+
12+
Since we can take advantage of a much faster dev server, it is encouraged to begin developing webview UI by running the `npm run start:webview` command and then editing the code in the `webview-ui/src` directory.
13+
14+
_Tip: Open the command palette and run the `Simple Browser` command and fill in `http://localhost:4200/` when prompted. This will open a simple browser environment right inside VS Code._
15+
16+
### Message passing
17+
18+
If you need to implement message passing between the webview context and extension context via the VS Code API, a helpful utility is provided in the `webview-ui/src/utilities/vscode.ts` file.
19+
20+
This file contains a utility wrapper around the `acquireVsCodeApi()` function, which enables message passing and state management between the webview and extension contexts.
21+
22+
This utility also enables webview code to be run in a browser-based dev server by using native web browser features that mock the functionality enabled by acquireVsCodeApi. This means you can keep building your webview UI with the dev server even when using the VS Code API.
23+
24+
### Move to traditional extension development
25+
26+
Once you're ready to start building other parts of your extension, simply shift to a development model where you run the `npm run build:webview` command as you make changes, press `F5` to compile your extension and open a new Extension Development Host window. Inside the host window, open the command palette (`Ctrl+Shift+P` or `Cmd+Shift+P` on Mac) and type `Hello World (Angular): Show`.
27+
28+
## Dependency management and project configuration
29+
30+
As mentioned above, the `webview-ui` directory holds a self-contained and isolated Angular application meaning you can (for the most part) treat the development of your webview UI in the same way you would treat the development of a regular Angular application.
31+
32+
To install webview-specific dependencies simply navigate (i.e. `cd`) into the `webview-ui` directory and install any packages you need or set up any Angular specific configurations you want.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Extension structure
2+
3+
This section provides a quick introduction into how this sample extension is organized and structured.
4+
5+
The two most important directories to take note of are the following:
6+
7+
- `src`: Contains all of the extension source code
8+
- `webview-ui`: Contains all of the webview UI source code
9+
10+
## `src` directory
11+
12+
The `src` directory contains all of the extension-related source code and can be thought of as containing the "backend" code/logic for the entire extension. Inside of this directory you'll find the:
13+
14+
- `panels` directory
15+
- `utilities` directory
16+
- `extension.ts` file
17+
18+
The `panels` directory contains all of the webview-related code that will be executed within the extension context. It can be thought of as the place where all of the "backend" code for each webview panel is contained.
19+
20+
This directory will typically contain individual TypeScript or JavaScript files that contain a class which manages the state and behavior of a given webview panel. Each class is usually in charge of:
21+
22+
- Creating and rendering the webview panel
23+
- Properly cleaning up and disposing of webview resources when the panel is closed
24+
- Setting message listeners so data can be passed between the webview and extension
25+
- Setting the initial HTML markdown of the webview panel
26+
- Other custom logic and behavior related to webview panel management
27+
28+
As the name might suggest, the `utilties` directory contains all of the extension utility functions that make setting up and managing an extension easier. In this case, it contains `getUri.ts` which contains a helper function which will get the webview URI of a given file or resource.
29+
30+
Finally, `extension.ts` is where all the logic for activating and deactiving the extension usually live. This is also the place where extension commands are registered.
31+
32+
## `webview-ui` directory
33+
34+
The `webview-ui` directory contains all of the Angular-based webview source code and can be thought of as containing the "frontend" code/logic for the extension webview.
35+
36+
This directory is special because it contains a full-blown Angular application which was created using the Angular CLI. As a result, `webview-ui` contains its own `package.json`, `node_modules`, `tsconfig.json`, and so on––separate from the `hello-world` extension in the root directory.
37+
38+
This strays a bit from other extension structures, in that you'll usually find the extension and webview dependencies, configurations, and source code more closely integrated or combined with each other.
39+
40+
However, in this case, there are some unique benefits and reasons for why this sample extension does not follow those patterns such as easier management of conflicting dependencies and configurations, as well as the ability to use a browser-based dev server, which drastically improves the speed of developing your webview UI, versus recompiling your extension code every time you make a change to the webview.

0 commit comments

Comments
 (0)