Skip to content

Commit 27d239a

Browse files
authored
fix: support rendering into ShadowRoot containers (#223)
* fix: fix Shadow DOM rendering issues * fix: cr * chore:remove queryInShadowRoots
1 parent 31e571d commit 27d239a

4 files changed

Lines changed: 17 additions & 5 deletions

File tree

__tests__/unit/options/parse-options.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ describe('parseOptions', () => {
198198
);
199199
});
200200

201+
it('accepts a ShadowRoot instance as the container target', () => {
202+
const host = document.createElement('div');
203+
document.body.appendChild(host);
204+
205+
const shadowRoot = host.attachShadow({ mode: 'open' });
206+
const parsed = parseOptions({
207+
container: shadowRoot,
208+
} as any);
209+
210+
expect(parsed.container).toBe(shadowRoot);
211+
});
212+
201213
it('skips design when structure is null', () => {
202214
itemMap.set('custom-item', {
203215
type: 'custom-item',

src/options/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function parseOptions(
5252
: undefined;
5353

5454
const parsed: Partial<ParsedInfographicOptions> = {
55-
container: parsedContainer as HTMLElement,
55+
container: parsedContainer as Element | ShadowRoot,
5656
padding: parsePadding(padding),
5757
};
5858

src/options/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { Data, Padding, ParsedData } from '../types';
55
import type { Path } from '../utils';
66

77
export interface InfographicOptions {
8-
/** 容器,可以是选择器或者 HTMLElement */
9-
container?: string | HTMLElement;
8+
/** 容器,可以是选择器、Element 或 ShadowRoot */
9+
container?: string | Element | ShadowRoot;
1010
/** 宽度 */
1111
width?: number | string;
1212
/** 高度 */
@@ -37,7 +37,7 @@ export interface InfographicOptions {
3737
}
3838

3939
export interface ParsedInfographicOptions {
40-
container: HTMLElement;
40+
container: Element | ShadowRoot;
4141
width?: number | string;
4242
height?: number | string;
4343
padding?: Padding;

src/renderer/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class Renderer implements IRenderer {
8686
});
8787

8888
try {
89-
observer.observe(document, {
89+
observer.observe(this.options.container, {
9090
childList: true,
9191
subtree: true,
9292
});

0 commit comments

Comments
 (0)