This site is built with Viewi itself. It is experimental and still in development. If you see any bugs please do not hesitate and open an issue or DM me on Twitter.

DOM Helper

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]);
    }
}