Skip to content

Commit

Permalink
feat(node): Added resize event (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowoz committed Nov 15, 2023
1 parent a79a5d2 commit 61ccf7e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup | Node.js
uses: actions/setup-node@v4
with:
node-version: "16.0.0"
node-version: "16.13.0"

- name: Install dependencies and cache
uses: bahmutov/npm-install@v1
Expand Down
2 changes: 2 additions & 0 deletions sandpack-client/src/clients/node/inject-scripts/index.ts
Expand Up @@ -8,13 +8,15 @@ import { INJECT_MESSAGE_TYPE } from "@codesandbox/nodebox";
import consoleHook from "../../../inject-scripts/dist/consoleHook.js";

import { setupHistoryListeners } from "./historyListener";
import { watchResize } from "./resize.js";

const scripts = [
{ code: setupHistoryListeners.toString(), id: "historyListener" },
{
code: "function consoleHook({ scope }) {" + consoleHook + "\n};",
id: "consoleHook",
},
{ code: watchResize.toString(), id: "watchResize" },
];

export const injectScriptToIframe = (
Expand Down
57 changes: 57 additions & 0 deletions sandpack-client/src/clients/node/inject-scripts/resize.ts
@@ -0,0 +1,57 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

export function watchResize({ scope }: { scope: { channelId: string } }) {
let lastHeight = 0;

function getDocumentHeight(): number {
if (typeof window === "undefined") return 0;

const { body } = document;
const html = document.documentElement;

return Math.max(body.scrollHeight, body.offsetHeight, html.offsetHeight);
}

function sendResizeEvent() {
const height = getDocumentHeight();

if (lastHeight !== height) {
window.parent.postMessage(
{
type: "resize",
height,
codesandbox: true,
channelId: scope.channelId,
},
"*"
);
}

lastHeight = height;
}

sendResizeEvent();

let throttle: any;
const observer = new MutationObserver(() => {
if (throttle === undefined) {
sendResizeEvent();

throttle = setTimeout(() => {
throttle = undefined;
}, 300);
}
});

observer.observe(document, {
attributes: true,
childList: true,
subtree: true,
});

/**
* Ideally we should only use a `MutationObserver` to trigger a resize event,
* however, we noted that it's not 100% reliable, so we went for polling strategy as well
*/
setInterval(sendResizeEvent, 300);
}
12 changes: 5 additions & 7 deletions sandpack-react/src/Playground.stories.tsx
Expand Up @@ -4,14 +4,12 @@ import React from "react";

import { Sandpack } from "./";

export default {
title: "Intro/Playground",
};

export const Basic: React.FC = () => {
return (
<Sandpack
options={{ classes: { "sp-layout": "fooo" } }}
template="nextjs"
theme={themes.sandpackDark}
/>
);
return <Sandpack template="vite" theme={themes.sandpackDark} />;
};

export const EslintBasic = () => (
Expand Down

1 comment on commit 61ccf7e

@vercel
Copy link

@vercel vercel bot commented on 61ccf7e Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.