use Viewi\Components\Services\DomHelper; ... class DomHelper { public static function getDocument(): ?HtmlNode; public static function getWindow(): ?HtmlNode; }
Useful when you have to deal with a document or a window objects on clint-side. For example:
function __rendered() { // click outside $document = DomHelper::getDocument(); if ($document !== null) { $document->addEventListener('click', $this->onClickOutside, true); } // resize event $document = DomHelper::getWindow(); if ($document !== null) { $document->addEventListener('resize', $this->onResize, ['passive' => true]); } } // !! Do not forget to unsubscribe on destroy !! function __destroy() { // remove click outside $document = DomHelper::getDocument(); if ($document !== null) { $document->removeEventListener('click', $this->onClickOutside, true); } // remove resize event $document = DomHelper::getWindow(); if ($document !== null) { $document->removeEventListener('resize', $this->onResize, ['passive' => true]); } }