8000 fix(prototype): support precise overlay placement by mmaazrana · Pull Request #700 · doop-design/doop · GitHub
[go: up one dir, main page]

Skip to content
Open
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
28 changes: 18 additions & 10 deletions .context/specs/prototyping.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ Interactions live in the frame HTML as data attributes on the element that trigg
persist, sync, version and export with the frame, and agents author them with the ordinary HTML
tools; there is no interaction table or protocol message.

| attribute | values |
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `data-doop-on` | `click`, `hover` (pointer enters), `press` (pointer down; release reverts), `delay:<ms>` (after the frame is shown), `key:<Key>` |
| `data-doop-action` | `navigate:<frameId>`, `back`, `overlay:<frameId>`, `close-overlay`, `scroll-to:<css selector>`, `url:<http(s) url>` (new tab) |
| `data-doop-transition` | `instant` (default), `dissolve`, `slide-left`, `slide-right`, `slide-up`, `slide-down`, `push-left`, `push-right` |
| `data-doop-duration` | milliseconds, default 300, capped at 5000 |
| attribute | values |
| -------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `data-doop-on` | `click`, `hover` (pointer enters), `press` (pointer down; release reverts), `delay:<ms>` (after the frame is shown), `key:<Key>` |
| `data-doop-action` | `navigate:<frameId>`, `back`, `overlay:<frameId>`, `close-overlay`, `scroll-to:<css selector>`, `url:<http(s) url>` (new tab) |
| `data-doop-transition` | `instant` (default), `dissolve`, `slide-left`, `slide-right`, `slide-up`, `slide-down`, `push-left`, `push-right` |
| `data-doop-overlay-x`, `data-doop-overlay-y` | pair of finite decimal coordinates in design pixels, without CSS units, from the opening frame’s top-left 8000 |
| `data-doop-duration` | milliseconds, default 300, capped at 5000 |

`key:` takes a `KeyboardEvent.key` (`ArrowRight`, `Escape`, `Enter`; `Space` means the space bar).
Several interactions on one element use the JSON form,
`data-doop-interactions='[{"on":"click","action":"navigate:abc","transition":"dissolve"}]'`
(`on`, `action`, optional `transition` and `duration`). An element may carry both forms; the
single form is read first. `parseInteractions` drops every invalid entry rather than repairing it:
(`on`, `action`, optional `transition`, `duration`, and the numeric pair `overlayX`, `overlayY`
for overlays). An element may carry both forms; the single form is read first. `parseInteractions` drops every invalid entry rather than repairing it:
an unknown trigger, action or frame-id shape, a `url:` that is not http or https. Unknown
transitions and bad durations fall back to the defaults.

Expand Down Expand Up @@ -61,7 +62,11 @@ shrunk to fit but never enlarged past its design size, and stays live (each scre
target beside the current screen, waits for it to render, then animates the two wrappers with
the Web Animations API (`transitionFrames`). `back` plays the arriving transition in reverse.
A navigation during a running animation snaps it to its end.
- Overlays open centered over a scrim at the base screen's scale (fit to 94% of the viewport).
- Overlays open over a scrim and fit within 94% of the stage. A centered overlay uses the base
screen's scale. A custom overlay uses the opening screen's scale and takes its X and Y from
the top-left of the opening screen’s unscrolled document. The player follows the trigger's movement during scroll,
including fixed and sticky headers, overflow panels and nested submenus. It keeps the whole overlay within the stage. An omitted
or invalid coordinate pair opens the overlay centered. `get_prototype` flags a malformed pair.
The scrim or a `close-overlay` closes the top one; navigating closes them all. Only the top
screen is in prototype mode: a covered screen or overlay is switched off (its `delay:` timers
stop, and restart when it is uncovered), the player drops any action it still sends, and a
Expand Down Expand Up @@ -94,7 +99,10 @@ have room for Comment only in the strip; Prototype mode toggles from the Canvas

- **Interactions** (Prototype tab): the picked element's interactions, each with trigger, action,
target (a frame picker over every frame on the canvas, a selector or a URL), transition and
duration. Each change rewrites the element's attributes through `saveFrameHtml`, one undo step.
duration. Open overlay also offers Center and Custom placement. Dragging an overlay coordinate,
delay or duration previews the value in the field and saves once on release. Escape cancels the drag. A concurrent edit to
the same element's interactions takes precedence over the pending gesture. Each saved change
rewrites the element's attributes through `saveFrameHtml` as one undo step.
Locked layers and frames are read-only; script-created elements, which have no markup, say so.
The Flow row makes the frame its page's start.
- **Connectors** (`PrototypeOverlay`, world space): every live frame on the page reports its
Expand Down
Binary file added .github/assets/prototype-overlay-placement.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/prototype-overlay-scroll.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ action (go to a frame, open one as an overlay, go back, close the overlay, scrol
with an optional transition: dissolve, slide or push. Connectors on the canvas show where every
element leads, and a **Start** flag marks the frame each page's flow begins on.

For a dropdown, make a frame the size of the menu. On the button that opens it, choose **Open
overlay**, pick the menu frame, and set **Position** to **Custom**. Enter X and Y in pixels from
the opening frame's top-left corner. For example, a button at X 24 and Y 16 with a height of 40
takes a menu at X 24 and Y 56. **Center** is the default for dialogs.

<p align="center">
<img src=".github/assets/prototype-canvas.jpg" alt="A travel app prototype in Prototype mode: four iPhone screens with liquid-glass panels over photos, blue connectors from a trending card to the destination screen, from Book stay to the booking sheet overlay and from Confirm and pay to the confirmation, and a Start flag on the first screen" width="100%">
</p>
Expand All @@ -308,7 +313,7 @@ export with the frame, and agents write them with the same tools they design wit
<button data-doop-on="click" data-doop-action="navigate:<frameId>" data-doop-transition="push-left">Book stay</button>
```

Agents check their wiring with `get_prototype`, which flags links to missing frames, and set
Agents check their wiring with `get_prototype`, which flags missing frames and invalid overlay coordinates, and set
the start with `set_prototype_start`. Both the MCP server and the built-in Doop Agent have them.
The full attribute reference is in [`.context/specs/prototyping.md`](.context/specs/prototyping.md).

Expand Down
7 changes: 6 additions & 1 deletion server/guide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ only run in the player):
element, e.g. <body>) | key:<Key> (KeyboardEvent.key: ArrowRight, Escape, Enter, Space).
- data-doop-action: navigate:<frameId> | back | overlay:<frameId> (opens that frame as a modal
over a scrim) | close-overlay | scroll-to:<css selector> | url:<https url> (new tab).
- For a dropdown, size its frame to the menu and set both data-doop-overlay-x and
data-doop-overlay-y on the trigger. They place the menu's top-left in design pixels from
the opening document's top-left before scrolling (e.g. x 24, y 60). The menu follows its trigger as the frame or an overflow panel scrolls.
Use plain numbers without CSS units. Omit both for a centered modal.
In the JSON form, use numeric overlayX and overlayY on the overlay interaction.
- data-doop-transition: instant (default) | dissolve | slide-left | slide-right | slide-up |
slide-down | push-left | push-right; data-doop-duration in ms (default 300).
- Several interactions on one element: data-doop-interactions='[{"on":"click","action":
Expand All @@ -159,7 +164,7 @@ only run in the player):
handlers between frames.
- Use real frame ids from the canvas, never invented ones. Size overlay frames to the modal itself, not the page.
- set_prototype_start picks the frame the flow starts on; get_prototype lists every interaction
and flags targets that do not exist — call it after wiring and fix what it flags. The player
and flags missing targets or invalid overlay coordinates — call it after wiring and fix what it flags. The player
lives at /c/<canvasId>/proto (append ?frame=<id> to open on a specific screen).`

/** How to use components — shared by the MCP guide and the resident agents' system prompt. */
Expand Down
2 changes: 1 addition & 1 deletion server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ export function buildMcpServer(owner?: string, ownerId?: string, clientName?: st
{
title: 'Read a canvas prototype',
description:
'List the clickable-prototype wiring of a canvas: each page with its flow start frame, and every interaction with its trigger, action and target. Interactions are attributes on the triggering element in frame HTML, written with edit_frame_html: data-doop-on (click | hover | press | delay:<ms> | key:<Key>) and data-doop-action (navigate:<frameId> | back | overlay:<frameId> | close-overlay | scroll-to:<selector> | url:<https url>), optionally data-doop-transition and data-doop-duration; the guide\'s "Prototypes" section has the full syntax. Interactions whose target frame does not exist are flagged with a problem; fix them with edit_frame_html.',
'List the clickable-prototype wiring of a canvas: each page with its flow start frame, and every interaction with its trigger, action and target. Interactions are attributes on the triggering element in frame HTML, written with edit_frame_html: data-doop-on (click | hover | press | delay:<ms> | key:<Key>) and data-doop-action (navigate:<frameId> | back | overlay:<frameId> | close-overlay | scroll-to:<selector> | url:<https url>), optionally data-doop-transition and data-doop-duration, plus data-doop-overlay-x and data-doop-overlay-y for a positioned overlay; the guide\'s "Prototypes" section has the full syntax. Missing target frames and invalid overlay coordinates are flagged with a problem; fix them with edit_frame_html.',
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
inputSchema: { canvas_id: z.string(), agent_name: agentName },
},
Expand Down
1 change: 1 addition & 0 deletions server/prototypeTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function prototypeReport(canvas: Canvas) {
...(targetsFrame(link.interaction) ? { targetName: names.get(link.interaction.target) ?? null } : {}),
transition: link.interaction.transition,
duration: link.interaction.duration,
...(link.interaction.overlayPosition ? { overlayPosition: link.interaction.overlayPosition } : {}),
...(link.problem ? { problem: link.problem } : {}),
})),
}
Expand Down
65 changes: 52 additions & 13 deletions shared/prototype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export const PROTO_ATTRS = {
action: 'data-doop-action',
transition: 'data-doop-transition',
duration: 'data-doop-duration',
overlayX: 'data-doop-overlay-x',
overlayY: 'data-doop-overlay-y',
interactions: 'data-doop-interactions',
} as const

