Comments

mherchel created an issue. See original summary.

nod_’s picture

we prefer using data- attribute for JS related selection, that way themers are free to do whatever they need with classes without breaking the JS. It came up a few times the past weeks so something must not be clear on core side.

Essentially: js- prefix: better than a "simple" class, dedicated data- attribute = best.

mherchel’s picture

Title: All JS selectors should be prefixed with js- » JavaScript should be selecting [data-*] attributes (or js- prefixed classes)
mherchel’s picture

Thanks!

mherchel’s picture

Issue tags: +JavaScript
thedrupalkid’s picture

Assigned: Unassigned » thedrupalkid
thedrupalkid’s picture

As I see we are adding data attribute for js classes, I have spent some time looking for the classes which actually needs to be prefixed but found either they already have a data attribute or they are used both in CSS as well as js. Can you guys point to what more to be expected in this issue?

Pooja Ganjage’s picture

StatusFileSize
new23.43 KB

Hi,

I am applying patch for adding js prefix class in js files.

Kindly review once.

thedrupalkid’s picture

Hey @Pooja, We have to update the DOM also with the same changes but that is not needed until and unless the class is used only in js, as @nod_ also commented they prefer data- prefix. Please read my above comment as this will not help us updating the prefixes as they are used in js as well as the same classes are used in CSS.

Let me provide you an example if we change .site-branding__inner class it is used in the below template.

/olivero/templates/block/block--system-branding-block.html.twig

<div class="site-branding__inner">
    {% if site_logo %}
      <a href="{{ path('<front>') }}" rel="home" class="site-branding__logo">
        <img src="{{ site_logo }}" alt="{{ 'Home'|t }}" />
      </a>
    {% endif %}
    {% if site_name %}
      <div class="site-branding__text">
        {% if site_name %}
          <div class="site-branding__name">
            <a href="{{ path('<front>') }}" title="{{ 'Home'|t }}" rel="home">{{ site_name }}</a>
          </div>
        {% endif %}
      </div>
    {% endif %}
  </div>

Now if you are updating in js DOM also needs to be updated and the same class we are using here is CSS line number 79, it will break if you update the class in DOM.
/olivero/css/dist/components/header-site-branding.css

My concern is the same in the above comment:

I have spent some time looking for the classes which actually needs to be prefixed but found either they already have a data attribute or they are used both in CSS as well as js

Pooja Ganjage’s picture

Yes you are right, but can I provide different css file name for js file.

thedrupalkid’s picture

Let's wait for maintainers comment, I'll try to connect via slack with them.

Pooja Ganjage’s picture

Okay

Thanks

mherchel’s picture

Thanks for working on this!

I have spent some time looking for the classes which actually needs to be prefixed but found either they already have a data attribute or they are used both in CSS as well as js

If the classes are used in CSS, the class name needs to be left as is, and add data attributes or js- prefixed class names as needed.

If you already see a data attribute, you should use this in the JS. Many of the templates were copied over from themes such as Claro and Classy, so the data attributes may already exist.

thedrupalkid’s picture

Welcome @mherchel, Let's me scan through all the js specific attribute and add prefixes.

Pooja Ganjage’s picture

StatusFileSize
new141.35 KB

Hi,

I am applying patch for this issue.

Kindly review the patch once.

Thanks.

mherchel’s picture

Status: Active » Needs work

Thanks for the work @Pooja Ganjage

We only want to change the selectors that the JavaScript is querying. We do not want to make changes to the CSS.

So, you'll need to add additional data attributes (or classes) into the DOM, and then make changes to the JavaScript to use these new attributes or classes. Please do not make changes to the CSS.

mherchel’s picture

Looking further at the patch, I think you only did a search and replace. Did you test your patch yourself?

thedrupalkid’s picture

I have scanned all the classes and working on a patch now. Moving all the required classes to js- prefix.

thedrupalkid’s picture

Assigned: thedrupalkid » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.86 KB

Hello @mherchel, I have scanned all the js files and applied the prefix in the patch. Please review.

here is the approach I have followed and made changes


<strong>
js/comments.es6.js
js/comments.js
</strong>

show-hide-wrapper changed to .js-show-hide-wrapper

