Skip to content

Instantly share code, notes, and snippets.

@Willmo36
Created March 11, 2020 15:55
Show Gist options
  • Save Willmo36/27fdff1c4a5ca031223c45737092344d to your computer and use it in GitHub Desktop.
Save Willmo36/27fdff1c4a5ca031223c45737092344d to your computer and use it in GitHub Desktop.
composedPath fallback via unfolding
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