Skip to content

Commit fa9f32e

Browse files
committed
dynamic sizing better
1 parent 9c8861a commit fa9f32e

File tree

45 files changed

+253
-133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+253
-133
lines changed

exercises/03.complex/03.problem.sizing/worker/mcp/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export async function initializeTools(agent: EpicMeMCP) {
8282
iframeUrl: iframeUrl.toString(),
8383
},
8484
encoding: 'text',
85-
// 🐨 add uiMetadata with the key 'preferred-frame-size' with a width of 800px and a height of 600px
85+
// 🐨 add uiMetadata with the key 'preferred-frame-size' with a width of 600px and a height of 800px
8686
}),
8787
],
8888
}

exercises/03.complex/03.solution.sizing/worker/mcp/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function initializeTools(agent: EpicMeMCP) {
8383
},
8484
encoding: 'text',
8585
uiMetadata: {
86-
'preferred-frame-size': ['800px', '600px'],
86+
'preferred-frame-size': ['600px', '800px'],
8787
},
8888
}),
8989
],

exercises/03.complex/04.problem.dynamic-sizing/README.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ sequenceDiagram
4545
</callout-info>
4646

4747
Now implement the size measurement to make the journal viewer fit perfectly.
48+
49+
💰 I'll be in the instructions a bit because this isn't a React workshop, so if
50+
you're unfamiliar with React, don't worry.

exercises/03.complex/04.problem.dynamic-sizing/app/routes/ui/journal-viewer.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { useEffect, useState, useTransition } from 'react'
1+
import {
2+
useEffect,
3+
useState,
4+
useTransition,
5+
// 💰 you'll need this:
6+
useRef,
7+
} from 'react'
28
import {
39
ErrorBoundary,
410
useErrorBoundary,
@@ -17,12 +23,19 @@ export default function JournalViewer({ loaderData }: Route.ComponentProps) {
1723
const [deletedEntryIds, setDeletedEntryIds] = useState<Set<number>>(
1824
() => new Set([]),
1925
)
26+
// 🐨 create a ref for the root element of this component
27+
// 💰 (this isn't a react workshop, here's how you do that):
28+
// const rootRef = useRef<HTMLDivElement>(null)
2029

2130
useEffect(() => {
2231
window.parent.postMessage({ type: 'ui-lifecycle-iframe-ready' }, '*')
2332

24-
// 🐨 get the height of the document.documentElement
25-
// 🐨 get the width of the document.documentElement
33+
// 🐨 get the root element
34+
// 💰 const root = rootRef.current
35+
// 🐨 if the root is null, return
36+
// 🐨 get the height and width of the root
37+
// 💰 const height = root.scrollHeight
38+
// 💰 const width = root.scrollWidth
2639
// 🐨 call window.parent.postMessage with the type 'ui-size-change' and the payload { height, width }
2740
// 🐨 set the targetOrigin to '*'
2841
}, [])
@@ -32,7 +45,11 @@ export default function JournalViewer({ loaderData }: Route.ComponentProps) {
3245
}
3346

3447
return (
35-
<div className="bg-background max-h-[800px] overflow-y-auto p-4">
48+
<div
49+
// 🐨 add the ref to the div
50+
// 💰 ref={rootRef}
51+
className="bg-background max-h-[800px] overflow-y-auto p-4"
52+
>
3653
<div className="mx-auto max-w-4xl">
3754
<div className="bg-card mb-6 rounded-xl border p-6 shadow-lg">
3855
<h1 className="text-foreground mb-2 text-3xl font-bold">

exercises/03.complex/04.problem.dynamic-sizing/worker/mcp/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function initializeTools(agent: EpicMeMCP) {
8383
},
8484
encoding: 'text',
8585
uiMetadata: {
86-
'preferred-frame-size': ['800px', '600px'],
86+
'preferred-frame-size': ['600px', '800px'],
8787
},
8888
}),
8989
],

