Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom style tag #5639

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ba5ee7a
#3940 add option to append styles on an element other than document.head
ivanhofer Nov 3, 2020
28a29bb
#3940 append styles on an element other than document.head
ivanhofer Nov 3, 2020
8718229
Merge remote-tracking branch 'origin/master' into customStyleTag
ivanhofer Nov 3, 2020
4026fb0
Merge remote-tracking branch 'upstream/master' into customStyleTag
ivanhofer Nov 3, 2020
f2221fc
#3940 use @noop instead of null
ivanhofer Nov 4, 2020
9ad664e
#3940 refactor check if style is already present
ivanhofer Nov 4, 2020
0968e55
#3940 revert auto-formatting changes
ivanhofer Nov 6, 2020
a664d7d
Merge remote-tracking branch 'upstream/master' into customStyleTag
ivanhofer Nov 10, 2020
c0ac80f
add 'noop' param to all tests
ivanhofer Nov 10, 2020
7bd2ed6
add 'noop' param to some more tests
ivanhofer Nov 10, 2020
bf528d1
add missing css to customElement constructor
ivanhofer Nov 10, 2020
5ad396b
refactor add styles method call to reduce generated output
ivanhofer Nov 11, 2020
c154895
init $$ in component with empty object value
ivanhofer Nov 12, 2020
675d6dd
Revert "add 'noop' param to some more tests"
ivanhofer Nov 12, 2020
ae47317
Revert "add 'noop' param to all tests"
ivanhofer Nov 12, 2020
f5f56ce
pass correct add_css method
ivanhofer Nov 12, 2020
9a4cf3c
remove noop from from customElement init
ivanhofer Nov 12, 2020
83d5fe8
fix tests using new add_css methods
ivanhofer Nov 12, 2020
bf87612
Revert "init $$ in component with empty object value"
ivanhofer Nov 12, 2020
b80990d
rename 'that' to 'component'
ivanhofer Nov 14, 2020
4f9a15e
slightly improve output size by refactoring add_css method
ivanhofer Nov 14, 2020
b0cacd7
slightly improve output size by refactoring add_css method
ivanhofer Nov 14, 2020
5c3038c
fix tests
ivanhofer Nov 14, 2020
6d85265
move document.head to addCssToComponent method
ivanhofer Nov 14, 2020
b6e9574
use snake_case for method names
ivanhofer Nov 14, 2020
e8065eb
don't save customStyleTag in component.$$
ivanhofer Nov 20, 2020
b38e2b7
refactor add_css_to_component to not save customStyleTag in component.$$
ivanhofer Nov 20, 2020
f611abc
fix failing tests
ivanhofer Nov 20, 2020
7b56dba
Merge branch 'master' into customStyleTag
ivanhofer Nov 20, 2020
d54ab8c
move append_styles function to dom.ts
ivanhofer Nov 20, 2020
ada8969
fix failing tests
ivanhofer Nov 20, 2020
801001d
add docs for customStyleTag
ivanhofer Nov 25, 2020
a474470
Merge branch 'master' into customStyleTag
ivanhofer Nov 25, 2020
9c82611
Merge branch 'master' into customStyleTag
ivanhofer Nov 29, 2020
54d3e91
add customStyleTag to component constructor options
ivanhofer Nov 29, 2020
ee78985
update style_manager to use customStyleTag
ivanhofer Nov 29, 2020
903e410
Merge branch 'master' into customStyleTag
ivanhofer Dec 2, 2020
7a79cfc
Merge branch 'master' into customStyleTag
ivanhofer Dec 3, 2020
901204e
add customStyleTag to component constructor options
ivanhofer Dec 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions site/content/docs/03-run-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ The following initialisation options can be provided:
| `props` | `{}` | An object of properties to supply to the component
| `hydrate` | `false` | See below
| `intro` | `false` | If `true`, will play transitions on initial render, rather than waiting for subsequent state changes
| `customStyleTag` | `document.head` | An `HTMLElement` the styles should be appended to. It will only work if the component was compiled with the [`css: true` option](docs#svelte_compile).

Existing children of `target` are left where they are.

Expand Down
13 changes: 6 additions & 7 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ export default function dom(

if (should_add_css) {
body.push(b`
function ${add_css}() {
var style = @element("style");
style.id = "${component.stylesheet.id}-style";
style.textContent = "${styles}";
@append(@_document.head, style);
function ${add_css}(options) {
@append_styles(options, "${component.stylesheet.id.replace('svelte-', '')}", "${styles}");
}
`);
}
Expand Down Expand Up @@ -536,8 +533,10 @@ export default function dom(
class ${name} extends ${superclass} {
constructor(options) {
super(${options.dev && 'options'});
${should_add_css && b`if (!@_document.getElementById("${component.stylesheet.id}-style")) ${add_css}();`}
@init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment': 'null'}, ${not_equal}, ${prop_indexes}, ${dirty});

${should_add_css && b`${add_css}(options);`}

@init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment' : 'null'}, ${not_equal}, ${prop_indexes}, ${dirty});
${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: create_fragment.name });`}

${dev_props_check}
Expand Down
1 change: 1 addition & 0 deletions src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface T$$ {
on_mount: any[];
on_destroy: any[];
skip_bound: boolean;
customStyleTag?: HTMLElement;
}

export function bind(component, name, callback) {
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/internal/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export class SvelteComponentDev extends SvelteComponent {
$$prop_def: Props;

constructor(options: {
target: Element;
target: Element | ShadowRoot;
customStyleTag: Element | ShadowRoot;
anchor?: Element;
props?: Props;
hydrate?: boolean;
Expand Down Expand Up @@ -216,7 +217,8 @@ export class SvelteComponentTyped<
$$slot_def: Slots;

constructor(options: {
target: Element;
target: Element | ShadowRoot;
customStyleTag: Element | ShadowRoot;
anchor?: Element;
props?: Props;
hydrate?: boolean;
Expand Down
22 changes: 22 additions & 0 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { has_prop } from './utils';

let appendStylesTo = document.head;

export function append_styles(
{ customStyleTag },
styleSheetId: string,
styles: string,
styleId:string = `svelte-${styleSheetId}-style`) {

if (customStyleTag) appendStylesTo = customStyleTag;

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: Node, node: Node) {
target.appendChild(node);
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/internal/style_manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { element } from './dom';
import { append_empty_stylesheet } from './dom';
import { raf } from './environment';

interface ExtendedDoc extends Document {
Expand Down Expand Up @@ -31,7 +31,7 @@ export function create_rule(node: Element & ElementCSSInlineStyle, a: number, b:
const name = `__svelte_${hash(rule)}_${uid}`;
const doc = node.ownerDocument as ExtendedDoc;
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 = {});

if (!current_rules[name]) {
Expand Down
10 changes: 4 additions & 6 deletions test/js/samples/collapses-text-around-comments/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
SvelteComponent,
append,
append_styles,
attr,
detach,
element,
Expand All @@ -13,11 +14,8 @@ import {
text
} from "svelte/internal";

function add_css() {
var style = element("style");
style.id = "svelte-1a7i8ec-style";
style.textContent = "p.svelte-1a7i8ec{color:red}";
append(document.head, style);
function add_css(options) {
append_styles(options, "1a7i8ec", "p.svelte-1a7i8ec{color:red}");
}

function create_fragment(ctx) {
Expand Down Expand Up @@ -58,7 +56,7 @@ function instance($$self, $$props, $$invalidate) {
class Component extends SvelteComponent {
constructor(options) {
super();
if (!document.getElementById("svelte-1a7i8ec-style")) add_css();
add_css(options);
init(this, options, instance, create_fragment, safe_not_equal, { foo: 0 });
}
}
Expand Down
11 changes: 4 additions & 7 deletions test/js/samples/css-media-query/expected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* generated by Svelte vX.Y.Z */
import {
SvelteComponent,
append,
append_styles,
attr,
detach,
element,
Expand All @@ -11,11 +11,8 @@ import {
safe_not_equal
} from "svelte/internal";

function add_css() {
var style = element("style");
style.id = "svelte-1slhpfn-style";
style.textContent = "@media(min-width: 1px){div.svelte-1slhpfn{color:red}}";
append(document.head, style);
function add_css(options) {
append_styles(options, "1slhpfn", "@media(min-width: 1px){div.svelte-1slhpfn{color:red}}");
}

function create_fragment(ctx) {
Expand All @@ -41,7 +38,7 @@ function create_fragment(ctx) {
class Component extends SvelteComponent {
constructor(options) {
super();
if (!document.getElementById("svelte-1slhpfn-style")) add_css();
add_css(options);
init(this, options, null, create_fragment, safe_not_equal, {});
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/sourcemaps/samples/compile-option-dev/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const b64dec = s => Buffer.from(s, 'base64').toString();

export async function test({ assert, css, js }) {

// We check that the css source map embedded in the js is accurate
const match = js.code.match(/\tstyle\.textContent = "(.*?)(?:\\n\/\*# sourceMappingURL=data:(.*?);charset=(.*?);base64,(.*?) \*\/)?";\n/);
// We check that the css source map embedded in the js is accurate
const match = js.code.match(/\tappend_styles\(options, ".{6}", "(.*?)(?:\\n\/\*# sourceMappingURL=data:(.*?);charset=(.*?);base64,(.*?) \*\/)?"\);\n/);
assert.notEqual(match, null);

const [mimeType, encoding, cssMapBase64] = match.slice(2);
Expand Down