Skip to content

Commit 2da8354

Browse files
authored
Fix widgets sometimes rendering in light theme when VS Code is in a dark theme (#2120)
(See related issues microsoft/vscode-jupyter#7161 and microsoft/vscode-jupyter#9403 for more background) Our CSS override to address the widget theming issue wasn't working somtimes, which was very frustrating, as in Dark Mode our widgets would often appear as they should in light mode. The reason is that the order in which the style sheets get injected is non-deterministic, and as the 'problematic' CSS rule and the override to fix it have the same specificity, it would be 'last one wins'. Sometimes our style-sheet is last and it works... <img width="955" alt="image" src="https://github.com/user-attachments/assets/281524d8-a3d5-4d15-a649-442a21b5ddec" /> And sometimes the IPyWidgets 'force it to white' rule is last so the issue persists... <img width="960" alt="image" src="https://github.com/user-attachments/assets/f60f762e-0312-49de-9a2d-6f3cf4833b20" /> The unfortunate best _hack_ I could come up with is to programmatically ensure the override CSS rule is always last in the `style`s in the header. Not pretty, but it works.
1 parent fb06836 commit 2da8354

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

npm/qsharp/ux/qsharp-ux.css

-5
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,6 @@ html {
501501

502502
/* Space chart */
503503

504-
/* Needed until https://github.com/microsoft/vscode-jupyter/issues/7161 is resolved */
505-
.cell-output-ipywidget-background {
506-
background-color: transparent !important;
507-
}
508-
509504
#pieChart {
510505
fill: var(--main-color);
511506
}

widgets/js/index.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ type RenderArgs = {
4040
export function render({ model, el }: RenderArgs) {
4141
const componentType = model.get("comp");
4242

43+
// There is an existing issue where in VS Code it always shows the widget background as white.
44+
// (See https://github.com/microsoft/vscode-jupyter/issues/7161)
45+
// We tried to fix this in CSS by overridding the style, but there is a race condition whereby
46+
// depending on which style gets injected first (ours or ipywidgets), it may or may not work.
47+
48+
// The solution here is to force our own override to be last in the style list if not already.
49+
// It's a bit of a hack, but it works, and I couldn't find something better that wouldn't be fragile.
50+
51+
if (
52+
!el.ownerDocument.head.lastChild?.textContent?.includes("widget-css-fix")
53+
) {
54+
const forceStyle = el.ownerDocument.createElement("style");
55+
forceStyle.textContent = `/* widget-css-fix */ .cell-output-ipywidget-background {background-color: transparent !important;}`;
56+
el.ownerDocument.head.appendChild(forceStyle);
57+
}
58+
4359
switch (componentType) {
4460
case "SpaceChart":
4561
renderChart({ model, el });

0 commit comments

Comments
 (0)