Skip to content

[TwigComponent][DX] Better support for boolean ARIA attributes (i.e. aria-hidden) #1701

Closed
@gremo

Description

@gremo

In short, a boolean attribute in ARIA is an enumerated attribute in the form of strings "true" and "false". This is different from HTML boolean attributes like checked, open or disabled.

In ComponentAttributes we correctly handle HTML boolean attributes but there is not special "treatment" for ARIA enumerated attributes.

Many developers, especially those just starting out or inexperienced with ARIA (me including), could write the following:

{# templates/components/Icon.html.twig #}
{% props icon %}

<i
    {{ attributes.defaults({
        class: html_classes("fa-solid", icon),
        "aria-hidden": true, {# Wrong! It should be a string #}
    }) }}
></i>

Which would produce and invalid HTML (in the sense of the ARIA enumerated attributes explained above):

<i class="fa-solid fa-user" aria-hidden></i> <!-- Wrong! It should be a string -->

To make things easier and more DX, I would suggest to automatically convert true and false to "true" and "false", if the attribute is an ARIA one. Could be easy as:

// Transform ARIA enumerated attribute in case of boolean value
if (str_starts_with($key, 'aria-')) {
  $value = match($value) {
      true => "true",
      false => "false",
      default => $value,
  };
}

Or more concise:

if (is_bool($value) && str_starts_with($key, 'aria-')) {
  $value = $value ? "true" : "false";
}

... just before returing. WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions