Add option to make usage of shadow dom configurable

pull/2516/head
Zephraph 7 years ago
parent e7885f2cb1
commit 1d495c8ad1

@ -22,6 +22,8 @@ export default function dom(
block.has_outro_method = true; block.has_outro_method = true;
const should_use_shadow_dom = options.customElement && options.shadowDom !== false;
// prevent fragment being created twice (#1063) // prevent fragment being created twice (#1063)
if (options.customElement) block.builders.create.add_line(`this.c = @noop;`); if (options.customElement) block.builders.create.add_line(`this.c = @noop;`);
@ -64,7 +66,7 @@ export default function dom(
// TODO injecting CSS this way is kinda dirty. Maybe it should be an // TODO injecting CSS this way is kinda dirty. Maybe it should be an
// explicit opt-in, or something? // explicit opt-in, or something?
const should_add_css = ( const should_add_css = (
!options.customElement && !should_use_shadow_dom &&
component.stylesheet.has_styles && component.stylesheet.has_styles &&
options.css !== false options.css !== false
); );
@ -445,8 +447,9 @@ export default function dom(
constructor(options) { constructor(options) {
super(); super();
${css.code && `this.shadowRoot.innerHTML = \`<style>${escape(css.code, { only_escape_at_symbol: true }).replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`} ${css.code && should_use_shadow_dom && `this.shadowRoot.innerHTML = \`<style>${escape(css.code, { only_escape_at_symbol: true }).replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
// TODO: Figure out what target should point to
@init(this, { target: this.shadowRoot }, ${definition}, create_fragment, ${not_equal}, ${prop_names}); @init(this, { target: this.shadowRoot }, ${definition}, create_fragment, ${not_equal}, ${prop_names});
${dev_props_check} ${dev_props_check}

@ -54,6 +54,7 @@ export interface CompileOptions {
hydratable?: boolean; hydratable?: boolean;
legacy?: boolean; legacy?: boolean;
customElement?: boolean; customElement?: boolean;
shadowDom?: boolean;
tag?: string; tag?: string;
css?: boolean; css?: boolean;

@ -117,8 +117,10 @@ if (typeof HTMLElement !== 'undefined') {
SvelteElement = class extends HTMLElement { SvelteElement = class extends HTMLElement {
constructor() { constructor() {
super(); super();
if (options.shadowDom !== false) {
this.attachShadow({ mode: 'open' }); this.attachShadow({ mode: 'open' });
} }
}
connectedCallback() { connectedCallback() {
for (const key in this.$$.slotted) { for (const key in this.$$.slotted) {

Loading…
Cancel
Save