Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect light/dark theme programmatically in Visual Studio Code

I'm developing a Visual Studio Code extension that enables previewing mermaid diagrams:

enter image description here

The extension uses a default stylesheet that works fine if using the light theme. However, if the user has switched Visual Studio Code to use the dark theme, the stylesheet has some rules that are not compatible with the default dark stylesheet:

enter image description here

Is is possible to detect programmatically the active theme type (e.g. light/dark) so that I could provide a different stylesheet for each case?

I would like to use the stylesheets bundled in mermaid and not craft completely different ones in my extension.

like image 553
Vlad Stirbu Avatar asked May 16 '16 15:05

Vlad Stirbu


People also ask

How do you get the Light theme in VS code?

In VS Code, open the Color Theme picker with File > Preferences > Color Theme. (Code > Preferences > Color Theme on macOS).

How do I change my Dark theme to Light in VS code?

On the menu bar, select Tools > Options. In the options list, select Environment > General. In the Color theme list, choose between the default Dark theme, the Blue theme, the Blue (Extra Contrast) theme, and the Light theme. Or, choose the Use system setting option to select the theme that Windows uses.

How can I tell what theme My visual studio is using?

On the menu bar, choose Tools > Options. In the options list, choose Environment > General. In the Color theme list, choose either the default Blue theme, the Light theme, the Dark theme, or the Blue (Extra Contrast) theme.


1 Answers

Visual Studio Code 1.3 added this feature:

When previewing html, we expose the style of the current theme via class names of the body element. Those are vscode-light, vscode-dark, and vscode-high-contrast.

Inspecting for one of these values using JavaScript allows customisation of the preview stylesheet to match the active theme in the editor.

like image 156
Vlad Stirbu Avatar answered Sep 27 '22 22:09

Vlad Stirbu