-
Notifications
You must be signed in to change notification settings - Fork 780
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
V2 class/className attribute API #754
Comments
I'd find this very handy to have built in. Most of my CSS lately is with utility libs like Tailwind, and manually building up the class combinations is tedious. I should probably use classcat, but sometimes laziness wins! I assume regular strings will still be useable also? |
Yup. You can use a string | array | object. See above, I added more examples. |
Great! This is exactly the function to be built-in 👏 |
I misread the title to mean JavaScript classes and was briefly like: 🤯 |
Oh, I love this feature in Vue! |
Since classes concatenation feature is not always necessary and useless for css-in-js approaches which usually calculate a single class name in runtime, I am worried about performance impact. Browser's DOM contains classList style data structure besides What about composable components, will you need to use something like |
@frenzzy Adding a
Let's measure thatWe don't want to optimize prematurely, so let's ask the real question here, what/which is faster? The results indicate that className vs V2 className are virtually the same and that classList is the slowest. |
Can you show us an example? I am not sure I understand what you mean. |
Hmm, so if I write a component that always adds its own classes but is also supposed to accept classes via props, I'd have to do const Component = props => {
const { class: classProp } = props;
let className;
if (Array.isArray(classProp)) {
className = [...classProp, 'predefined classes'];
} else {
className = [classProp, 'predefined classes'];
}
return (
<somemarkup class={className} />
);
}; Or will hyperapp always flatten the Array so you can wrap the received Edit: You can of course make it a little more concise with a ternary and shorter names, like const Component = ({ class: cls }) => {
const className = [Array.isArray(cls) ? ...cls : cls, 'predefined classes'];
// and so on
}; but I still don't think it's the optimal solution – unless I'm missing something here :) |
@jonaskuske 👍Arrays will be recursively flattened. <div class={[[["foo", ["bar", ["baz", [{ bam: true }]]]]]]}>Hello</div>
//=> <div class="foo bar baz">Hello</div> So, your Component can be written as follows. const Component = props => {
return (
<somemarkup class={[props.class, "predefined", "classes"]} />
);
}; |
Almost no effect on performance and super easy classes extension.. const Button = (props) => <button {...props} class={['material-ui-btn', props.class]} /> 💯 😍 |
Do these get rendered inline or do they compile to a stylesheet with generated classes? |
@TheNando This topic is just about the class list, not inline styles. Inline styles get attached to the DOM node properties. |
Nope, just added to the className property. |
Summary
I find myself using classcat quite often when I want to add or remove a class to/from a DOM element. The idea of conditionally adding classes in a declarative style suits the nature of the VDOM well.
With V2 I want to introduce the ability to toggle element classes on and off through the
class
attribute. This means that in addition to a string, you will be able to use an object or array for this attribute.The behavior will be adapted from classcat specifically for Hyperapp. Classcat will continue to exist as an independent package which can be used with other frameworks or as a standalone library.
Example
class
as a stringclass
as an objectclass
as an arrayPrior Art
👋 Do you think you'll find this useful? Do you have any ideas for improvements? What else would you like to see?
The text was updated successfully, but these errors were encountered: