utils/dom/shadowrootregistry
Classes
module:utils/dom/shadowrootregistry~ShadowRootRegistry
Type Definitions
module:utils/dom/shadowrootregistry~ShadowRootRegistryAddEventmodule:utils/dom/shadowrootregistry~ShadowRootRegistryRemoveEvent
Functions
listenToShadowRoots( registry, options = { options.callback, options.emitter, options.event, options.listenerOptions? } ) → () => voidmodule:utils/dom/shadowrootregistry~listenToShadowRootsSome DOM events never reach a listener on
documentif they originate inside a shadow root —scrollandmouseenter/mouseleave, for instance, do not cross a shadow boundary at all. This function is how code that needs those events anyway reacts to them for every shadow root a registry is currently tracking: it attachesemitter's callback directly to each one tracked byregistry, and keeps that up to date — attaching to roots that appear later, detaching from roots that disappear — for as long as the returned function is not called.Attach it alongside the
documentlistener rather than instead of it — one covers the light DOM, the other each shadow root:
Copy codeview.listenTo( global.document, 'scroll', repositionOnScroll, { useCapture: true } ); const stopListening = listenToShadowRoots( editor.ui.shadowRootRegistry, { emitter: view, event: 'scroll', callback: repositionOnScroll, listenerOptions: { useCapture: true } } );The
useCaptureabove is not incidental. The events this function exists for do not bubble when fired on an element, so a listener on a shadow root only sees one that originates inside that root during the capture phase. Without it the listener is attached but never fires.Call the returned function to detach.
Parameters
registry: ShadowRootRegistryThe registry whose shadow roots should be listened to.
options: objectDescribes the listener to attach to each shadow root.
Propertiesoptions.callback: ( evt: EventInfo, domEvent: any ) => voidThe event handler.
options.emitter: DomEmitterThe emitter to which this behavior should be added.
options.event: keyof DomEventMapThe DOM event to listen to.
options.listenerOptions?: CallbackOptions & objectListener options, passed through to
DomEmitter#listenTo().
Returns
() => void