<strong>Debug.js No change</strong>
.site-branding__inner (header-site-branding.css)
.site-branding.css (site-branding__logo)
.site-branding__name (site-branding__logo)
.olivero-debug(debug.css)
.is-always-mobile-nav(feature-bottom.css)

<strong>
js/messages.es6.js
js/messages.js
</strong>

.messages__container(message.css)
.messages__button
.messages__close
.visually-hidden (Used in multiple css files off-canvas-base.css)
.messages-processed Changed to .js-messages-processed 

All other files either have the js prefix or do exist in css to which the change is not required.

thedrupalkid’s picture

StatusFileSize
new2.57 KB

Please review this one as the above is not related and uploaded by mistake.

thedrupalkid’s picture

StatusFileSize
new2.28 KB

Removing new line change in this patch.

Pooja Ganjage’s picture

StatusFileSize
new3.86 KB

Hi,

thedrupalkid you missed some where to include js prefix class.

I am applying updated patch.

Kindly review once.

Let me know for any correction.

Thanks.

thedrupalkid’s picture

@Pooja: Thanks for the patch, as Mike has suggested testing changes before creating a patch will be beneficial and the additional changes will break the js functionality as we are using it in CSS as well as in DOM.
Changes to this:

const overlay = document.querySelector('.js-overlay');
".js-overlay" does not exist.

Also used on page.template.twig which is very generic and break everything:

Also changing this