Expand All @@ -56,6 +58,8 @@ export interface Interaction {
action: ProtoAction
/** a frame id (navigate, overlay), a CSS selector (scroll-to) or an http(s) URL (url) */
target?: string
/** Top-left in design pixels. Omitted for center; null for an invalid pair that plays centered. */
overlayPosition?: { x: number; y: number } | null
transition: ProtoTransition
duration: number
}
Expand All @@ -65,10 +69,25 @@ export interface InteractionAttrs {
on?: string | null
action?: string | null
transition?: string | null
overlayX?: string | number | null
overlayY?: string | number | null
duration?: string | null
interactions?: string | null
}

/** DOM readers and the server's HTML scanner use the same attribute names. */
export function interactionsFromAttributes(get: (name: string) => string | null | undefined): Interaction[] {
return parseInteractions({
on: get(PROTO_ATTRS.on),
action: get(PROTO_ATTRS.action),
transition: get(PROTO_ATTRS.transition),
duration: get(PROTO_ATTRS.duration),
overlayX: get(PROTO_ATTRS.overlayX) ?? undefined,
overlayY: get(PROTO_ATTRS.overlayY) ?? undefined,
interactions: get(PROTO_ATTRS.interactions),
})
}

/**
* Parse an element's interaction attributes: the single-attribute form first,
* then every entry of the JSON form. Invalid entries are dropped, never
Expand All @@ -90,7 +109,13 @@ export function parseInteractions(attrs: InteractionAttrs): Interaction[] {
function str(v: unknown): string | null {
return typeof v === 'string' ? v.trim() : typeof v === 'number' ? String(v) : null
}
function one(on: unknown, action: unknown, transition: unknown, duration: unknown): Interaction | null {
function coordinate(value: unknown): number {
if (typeof value === 'number') return value
if (typeof value !== 'string' || !/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(value.trim())) return NaN
return Number(value)
}
function one(entry: Partial<Record<keyof InteractionAttrs, unknown>>): Interaction | null {
const { on, action, transition, duration, overlayX, overlayY } = entry
const trigger = str(on)
const act = str(action)
if (!trigger || !act) return null
Expand Down Expand Up @@ -125,6 +150,12 @@ export function parseInteractions(attrs: InteractionAttrs): Interaction[] {
out.target = url.href
} else return null

if (out.action === 'overlay' && (overlayX !== undefined || overlayY !== undefined)) {
const x = coordinate(overlayX)
const y = coordinate(overlayY)
out.overlayPosition = Number.isFinite(x) && Number.isFinite(y) ? { x, y } : null
}

const t = str(transition)
if (t && transitions.indexOf(t) !== -1) out.transition = t as ProtoTransition
const d = str(duration)
Expand All @@ -134,7 +165,7 @@ export function parseInteractions(attrs: InteractionAttrs): Interaction[] {

const list: Interaction[] = []
if (attrs.on != null || attrs.action != null) {
const single = one(attrs.on, attrs.action, attrs.transition, attrs.duration)
const single = one(attrs)
if (single) list.push(single)
}
if (attrs.interactions) {
Expand All @@ -149,7 +180,7 @@ export function parseInteractions(attrs: InteractionAttrs): Interaction[] {
if (list.length >= maxEntries) break
if (!entry || typeof entry !== 'object') continue
const e = entry as Record<string, unknown>
const parsedEntry = one(e.on, e.action, e.transition, e.duration)
const parsedEntry = one(e)
if (parsedEntry) list.push(parsedEntry)
}
}
Expand Down Expand Up @@ -181,22 +212,30 @@ export function interactionAttributes(list: Interaction[]): Record<string, strin
[PROTO_ATTRS.action]: null,
[PROTO_ATTRS.transition]: null,
[PROTO_ATTRS.duration]: null,
[PROTO_ATTRS.overlayX]: null,
[PROTO_ATTRS.overlayY]: null,
[PROTO_ATTRS.interactions]: null,
}
const [only] = list
if (!only) return none
if (list.length === 1) {
const position = only.action === 'overlay' ? only.overlayPosition : undefined
return {
...none,
[PROTO_ATTRS.on]: triggerString(only),
[PROTO_ATTRS.action]: actionString(only),
[PROTO_ATTRS.overlayX]: position ? String(position.x) : null,
[PROTO_ATTRS.overlayY]: position ? String(position.y) : null,
[PROTO_ATTRS.transition]: only.transition === 'instant' ? null : only.transition,
[PROTO_ATTRS.duration]: only.duration === DEFAULT_PROTO_DURATION ? null : String(only.duration),
}
}
const entries = list.map((i) => ({
on: triggerString(i),
action: actionString(i),
...(i.action === 'overlay' && i.overlayPosition
? { overlayX: i.overlayPosition.x, overlayY: i.overlayPosition.y }
: {}),
...(i.transition === 'instant' ? {} : { transition: i.transition }),
...(i.duration === DEFAULT_PROTO_DURATION ? {} : { duration: i.duration }),
}))
Expand Down Expand Up @@ -304,13 +343,7 @@ export function scanInteractions(html: string): ScannedInteractive[] {

function collect(tag: string, attrs: Map<string, string>, text: string) {
if (!attrs.has(PROTO_ATTRS.on) && !attrs.has(PROTO_ATTRS.interactions) && !attrs.has(PROTO_ATTRS.action)) return
const interactions = parseInteractions({
on: attrs.get(PROTO_ATTRS.on),
action: attrs.get(PROTO_ATTRS.action),
transition: attrs.get(PROTO_ATTRS.transition),
duration: attrs.get(PROTO_ATTRS.duration),
interactions: attrs.get(PROTO_ATTRS.interactions),
})
const interactions = interactionsFromAttributes((name) => attrs.get(name))
const id = attrs.get('id') || undefined
const cls = attrs.get('class')?.trim().split(/\s+/)[0]
const label = id ? `#${id}` : text ? `"${text}"` : cls ? `.${cls}` : tag
Expand Down Expand Up @@ -339,7 +372,7 @@ export interface PrototypeLink {
fromFrameId: string
element: string
interaction: Interaction
/** set when the target frame does not exist on this canvas */
/** Authoring problems with the target frame or overlay coordinates. */
problem?: string
}

Expand All @@ -350,12 +383,18 @@ export function prototypeLinks(canvas: Canvas): PrototypeLink[] {
for (const frame of canvas.frames) {
for (const el of scanInteractions(frame.html)) {
for (const interaction of el.interactions) {
const missing = targetsFrame(interaction) && !ids.has(interaction.target)
const problems = []
if (targetsFrame(interaction) && !ids.has(interaction.target))
problems.push(`target frame ${interaction.target} does not exist on this canvas`)
if (interaction.overlayPosition === null)
problems.push(
'invalid overlay coordinates: supply both X and Y as numbers without CSS units, or omit both to center',
)
links.push({
fromFrameId: frame.id,
element: el.label,
interaction,
...(missing ? { problem: `target frame ${interaction.target} does not exist on this canvas` } : {}),
...(problems.length ? { problem: problems.join('. ') } : {}),
})
}
}
Expand Down
Loading
Loading
0