Getting started
What is VSCode Elements?
Section titled “What is VSCode Elements?”The “webview API” enables extensions to build entirely customizable views within Visual Studio Code. While the VSCode API grants access to different UI elements, none of these UI widgets are functional in the “webview.” VSCode Elements addresses this by re-implementing these UI controls as web components (aka custom elements), and introducing some new ones. This allows for the creation of user interfaces that look like the native user interface of VSCode.
You may not need VSCode Elements
Section titled “You may not need VSCode Elements”If you prefer traditional HTML and CSS over JavaScript, or if you’d rather avoid adding a new dependency, consider checking out the VSCode Elements Lite project. It offers a collection of HTML and CSS snippets that, in many cases, are perfectly suitable. This approach is both simple and robust.
Installation
Section titled “Installation”You can access the library as NPM package:
npm i @vscode-elements/elements
Including the bundled version
Section titled “Including the bundled version”This is the most straightforward method, and while it may not be the most optimized, it gets the job done.
<script src="node_modules/@vscode-elements/elements/dist/bundled.js" type="module"></script>
Importing only necessary components
Section titled “Importing only necessary components”import "@vscode-elements/elements/dist/vscode-button/index.js";
The imported components will be automatically registered. Once registered,
Vscode Elements can be used like standard HTML elements. A standard default view
can be provided using the :defined()
pseudo-class until the element is
registered.
vscode-buttton:not(:defined) { display: inline-block; height: 26px; visibility: hidden; width: 100px;}
Properties and attributes
Section titled “Properties and attributes”Custom elements are configurable through properties and attributes. Properties used in JavaScript while attributes are used in HTML.
An example of using a property:
const badge = document.querySelector("vscode-badge");badge.variant = "activity-bar-counter";
An attribute example
<vscode-badge variant="activity-bar-counter"></vscode-badge>
The attribute name may differs from the property name. In the API documentation, the attribute corresponding to the property is always indicated if it exists.
Attribute and property reflection
Section titled “Attribute and property reflection”Property reflection means that the related attribute will be synchronized when the property is changed. When a property is reflected it will be indicated with the reflects badge in the API documentation.
Methods
Section titled “Methods”Methods are callable functions on the element instances.
const textfield = document.querySelector("vscode-textfield");textfield.reportValidity();
Events
Section titled “Events”Most of the components dispatch events.
const tabs = document.querySelector("vscode-tabs");tabs.addEventListener("vsc-tabs-select", () => { console.log("A tab has been selected.");});
Most of the components use native events if possible. When a component has a custom event, the event
is exported as a TypeScript type. All exported events are descendants of the CustomEvent
class.
import type { VscTabsSelectEvent } from "@vscode-elements/elements/dist/vscode-tabs/vscode-tabs";
Slots are used to accept HTML content. The default slot, which is the content between the HTML tags, is the most common slot:
<vscode-button>Click here</vscode-button>
Many components have named slots. In the following example the badge component
is placed in the decorations
slot.
<vscode-collapsible title="Decorations example" class="collapsible"> <vscode-badge variant="counter" slot="decorations">99</vscode-badge> <p> Suspendisse potenti. Maecenas eu egestas metus. Nulla eget placerat mi, et efficitur augue. </p></vscode-collapsible>
Custom CSS properties
Section titled “Custom CSS properties”The appearance of VSCode elements can be configured using custom CSS properties. These variables are provided by VSCode, and in most cases, it is not recommended to change them manually, although they can be modified if necessary.
vscode-button { --vscode-button-hoverBackground: red;}