npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vscode-webview-tools

v0.1.1

Published

Miscellaneous tools for building things in VS Code webviews

Downloads

452

Readme

vscode-webview-tools

Run Tests

Miscellaneous tools for building things in VS Code webviews.

npm install --save vscode-webview-tools

Table of Contents

API

Colors

An enumeration of theme variables (colors, mostly) exposed by VS Code.

export const enum Colors {
  TabActiveBackground = 'tab-activeBackground',
  StatusBarBorder = 'statusBar-border',
  // 400 more, or so...

parseColors(): { [key in Color]: string }

Gets the map of colors variables to what they're set to in the webview. For example, you can make a custom input validation box-appearing thing:

import { parseColors } from 'vscode-webview-tools';

const colors = parseColors();

const MyErrorMessage = () => (
  <div
    style={{
      background: colors[Color.InputValidationErrorBackground],
      border: `1px solid ${colors[Color.InputValidationErrorBorder]}`,
      color: colors[Color.InputValidationErrorForeground],
    }}
  >
    Your input is invalid!
  </div>
);

observeColors(callback: (colors: { [key in Color]: string }) => void): () => void

Watches for changes to the colors and fires a callback when initially invoked and whenever the colors change. It returns a function that can be used to stop listening for color changes.

const stopListening = observeColors((colors) => {
  console.log('The editor text color is now', colors[Color.EditorForeground]);
});

setTimeout(stopListening, 5000);

You can easily wrap this into a React/Preact hook, for example:

import { Color, useColors } from 'vscode-webview-tools';
import { useState, useEffect } from 'preact/hooks';

const useColors = () => {
  const [colors, setColors] = useState<{ [key: string]: string }>({});
  useEffect(() => observeColors(setColors), []);
  return colors;
};

const SomeText = () => {
  const colors = useColors();
  return (
    <span style={{ color: colors[Color.EditorForeground] }}>
      This will always appear the same color as what's in the editor
    </span>
  );
};

Theme

The general VS Code theme type, exported as an enum.

export const enum Theme {
  Light = 'vscode-light',
  Dark = 'vscode-dark',
  HighContrast = 'vscode-high-contrast',
}

getTheme(): Theme

Gets the current theme.

import { getTheme, Theme } from 'vscode-webview-tools';

const SomeText = () => (
  <span style={{ color: getTheme() === Theme.Light ? 'white' : 'black' }}>Hi!</span>
);

observeTheme(callback: (theme: Theme) => void): () => void

Functions identically to observeColors, but for the theme. Can be wrapped in a hook the same way:

import { Theme, useTheme } from 'vscode-webview-tools';
import { useState, useEffect } from 'preact/hooks';

const useTheme = () => {
  const [theme, setTheme] = useState<{ [key: string]: string }>({});
  useEffect(() => observeTheme(setTheme), []);
  return theme;
};

const SomeText = () => {
  const theme = useTheme();
  return <span style={{ color: getTheme() === Theme.Light ? 'white' : 'black' }}>Hi!</span>;
};