Skip to content

Commit 76488e2

Browse files
author
Michael Mrowetz
committed
add find parentByClass helper
1 parent 2567cc5 commit 76488e2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: src/ts/helpers/dom.ts

+15
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,18 @@ export function forEachNodeList<T extends Node>(list: NodeListOf<T>, fn: {(el: T
6666
fn(list.item(i), i);
6767
}
6868
}
69+
70+
/**
71+
* Helper to recousivly find parent with `className`
72+
* @param base `Element` to start from
73+
* @param className class that the parent should have
74+
*/
75+
export function findParentByClassName(base: Element, className: string) {
76+
if (base.parentElement === undefined) {
77+
return undefined;
78+
}
79+
if (base.parentElement.classList.contains(className)) {
80+
return base.parentElement;
81+
}
82+
return findParentByClassName(base.parentElement, className);
83+
};

0 commit comments

Comments
 (0)