SmartDate should use classes instead of setting styles

Created on 27 October 2024, about 2 months ago

Problem/Motivation

SmartDate uses styles on elements to toggle visibility, it should use classes to allow more flexibility.

Proposed resolution

Replace every element.style.visibility = or element.style.display = by something like element.classList.toggle("hidden").

Remaining tasks

Implement MR.

✨ Feature request
Status

Active

Version

4.2

Component

Code

Created by

πŸ‡«πŸ‡·France mably

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @mably
  • Pipeline finished with Success
    about 2 months ago
    Total: 159s
    #322456
  • πŸ‡¨πŸ‡¦Canada mandclu

    Can you provide an example or two of where the current approach isn't sufficiently flexible?

  • Pipeline finished with Success
    about 2 months ago
    Total: 165s
    #322463
  • πŸ‡«πŸ‡·France mably

    Hi @mandclu, thanks for chiming in.

    Some users have asked if I could try to have this new module β†’ I just created work with SmartDate.

    And I had some problem displaying the reset button with the way the date and time were hidden/shown in smartdate (the reverse flex part in particular).

    I think that using classes instead of manual style toggling on element is also good practice, it could be something interesting to implement in SmartDate.

    Here is what says Chat GPT on the subject:

    Using classes to toggle styles is generally considered better practice in HTML for several reasons:

    • Separation of Concerns: Classes keep the HTML, CSS, and JavaScript separate, making the code easier to read, understand, and maintain. Styles defined in CSS are separate from the HTML structure and JavaScript behavior, improving code organization.
    • Consistency and Reusability: If you need to apply the same style in multiple places, classes allow you to apply the style consistently across elements by simply toggling a class. Directly setting inline styles requires repetitive code and becomes difficult to manage.
    • Efficiency: Classes leverage the CSS cascade and browser optimization for faster rendering. Inline styles, on the other hand, override CSS rules, which can impact performance, especially if you’re making many changes.
    • Simpler JavaScript: Adding or removing classes with JavaScript (e.g., element.classList.toggle('class-name')) is simpler and usually more readable than modifying inline styles individually. It also reduces potential errors with inline styles needing exact values.
    • Easier Maintenance: If you decide to adjust the style later, modifying a single CSS class updates all elements with that class, while inline styles require individual adjustments or complex JavaScript to change them.

    However, there are cases where inline styles might be useful, such as dynamically setting properties that are unique to a single element (like height or width based on calculated values).

    For most use cases, though, toggling classes is preferable.

    My module with the patch:

    And without:

  • Pipeline finished with Success
    about 2 months ago
    Total: 372s
    #322475
  • Pipeline finished with Success
    about 2 months ago
    Total: 192s
    #322913
  • Pipeline finished with Success
    about 2 months ago
    Total: 280s
    #328161
Production build 0.71.5 2024