-
Notifications
You must be signed in to change notification settings - Fork 438
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
Use data attributes instead of class names to identify html elements #846
base: master
Are you sure you want to change the base?
Conversation
Thank you, but what is the benefit here? This adds another layer of complexity - handling both class and data attributes I can solve #738, I just chose not to because I don't want people to mess with the inner |
I agree that there is a certain overhead, but in my opinion it is definitely worth because of the following reasons:
|
What do you mean change the style as they want without any impact on the behavior? The styles are not coupled to the JS logic.. and you can add multiple class names per element (from the Tagify settings object) |
Currently, there is a connection between what you put in in settings.classNames and how the tagify control works (because whatever you put in the class names in order to style the control is used as selectors by tagify). Thus, there is a connection between the styles (class names) and the behavior (selectors). For example, if you add another completely unrelated html element that has the same class as your tagify tag's, then they are treated by tagify as a normal tag. In this way, styles are indeed coupled to the logic/behavior.
In my experience, this doesn't work reliably. Indeed, in a lot of places the selector is defined as |
That can never happen because the class names are very unique, unless someone did it intentionally, and therefore they probably know what they are doing.
This isn't a problem, I can only use the first class name as the JS selector. I initially used custom HTML tag names for the selectors and then switched to classes, but for bulletproofing things, I can switch back to custom HTML tags. I have been doing this for almost 20 years.. For example, this is the <tag title="${(tagData.title || tagData.value)}"
contenteditable='false'
spellcheck='false'
tabIndex="${this.settings.a11y.focusableTags ? 0 : -1}"
class="${this.settings.classNames.tag} ${tagData.class ? tagData.class : ""}"
${this.getAttributes(tagData)}>
<x title='' class="${this.settings.classNames.tagX}" role='button' aria-label='remove tag'></x>
<div>
<tagText class="${this.settings.classNames.tagText}">${tagData[this.settings.tagTextProp] || tagData.value}</tagText>
</div>
</tag> As you can see, these are the Tagify-only tags: For class names which are add/removed only by Tagify - they should stay as-is (as classes) Anyway, this and your PR are breaking-changes and can affect people that already use class names to style their tagify components. I would have to have to write a warning in the README for this specific change. |
Fair enough. This was meant more as an example to demonstrate the connection.
Currently, it's a problem. Using the first class name might work, based on context. For example, if one sets
If I may, I would advice not to use custom html tags for this purpose. Data attributes were developed especially for the purpose to provide a standard and well-recognized way to implement such meta-data into html elements. I think it would also not help with differentiating between different status of tags (editing, hiding, ...).
I think you misunderstood my PR then somewhat. I didn't change anything concerning the styles. In particular, all css classes are still applied as they are currently are, so existing users of the control don't need to change anything. It's a purely under-the-hood change, about how different states and controls are identified and thus should be completely backwards compatible. |
I see, I now better understand the intentions, for bulletproofing the selectors in the JS logic to, but Tagify also relies heavily on CSS so a developer should not do: settings.className.tag = "p-3 justify center-align" Because it will be missing the class name of the tag and thus break things visually, which then makes the component useless... I can understand why developers might want to add class names but cannot think of why would they completely remove the default ones..so I always expected developers do only add, keeping the default name because they obviously understand it's important to keep and should always be the first in the list of class names: 👇
settings.className.tag = "tagify__tag p-3 justify center-align" So I understand you want to de-couple these from the JS logic, so JS won't break, but the UI will break anyway without I don't think anyone would completely re-write the CSS so the I can add a section in the README to explicitly explain this whole thing |
That's actually my intention 😄. I started with this and then I discovered the current limitations when it comes to multiple classes, which lead me to this PR. Especially when integrating with popular CSS frameworks (such as Bootstrap, tailwind etc), quite a few changes to the css are necessarily and it is easier to start from 0. I agree that carrying for example "tagify__tag" around (and if it's only as a dummy class) would work in most use cases. But I do think that separating styling from the selectors used in the javascript code is advantageous and preferable over some conventions of css classes. |
yeah makes sense. |
@yairEO did you already found some time to look at this PR? |
Sorry... had some real-life-stuff wasting my time instead of coding.. I was supposed to go over this in the weekend but didn't get to it. This is high on my to-do list, so don't worry. Tagify is my hobby, along with dozens other projects on Github and I devote my time between them all |
Why did you choose |
Because there could be for example multiple tags, so |
Ho, THAT limitation :D |
@yairEO ping 😉 |
@tobiasdiez - hi, I was working hard on other open-source projects of mine since Tagify is already doing very well, so I only had time for bugfixes and not large changes such as this. Is this something which is urgent to you? |
@yairEO Don't worry. I'm maintaining a few open-source projects myself, so I know that time is a precious resource. I wouldn't say it's urgent, but nonetheless it would be nice to have this PR merged. It's a nice additional feature that users can now specify arbitrary style classes. Moreover, the longer this PR is open the larger the chance is that changes to the main branch are incompatible, possibly leading to bugs etc. |
This is a great idea! Going for data attributes is also what some frameworks propose to do more, e.g. StimulusJS |
I've started, will do more work on this soon. |
How is this going? Seems the project is not updated anymore. It was great! |
Yeah it's not really getting updates because of my time going to other things.. I have a billion open tabs on things to do or read and it seems I can barely keep up, This project isn't dead - I really want to get some things done. |
Yes, I understand what you mean perfectly. Still working great so that's ok. Still is one of the best solutions out there for tags ;) |
Instead of using class names, now data attributes are used to identify html elements. In particular, all selectors are reworked to no longer use class names. This makes it possible to use arbitary class names to style the elements, and in particular fixes #738.