-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Appending classes to nested components #289
Comments
What's the problem doing this yourself in component's markup? E.g.:
|
@mefjuu Nothing really, it just seemed like a natural thing to have. It's a bit nitpicky but currently every component that you want to pass classes to will need be made with this in consideration. |
This is one of the things spreading #280 would help with as you can pass all attributes to the child. |
Ehhhh I don't like the idea behind this. This is essentially saying that certain property/attribute names, when passed to components, will be treated specially. This feels like a gross change, and one that unnecessarily complicates the framework. |
I agree with @Conduitry |
As do I — there are a few reasons not to do this. Firstly, a component can have an arbitrary number of top-level elements — what happens in those situations? Does every element receive those attributes? Secondly, which attributes are applied? If the <div>{{class}}</div> ...does the Thirdly, what if the component template already specifies a class? <div class='foo'>...</div> Does it become Fourthly, depending on the answers to the questions above, the implementation gets hairy — every component would have to have extra code to accommodate this behaviour whether or not it benefitted from it, which runs counter to Svelte's philosophy. Fifthly, it takes control away from the component author — suddenly it's impossible to know how a component will actually be rendered while you're writing it. Sixthly, it's unnecessary! As @mefjuu points out, this doesn't actually add any expressive power. If you really needed to add arbitrary styles to the top level elements of a child component, you could do it like this: <div class='component-wrapper'>
<SomeComponent/>
</div>
<style>
.component-wrapper > * {
color: blue;
}
</style> For all those reasons I'm afraid I have to close this as wontfix! |
While I agree it's best to not automatically append the
Then in the component definition use it like so:
This currently does not work, apparently due to class being reserved word. While my example may not be convincing, I'm working on Svelte components for bootstrap , but users wrapping every component in a div to add classes is unwieldy, as is 'inventing' an attribute name such as 'className'. Would be great if svelte could provide a way to access easily. |
Hm that is a fair point. There is precedent for using a |
Yeah I actually just ran into this, since |
I wonder if we could tweak import { parseExpressionAt } from 'acorn';
export default function readExpression ( parser ) {
+ const start = parser.index;
+
+ const name = parser.readUntil( /\s*}}/ );
+ if ( /^\w$/.test( name ) ) {
+ parser.allowWhitespace();
+ parser.eat( '}}', true );
+ return {
+ type: 'Identifier',
+ start,
+ end: start + name.length,
+ name
+ };
+ }
+
+ parser.index = start;
try {
const node = parseExpressionAt( parser.template, parser.index );
parser.index = node.end;
// TODO check it's a valid expression. probably shouldn't have
// [arrow] function expressions, etc
return node;
} catch ( err ) {
parser.acornError( err );
}
} In other words, bypass Acorn altogether for straightforward cases like |
Ah, whoops — just noticed @Conduitry reopened this before I created the new issue. I'll close this anyway since #383 is more specific |
SomeComponent does not get the test class added.
This would be really helpful to have!
edit: doesn't work for
id
as well.The text was updated successfully, but these errors were encountered: