Skip to content

Commit 433dc8e

Browse files
committed
Merge branch 'master' of https://github.com/react-component/util
2 parents 4d26549 + 276f89a commit 433dc8e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Dom/shadow.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function inShadow(ele: Node) {
2+
return ele?.getRootNode() !== ele?.ownerDocument;
3+
}
4+
5+
export function getShadowRoot(ele: Node) {
6+
return inShadow(ele) ? ele?.getRootNode() : null;
7+
}

tests/shadow.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getShadowRoot } from '../src/Dom/shadow';
2+
3+
describe('shadow', () => {
4+
describe('getShadowRoot', () => {
5+
it('show work', () => {
6+
const shadowRoot = document.createElement('div');
7+
document.body.appendChild(shadowRoot);
8+
9+
const shadow = shadowRoot.attachShadow({ mode: 'open' });
10+
const inShadowButton = document.createElement('button');
11+
shadow.appendChild(inShadowButton);
12+
13+
expect(getShadowRoot(inShadowButton)).toBe(shadow);
14+
});
15+
16+
it('show return null', () => {
17+
const button = document.createElement('button');
18+
document.body.appendChild(button);
19+
20+
expect(getShadowRoot(button)).toBeNull();
21+
});
22+
});
23+
});

0 commit comments

Comments
 (0)