exercises/03.complex/04.solution.dynamic-sizing/app/routes/ui/journal-viewer.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState, useTransition } from 'react'
1+
import { useEffect, useState, useTransition, useRef } from 'react'
22
import {
33
ErrorBoundary,
44
useErrorBoundary,
@@ -17,12 +17,16 @@ export default function JournalViewer({ loaderData }: Route.ComponentProps) {
1717
const [deletedEntryIds, setDeletedEntryIds] = useState<Set<number>>(
1818
() => new Set([]),
1919
)
20+
const rootRef = useRef<HTMLDivElement>(null)
2021

2122
useEffect(() => {
2223
window.parent.postMessage({ type: 'ui-lifecycle-iframe-ready' }, '*')
2324

24-
const height = document.documentElement.scrollHeight
25-
const width = document.documentElement.scrollWidth
25+
const root = rootRef.current
26+
if (!root) return
27+
28+
const height = root.scrollHeight
29+
const width = root.scrollWidth
2630

2731
window.parent.postMessage(
2832
{ type: 'ui-size-change', payload: { height, width } },
@@ -35,7 +39,10 @@ export default function JournalViewer({ loaderData }: Route.ComponentProps) {
3539
}
3640

3741
return (
38-
<div className="bg-background max-h-[800px] overflow-y-auto p-4">
42+
<div
43+
ref={rootRef}
44+
className="bg-background max-h-[800px] overflow-y-auto p-4"
45+
>
3946
<div className="mx-auto max-w-4xl">
4047
<div className="bg-card mb-6 rounded-xl border p-6 shadow-lg">
4148
<h1 className="text-foreground mb-2 text-3xl font-bold">

exercises/03.complex/04.solution.dynamic-sizing/worker/mcp/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function initializeTools(agent: EpicMeMCP) {
8383
},
8484
encoding: 'text',
8585
uiMetadata: {
86-
'preferred-frame-size': ['800px', '600px'],
86+
'preferred-frame-size': ['600px', '800px'],
8787
},
8888
}),
8989
],

exercises/04.interactive/01.problem.links/app/routes/ui/journal-viewer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useTransition } from 'react'
1+
import { useState, useTransition, useRef } from 'react'
22
import {
33
ErrorBoundary,
44
useErrorBoundary,
@@ -22,8 +22,8 @@ export default function JournalViewer({ loaderData }: Route.ComponentProps) {
2222
const [deletedEntryIds, setDeletedEntryIds] = useState<Set<number>>(
2323
() => new Set([]),
2424
)
25-
26-
useMcpUiInit()
25+
const rootRef = useRef<HTMLDivElement>(null)
26+
useMcpUiInit(rootRef)
2727

2828
const handleEntryDeleted = (entryId: number) => {
2929
setDeletedEntryIds((prev) => new Set([...prev, entryId]))

exercises/04.interactive/01.problem.links/app/utils/mcp.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { useEffect } from 'react'
22

3-
export function useMcpUiInit() {
3+
export function useMcpUiInit(rootRef: React.RefObject<HTMLDivElement | null>) {
44
useEffect(() => {
55
window.parent.postMessage({ type: 'ui-lifecycle-iframe-ready' }, '*')
6+
if (!rootRef.current) return
67

7-
const height = document.documentElement.scrollHeight
8-
const width = document.documentElement.scrollWidth
8+
const height = rootRef.current.scrollHeight
9+
const width = rootRef.current.scrollWidth
910

1011
window.parent.postMessage(
1112
{ type: 'ui-size-change', payload: { height, width } },
1213
'*',
1314
)
14-
}, [])
15+
}, [rootRef])
1516
}
1617

1718
// 🐨 export a function called sendLinkMcpMessage that takes a url string and returns a promise

exercises/04.interactive/01.problem.links/worker/mcp/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function initializeTools(agent: EpicMeMCP) {
8383
},
8484
encoding: 'text',
8585
uiMetadata: {
86-
'preferred-frame-size': ['800px', '600px'],
86+
'preferred-frame-size': ['600px', '800px'],
8787
},
8888
}),
8989
],

0 commit comments

Comments
 (0)