Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions packages/flint-mcp/ui/src/BoardView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { boardPng } from './board-export';
import { SelectionChips, chipText, impliedParts, partText } from './selection-chips';
import { ViewSwitch, useCellViews, useChartTable, type CellView } from './chart-table-view';
import { ActionButton, ThemeControl } from './controls';
import { usePageScroll } from './page-scroll';
import { LINK_LAYER, conditionUpdates, gestureParts, shownFields, type ChartLink, type LinkCondition, type LinkSpec } from 'flint-chart/interactive';

export interface BoardChart {
Expand Down Expand Up @@ -733,16 +734,8 @@ export function BoardView({ app, initial, hostContext }: { app: App; initial: Bo
document.documentElement.style.background = style.background;
document.body.style.background = style.background;
}, [style.background]);
// Inline, the card is sized to the board, so the page never scrolls. A scrollbar would appear while the
// host catches up with a new height, narrow the board, lay every chart out again, and change the height
// again. In a pane the host sets the height, so a taller board scrolls, with its gutter kept so the
// scrollbar never narrows it.
const paned = hostContext?.displayMode === 'fullscreen' || hostContext?.displayMode === 'pip';
useEffect(() => {
document.documentElement.style.overflow = paned ? 'hidden auto' : 'hidden';
document.documentElement.style.scrollbarGutter = paned ? 'stable' : '';
document.body.style.overflow = paned ? 'visible' : 'hidden';
}, [paned]);
usePageScroll(hostContext, paned);
const byId = useMemo(() => new Map(charts.map((chart) => [chart.id, chart])), [charts]);

const grid = draft ?? board.grid;
Expand Down
12 changes: 3 additions & 9 deletions packages/flint-mcp/ui/src/ChartView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { chartIconFor } from './chart-icons';
import { SelectionChips, chipText, impliedParts, partText, type SelectionChip } from './selection-chips';
import { VIEW_SWITCH_LABEL, ViewSwitchIcon, useChartTable, type CellView } from './chart-table-view';
import { tablePng } from './table-export';
import { usePageScroll } from './page-scroll';
import { conditionUpdates, selectionParts, type LinkCondition } from 'flint-chart/interactive';
import { chartContext, chartView, type ChartGesture } from '../../src/chart-context';
import {
Expand Down Expand Up @@ -611,16 +612,9 @@ export function ChartView({ app, shown, hostContext }: { app: App; shown: Board;
}, []);
useEffect(() => () => chartBoxObserver.current?.disconnect(), []);

// Inline, the card is sized to the chart, so the page never scrolls: a scrollbar shown while the host
// catches up with a new height would narrow the chart and lay it out again. In a pane the host sets
// the height and the chart is laid out for what the pane leaves it; the page scrolls only past that,
// with its gutter kept so the scrollbar never narrows the chart.
// In a pane the host sets the height and the chart is laid out for what the pane leaves it.
const paned = hostContext?.displayMode === 'fullscreen' || hostContext?.displayMode === 'pip';
useEffect(() => {
document.documentElement.style.overflow = paned ? 'hidden auto' : 'hidden';
document.documentElement.style.scrollbarGutter = paned ? 'stable' : '';
document.body.style.overflow = paned ? 'visible' : 'hidden';
}, [paned]);
usePageScroll(hostContext, paned);

// Re-seed when a new tool input arrives from the host.
useEffect(() => setCurrent(input), [input]);
Expand Down
49 changes: 49 additions & 0 deletions packages/flint-mcp/ui/src/page-scroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { useEffect, useState } from 'react';
import type { McpUiHostContext } from '@modelcontextprotocol/ext-apps';

/** The tallest the host will draw the card inline, if it says. */
function hostMaxHeight(hostContext?: McpUiHostContext): number | undefined {
const dims = hostContext?.containerDimensions as { height?: number; maxHeight?: number } | undefined;
const cap = dims?.maxHeight ?? dims?.height;
return typeof cap === 'number' && Number.isFinite(cap) && cap > 0 ? cap : undefined;
}

/** How far a scrolling page must shrink below the cap before it stops scrolling, so the gutter cannot flip it back and forth. */
const HYSTERESIS = 16;

/**
* Whether the page scrolls. Inline, the card is sized to the page, so it never does: a scrollbar would
* appear while the host catches up with a new height, narrow the content, lay it out again, and change
* the height again. A host that stops growing the card at a height (VS Code: 75% of its window) says so
* in `containerDimensions.maxHeight`; a page taller than that scrolls, or its bottom is cut off. In a pane
* the host sets the height, so the page scrolls past it. A scrolling page keeps its gutter so the
* scrollbar never narrows it.
*/
export function usePageScroll(hostContext: McpUiHostContext | undefined, paned: boolean): void {
const cap = paned ? undefined : hostMaxHeight(hostContext);
const [over, setOver] = useState(false);
useEffect(() => {
if (cap === undefined || typeof ResizeObserver === 'undefined') {
setOver(false);
return;
}
const read = () => {
const height = document.body.scrollHeight;
setOver((prev) => (prev ? height > cap - HYSTERESIS : height > cap + 1));
};
read();
const observer = new ResizeObserver(read);
observer.observe(document.body);
return () => observer.disconnect();
}, [cap]);

const scrolls = paned || over;
useEffect(() => {
document.documentElement.style.overflow = scrolls ? 'hidden auto' : 'hidden';
document.documentElement.style.scrollbarGutter = scrolls ? 'stable' : '';
document.body.style.overflow = scrolls ? 'visible' : 'hidden';
}, [scrolls]);
}
29 changes: 26 additions & 3 deletions site/src/routes/McpExperience.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ export function McpExperience() {
);
}

/** One of the introduction's two chat turns, without its heading: the talk deck shows them one to a slide. */
export function McpChatTurn({ kind }: { kind: 'chart' | 'board' }) {
const { t } = useTranslation();
useMcpStyles(SCOPE);
useHostPage();
return (
<>
<style>{experienceStyles}</style>
<SimpleStory>
{kind === 'chart' ? (
<ChatTurn ask={t('mcp.exp.simple.chartAsk')} reply={t('mcp.exp.simple.chartReply')} placeholder={430}
card={(onContext) => <ChartCard card={SIMPLE_CHART} onContext={onContext} />}
/>
) : (
<ChatTurn ask={t('mcp.exp.simple.boardAsk')} reply={t('mcp.exp.simple.boardReply')} placeholder={560}
card={(onContext) => <BoardCard board={SIMPLE_BOARD} onContext={onContext} />}
/>
)}
</SimpleStory>
</>
);
}

/** The step-by-step stories, one chart and then a board, each a chat drawn with the real MCP cards. */
export function McpExperienceStories() {
useMcpStyles(SCOPE);
Expand All @@ -62,11 +85,11 @@ export function McpExperienceStories() {
}

/** One ask and what the agent produces, in a chat window that grows to hold it. */
function SimpleStory({ heading, children }: { heading: string; children: ReactNode }) {
function SimpleStory({ heading, children }: { heading?: string; children: ReactNode }) {
const { t } = useTranslation();
return (
<section className="mcp-exp mcp-exp-simple" aria-label={heading}>
<h3 className="mcp-exp-heading">{heading}</h3>
<section className="mcp-exp mcp-exp-simple" aria-label={heading ?? t('mcp.exp.chatTitle')}>
{heading && <h3 className="mcp-exp-heading">{heading}</h3>}
<div className="mcp-exp-chat mcp-exp-chat-auto">
<div className="mcp-exp-chat-bar">
<span className="mcp-exp-dot" style={{ background: '#ec6a5e' }} />
Expand Down
2 changes: 1 addition & 1 deletion site/src/routes/mcp-experience-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const coffeeByCountry: ChartAssemblyInput = {
chart_spec: {
chartType: 'Line Chart',
encodings: { x: { field: 'Year' }, y: { field: 'Bags' }, color: { field: 'Country' } },
title: 'Asia is learning to love coffee',
title: "Asia's love for coffee has grown",
// Wider than tall: the card holds 480px of chart, and the legend takes two rows above the plot.
baseSize: { width: 520, height: 280 },
subtitle: 'Coffee consumed in each country, million 60-kg bags, 2005–2025',
Expand Down
2 changes: 1 addition & 1 deletion site/src/talk/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ const layoutStep = (): Spec => ({
color: {
aggregate: 'sum', field: 'newUsers', type: 'quantitative', title: 'Net new users',
scale: { scheme: 'redblue', domainMid: 0 },
legend: { format: '~s', orient: 'top', direction: 'horizontal', gradientLength: 180 },
legend: { format: '~s' },
},
},
});
Expand Down
75 changes: 75 additions & 0 deletions site/src/talk/parts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FlintChart } from 'flint-chart/react';
import { ScaleToFit } from '../components/ScaleToFit';
import { VegaLiteView } from '../components/VegaLiteView';
import { SKETCHES } from './sketches';
import { C, ellipse } from './sketch';
import minimaps from './specs/minimaps.json';

/** A live Flint chart laid out for its box: the compiler sizes it to the room, nothing spills. */
Expand All @@ -26,6 +27,80 @@ export function VegaLite({ spec }: { spec: unknown }) {
);
}

export type Flaw = { role: 'x-axis' | 'y-axis' | 'legend' | 'marks'; label: string };

/**
* A Vega-Lite chart with its problems circled in red, hand-drawn over the parts Vega names (an axis,
* the legend, the marks), each with a short label. The rings follow the chart as it scales.
*/
export function FlawedVegaLite({ spec, flaws }: { spec: unknown; flaws: Flaw[] }) {
// Each ring carries its number; the labels are listed as text beside the chart (see FlawList).
const host = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const el = host.current, canvas = canvasRef.current;
if (!el || !canvas) return;
let ready = false;
const draw = () => {
const svg = el.querySelector('svg.marks, svg');
const w = el.clientWidth, h = el.clientHeight;
if (!ready || !svg || !w || !h) return;
const hb = el.getBoundingClientRect(), k = hb.width / w || 1;
const dpr = Math.max(2, window.devicePixelRatio * 1.5);
canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr);
const ctx = canvas.getContext('2d')!;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, w, h);
const axes = [...svg.querySelectorAll('.role-axis')].map((g) => g.getBoundingClientRect());
const pick = (role: Flaw['role']): DOMRect | undefined => {
if (role === 'legend') return svg.querySelector('.role-legend')?.getBoundingClientRect();
if (role === 'marks') return svg.querySelector('.role-mark')?.getBoundingClientRect();
// The x axis is the wide one, the y axis the tall one.
const sorted = [...axes].sort((a, b) => (role === 'x-axis' ? b.width / b.height - a.width / a.height : b.height / b.width - a.height / a.width));
return sorted[0];
};
flaws.forEach((flaw, i) => {
const r = pick(flaw.role);
if (!r) return;
const x = (r.left - hb.left) / k, y = (r.top - hb.top) / k, bw = r.width / k, bh = r.height / k;
const cx = x + bw / 2, cy = y + bh / 2;
ellipse(ctx, cx, cy, bw / 2 + 12, bh / 2 + 10, { w: 3, color: C.red, seed: 3000 + i * 7 });
// A numbered badge on the ring's upper left; the slide lists what each number means.
const rx = bw / 2 + 12, ry = bh / 2 + 10;
const bx = Math.min(Math.max(cx - rx * 0.72, 16), w - 16), by = Math.min(Math.max(cy - ry * 0.72, 16), h - 16);
ctx.save();
ctx.fillStyle = C.red; ctx.beginPath(); ctx.arc(bx, by, 15, 0, Math.PI * 2); ctx.fill();
ctx.fillStyle = '#fff'; ctx.font = "700 18px Inter, system-ui, sans-serif"; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.fillText(String(i + 1), bx, by + 1);
ctx.restore();
});
canvas.dataset.drawn = 'true';
};
void document.fonts.load("700 20px 'Kalam'").then(() => { ready = true; draw(); });
const ro = new ResizeObserver(draw);
ro.observe(el);
const mo = new MutationObserver(() => requestAnimationFrame(draw));
mo.observe(el, { childList: true, subtree: true });
return () => { ro.disconnect(); mo.disconnect(); };
}, [flaws]);
return (
<div ref={host} className="talk-fit" style={{ position: 'relative' }}>
<ScaleToFit height={0} fill maxScale={1.8}>
<VegaLiteView spec={spec} renderer="svg" />
</ScaleToFit>
<canvas ref={canvasRef} className="talk-sketch" style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }} />
</div>
);
}

