virtualstate / navigation

Native JavaScript navigation [web api] implementation
MIT License
77 stars 5 forks source link

Allow intercepting events from within shadow DOM. #17

Closed aearly closed 11 months ago

aearly commented 12 months ago

Fixes #14.

Checklist

fabiancook commented 11 months ago

Thanks! Makes sense and looks good.

Ran into this in tests... it might be though because of events being created outside of the DOM... so they don't have the full DOM API available

process unhandled rejection TypeError: ev.composedPath is not a function

I'll do a commit for this right now, would be good to change too:

export interface EventPrototype {
    target: ElementPrototype;
    composedPath?(): ElementPrototype[];
    defaultPrevented: unknown;
    submitter: Record<string, unknown>;
}

And use, assuming too that it could be possible to get no values returned as well (maybe if its a CustomEvent, has composedPath but isn't populated somehow?):

function getComposedPathTarget(event: EventPrototype) {
  if (!event.composedPath) {
    return event.target;
  }
  const targets = event.composedPath();
  return targets[0] ?? event.target; 
}