Your code isn’t at fault. The Masonry library doesn’t support this cross frame scenario. I can think of three ways you might get past this:
- Inject the masonry script into the iframe so it could be used from within the iframe
- Patch the masonry script and ship that version with your plugin
- Find an alternative library that doesn’t have the issue
The third option might be fruitless. I’m not sure there are many options out there and they might have the same sort of issues.
The second option I’ve tried. I patched a couple places in the library and it seems to work okay now. I’m not sure it’s 100% covered i.e. you might encounter an issue with some configuration I haven’t tried. Here’s a gist with the patched version if you want to try it. In WordPress, you’ll have to deregister the masonry script they bundled and register that one of course.
The first option might be a good one but you’d have to be sure to only try injecting the script into the iframe when there actually is one and you’d have to make sure your block waits to be sure the script has been made available in the iframe. There may be other potential gotchas as well.
On a side note, I’m considering submitting PRs to the libraries (the parts I patched are in separate dependencies (fizzy-ui-utils and outlayer) but not sure if/when they’d make it into a release.