Separate viewport and containerDimensions

This commit is contained in:
martinalong 2025-12-26 14:04:05 -08:00
commit 82e64d56bb
No known key found for this signature in database

View file

@ -461,16 +461,20 @@ interface HostContext {
displayMode?: "inline" | "fullscreen" | "pip";
/** Display modes the host supports */
availableDisplayModes?: string[];
/** Current and maximum dimensions available to the UI. */
/** Container dimensions for the iframe. Specify either width or maxWidth, and either height or maxHeight. */
containerDimensions?: (
| { height: number } // If specified, container is fixed at this height
| { maxHeight?: number } // Otherwise, container height is determined by the UI height, up to this maximum height (if defined)
) & (
| { width: number } // If specified, container is fixed at this width
| { maxWidth?: number } // Otherwise, container width is determined by the UI width, up to this maximum width (if defined)
);
/** Host window viewport dimensions */
viewport?: {
/** Viewport width (if fixed). Only pass width or maxWidth, not both. */
/** Window viewport width in pixels. */
width?: number;
/** Viewport height (if fixed). Only pass height or maxHeight, not both. */
/** Window viewport height in pixels. */
height?: number;
/** Maximum available viewport width in pixels (if constrained). */
maxWidth?: number;
/** Maximum available viewport height in pixels (if constrained). */
maxHeight?: number;
};
/** User's language/region preference (BCP 47, e.g., "en-US") */
locale?: string;
@ -520,20 +524,25 @@ Example:
}
},
"displayMode": "inline",
"viewport": { "width": 400, "maxHeight": 600 }
"containerDimensions": { "width": 400, "maxHeight": 600 }
"viewport": { "width": 1920, "height": 1080 },
}
}
}
```
### Viewport and Sizing
### Viewport and Dimensions
The `viewport` field in `HostContext` communicates sizing constraints between host and app. Each dimension (height and width) operates independently and can be either **fixed** or **flexible**.
The `HostContext` provides two separate fields for sizing information:
#### Viewport Modes
- **`containerDimensions`**: The dimensions of the container that holds the app. This controls the actual space the app occupies within the host. Each dimension (height and width) operates independently and can be either **fixed** or **flexible**.
| Mode | Viewport Field | Meaning |
|------|---------------|---------|
- **`viewport`**: The host window's dimensions (e.g., `window.innerWidth` and `window.innerHeight`). Apps can use this to make responsive layout decisions based on the overall screen size.
#### Dimension Modes
| Mode | Dimensions Field | Meaning |
|------|-----------------|---------|
| Fixed | `height` or `width` | Host controls the size. App should fill the available space. |
| Flexible | `maxHeight` or `maxWidth` | App controls the size, up to the specified maximum. |
| Unbounded | Field omitted | App controls the size with no limit. |
@ -542,33 +551,39 @@ These modes can be combined independently. For example, a host might specify a f
#### App Behavior
Apps should check the viewport configuration and apply appropriate CSS:
Apps should check the containerDimensions configuration and apply appropriate CSS:
```typescript
// In the app's initialization
const viewport = hostContext.viewport;
const containerDimensions = hostContext.containerDimensions;
if (viewport) {
if (containerDimensions) {
// Handle height
if ("height" in viewport) {
if ("height" in containerDimensions) {
// Fixed height: fill the container
document.documentElement.style.height = "100vh";
} else if (viewport.maxHeight) {
} else if ("maxHeight" in containerDimensions && containerDimensions.maxHeight) {
// Flexible with max: let content determine size, up to max
document.documentElement.style.maxHeight = `${viewport.maxHeight}px`;
document.documentElement.style.maxHeight = `${containerDimensions.maxHeight}px`;
}
// If neither, height is unbounded
// Handle width
if ("width" in viewport) {
if ("width" in containerDimensions) {
// Fixed width: fill the container
document.documentElement.style.width = "100vw";
} else if (viewport.maxWidth) {
} else if ("maxWidth" in containerDimensions && containerDimensions.maxWidth) {
// Flexible with max: let content determine size, up to max
document.documentElement.style.maxWidth = `${viewport.maxWidth}px`;
document.documentElement.style.maxWidth = `${containerDimensions.maxWidth}px`;
}
// If neither, width is unbounded
}
// Apps can also use viewport for additional data to make responsive layout decisions
const viewport = hostContext.viewport;
if (viewport?.width && viewport.width < 768) {
// Apply mobile-friendly layout
}
```
#### Host Behavior