Listening for changes using figma.on()

I want to change my plugin UI when the selection changes in the editor.

This is the code I am using to listen for changes in my code.ts file:

figma.on("selectionchange", () => {
  figma.ui.postMessage(selectedTextNodes.length)
})

And this is the code I’m using to receive the changes in ui.html:

onmessage = (event) => {
    document.getElementById('layers-selected').innerHTML = `${event.data.pluginMessage} text layer${event.data.pluginMessage === 1 ? '':'s'} selected`
  }

However, this isn’t working. Am I using a wrong approach? If so, what would be the right one?

The code snippets shown work as expected. Check the code you didn’t share.

2 Likes

Thanks. The bug was that I was sending the variable v=value, which was declared when the plugin first ran.

Instead, I should be sending real time ‘value’, and not the previously defined variable.