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

Setting inline styles with data elements breaks style #1830

Closed
mhkeller opened this issue Oct 30, 2018 · 3 comments · Fixed by #3432
Closed

Setting inline styles with data elements breaks style #1830

mhkeller opened this issue Oct 30, 2018 · 3 comments · Fixed by #3432
Labels

Comments

@mhkeller
Copy link

Here's a REPL demonstration: https://svelte.technology/repl?version=2.11.0&gist=02e988596c0d0888490384ec44d7f99c

Putting a data variable into an inline style tag will try to concatenate your new key:value; with the preceding value. The result is that this text, which should be 0.25 opacity and yellow has no styles applied to it at all.

<h1 style="opacity: 0.25;{color}">Hello {name}!</h1>

<script>
export default {
	data () {
		return {
			color: 'color: #fc0;'
		}
	}
}
</script>

This is the generated JavaScript:

setStyle(h1, "opacity", "0.25" + ctx.color);
@EmilTholin
Copy link
Member

Svelte will change to using cssText if the entire style attribute is taken from a variable:

<h1 style="{style}">Hello {name}!</h1>

<script>
export default {
  data() {
    return {
      style: 'opacity: 0.25; color: #fc0'
    }
  }
}
</script>

Just having the color value in a variable works as well:

<h1 style="opacity: 0.25;color:{color}">Hello {name}!</h1>

<script>
export default {
  data() {
    return {
      color: '#fc0'
    }
  }
}
</script>

It might be possible that a combination of both can be implemented by generating appropriate setStyle calls at runtime.

@Conduitry
Copy link
Member

#3309 is a duplicate of this issue, but has some more examples that we may or may not want to call bugs.

@loilo
Copy link

loilo commented Jul 29, 2019

In case these examples are not considered bugs, it may be a good idea to use static analysis to enforce that only values can be interpolated. It's pretty confusing as it is right now, especically because partial interpolation works just fine for other attributes.

That said, I strongly advocate for considering these behaviors bugs. Inline styles are highly volatile properties that should be met with the according flexibility.

Coming from a Vue background, high-level class/style usage is probably one of the things I struggle with the most, but that again is a topic for another discussion. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants