File size: 2,333 Bytes
e1cc3bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { useEffect, RefObject } from "react";
import { App } from "../app";

/**
 * React hook that automatically reports UI size changes to the host.
 *
 * Uses `ResizeObserver` to watch `document.body` and `document.documentElement` for
 * size changes and sends `ui/notifications/size-changed` notifications.
 *
 * The hook automatically cleans up the `ResizeObserver` when the component unmounts.
 *
 * **Note**: This hook is rarely needed since the {@link useApp} hook automatically enables
 * auto-resize by default. This hook is provided for advanced cases where you
 * create the {@link App} manually with `autoResize: false` and want to add auto-resize
 * behavior later.
 *
 * @param app - The connected {@link App} instance, or null during initialization
 * @param elementRef - Currently unused. The hook always observes `document.body`
 *   and `document.documentElement` regardless of this value. Passing a ref will
 *   cause unnecessary effect re-runs; omit this parameter.
 *
 * @example Manual App creation with custom auto-resize control
 * ```tsx
 * function MyComponent() {
 *   // For custom App options, create App manually instead of using useApp
 *   const [app, setApp] = useState<App | null>(null);
 *   const [error, setError] = useState<Error | null>(null);
 *
 *   useEffect(() => {
 *     const myApp = new App(
 *       { name: "MyApp", version: "1.0.0" },
 *       {}, // capabilities
 *       { autoResize: false } // Disable default auto-resize
 *     );
 *
 *     const transport = new PostMessageTransport(window.parent, window.parent);
 *     myApp.connect(transport)
 *       .then(() => setApp(myApp))
 *       .catch((err) => setError(err));
 *   }, []);
 *
 *   // Add manual auto-resize control
 *   useAutoResize(app);
 *
 *   if (error) return <div>Connection failed: {error.message}</div>;
 *   return <div>My content</div>;
 * }
 * ```
 *
 * @see {@link App.setupSizeChangedNotifications} for the underlying implementation
 * @see {@link useApp} which enables auto-resize by default
 * @see {@link App} constructor for configuring `autoResize` option
 */
export function useAutoResize(
  app: App | null,
  elementRef?: RefObject<HTMLElement | null>,
) {
  useEffect(() => {
    if (!app) {
      return;
    }

    return app.setupSizeChangedNotifications();
  }, [app, elementRef]);
}