Skip to content

Commit ea283bc

Browse files
authored
feat(dom): findAncestor deprecated (#221)
Use findClosest instead DEPRECATED: findAncestor deprecated Use fincClosest instead with element.parentElement
1 parent e173f26 commit ea283bc

6 files changed

Lines changed: 27 additions & 29 deletions

File tree

docs/docs/dom/find-ancestor.mdx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
# `findAncestor`
1+
import { Callout } from '#components/callout/callout';
22

33
export const meta = {
44
title: 'findAncestor',
55
category: 'DOM',
66
menuPriority: 90,
77
};
88

9+
# `findAncestor`
10+
11+
<Callout intent='danger'>
12+
<Callout.Heading>Deprecated</Callout.Heading>
13+
<Callout.Main>
14+
Use [findClosest](./?path=/dom/find-closest) with `element.parentElement` instead.
15+
</Callout.Main>
16+
</Callout>
17+
918
Finds closest ancestor element that matches condition.
1019

1120
```ts

docs/docs/dom/find-closest.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ import { findClosest } from '@krutoo/utils';
1414
const element = document.querySelector('#something');
1515

1616
// element or null returns
17-
const closestTarget = findClosest(element, parent => parent.classList.contains('target'));
17+
const closestTarget = findClosest(element, el => el.classList.contains('target'));
1818
```

docs/package-lock.json

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/components/callout/callout.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import classNames from 'classnames';
33
import styles from './callout.m.css';
44

55
export interface CalloutProps extends HTMLAttributes<HTMLDivElement> {
6-
level?: 'info' | 'danger';
6+
intent?: 'info' | 'danger';
77
}
88

99
export interface CalloutHeadingProps extends HTMLAttributes<HTMLHeadingElement> {}
1010

1111
export interface CalloutMainProps extends HTMLAttributes<HTMLDivElement> {}
1212

13-
export function Callout({ level = 'info', children, className, ...restProps }: CalloutProps) {
13+
export function Callout({ intent = 'info', children, className, ...restProps }: CalloutProps) {
1414
return (
1515
<div
1616
{...restProps}
17-
className={classNames(styles.callout, styles[level], className)}
18-
data-intent={level}
17+
className={classNames(styles.callout, className)}
18+
data-intent={intent}
1919
data-marker='callout'
2020
>
2121
{children}

src/dom/find-ancestor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param element Target element to find ancestor.
44
* @param predicate Function that check ancestor.
55
* @returns Element or null.
6-
* @todo Replace by `findClosest`?
6+
* @deprecated Use `findClosest(element.parentElement)` instead.
77
*/
88
export function findAncestor(
99
element: Element,

src/dom/get-positioned-parent-offset.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Point2d } from '../math/mod.ts';
2-
import { findAncestor } from './find-ancestor.ts';
2+
import { findClosest } from './find-closest.ts';
33
import { isScrollable } from './is-scrollable.ts';
44

55
export interface PositioningOptions {
@@ -21,10 +21,12 @@ export function getPositionedParentOffset(
2121
return { x: 0, y: 0 };
2222
}
2323

24-
const offsetParent = findAncestor(
25-
element,
26-
strategy === 'fixed' ? isContainingBlockForFixed : isContainingBlock,
27-
);
24+
const offsetParent = element.parentElement
25+
? findClosest(
26+
element.parentElement,
27+
strategy === 'fixed' ? isContainingBlockForFixed : isContainingBlock,
28+
)
29+
: null;
2830

2931
const offset: Point2d = {
3032
x: 0,
@@ -48,7 +50,9 @@ export function getPositionedParentOffset(
4850
offset.y += cssValueToNumber(parentStyle.borderTopWidth);
4951
}
5052

51-
const scrollParent = findAncestor(element, isScrollable) ?? document.documentElement;
53+
const scrollParent =
54+
(element.parentElement ? findClosest(element.parentElement, isScrollable) : null) ??
55+
document.documentElement;
5256

5357
// IMPORTANT: check offsetParent's scrollTop/scrollLeft
5458
if (offsetParent && offsetParent === scrollParent) {

0 commit comments

Comments
 (0)