Skip to main content
Solved

How to listen for frame changes in embed


Hi,

I know that Figma emitted events so that developers can listen to some of the event that Figma notifies, but how do I know if the prototype is presenting the last frame?

Thank You!

Best answer by tank666

Figma recently updated the documentation for embeds.
https://www.figma.com/developers/embed#:~:text=Events%20emitted%20from%20the%20embed

Store the nodeId value of the last frame and compare it with the presentedNodeId value that is available in the event message data with the PRESENTED_NODE_CHANGED type. Or with targetNodeId in a message with the MOUSE_PRESS_OR_RELEASE type.

View original
This topic has been closed for replies.

7 replies

Gleb
  • Power Member
  • 4715 replies
  • March 14, 2024

This is only available when running a plugin and using the plugins API, which is not accessible in embeds. There is no way to listen to events in embeds.


tank666
  • 4873 replies
  • Answer
  • March 14, 2024

Figma recently updated the documentation for embeds.
https://www.figma.com/developers/embed#:~:text=Events%20emitted%20from%20the%20embed

Store the nodeId value of the last frame and compare it with the presentedNodeId value that is available in the event message data with the PRESENTED_NODE_CHANGED type. Or with targetNodeId in a message with the MOUSE_PRESS_OR_RELEASE type.


Gleb
  • Power Member
  • 4715 replies
  • March 14, 2024

Oh wow cool!


Sorry for the late respond.

Thank you but, how do I get that nodeId? If I’m trying to get that from the NEW_STATE event, the nodeId is not the same as the presentedNodeId that emitted from PRESENTED_NODE_CHANGED event.

Here’s how I handle that:

1 window.addEventListener('message', event => {
2 const { type, ...rest } = event.data;
3 if (!type) return;
4 if (type === 'NEW_STATE') {
5 console.log(
6 '[NEW_STATE] event.data.data.nodeId:',
7 event.data.data.nodeId
8 );
9 return;
10 }
11 if (type.includes('PRESENTED_NODE_CHANGED')) {
12 const { presentedNodeId } = rest.data;
13 console.log(
14 '[PRESENTED_NODE_CHANGED] presentedNodeId:',
15 presentedNodeId
16 );
17 }
18 });
19

And this is the logs:


tank666
  • 4873 replies
  • March 18, 2024

Get the nodeId of the last frame manually (for example, from a prototype or design link) and hardcode it.

This is a new API that I think is just starting to develop. Perhaps more features and data will be added in the future.


Right, Thank you!!


@tank666,

I have similar question. I was able to know and compare it with the last node id. However, there is a scenario where the last FRAME has 2 buttons (Button A and Button B).

If they click on Button A. I want to notify the parentWindow (where prototype is running) that the “User is done interacting with a prototype. So close the prototype”

If they click on Button B. I want to continue showing next-page.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings