implement 'stylesTarget'

pull/5870/head
Ivan Hofer 6 years ago
parent f757de328a
commit 3c172aebd2

@ -49,11 +49,8 @@ export default function dom(
if (should_add_css) { if (should_add_css) {
body.push(b` body.push(b`
function ${add_css}() { function ${add_css}(options) {
var style = @element("style"); @append_styles(options, "${component.stylesheet.id.replace('svelte-', '')}", "${styles}");
style.id = "${component.stylesheet.id}-style";
style.textContent = "${styles}";
@append(@_document.head, style);
} }
`); `);
} }

@ -38,6 +38,7 @@ interface T$$ {
on_destroy: any[]; on_destroy: any[];
skip_bound: boolean; skip_bound: boolean;
on_disconnect: any[]; on_disconnect: any[];
stylesTarget?: HTMLElement|ShadowRoot;
} }
export function bind(component, name, callback) { export function bind(component, name, callback) {

@ -140,7 +140,15 @@ export class SvelteComponentDev extends SvelteComponent {
*/ */
$$slot_def: any; $$slot_def: any;
constructor(options: IComponentOptions) { constructor(options: {
target: Element|ShadowRoot;
stylesTarget: Element|ShadowRoot;
anchor?: Element;
props?: Props;
hydrate?: boolean;
intro?: boolean;
$$inline?: boolean;
}) {
if (!options || (!options.target && !options.$$inline)) { if (!options || (!options.target && !options.$$inline)) {
throw new Error("'target' is a required option"); throw new Error("'target' is a required option");
} }
@ -232,7 +240,15 @@ export class SvelteComponentTyped<
*/ */
$$slot_def: Slots; $$slot_def: Slots;
constructor(options: IComponentOptions<Props>) { constructor(options: {
target: Element|ShadowRoot;
stylesTarget: Element|ShadowRoot;
anchor?: Element;
props?: Props;
hydrate?: boolean;
intro?: boolean;
$$inline?: boolean;
}) {
super(options); super(options);
} }
} }

@ -110,6 +110,26 @@ function init_hydrate(target: NodeEx) {
const anchor = j < lis.length ? lis[j] : null; const anchor = j < lis.length ? lis[j] : null;
target.insertBefore(toMove[i], anchor); target.insertBefore(toMove[i], anchor);
} }
let appendStylesTo = document.head;
export function append_styles(
{ stylesTarget },
styleSheetId: string,
styles: string,
styleId: string = `svelte-${styleSheetId}-style`
) {
if (stylesTarget) appendStylesTo = stylesTarget;
if (!appendStylesTo.querySelector('#' + styleId)) {
const style = element('style');
style.id = styleId;
style.textContent = styles;
append(appendStylesTo, style);
}
}
export function append_empty_stylesheet() {
return appendStylesTo.appendChild(element('style') as HTMLStyleElement);
} }
export function append(target: NodeEx, node: NodeEx) { export function append(target: NodeEx, node: NodeEx) {

@ -1,4 +1,4 @@
import { element } from './dom'; import { append_empty_stylesheet } from './dom';
import { raf } from './environment'; import { raf } from './environment';
interface ExtendedDoc extends Document { interface ExtendedDoc extends Document {
@ -31,7 +31,7 @@ export function create_rule(node: Element & ElementCSSInlineStyle, a: number, b:
const name = `__svelte_${hash(rule)}_${uid}`; const name = `__svelte_${hash(rule)}_${uid}`;
const doc = node.ownerDocument as ExtendedDoc; const doc = node.ownerDocument as ExtendedDoc;
active_docs.add(doc); active_docs.add(doc);
const stylesheet = doc.__svelte_stylesheet || (doc.__svelte_stylesheet = doc.head.appendChild(element('style') as HTMLStyleElement).sheet as CSSStyleSheet); const stylesheet = doc.__svelte_stylesheet || (doc.__svelte_stylesheet = append_empty_stylesheet().sheet as CSSStyleSheet);
const current_rules = doc.__svelte_rules || (doc.__svelte_rules = {}); const current_rules = doc.__svelte_rules || (doc.__svelte_rules = {});
if (!current_rules[name]) { if (!current_rules[name]) {

Loading…
Cancel
Save