Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Chat: join the sidebar to the tab band without a page-wide :has()

luishron12318cf8c766555
diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsx
index fa668b8e97..44a52b1254 100644
--- a/src/components/ChatView.tsx
+++ b/src/components/ChatView.tsx
@@ -116,6 +116,7 @@ import { ChatBackdrop, GlassBar, GlassScrollFrame } from "./GlassScrollFrame";
import { PagePaneContext } from "./app/PageTools";
import { useTranscriptViewport } from "@/hooks/use-transcript-viewport"; import { staysInView } from "../../shared/person-wait";
import { useUnreadDivider } from "@/hooks/use-unread-divider";
+import { useTabsFrameOnRoot } from "@/hooks/use-tabs-frame-on-root";
import { NewMessagesDivider } from "./NewMessagesDivider";
import { unreadMessageIds } from "@/lib/unread-divider"; import { UnreadContextOverlay } from "./UnreadContextOverlay";
import { appendDraftAttachments, useReplyDraft } from "@/lib/drafts";
@@ -759,6 +760,7 @@ export const ChatView = memo(function ChatView({ bot: profile, inForkPanel = fal
const phone = useMediaQuery(PHONE_LAYOUT);
const phoneMenu = phone && !remoteClient;
const simpleHead = !advanced && !remoteClient, headTabs = useThreadTabsShown(bot.id) && !inForkPanel;
+ useTabsFrameOnRoot(headTabs ? (simpleHead ? "simple" : "advanced") : undefined);
const canWrite = useCanWriteIn(bot.threadId, bot.yourAccess), mayWrite = canWrite !== false;
const computerStarting = computerStartLine(state.computerStart, bot.name); useScreensWhile(Boolean(computerStarting));
diff --git a/src/components/Sidebar.header.test.ts b/src/components/Sidebar.header.test.ts
index 0e513771fe..9e2f9af90a 100644
--- a/src/components/Sidebar.header.test.ts
+++ b/src/components/Sidebar.header.test.ts
@@ -210,7 +210,7 @@ describe("sidebar top row", () => {
for (const classes of round) expect(classes).toContain("size-9");
expect(row).toMatch(/data-rail-toggle/);
const css = readFileSync(join(dirname(fileURLToPath(import.meta.url)), "..", "styles.css"), "utf8").replace(/\r\n/g, "\n");
- const beside = ':root:has([data-chathead-tabs="simple"]) [data-sidebar-top-row="simple"]';
+ const beside = ':root[data-chat-tabs-frame="simple"] [data-sidebar-top-row="simple"]';
expect(css).toContain(`${beside} { padding-top: 14px; }`);
expect(css).toContain(`${beside} :is([data-rail-toggle], [data-sidebar-top-buttons] button.size-9) { width: 32px; height: 32px; }`);
});
diff --git a/src/hooks/use-tabs-frame-on-root.dom.test.ts b/src/hooks/use-tabs-frame-on-root.dom.test.ts
new file mode 100644
index 0000000000..cd73f0cb22
--- /dev/null
+++ b/src/hooks/use-tabs-frame-on-root.dom.test.ts
@@ -0,0 +1,37 @@
+// @vitest-environment happy-dom
+import { act, createElement } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+import { useTabsFrameOnRoot } from "./use-tabs-frame-on-root";
+
+let root: Root;
+let container: HTMLElement;
+beforeEach(() => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+});
+afterEach(() => {
+ container.remove();
+ delete document.documentElement.dataset.chatTabsFrame;
+ vi.unstubAllGlobals();
+});
+
+function Header({ look }: { look: "simple" | "advanced" | undefined }) {
+ useTabsFrameOnRoot(look);
+ return null;
+}
+const show = (look: "simple" | "advanced" | undefined) => act(async () => root.render(createElement(Header, { look })));
+
+it("names the header's tab band on the root while it shows, and clears it when it goes", async () => {
+ await show("simple");
+ expect(document.documentElement.dataset.chatTabsFrame).toBe("simple");
+ await show("advanced");
+ expect(document.documentElement.dataset.chatTabsFrame).toBe("advanced");
+ await show(undefined);
+ expect(document.documentElement.dataset.chatTabsFrame).toBeUndefined();
+ await show("simple");
+ await act(async () => root.unmount());
+ expect(document.documentElement.dataset.chatTabsFrame).toBeUndefined();
+});
diff --git a/src/hooks/use-tabs-frame-on-root.ts b/src/hooks/use-tabs-frame-on-root.ts
new file mode 100644
index 0000000000..e6214a9f40
--- /dev/null
+++ b/src/hooks/use-tabs-frame-on-root.ts
@@ -0,0 +1,16 @@
+import { useLayoutEffect } from "react";
+
+/**
+ * Names the chat header's tab band on the root element, for the rules that join the sidebar to it. Matching the header
+ * from the root instead (`:root:has(...)`) makes the browser restyle the whole page each time a streaming reply adds a node.
+ */
+export function useTabsFrameOnRoot(look: "simple" | "advanced" | undefined): void {
+ useLayoutEffect(() => {
+ if (!look) return;
+ const root = document.documentElement;
+ root.dataset.chatTabsFrame = look;
+ return () => {
+ if (root.dataset.chatTabsFrame === look) delete root.dataset.chatTabsFrame;
+ };
+ }, [look]);
+}
diff --git a/src/lib/page-wide-has.test.ts b/src/lib/page-wide-has.test.ts
new file mode 100644
index 0000000000..2d261165cf
--- /dev/null
+++ b/src/lib/page-wide-has.test.ts
@@ -0,0 +1,13 @@
+import { readFileSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+import { expect, it } from "vitest";
+
+const css = readFileSync(join(dirname(fileURLToPath(import.meta.url)), "..", "styles.css"), "utf8");
+
+// A `:has()` on the root, html or body is checked again on every node added anywhere in the page, and its match
+// restyles the whole page: with several replies streaming that is a full restyle many times a second.
+it("matches nothing from the root with :has()", () => {
+ const rules = css.replace(/\/\*[\s\S]*?\*\//g, "").match(/(?:^|[\s,{}>+~(])(?::root|html|body)(?:\[[^\]]*\]|:[\w-]+(?:\([^)]*\))?)*:has\(/gm) ?? [];
+ expect(rules).toEqual([]);
+});
diff --git a/src/styles.css b/src/styles.css
index 21a2ad7a98..d4092f8171 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -3932,12 +3932,12 @@ button:disabled.bg-danger {
[data-chathead-tabs]::before { left: 0; background: radial-gradient(circle at 100% 100%, transparent calc(var(--chat-card-radius) - 0.5px), var(--color-edge) calc(var(--chat-card-radius) - 0.5px), var(--color-edge) var(--chat-card-radius), var(--color-panel) calc(var(--chat-card-radius) + 0.5px)); }
[data-chathead-tabs]::after { right: 0; background: radial-gradient(circle at 0 100%, transparent calc(var(--chat-card-radius) - 0.5px), var(--color-edge) calc(var(--chat-card-radius) - 0.5px), var(--color-edge) var(--chat-card-radius), var(--color-panel) calc(var(--chat-card-radius) + 0.5px)); }
[data-chathead-tabs]:has([data-panel-beside])::after { display: none; }
- :root:has([data-chathead-tabs="simple"]) [data-sidebar] { border-image: linear-gradient(to bottom, transparent 60px, var(--color-edge) 60px) 1; }
- :root:has([data-chathead-tabs="advanced"]) [data-sidebar] { border-image: linear-gradient(to bottom, transparent 54px, var(--color-edge) 54px) 1; }
- :root[data-skin="midnight"]:has([data-chathead-tabs]) { --color-frame: #0a0a0a; }
- :root[data-skin="midnight"]:has([data-chathead-tabs]) :is([data-sidebar], [data-places-rail], [data-chathead-tabs]) { --color-panel: #0a0a0a; }
- :root:has([data-chathead-tabs="simple"]) [data-sidebar-top-row="simple"] { padding-top: 14px; }
- :root:has([data-chathead-tabs="simple"]) [data-sidebar-top-row="simple"] :is([data-rail-toggle], [data-sidebar-top-buttons] button.size-9) { width: 32px; height: 32px; }
+ :root[data-chat-tabs-frame="simple"] [data-sidebar] { border-image: linear-gradient(to bottom, transparent 60px, var(--color-edge) 60px) 1; }
+ :root[data-chat-tabs-frame="advanced"] [data-sidebar] { border-image: linear-gradient(to bottom, transparent 54px, var(--color-edge) 54px) 1; }
+ :root[data-skin="midnight"][data-chat-tabs-frame] { --color-frame: #0a0a0a; }
+ :root[data-skin="midnight"][data-chat-tabs-frame] :is([data-sidebar], [data-places-rail], [data-chathead-tabs]) { --color-panel: #0a0a0a; }
+ :root[data-chat-tabs-frame="simple"] [data-sidebar-top-row="simple"] { padding-top: 14px; }
+ :root[data-chat-tabs-frame="simple"] [data-sidebar-top-row="simple"] :is([data-rail-toggle], [data-sidebar-top-buttons] button.size-9) { width: 32px; height: 32px; }
[data-chathead-tabs] [data-chathead-row] { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; height: 100%; }
[data-chathead-tabs] [data-chathead-identity] { grid-column: 2; justify-self: stretch; align-self: stretch; -webkit-app-region: drag !important; }
[data-chathead-tabs] [data-chathead-identity] > :not([data-thread-tabs], [data-chathead-status]) { display: none; }