From 7775bbafd5a3bdb44d4fd3814fd24cafac655b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Silvani?= Date: Sun, 15 Sep 2024 10:08:15 -0300 Subject: [PATCH] Initialize audio on Strudel on first user interaction (keydown/mouse) (#294) Handle mouse and keydown events from the `WebTargetIframe` component and send a message to the iframe when they occur. This allows `StrudelWrapper` to initialize audio immediately after detecting user interaction, ensuring the audio worklet is loaded correctly. Fixes #286 --- .../web/src/components/web-target-iframe.tsx | 17 +++++++++++++++++ packages/web/src/lib/strudel-wrapper.ts | 11 +++++++++-- packages/web/src/routes/frames/strudel.tsx | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/web/src/components/web-target-iframe.tsx b/packages/web/src/components/web-target-iframe.tsx index 2341c25b..1a9c829b 100644 --- a/packages/web/src/components/web-target-iframe.tsx +++ b/packages/web/src/components/web-target-iframe.tsx @@ -37,6 +37,23 @@ export const WebTargetIframe = ({ target, session }: WebTargetIframeProps) => { }; }, [session, ref]); + // Handle user interactions + useEffect(() => { + const handleUserInteraction = () => { + if (!ref.current) return; + const interactionMessage = { type: "user-interaction" }; + ref.current.contentWindow?.postMessage(interactionMessage, "*"); + }; + + window.addEventListener("click", handleUserInteraction); + window.addEventListener("keydown", handleUserInteraction); + + return () => { + window.removeEventListener("click", handleUserInteraction); + window.removeEventListener("keydown", handleUserInteraction); + }; + }, [ref]); + return (