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

CSS naming conventions for JS targeting #11

Open
afercia opened this issue Apr 24, 2017 · 2 comments
Open

CSS naming conventions for JS targeting #11

afercia opened this issue Apr 24, 2017 · 2 comments

Comments

@afercia
Copy link
Contributor

afercia commented Apr 24, 2017

Splitting this out from #6

See also discussion on Slack during Settings API meeting:
https://wordpress.slack.com/archives/C02RQBWTW/p1493049728130607

The main goal should be establishing a solid convention for naming CSS classes or ID attributes meant to be exclusively used for JS targeting. Quoting from Helen's doc"

Reusing styling class names is fragile and obscures intent when doing long-term maintenance.

@afercia
Copy link
Contributor Author

afercia commented Apr 27, 2017

One good example of this is what Twitter does. They use CSS classes, regardless if they're used to target unique elements or multiple elements.

Some random examples:

js-global-actions
js-search-form
js-shortcut
js-signout-button
js-initial-focus
js-actionMomentMakerAddTweetToOtherMoment
js-toggleState
js-actionButton
wtf-module js-wtf-module
js-close
js-focus-on-open
js-last-tabstop
js-selectable
ProfileTweet-actionButton u-textUserColorHover dropdown-toggle js-dropdown-toggle
IconContainer js-tooltip
js-first-tabstop
share-via-dm js-actionShareViaDM
copy-link-to-tweet js-actionCopyLinkToTweet
embed-link js-actionEmbedTweet

Notice how they separate CSS classes meant for styling from the ones meant for JS, for example: dropdown-toggle js-dropdown-toggle

@felixarntz
Copy link
Contributor

js- prefixed classes is certainly the right way for multiple elements in my opinion, and I'm also slowly changing my mind that even more distinct cases using a JS-specific class instead of the ID makes sense. :)

What should the structure of the class names be like? Can we specify patterns like js-{element} (for general functionality, such as making every element with that class a datepicker for example) and js-{context}-{action} (for more specify things)? Those are only very basic examples, I'm generally wondering whether we can even come up with such a flexible pattern set or whether we need to decide that per use-case.

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

No branches or pull requests

2 participants