Forward ref to iframe in SandpackPreview #238
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What kind of change does this pull request introduce?
Improvement:
Adds the possibility to get a ref to the preview iframe on the
SandpackPreview
component. ie:<SandpackPreview ref={myRefFromUseRef} />
What is the current behavior?
Currently, it is not possible to have a reference to the preview iframe when using the react components, the only way is to use the Sandpack client.
What is the new behavior?
Getting a reference to the iframe allows to use the postMessage API to communicate between the parent window and the app running in the preview.
What steps did you take to test this? This is required before we can merge, make sure to test the flow you've updated.
<SandpackPreview ref={myRefFromUseRef} />
Checklist
Comments
I figure that you might have an official way to communicate between the preview and the parent in your roadmap and that you might be wary about exposing this in the meantime, as you are using postMessage already for internals and there is a risk of reserved message clashes.
I've lost quite some hairs on the subject at GraphCMS (and still am) for our UI extensions feature, so feel free to DM me on Discord about this PR or this subject in general, happy to contribute here :)