microsoft / vscode-webview-ui-toolkit

A component library for building webview-based extensions in Visual Studio Code.
MIT License
2.02k stars 144 forks source link

Theming doesn't work in notebook output webviews #410

Open pwang347 opened 2 years ago

pwang347 commented 2 years ago

Describe the bug

Theming doesn't seem to be working for VS Code notebook outputs: image

Not sure if this issue belongs here or in vscode itself, but I do see that the webviews are correctly providing some theme information there (maybe the color tokens are named differently for outputs?) FYI @mattbierner

To reproduce

  1. Register a notebook output renderer (e.g. see https://github.com/microsoft/vscode-extension-samples/tree/main/notebook-renderer-react-sample)
  2. In the renderer add a primary-styled button using the toolkit.
  3. Observe that the button isn't properly styled

Expected behavior

The button should have the correct color as it does in other types of webviews, e.g. image

Current behavior

See the image above

Desktop (please complete the following information):

Additional context

hawkticehurst commented 2 years ago

Thanks for the report @pwang347!

I tried to follow your reproduction steps but it seems like the sample is incomplete so I didn't get too far (would you happen to know of any other minimal notebook samples I could reference?)

With that said, off the top of my head I'd wager this could be an issue with your toolkit version –– potentially try updating to the latest.

If that doesn't work could indeed be an issue with the notebook environment at large?? @mjbvz do you know if there are any discrepancies between the CSS variables that get exposed to webviews and notebooks?

mjbvz commented 2 years ago

The vscode theme variables seem to work for me in notebook outputs:

... 

const root = shadow.querySelector<HTMLElement>('#root')!;

const button = document.createElement('div');
button.textContent = "Ugly button";
button.style.padding = "20px";
button.style.background = "var(--vscode-button-background)";
Screenshot 2022-11-30 at 4 32 22 PM

@hawkticehurst Does the toolkit provides its own theme colors? If so, how / where are these set?

hawkticehurst commented 2 years ago

@mjbvz Thanks for clarifying/demoing the notebook output!

We do provide a set of fallback colors (based on the default dark theme) for when the components are used outside of a VS Code context (inside Storybook for example), but otherwise, all the components pull directly from the same CSS variables (or at least they should be).

These colors are defined and set across a few different files:

Based on your example, it looks like something about this code is broken since the variable works and @pwang347's button seems to be defaulting to the dark theme fallback colors

hawkticehurst commented 2 years ago

@mjbvz if you see anything in the above code that stands out to you please let me know

@pwang347 with all this in mind, if you could provide a small example that reproduces this issue (i.e. a tiny notebook extension that uses a toolkit button that I can run locally on my machine) so I can dive a bit deeper into our theming code that would be greatly appreciated

pwang347 commented 2 years ago

@hawkticehurst hey! please see https://github.com/pwang347/vscode-renderer-test

I can reproduce the following issue here by opening the IPYNB file I provided in the root of the repo: image

Essentially the steps are

  1. Clone the project
  2. Run npm ci; npm run compile
  3. Open the project in VS Code, start it using the Run and Debug menu
  4. Open the test.ipynb file
  5. Execute the first cell

Important note

I could not build the project on latest webview-ui-toolkit, I got the following error: image

So I ended up reverting the version to 0.9.2

hawkticehurst commented 1 year ago

Thank you for the reproduction it has been immensely helpful!

A couple of updates:

Update the toolkit to the latest version and then update the module.rules section of your webpack config with the following:

{ 
  test: /\.m?js/, 
  resolve: { 
    fullySpecified: false 
  } 
}

The most recent toolkit version, unfortunately, does not solve the original theming issue (but at least your buttons will have rounded corners now 🤷🏻‍♂️😅):

Screen Shot 2022-12-06 at 4 28 10 PM

Signing off for today, but I'll keep diving into why the toolkit components are not correctly picking up the theme variables in this context over the next week or two as I have time.

pwang347 commented 1 year ago

@hawkticehurst good to know about the workaround, and thanks for taking a look!

znorman-harris commented 1 year ago

Any updates on this issue? I'm thinking about using the components for a notebook renderer as well and can see it is still a problem.