Problem/Motivation
We expose many class attributes in templates and other layers (for example preprocess), but it's never clear which classes are needed for JavaScript functionality, leaving themers in the dark if they want to slim down their markup and not break JS.
Proposed resolution
Add CSS classes for JavaScript functionality. Classes will use the original name and be prefixed with .js- and be added to the existing class (in Classy) for separating design and functionality.
Classy
<div class="foo"> -> <div class ="js-foo foo">
Core
<div class="foo"> -> <div class ="js-foo">
This is a step along the way to using data attributes, but even if we don't get to data attributes it's still an improvement.
Remaining tasks
See child issues.
User interface changes
None for themes extending Classy. Possible visual changes to themes not extending Classy.
API changes
n/a
Beta phase evaluation
| Issue category | Task because nothing is broken |
|---|---|
| Issue priority | Not critical because nothing is broken |
| Unfrozen changes | Unfrozen because it mostly just affects templates and JS which is not frozen |
| Prioritized changes | The main goal of this issue is to improve themer experience and arguably it reduces fragility where JavaScript and markup intersect. |
| Disruption | Shouldn't be too disruptive as it is mostly affecting CSS classes that are added to markup. Themes extending Classy will only have classes added. Themes not extending Classy will have classes removed but they can be added back via template overrides. |
| Comment | File | Size | Author |
|---|---|---|---|
| #87 | interdiff-2431671-js-prefix-out-of-scope.txt | 1.89 KB | star-szr |
| #84 | 2431671-84.patch | 56.59 KB | star-szr |
Issue fork drupal-2431671
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
mortendk commentedComment #2
mortendk commentedComment #3
mortendk commentedComment #4
mortendk commentedComment #5
mortendk commentedComment #6
mortendk commentedComment #7
mortendk commentedfirst stap at this
.form-required.js-form-requiredis used for the selectors .find & .toggleclass where its used for jsthe original form-required is still used for styling.
classes prefixed as selectors
.js-form-item, .js-form-submit, .js-form-wrapper js-form-requiredComment #8
mortendk commentedComment #10
mortendk commented.js-form-wrappercleaned upComment #11
mortendk commentedComment #13
mortendk commentedComment #15
mortendk commentedComment #16
mortendk commented.js-field-parentprefixedComment #18
mortendk commentedComment #19
mortendk commentedComment #20
rteijeiro commentedRe-rolled and tested. Everything seems ok so mortendk didn't break anything :P
Form BEFORE
Formn AFTER
Comment #21
mortendk commentedComment #22
rachel_norfolkWhere we are inserting the js-**** class, there are a couple of examples of where we remove some **** clsses that *might* be expected by themers. Some themers might be tempted to then use the js-**** classes to apply styles and that's not what we want.
An example would be
While it's a pain to add classes rather than remove them, I don't think we have a choice here.
Could always bite the bullet and go for data attributes...
Comment #23
rachel_norfolkI'm going to have a go at these data attibute things - might need people leaning over my shoulder checking...
Today and probably tomorrow at #dclondon.
Comment #24
rachel_norfolkSo after much whiteboard writing and crossing out with Morten, I've just added back in any "plain" (think non-js-***) classes back into the code.
We can now look to take all the js-classes and make them into data-* attributes next - once we decide on a naming scheme for them!
Comment #25
rachel_norfolkAs a note of what we have found out so far about data-* attributes: (with help from JanneKalliola)
Comment #26
jessebeach commentedUsing
data-js-*attribute names is fine. It's similar to how other frameworks, like Angular, structure functional attributes: https://docs.angularjs.org/guide/directiveComment #27
rachel_norfolkRight - I'm going to have a go at it then!
Comment #28
nod_We can do this in two steps, having classes prefixed by js- will allow CSS folks to figure out their dependencies on those classes without messing up the js. That looks done no?
Once we're there we can go back to the JS and see what to do on the JS side. For the data attribute that's being discussed in #1305882: drupal_html_id() considered harmful; remove ajax_html_ids to use GET (not POST) AJAX requests too. I think we should have a
drupalin the name somewhere to avoid conflicts with other frameworks. In the issuedata-html-idis proposed (I would preferdata-drupal-idbut that's a detail).Comment #29
rachel_norfolkSo, for my own memory as much as anything on a Sunday after a Drupalcamp Saturday night:
Does that sound good and should it be written down somewhere more useful than the middle of an issue?
Comment #30
mortendk commentedyes it should and make sense :)
... but im to hungover right now to figure out where it should be done
Comment #31
davidhernandezRE: #29, 3 - do we need to ever add a class via JS? Can we not set a data attribute? If the use case is to add a class that is used for style, that sohuldn't need a js- prefix.
Comment #32
attiks commented#31 Adding classes using javascript is still possible, and most third party plugins will still use it, I guess this issue is more of what is outputted by the server.
#29 I think it is best to update the issue summary to reflect your proposal
Comment #33
mortendk commented+1 for getting the css - js seperation done now then we can figure out the data-attributes later. The seperation is the important thing here.
Comment #34
mortendk commentedI would (humbly) suggest that we move the data- attribute discussion to a follow gets the js- & css seperation done now.
Then we can move on and clean up the css & make the banana seperation work, which this is a blocker for (a banana blocker)
Im getting very nervous that well be hanging in "make perfect" solutions, so we never get to the most important thing in Drupal 8 (...) Which is separate & clean up so the CSS dont screw with the JS.
im reuploading patch #18 for review renamed to #34
Comment #36
mortendk commentedComment #38
mortendk commentedComment #39
rteijeiro commentedShould this be:
js-form-disabled?Should this contain
form-wrapperclass too?Should we keep this?
Should we keep
form-itemandform-wrapperclasses?Should we remove this one then?
Should we keep this one?
Comment #40
mortendk commented1. form-disabled - nope form disabled is just styling?
Afaik we dont have a policy that says that classes that gets added by js needs to be prefixed ?
2. nope it should only test on the
.js-form-wrapper, if it test on form-wrapper then it would break if a theme did.epic-form-wrapper__something--clever3.
.form-requiredisp ure styling as well, no functionality4. nope everything used by javascript we should make with a prefix, so themer dont break something by accident. so we dont wanna keep the non prefixed in core. we wanna make it clear that its "only" visual class
5 + 6 yes we should both of em, to make sure we dont have created any dependencies on em :)
Comment #41
mortendk commentedComment #42
rteijeiro commentedSo then this should be removed too.
And this one should be removed as well.
Comment #43
mortendk commented1+2 nope they are both in classy :) so no cleanup there (yet)
Comment #44
rachel_norfolkI've had a look through the code and it looks good to me. I'm not an expert on the test classes so would like someone to check those are still valid.
The forms js continues to run without error on the fields I have tested. The js-* classes all appear to be in place. Any js only uses js-* classes as selectors, as desired.
The form appearance has not been changed by this patch.
Comment #45
rteijeiro commentedBack to RTBC I guess.
Comment #47
star-szrA few points/questions, this is looking very good overall though!
Hate to say it but: grammar! "…fields 'form-required' & 'js-form-required' classes are appended…"
Trailing comma here please to match our array coding standards: https://www.drupal.org/coding-standards#array.
I gather from #39 #40 why the core templates remove form-required but the classy templates do not. But just want to mention styling for form-required still exists in system.theme.css and make sure we're cool with that :) I am probably missing a fundamental banana concept here.
Short a space after the first comma :)
Should we split both Classy's fieldset and container classes out into a
set classesblock? We usually do when it's more than one or two or if there's complex logic, I think these both fit that criteria.Comment #48
mortendk commented@cottser
3. yes
.form-requiredis styling that we dont care about for core templates - but do in classy. When we get there in bananan #3 aka the-big-css-reconstruction-road-to-nirvana well take care of that part in short yes were very cool with that (+ system.css have so much stuff in it tthat it needs a proper cleaning to)fixed the others issues and split ud and set a classes for the templates with multiple classes in, its easier to read & play with :)
new patch & interdiff is uploaded & i even fixed the grammar ;) ... /me thinx
Comment #49
star-szrCool, interdiff doesn't make sense but I eyeballed the changes and they all look good but the grammar part seems to have stayed the same, my evil eyes are not detecting any changes to those lines :(
Comment #50
mortendk commentedevil eye cottser do it again
patch & interdiff uploaded
I guess we also need a change record to this ?
Comment #51
star-szrLooking better, thanks @mortendk, add two characters and I'm happy :)
class = classes :)
More importantly, were the changes to indentation.html.twig and theme_indentation() removed on purpose because of #2449445: Add "indentation" class back to indentation theme hook, use it for styling? I noticed earlier patches had those changes and it may conflict but I think we should add that back here because importantly the patch here adds both
indentationandjs-indentationto the theme function and template file, one for CSS, one for JS.I think a change record would be great here. I noticed the change record for #2426595: Rename indentation class to js-indentation didn't get published so let's keep it that way and work on a bigger change record to encompass all of these changes.
Also we need a beta evaluation in the issue summary and the issue summary needs a bit of love. And the banana tag is like the batman version :)
Comment #52
mortendk commented.js-indentation&.indentationare fixed in other issues so no need to have it in here as well. just checked it looks right.yes classes as there's more than one.
+1 lets do a big class update for js form's, else ppl would be confused.
So i have no idea of what should be in a beta evalution? need a bit of pointers there
Comment #53
star-szrDreditor has a beta evaluation template button, I can help with that later though if you ping on IRC.
On the indentation it's a bit complicated IMO because we also have a theme function which is the default output unless we add a theme function override in Classy. Right now there is a mismatch.
For practical purposes I think we need to do one of these:
indentationandjs-indentation.indentationclass and removeindentationfrom theme_indentation(). That way non-classy would never get theindentationclass, and Classy would, consistently. Classy doesn't have a .theme now but we could add one?Edited to make the second option make sense for both Classy and core's default markup.
Comment #54
mortendk commented@cottset updated the patch #2449445: Add "indentation" class back to indentation theme hook, use it for styling so we keep consistent with class="js-foo foo" lets keep em seprated and not put that in here
Comment #55
mortendk commentedupdates summary
Comment #56
mortendk commentedRemoved the js-indentation its done over in #2449445: Add "indentation" class back to indentation theme hook, use it for styling
should be ready to go
Comment #57
mortendk commentedComment #58
mortendk commentedComment #59
davidhernandezComment #60
davidhernandezComment #62
mortendk commentedsigh removed a test for the indentation ...
Comment #64
mortendk commentednow no more indentation mess :/
Comment #65
mortendk commentedComment #66
star-szrThere is a line in field_ui.js that needs to be changed to actually use the js- class:
$row.find('select.field-parent').val('');There is also this line in field_ui.inc that should be updated:
'#attributes' => array('class' => array('field-parent')),core/misc/machine-name.js:
var $wrapper = $target.closest('.form-item');core/modules/filter/filter.filter_html.admin.js:
that.$allowedHTMLDescription = that.$allowedHTMLFormItem.closest('.form-item').find('.description');Should we update language-negotiation-configure-form.html.twig?:
core/modules/locale/locale.admin.js:
$rowToMark.find('td:first-child .form-item').append(marker);field-multiple-value-form.html.twig?:
<div class="form-item">form-element.html.twig?:
I'm stopping here for form-item. Search yourself for form-item and you'll find a lot of things that probably need to be updated throughout core.
I know you're having fun but IMO these changes are out of scope :)
I only grepped
field-parentandform-itemwith the patch applied, this needs to be gone through very carefully I think.Also this part from the issue summary is not clear to me, what's the difference in the two lists? "classes added & used by js" and "selectors changed in js"?
Comment #67
mortendk commentedComment #68
mortendk commented@cottset man i hate when your right ;)
Comment #69
star-szrNot sure if we have a higher level meta yet, so adding in this old issue about using data attributes so it doesn't get lost.
Not saying we need to change direction here :)
Comment #70
mortendk commentedoooh man this is growing ;)
Comment #72
mortendk commentedrerolled
Comment #74
mortendk commentedfixing test
Comment #75
mortendk commentedComment #76
rteijeiro commentedRe-rolled!
Comment #77
star-szrWith the patch size being that much now, would it make any sense to break this down and make it a meta? Over in #2407565: Consensus Banana Phase 1, cleanup I just identified that form-managed-file should be js- prefixed as well.
Maybe we can do some logical grouping so we don't have merge conflicts and such, I'm not necessarily suggesting 1 class per issue.
Comment #78
mortendk commented@cottser im not sure it would make it easier for us to split it up - Its allready a pita as it is to get all around any forms, everytime we add on class in one place, it have to be added 200 other places, im afraid this is the easier way to do it & get it all done in one go, splitting it up i can see a huge amount of fun where we end up rerolling forever ;)
get this one in and the do followup as we find classes that needs a kiss with js- ?
Comment #79
star-szrI'm going for it (splitting this up), I think this needs a push forward and I think that is the best way. This patch as is can be maintained as is but it's going to be very hard IMO to get it reviewed and tested.
Comment #80
mortendk commentedcoool then lets split it up - cause yeah its huuuuuge (and a pita)
wheres the new issues then ;)
Comment #81
star-szrWill update here once done :)
Comment #82
nod_Split things big, all misc/ in one patch, all themes in one patch (they don't have much CSS) and at most one issue by module. One issue per file is horrible to manage, did it a couple of times for jshint/eslint and clean-up, won't do it again.
Comment #83
star-szrI was thinking of doing one issue per class in the issue summary, I figured that would be easier to test. I'm also not sure if this one could actually be split by module without leaving things in a broken state.
Comment #84
star-szrFirst a reroll.
Comment #86
star-szrAlmost done, I'm going to make the sub-issues tomorrow, update this issue's summary, and post an interdiff of the changes that didn't make it into any of the sub-issues (really just the out of scope type stuff I pointed out in #66.3).
Comment #87
star-szrUpdating issue summary and converting to a meta. All child issues have a patch. Attaching the interdiff here of the changes that didn't make the cut into the child issues.
Every other change from the latest patch here is in one of the child issues, I combined all the child issues into a git branch and diffed against the latest patch here to produce the attached interdiff.
Comment #88
alexpottI talked about this with @catch today... nothing conclusive :(
@alexpott: Any feelings about https://www.drupal.org/node/2431671?
@catch: I liked it until I saw the patch.
Looks like it's duplicating some class so they have with and without the prefix?
@alexpott: Yep
Because we still have styling that relies on the class too
I'm torn
because if you were developing your own cms the duplication would be stupid
but telling themers this is needed for javascript will save a lot of current headaches
@catch: So the issue is, if we ended up not needing it for js, then we might remove the class altogether, then the styling wouldn't work.
So having both stops that.
But... nothing stops someone styling based on a non-duplicated js-prefixed class.
Then you have the same problem.
So I'd say either duplicate everything, or don't duplicate at all.
@alexpott: Do you mind if I post some of this on the meta?
@catch: Fine with me.
Comment #89
star-szrWould going all the way and using data attributes for the JS functionality help? We saw this as a stepping stone but if it's too contentious…
Small comment:
Of course we can't stop someone styling with a js-prefixed class :), but: Classy always duplicates. So is the last line proposing that non-Classy duplicate the classes as well?
Comment #90
davidhernandezRE #88, there are a couple mitigating factors, though the big issue is whether we'll get them done in time for release. Most of the non-js classes would get removed from core templates, so the duplication would only be in Classy. Another factor is eventually moving these to data attributes, so we can get rid of the classes.
Right now splitting them off would let us keep moving forward with the whole declassification without having to rework all the JS.
Comment #91
davidhernandezCottser and I were just talking about this. We could skip straight to using data attributes, if that is more palatable. We'll need to evaluate how much work that is. We would also likely expand this, as the current issues are just tackling the form classes.
Comment #92
mortendk commented@david We talked about this a couple of times, my fear is that it will take forever to do that, the patch is allready huge.
We wanna go for the win here that gives us the seperation thats so important for the banana & headaches all over the frontend world, creating data-attributes we can then do later, when a solution is found for that
@alex yes themers can do dumb things & we can't stop em. but if provided with the right tools we can prevent much of the dumb things ;)
Comment #93
ksenzeeEven if you were developing your own CMS, the duplication might well be useful. I do something similar when I'm creating markup for our themers (which they lovingly call "unstyled garbage"). Any class that is added for the benefit of people writing JS gets a js- prefix. Any class added for the CSS folks gets no prefix. It makes changing markup later so much easier.
Moving straight to data attributes would be a fine idea if there were time, but using classes isn't yucky or anything. It's still a nice improvement.
Comment #94
alexpott@ksenzee - yep I agree - I'm +1 on the approach of duplicating the classes. I'll try to get consensus with the other maintainers.
Comment #95
lauriiiDo we have consensus on the approach?
Comment #96
davidhernandezI think in the three issues remaining we are going with the classy approach, to keep it simple and get the prefixed classes in.
<div class="foo"> -> <div class ="js-foo foo">Comment #97
nod_Yeah going straight to data was risky so we agreed on doing all the js- prefix first, which will make the move to data attributes easier afterwards.
Comment #98
davidhernandezAll done!
Comment #99
nod_This is not done, there are a bunch of scripts still using classes. Like toolbar, tabledrag or blocks
Comment #100
davidhernandezWell, done as far as the child issues we outlined were needed for the Classy changes. I think that was mostly the form items. Are we going to work on others? I'm not sure whether we can do that post release.
Comment #101
lewisnymanWe should be able to if we don't remove the old classes
Comment #104
andypostComment #118
quietone commentedComment #120
quietone commentedThere hasn't been discussion here for 10 years. Is this still relevant to a currently supported version of Drupal.
Is there work here to do?
Comment #121
rachel_norfolkWow. This is a blast from the past!
This issue was a stepping stone on the way to data-* attributes we now know and love.
We love them, right? ;-)
So, this work has now been superseded and this issue can be closed. To be honest, it was part of what got us to where we are so, whilst it was never merged, I think I could argue it was “fixed”.
Comment #122
rachel_norfolkI’ll let a maintainer make that decision, though.