Keep ref chips inside the markdown paragraph

A chip in bot text is rendered in a real <p>, and five of them put a div beside their label: twui's Modal wraps its target in one, and the secret chip returned a Row. React logged "p cannot contain a nested div" for each.

The new RefChipModal wraps the trigger in a span and mounts the modal only while open, so the card still opens exactly as before.
This commit is contained in:
2026-11-19 10:41:49 +01:00
parent 7edb5432c2
commit fca98d45a8
8 changed files with 279 additions and 27 deletions
@@ -1,6 +1,5 @@
import { useState } from "react";
import { twMerge } from "tailwind-merge";
import Modal from "@moduletrace/twui/elements/Modal";
import Button from "@moduletrace/twui/layout/Button";
import H2 from "@moduletrace/twui/layout/H2";
import Span from "@moduletrace/twui/layout/Span";
@@ -8,6 +7,7 @@ import Stack from "@moduletrace/twui/layout/Stack";
import { MessageCircleQuestion } from "lucide-react";
import useBotQuestion from "../(hooks)/use-bot-question";
import BotQuestionCard from "./bot-question-card";
import RefChipModal from "./ref-chip-modal";
type Props = {
question_id: number;
@@ -42,14 +42,15 @@ export default function BotQuestionRefChip({ question_id }: Props) {
const pending = view.status == "pending";
return (
<Modal
target={
<RefChipModal
trigger={
<Button
type="button"
variant={pending ? undefined : "outlined"}
color={pending ? undefined : "gray"}
size="smaller"
title={pending ? `Answer ${name}` : `${name}: ${status}`}
onClick={() => setOpen(true)}
className={twMerge(
"inline-flex h-6 max-w-full gap-1.5! flex-nowrap align-middle px-2! text-[13px] font-bold whitespace-nowrap",
!pending && "text-ink",
@@ -60,9 +61,8 @@ export default function BotQuestionRefChip({ question_id }: Props) {
<Span className="min-w-0 truncate">{`${name} · ${status}`}</Span>
</Button>
}
targetWrapperProps={{ className: "inline-flex max-w-full align-middle" }}
open={open}
setOpen={setOpen}
onClose={() => setOpen(false)}
className="max-w-xl bg-paper p-6"
>
<Stack className="w-full gap-1! items-stretch">
@@ -79,6 +79,6 @@ export default function BotQuestionRefChip({ question_id }: Props) {
<Stack className="mt-2 max-h-[65vh] w-full gap-0! items-stretch overflow-y-auto">
<BotQuestionCard view={view} onDecided={setView} onDone={() => setOpen(false)} />
</Stack>
</Modal>
</RefChipModal>
);
}
@@ -1,6 +1,5 @@
import { useState } from "react";
import { twMerge } from "tailwind-merge";
import Modal from "@moduletrace/twui/elements/Modal";
import Button from "@moduletrace/twui/layout/Button";
import H2 from "@moduletrace/twui/layout/H2";
import Span from "@moduletrace/twui/layout/Span";
@@ -10,6 +9,7 @@ import useHookProposal from "../(hooks)/use-hook-proposal";
import hookProposalTitle from "../(utils)/hook-proposal-title";
import proposalStatusLabel from "../(utils)/proposal-status-label";
import HookProposalCard from "./hook-proposal-card";
import RefChipModal from "./ref-chip-modal";
type Props = {
proposal_id: number;
@@ -36,14 +36,15 @@ export default function HookProposalRefChip({ proposal_id }: Props) {
const status = proposalStatusLabel({ view });
return (
<Modal
target={
<RefChipModal
trigger={
<Button
type="button"
variant="outlined"
color="gray"
size="smaller"
title={`Review: ${title}`}
onClick={() => setOpen(true)}
className={twMerge(
"inline-flex h-6 max-w-full gap-1.5! flex-nowrap align-middle px-2! text-[13px] font-bold text-ink whitespace-nowrap",
"[&_.twui-button-content-wrapper]:min-w-0",
@@ -53,9 +54,8 @@ export default function HookProposalRefChip({ proposal_id }: Props) {
<Span className="min-w-0 truncate">{`${title} · ${status}`}</Span>
</Button>
}
targetWrapperProps={{ className: "inline-flex max-w-full align-middle" }}
open={open}
setOpen={setOpen}
onClose={() => setOpen(false)}
className="max-w-xl bg-paper p-6"
>
<Stack className="w-full gap-1! items-stretch">
@@ -68,6 +68,6 @@ export default function HookProposalRefChip({ proposal_id }: Props) {
<Stack className="mt-2 max-h-[65vh] w-full gap-0! items-stretch overflow-y-auto">
<HookProposalCard view={view} onDecided={setView} onDone={() => setOpen(false)} />
</Stack>
</Modal>
</RefChipModal>
);
}
@@ -1,6 +1,5 @@
import { useState } from "react";
import { twMerge } from "tailwind-merge";
import Modal from "@moduletrace/twui/elements/Modal";
import Button from "@moduletrace/twui/layout/Button";
import H2 from "@moduletrace/twui/layout/H2";
import Span from "@moduletrace/twui/layout/Span";
@@ -11,6 +10,7 @@ import useExtensionProposal from "../(hooks)/use-extension-proposal";
import proposalStatusLabel from "../(utils)/proposal-status-label";
import proposalTitle from "../(utils)/proposal-title";
import ProposalCard from "./proposal-card";
import RefChipModal from "./ref-chip-modal";
type Props = {
proposal_id: number;
@@ -40,14 +40,15 @@ export default function ProposalRefChip({ proposal_id }: Props) {
const status = proposalStatusLabel({ view });
return (
<Modal
target={
<RefChipModal
trigger={
<Button
type="button"
variant="outlined"
color="gray"
size="smaller"
title={`Review: ${title}`}
onClick={() => setOpen(true)}
className={twMerge(
"inline-flex h-6 max-w-full gap-1.5! flex-nowrap align-middle px-2! text-[13px] font-bold text-ink whitespace-nowrap",
"[&_.twui-button-content-wrapper]:min-w-0",
@@ -57,9 +58,8 @@ export default function ProposalRefChip({ proposal_id }: Props) {
<Span className="min-w-0 truncate">{`${title} · ${status}`}</Span>
</Button>
}
targetWrapperProps={{ className: "inline-flex max-w-full align-middle" }}
open={open}
setOpen={setOpen}
onClose={() => setOpen(false)}
className="max-w-xl bg-paper p-6"
>
<Stack className="w-full gap-1! items-stretch">
@@ -72,6 +72,6 @@ export default function ProposalRefChip({ proposal_id }: Props) {
<Stack className="mt-2 max-h-[65vh] w-full gap-0! items-stretch overflow-y-auto">
<ProposalCard view={view} onDecided={decided} />
</Stack>
</Modal>
</RefChipModal>
);
}
@@ -0,0 +1,45 @@
import { describe, expect, test } from "bun:test";
import { renderToString } from "react-dom/server";
import Button from "@moduletrace/twui/layout/Button";
import P from "@moduletrace/twui/layout/P";
import Span from "@moduletrace/twui/layout/Span";
import RefChipModal from "./ref-chip-modal";
/**
* A ref chip sits in a markdown paragraph, which takes phrasing content
* only. twui's own `Modal` target wrapper is a div, so the chip used to
* put a div in the paragraph and React logged "p cannot contain a nested
* div" for every question or proposal chip on the page.
*/
describe("RefChipModal", () => {
test("wraps its trigger in a span, with no block element beside it", () => {
const html = renderToString(
<P noMargin>
<RefChipModal trigger={<Button type="button" title="Review">Review</Button>} open={false} onClose={() => {}}>
<Span>Card</Span>
</RefChipModal>
</P>,
);
// twui's own Button fills itself with a div, valid inside a button
// (React only warns about a p in button scope), so the paragraph
// is broken only by a div before the button: twui's Modal used to
// wrap its target in one.
const first_button = html.indexOf("<button");
expect(html).toContain('<span class="twui-span inline-flex max-w-full align-middle">');
expect(html).toContain("Review");
expect(first_button).toBeGreaterThan(-1);
expect(html.slice(0, first_button)).not.toContain("<div");
});
test("mounts the card only while open", () => {
const html = renderToString(
<RefChipModal trigger={<Button type="button" title="Review">Review</Button>} open={false} onClose={() => {}}>
<Span>Card</Span>
</RefChipModal>,
);
expect(html).not.toContain("Card");
});
});
@@ -0,0 +1,38 @@
import type { ReactNode } from "react";
import Modal from "@moduletrace/twui/elements/Modal";
import Span from "@moduletrace/twui/layout/Span";
type Props = {
trigger: ReactNode;
open: boolean;
onClose: () => void;
className?: string;
children: ReactNode;
};
/**
* A ref chip's trigger and the card it opens (`[question:ID]`,
* `[proposal:ID]`, ...). The chip sits in a markdown paragraph, which takes
* phrasing content only: the trigger is a span around the caller's button,
* and the card is mounted only while open, since twui's own `target`
* wrapper is a div and a div inside the paragraph is invalid (React says
* so in the console).
*/
export default function RefChipModal({ trigger, open, onClose, className, children }: Props) {
return (
<>
<Span className="inline-flex max-w-full align-middle">{trigger}</Span>
{open ? (
<Modal
open
setOpen={(next_open) => {
if (!next_open) onClose();
}}
className={className}
>
{children}
</Modal>
) : null}
</>
);
}
@@ -0,0 +1,170 @@
import { afterAll, describe, expect, mock, test } from "bun:test";
import { renderToString } from "react-dom/server";
import P from "@moduletrace/twui/layout/P";
import type {
BotQuestionView,
ExtensionProposalView,
HookProposalView,
SecretRefView,
SecretRequestView,
} from "@/src/types";
const question: BotQuestionView = {
id: 1,
title: "Deploy now?",
questions: [{ question: "When?", kind: "single", options: ["Now", "Later"], allow_other: false }],
answers: null,
status: "pending",
bot_id: 2,
bot_name: "Ada",
};
const secret_request: SecretRequestView = {
id: 3,
key: "GITHUB_TOKEN",
description: "A token to push.",
reason: "To push the branch.",
status: "pending",
bot_id: 2,
bot_name: "Ada",
env_id: null,
};
const proposal: ExtensionProposalView = {
id: 4,
kind: "mcp",
extension_id: null,
name: "GitHub",
status: "pending",
expired: false,
expires_at: 0,
outcome: "",
result_id: null,
host: null,
envs: [],
details: [],
research: "",
sources: [],
};
const hook_proposal: HookProposalView = {
id: 5,
action: "create",
hook_id: null,
title: "GitHub pushes",
bot_id: 2,
bot_name: "Ada",
status: "pending",
expired: false,
expires_at: 0,
outcome: "",
result_id: null,
makes_url: true,
details: [],
reason: "So it hears about pushes.",
};
const secret: SecretRefView = {
id: 6,
key: "NPM_TOKEN",
description: "A token to publish.",
can_reveal: true,
};
mock.module("../(hooks)/use-bot-question", () => ({
default: () => ({ view: question, loading: false, setView: () => {} }),
}));
mock.module("../(hooks)/use-secret-request", () => ({
default: () => ({ view: secret_request, loading: false, setView: () => {} }),
}));
mock.module("../(hooks)/use-extension-proposal", () => ({
default: () => ({ view: proposal, loading: false, setView: () => {} }),
}));
mock.module("../(hooks)/use-hook-proposal", () => ({
default: () => ({ view: hook_proposal, loading: false, setView: () => {} }),
}));
mock.module("../(hooks)/use-secret-ref", () => ({
default: () => ({ view: secret, loading: false }),
}));
const { default: BotQuestionRefChip } = await import("./bot-question-ref-chip");
const { default: SecretRequestRefChip } = await import("./secret-request-ref-chip");
const { default: ProposalRefChip } = await import("./proposal-ref-chip");
const { default: HookProposalRefChip } = await import("./hook-proposal-ref-chip");
const { default: SecretRefChip } = await import("./secret-ref-chip");
/**
* twui's Button fills itself with a div, valid inside a button (React only
* warns about a p in button scope), so what a paragraph may not hold is a
* div outside one: twui's Modal used to wrap each chip's trigger in one.
*/
function outsideButtons(html: string) {
return html.replaceAll(/<button[\s\S]*?<\/button>/g, "");
}
afterAll(() => {
mock.restore();
});
/**
* Message markdown renders bot text in real paragraphs, so every ref chip
* that lands in it must be phrasing content: a block element beside its
* text was React's "p cannot contain a nested div" warning, once per chip
* on the page.
*/
describe("ref chips in a markdown paragraph", () => {
test("a question chip", () => {
const html = renderToString(
<P noMargin>
<BotQuestionRefChip question_id={question.id} />
</P>,
);
expect(html).toContain("Deploy now?");
expect(outsideButtons(html)).not.toContain("<div");
});
test("a secret request chip", () => {
const html = renderToString(
<P noMargin>
<SecretRequestRefChip request_id={secret_request.id} />
</P>,
);
expect(html).toContain("GITHUB_TOKEN");
expect(outsideButtons(html)).not.toContain("<div");
});
test("an extension proposal chip", () => {
const html = renderToString(
<P noMargin>
<ProposalRefChip proposal_id={proposal.id} />
</P>,
);
expect(html).toContain("Add MCP server GitHub");
expect(outsideButtons(html)).not.toContain("<div");
});
test("a hook proposal chip", () => {
const html = renderToString(
<P noMargin>
<HookProposalRefChip proposal_id={hook_proposal.id} />
</P>,
);
expect(html).toContain("Add hook GitHub pushes");
expect(outsideButtons(html)).not.toContain("<div");
});
test("a secret chip", () => {
const html = renderToString(
<P noMargin>
<SecretRefChip env_id={secret.id} />
</P>,
);
expect(html).toContain("NPM_TOKEN");
expect(outsideButtons(html)).not.toContain("<div");
});
});
@@ -1,5 +1,4 @@
import { useState } from "react";
import Row from "@moduletrace/twui/layout/Row";
import Span from "@moduletrace/twui/layout/Span";
import { Check, Copy, Eye, EyeOff, KeyRound } from "lucide-react";
import revealSecret from "@/src/functions/frontend/reveal-secret";
@@ -58,7 +57,7 @@ export default function SecretRefChip({ env_id }: Props) {
};
return (
<Row
<Span
title={view.description || view.key}
className="inline-flex h-6 max-w-full gap-1! flex-nowrap items-center align-middle rounded-default border border-hairline px-2 text-[13px] text-ink"
>
@@ -76,6 +75,6 @@ export default function SecretRefChip({ env_id }: Props) {
</>
) : null}
{error ? <Span className="text-[12px] text-ink-secondary whitespace-nowrap">{error}</Span> : null}
</Row>
</Span>
);
}
@@ -1,6 +1,5 @@
import { useState } from "react";
import { twMerge } from "tailwind-merge";
import Modal from "@moduletrace/twui/elements/Modal";
import Button from "@moduletrace/twui/layout/Button";
import H2 from "@moduletrace/twui/layout/H2";
import Span from "@moduletrace/twui/layout/Span";
@@ -8,6 +7,7 @@ import Stack from "@moduletrace/twui/layout/Stack";
import { KeyRound } from "lucide-react";
import useSecretRequest from "../(hooks)/use-secret-request";
import SecretRequestCard from "./secret-request-card";
import RefChipModal from "./ref-chip-modal";
type Props = {
request_id: number;
@@ -39,14 +39,15 @@ export default function SecretRequestRefChip({ request_id }: Props) {
const status = StatusLabels[view.status];
return (
<Modal
target={
<RefChipModal
trigger={
<Button
type="button"
variant="outlined"
color="gray"
size="smaller"
title={view.status == "pending" ? `Enter ${view.key}` : `${view.key}: ${status}`}
onClick={() => setOpen(true)}
className={twMerge(
"inline-flex h-6 max-w-full gap-1.5! flex-nowrap align-middle px-2! text-[13px] font-bold text-ink whitespace-nowrap",
"[&_.twui-button-content-wrapper]:min-w-0",
@@ -56,9 +57,8 @@ export default function SecretRequestRefChip({ request_id }: Props) {
<Span className="min-w-0 truncate">{`${view.key} · ${status}`}</Span>
</Button>
}
targetWrapperProps={{ className: "inline-flex max-w-full align-middle" }}
open={open}
setOpen={setOpen}
onClose={() => setOpen(false)}
className="max-w-xl bg-paper p-6"
>
<Stack className="w-full gap-1! items-stretch">
@@ -71,6 +71,6 @@ export default function SecretRequestRefChip({ request_id }: Props) {
<Stack className="mt-2 max-h-[65vh] w-full gap-0! items-stretch overflow-y-auto">
<SecretRequestCard view={view} onDecided={setView} onDone={() => setOpen(false)} />
</Stack>
</Modal>
</RefChipModal>
);
}