/** The numbered problems a FlawedVegaLite circles, as readable text. */
export function FlawList({ flaws }: { flaws: Flaw[] }) {
return (
<ol className="talk-flaws">
{flaws.map((flaw, i) => <li key={flaw.label}><span>{i + 1}</span>{flaw.label}</li>)}
</ol>
);
}

/** A site demo laid out at `width`, scaled to fit its box. Flint's pointer handling follows the scale. */
export function Demo({ width, children }: { width: number; children: ReactNode }) {
return (
Expand Down
4 changes: 3 additions & 1 deletion site/src/talk/sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,11 @@ export function card(ctx: Ctx, x: number, y: number, w: number, h: number, o: {
rect(ctx, x, y, w, h, { w: o.w ?? 3, color: o.color ?? C.ink, seed: o.seed || 0 });
}

export type FontKind = 'hand' | 'handBold' | 'mono';
export type FontKind = 'hand' | 'handBold' | 'mono' | 'sans';
export function font(size: number, kind: FontKind = 'hand'): string {
if (kind === 'mono') return `500 ${size}px ui-monospace, 'SF Mono', Menlo, Consolas, monospace`;
// The deck's own type, for sentences that sit in a sketch but should read as text.
if (kind === 'sans') return `400 ${size}px 'Inter Variable', Inter, system-ui, sans-serif`;
return `${kind === 'handBold' ? 700 : 400} ${size}px 'Kalam'`;
}

Expand Down
Loading
Loading