Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/react-component/util
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed May 23, 2023
2 parents 4d26549 + 276f89a commit 433dc8e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Dom/shadow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function inShadow(ele: Node) {
return ele?.getRootNode() !== ele?.ownerDocument;
}

export function getShadowRoot(ele: Node) {
return inShadow(ele) ? ele?.getRootNode() : null;
}
23 changes: 23 additions & 0 deletions tests/shadow.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getShadowRoot } from '../src/Dom/shadow';

describe('shadow', () => {
describe('getShadowRoot', () => {
it('show work', () => {
const shadowRoot = document.createElement('div');
document.body.appendChild(shadowRoot);

const shadow = shadowRoot.attachShadow({ mode: 'open' });
const inShadowButton = document.createElement('button');
shadow.appendChild(inShadowButton);

expect(getShadowRoot(inShadowButton)).toBe(shadow);
});

it('show return null', () => {
const button = document.createElement('button');
document.body.appendChild(button);

expect(getShadowRoot(button)).toBeNull();
});
});
});

0 comments on commit 433dc8e

Please sign in to comment.