Use Iframely embeds inside Web Components
When you place Iframely card and other rich media embed codes into your Shadow DOM, there may be issues. Global JavaScript selectors do not apply to Shadow DOM and we cannot find what we are looking for.
The known issues cover the resize events for rich media such as X, Facebook, Instagram and TikTok. Clicks in Iframely’s own summary cards may not open links either.
It is because Iframely’s platform embed.js script relies on the event messages from iFrames to parent, and we need to be able to match those messages back to an iFrame. If we cannot find an iFrame, the event handlers do not get called.
Help embed.js find your iFrames
Help embed.js find your iFrames in one of these ways:
You can mark your web component with CSS classname
"iframely-shadow". We’ll find iFrames there on our own. The class goes on the element that hosts the shadow root, and the shadow root must be open (attachShadow({ mode: 'open' })): embed.js can’t see inside a closed one. If needed, we’ll search recursively with the same class name, so nested components work too, as long as each host has the class.You can also change the name of that classname. Set the
shadowproperty withiframely.configure({ shadow: 'my-component' }), or add&shadow=my-componentto thesrcof your embed.js script.Lastly, you may override the entire
iframely.findIframe(options)function to match your specifics. Your function replaces the default one, which is in src/messaging.ts. It receives:contentWindow: the window that sent the message;src: part of the iFrame’s address, when the message gives one;domains: the iFrame hosts to search, orfalseto search every iFrame.
It returns the matching iFrame element, or nothing.
iframely.findIframe = function (options) {
// options.contentWindow, options.src, options.domains
// return the matching <iframe> element, or undefined
}; Unfurl links inside Shadow DOM
embed.js doesn’t scan inside Shadow DOM when the page loads. Once your component has rendered, pass its shadow root to iframely.load(). It builds the embeds inside: links in the HTML template, lazy iFrames, and size requests to iFrames that are already there.
host.classList.add('iframely-shadow');
const root = host.attachShadow({ mode: 'open' });
root.innerHTML = embedHtml; // HTML codes from the API, or the HTML template for your links
iframely.load(root); iframely.load() doesn’t replace the class name: embed.js still needs it to match messages from the iFrames back to them. Note also that iframely.unload() doesn’t cover embeds inside Shadow DOM.
Add Iframely styles inside the shadow root
embed.js adds its default styles for iframely-responsive to your page’s <head>, and Shadow DOM doesn’t see them. If your HTML codes rely on class names (see omit-CSS), or you unfurl links with the HTML template, add the required styles inside your shadow root too, for example in a <style> element.