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

add multiple class toggles example #2517

Merged
merged 2 commits into from
Apr 25, 2019
Merged

add multiple class toggles example #2517

merged 2 commits into from
Apr 25, 2019

Conversation

CreaturesInUnitards
Copy link
Contributor

This answers the immediate question I had when I first saw the class:foo syntax.

This answers the immediate question I had when I first saw the `class:foo` syntax.
@codecov-io
Copy link

codecov-io commented Apr 23, 2019

Codecov Report

Merging #2517 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2517   +/-   ##
=======================================
  Coverage   91.83%   91.83%           
=======================================
  Files           1        1           
  Lines          49       49           
=======================================
  Hits           45       45           
  Misses          4        4

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e7885f2...644c02e. Read the comment docs.

@Conduitry
Copy link
Member

class:is-admin isn't a good example, as this generates code that sets the class according to the value is-admin - i.e., is minus admin. The compiler should actually be throwing an exception in that case.

@CreaturesInUnitards
Copy link
Contributor Author

@Conduitry
Oh interesting. Because of course this works...

<script>
	let name = 'world';
</script>

<style>
	.foo-bang { color: red }
	.bar { color: blue }
</style>

<h1 class:foo-bang={name} class:bar={!name}>Hello {name}!</h1>

Generates this JS:

...
p(changed, ctx) {
	if (changed.name) {
		toggle_class(h1, "foo-bang", name);
		toggle_class(h1, "bar", !name);
	}
}
...

@Conduitry
Copy link
Member

Right, the class names themselves can be any valid class name - it's when you don't have the ={whatever} part that the compiler assumes the variable name is the same as the class name.

class:is-admin is equivalent to class:is-admin={is-admin}, which is technically valid, but is almost certainly not something you'd want to do.

@CreaturesInUnitards
Copy link
Contributor Author

Ah, gotcha, duh. Camelcased it, thanks!

@Rich-Harris Rich-Harris merged commit f386786 into sveltejs:master Apr 25, 2019
@Rich-Harris
Copy link
Member

nice improvement, thank you

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

Successfully merging this pull request may close these issues.

4 participants