-    if (isTabsMobileLayout() && !activeTab.matches('.tabs__tab:first-child')) {

will affect this:

<li{{ attributes.addClass('tabs__tab', is_active ? 'is-active') }}>

So these additional changes are not required.

Pooja Ganjage’s picture

Okay Thanks @thedrupalkid for your suggestions.

Pooja Ganjage’s picture

mherchel’s picture

Title: JavaScript should be selecting [data-*] attributes (or js- prefixed classes) » Olivero JavaScript should be selecting [data-*] attributes (or js- prefixed classes)
Project: Olivero » Drupal core
Version: 8.x-1.x-dev » 9.1.x-dev
Component: Code » Olivero theme
Status: Needs review » Needs work
Pooja Ganjage’s picture

StatusFileSize
new2.28 KB

Hi,

I am creating a patch for this issue on 9.1.x-dev drupal version.

Please review the patch.

Thanks.

Pooja Ganjage’s picture

Status: Needs work » Needs review
mherchel’s picture

Status: Needs review » Needs work

@Pooja Ganjage Please be sure to test your patches before submitting them.

There are a couple problems with this patch:

1. You changed the selectors, but you didn't change the markup. This will break the functionality.
2. See @nod_'s comment in #2. We should try to use data- attributes instead of js- prefixed classes.

Pooja Ganjage’s picture

StatusFileSize
new17.16 KB

updated patch.

mherchel’s picture

@Pooja Ganjage Once again, please be sure to test your patches before submitting them. The classes that you are adding do not exist. We also want to move away from classes and use data attributes (not data- prefixed classes) as selectors.

mherchel’s picture

sarvjeetsingh’s picture

Status: Needs work » Needs review
StatusFileSize
new1.45 KB

I have set a data attribute instead of class 'show-hide-wrapper'.

mherchel’s picture

Status: Needs review » Needs work

We need to make this changes to all JS selectors, not just one.

Pooja Ganjage’s picture

StatusFileSize
new4.97 KB

Hi,

Creating a patch to including changes to JS selectors.

Kindly review the patch.

Let me know if any suggestions.

Thanks.

Pooja Ganjage’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 35: 3153234-35.patch, failed testing. View results

paulocs’s picture

Assigned: Unassigned » paulocs
paulocs’s picture

Assigned: paulocs » Unassigned

messageWrapper.setAttribute('data-messages-list__item messages messages--', "messages-list__item messages messages--".concat(type, " messages-processed"));

which attribute is data-messages-list__item messages messages--? I'm not sure how to replace it, but it is seems wrong like that.

Pooja Ganjage’s picture

StatusFileSize
new4 KB
Pooja Ganjage’s picture

Status: Needs work » Needs review
mherchel’s picture

Status: Needs review » Needs work

@Pooja Ganjage The selectors are what needs to be changed, not the CSS classes that are being modified. The patch that you submitted will break a lot of functionality. Did you test this?

I appreciate the enthusiasm, but in the future, can you please attach videos of the changes working properly when submitting a patch?

agarzola’s picture

Status: Needs work » Needs review
StatusFileSize
new4.66 KB

Hi. I think I understand what needs to happen here. How would you feel about using data-olivero-* attributes for these selectors? I think it would be beneficial to set a convention whereby not only do we use data-* attribute selectors (a direction which I very much support), but we go one step further and scope these selectors to be Olivero-specific by using an olivero- prefix (hence data-olivero-*). This is not unlike Drupal’s own data-drupal-* attribute selectors (data-drupal-nav-tabs, for instance).

I’ve created a very contrived patch that accomplishes my proposed solution for the selectors in tabs.es6.js as well as associated template files to get opinions on this direction. Please bear in mind this is by no means an exhaustive patch; it is only meant to illustrate how this idea would look before spending any significant amount of time on this.

I look forward to hearing your feedback. Thanks!

markdorison’s picture

StatusFileSize
new5 KB

Re-rolled patch in #43.

pcate’s picture

How would you feel about using data-olivero-* attributes for these selectors?

I think this could cause confusion down the line for themes that use Olivero as a base theme, or use a contrib theme that uses Olivero as a base theme. I'm thinking particularly of how extensively classy is used by custom and contrib themes as a base theme.

Also, if this naming convention was used for attributes in a JS theme function (which can be overridden by a theme), this could break functionality. While that could technically happen with any data- attributes, having the theme name in the attribute might give the mistaken impression that they are not needed or should be changed to include the theme's name. e.g. data-awesome-theme-*

If using an additional identifier for data- attributes is needed, I would prefer a more generically named one. Is there a reason data-drupal-* should not be used? I'm not sure if that convention is only supposed to be used for core modules.

pcate’s picture

@mherchel would it perhaps be better if this issue became a meta issue and separate sub-issues were created for the changes to each JS file (and associate twig template)? It might make it easier to review changes, test, and spread out the workload.

agarzola’s picture

I think this could cause confusion down the line for themes that use Olivero as a base theme, or use a contrib theme that uses Olivero as a base theme. I'm thinking particularly of how extensively classy is used by custom and contrib themes as a base theme.

Good point, @PCate.

Is there a reason data-drupal-* should not be used? I'm not sure if that convention is only supposed to be used for core modules.

This is why I was reluctant to suggest data-drupal-*, but given the points you stated above, I’m inclined to suggest we move forward without scoping (i.e. data-*).

---

Creating separate tasks for each JS file in the theme makes sense, especially from a reviewing/testing perspective. Happy to contribute to this issue further once we have a definitive way forward!

mherchel’s picture

Title: Olivero JavaScript should be selecting [data-*] attributes (or js- prefixed classes) » [Meta] Olivero JavaScript should be selecting [data-drupal-selector attributes where possible
Version: 9.1.x-dev » 9.3.x-dev
Issue summary: View changes

@mherchel would it perhaps be better if this issue became a meta issue and separate sub-issues were created for the changes to each JS file (and associate twig template)? It might make it easier to review changes, test, and spread out the workload.

Agree that this should be a meta issue. Converting it to one now.

mherchel’s picture

mherchel’s picture

Title: [Meta] Olivero JavaScript should be selecting [data-drupal-selector attributes where possible » [Meta] Olivero JavaScript should be selecting [data-drupal-selector] attributes where possible
Issue summary: View changes
mherchel’s picture

Issue summary: View changes
mherchel’s picture

Issue summary: View changes
mherchel’s picture

Status: Needs review » Active
mherchel’s picture

Issue summary: View changes
mherchel’s picture

Issue summary: View changes
mherchel’s picture

mherchel’s picture

Updating status

catch’s picture

Is it just #3212975: Olivero: Normalize JavaScript selectors in messages.es6.js left to do here? If so should we switch out the issues on #3177296: [META] Make Olivero stable and close this meta?

mherchel’s picture

Status: Active » Closed (outdated)

If so should we switch out the issues on #3177296: [META] Make Olivero stable and close this meta?

Done!