Drupal core prefixes classes needed to provide Javascript funtionality wir 'js-'. Commerce should do so as well to indicate which classes can be removed and which are required. One example in commerce_cart:

Current

commerce_cart.module:

<?php
/**
 * Prepares variables for the cart block element template.
 */
function template_preprocess_commerce_cart_block(&$variables) {
  $variables['attributes']['class'][] = 'cart--cart-block';
}

?>

commerce_cart.js:

var $cart = $context.find('.cart--cart-block');

Should be

commerce_cart.module:

<?php
/**
 * Prepares variables for the cart block element template.
 */
function template_preprocess_commerce_cart_block(&$variables) {
  $variables['attributes']['class'][] = 'js-cart--cart-block';
}

?>

commerce_cart.js:

var $cart = $context.find('.js-cart--cart-block');
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Lukas von Blarer created an issue. See original summary.

Lukas von Blarer’s picture

Title: Prefex classes needed for JS functionality with 'js-' » Prefix classes needed for JS functionality with 'js-'
gauravjeet’s picture

Status: Active » Needs review
FileSize
7.26 KB

Have prefixed class names with 'js-'. Only those class names been changed that are being added into Library via #attributes.

https://github.com/drupalcommerce/commerce/pull/743

Thomas Cys’s picture

Great work but in my opinion .js-* classes should only be used to denote behaviour.

Lukas von Blarer’s picture

What do you mean by behavior?

The "js-" class prefixing is there to indicate that removing it might break javascript, right?

Thomas Cys’s picture

What i mean is that you shouldn't style "js-" prefixed classes.

Lukas von Blarer’s picture

Ok, then we agree that all classes required by javascript should be prefixed?

Thomas Cys’s picture

Ofcourse but they shouldn't have css attached to them.

Lukas von Blarer’s picture

Yes, for that separate classes are needed. I guess we leave the existing ones in place and duplicate them with the js- prefix?

Lukas von Blarer’s picture

Is it too late to get this in before a stable release?

bojanz’s picture

It can still happen.

Lukas von Blarer’s picture

Status: Needs review » Needs work

The patch is looking good so far. I encountered a few occurances of the .cart class:

CartBlock.php:

$cart_views[] = [
  '#prefix' => '<div class="cart cart-block">',
  '#suffix' => '</div>',
  '#type' => 'view',
  '#name' => $available_views[$cart_id],
  '#arguments' => [$cart_id],
  '#embed' => TRUE,
];

CartController.php:

$build[$cart_id] = [
  '#prefix' => '<div class="cart cart-form">',
  '#suffix' => '</div>',
  '#type' => 'view',
  '#name' => $cart_views[$cart_id],
  '#arguments' => [$cart_id],
  '#embed' => TRUE,
];

I guess those are not needed by JS, but they are kind of hard to get rid of. Can we solve this differently?

Lukas von Blarer’s picture

Status: Needs work » Needs review

But actually this is a different issue. We should move that HTML to templates. I created an issue for that: #2900616: Move HTML to twig templates