Created
March 11, 2020 15:55
-
-
Save Willmo36/27fdff1c4a5ca031223c45737092344d to your computer and use it in GitHub Desktop.
composedPath fallback via unfolding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { array } from 'fp-ts/lib/Array'; | |
import { flow, tuple } from 'fp-ts/lib/function'; | |
import * as Option from 'fp-ts/lib/Option'; | |
export const composedPath = (e: MouseEvent) => { | |
if (e['composedPath']) { | |
return e.composedPath(); | |
} | |
if (e.target instanceof HTMLElement) { | |
return array.unfold( | |
e.target, | |
flow( | |
Option.fromNullable, | |
Option.map(element => tuple(element, element.parentElement)), | |
), | |
); | |
} | |
throw new Error(`Cannot find DOM element path for event: ${event}. Is the target a HTML Element?`); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment