Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: scope ag-grid styles to plugin to avoid style pollution (#3601)

Myles Scolnick7f23f72adbe9
diff --git a/frontend/src/plugins/core/builder.ts b/frontend/src/plugins/core/builder.ts
index f47bb39898..1b0f5fc0e9 100644
--- a/frontend/src/plugins/core/builder.ts
+++ b/frontend/src/plugins/core/builder.ts
@@ -7,7 +7,12 @@ type Renderer<S, D, F> = (props: IPluginProps<S, D, F>) => JSX.Element;
// If this simple builder pattern becomes unwieldy, we can switch to a more
// complex builder pattern with individual classes.
-export function createPlugin<S>(tagName: string) {
+export function createPlugin<S>(
+ tagName: string,
+ opts: {
+ cssStyles?: string[];
+ } = {},
+) {
return {
/**
* Data schema for the plugin.
@@ -26,6 +31,7 @@ export function createPlugin<S>(tagName: string) {
*/
renderer(renderer: Renderer<S, D, F>): IPlugin<S, D, F> {
return {
+ ...opts,
tagName,
validator,
functions,
@@ -39,6 +45,7 @@ export function createPlugin<S>(tagName: string) {
*/
renderer(renderer: Renderer<S, D, unknown>): IPlugin<S, D> {
return {
+ ...opts,
tagName,
validator,
render: renderer,
diff --git a/frontend/src/plugins/core/registerReactComponent.tsx b/frontend/src/plugins/core/registerReactComponent.tsx
index 317a803812..d92297f438 100644
--- a/frontend/src/plugins/core/registerReactComponent.tsx
+++ b/frontend/src/plugins/core/registerReactComponent.tsx
@@ -414,9 +414,13 @@ export function registerReactComponent<T>(plugin: IPlugin<T, unknown>): void {
// Custom styles provided by the plugin
if (plugin.cssStyles) {
- const style = document.createElement("style");
- style.textContent = plugin.cssStyles.join("\n");
- shadowRoot.append(style);
+ const pluginSheet = new CSSStyleSheet();
+ pluginSheet.replaceSync(plugin.cssStyles.join("\n"));
+ // Add plugin styles last to take precedence
+ shadowRoot.adoptedStyleSheets = [
+ ...shadowRoot.adoptedStyleSheets,
+ pluginSheet,
+ ];
}
}
@@ -448,6 +452,13 @@ export function registerReactComponent<T>(plugin: IPlugin<T, unknown>): void {
});
shadowRoot.append(...styleSheets);
+
+ // Custom styles provided by the plugin
+ if (plugin.cssStyles) {
+ const style = document.createElement("style");
+ style.textContent = plugin.cssStyles.join("\n");
+ shadowRoot.append(style);
+ }
}
private isAdoptedStyleSheetsSupported() {
diff --git a/frontend/src/plugins/impl/DataEditorPlugin.tsx b/frontend/src/plugins/impl/DataEditorPlugin.tsx
index 11c626eec4..35aed75141 100644
--- a/frontend/src/plugins/impl/DataEditorPlugin.tsx
+++ b/frontend/src/plugins/impl/DataEditorPlugin.tsx
@@ -11,9 +11,9 @@ import { LoadingTable } from "@/components/data-table/loading-table";
import { Alert, AlertTitle } from "@/components/ui/alert";
import { DelayMount } from "@/components/utils/delay-mount";
import React from "react";
-import "./data-editor/grid.css";
-import "ag-grid-community/styles/ag-grid.css";
-import "ag-grid-community/styles/ag-theme-quartz.css";
+import gridCss from "./data-editor/grid.css?inline";
+import agGridCss from "ag-grid-community/styles/ag-grid.css?inline";
+import agThemeCss from "ag-grid-community/styles/ag-theme-quartz.css?inline";
import { DATA_TYPES } from "@/core/kernel/messages";
import { toFieldTypes } from "@/components/data-table/types";
@@ -31,7 +31,9 @@ interface Edits {
// Lazy load the data editor since it brings in ag-grid
const LazyDataEditor = React.lazy(() => import("./data-editor/data-editor"));
-export const DataEditorPlugin = createPlugin<Edits>("marimo-data-editor")
+export const DataEditorPlugin = createPlugin<Edits>("marimo-data-editor", {
+ cssStyles: [gridCss, agGridCss, agThemeCss],
+})
.withData(
z.object({
initialValue: z.object({
diff --git a/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx b/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx
index 0cc050a7fa..0947a2d8d1 100644
--- a/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx
+++ b/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx
@@ -71,12 +71,40 @@ const AnyWidgetSlot = (props: Props) => {
// Mount the CSS
useEffect(() => {
- if (!css || !props.host.shadowRoot) {
+ const shadowRoot = props.host.shadowRoot;
+ if (!css || !shadowRoot) {
return;
}
+
+ // Try constructed stylesheets first
+ if (
+ "adoptedStyleSheets" in Document.prototype &&
+ "replace" in CSSStyleSheet.prototype
+ ) {
+ const sheet = new CSSStyleSheet();
+ try {
+ sheet.replaceSync(css);
+ if (shadowRoot) {
+ shadowRoot.adoptedStyleSheets = [
+ ...shadowRoot.adoptedStyleSheets,
+ sheet,
+ ];
+ }
+ return () => {
+ if (shadowRoot) {
+ shadowRoot.adoptedStyleSheets =
+ shadowRoot.adoptedStyleSheets.filter((s) => s !== sheet);
+ }
+ };
+ } catch {
+ // Fall through to inline styles if constructed sheets fail
+ }
+ }
+
+ // Fallback to inline styles
const style = document.createElement("style");
style.innerHTML = css;
- props.host.shadowRoot.append(style);
+ shadowRoot.append(style);
return () => {
style.remove();
};