From c425ecaf3df8103e7fccba1727bfe82d71d9b740 Mon Sep 17 00:00:00 2001 From: Mikko Tapionlinna Date: Thu, 29 Aug 2024 12:19:53 +0300 Subject: [PATCH] UHF-8804: Fix card accessibility issues --- src/js/react/common/Card.tsx | 4 ++-- src/js/react/common/Tags.tsx | 37 ++++++++++++++++++++--------------- templates/component/card.twig | 2 +- templates/misc/tag-list.twig | 11 +++++++++-- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/js/react/common/Card.tsx b/src/js/react/common/Card.tsx index 72050a4c4..879c2d12c 100644 --- a/src/js/react/common/Card.tsx +++ b/src/js/react/common/Card.tsx @@ -100,7 +100,7 @@ function CardItem({ {cardCategoryTag &&
- +
} @@ -156,7 +156,7 @@ function CardItem({ {cardTags && cardTags.length > 0 &&
- +
} diff --git a/src/js/react/common/Tags.tsx b/src/js/react/common/Tags.tsx index 9066ab889..c3f8174b4 100644 --- a/src/js/react/common/Tags.tsx +++ b/src/js/react/common/Tags.tsx @@ -1,29 +1,34 @@ +import React from 'react'; import type TagType from '@/types/TagType'; interface TagsProps { tags: Array; isInteractive?: boolean; langAttribute?: any; + insideCard?: boolean; } -export function Tags({ tags, isInteractive, langAttribute }: TagsProps): JSX.Element { +export function Tags({ tags, isInteractive, langAttribute, insideCard }: TagsProps): JSX.Element { const typeClass = isInteractive ? 'content-tags__tags--interactive' : 'content-tags__tags--static'; - return ( -
-
    - {tags.map((item: TagType, key: number) => ( -
  • - {item.tag} -
  • - ), - )} -
-
+ // When inside a card, use a div instead of a section to avoid duplicating the aria-label description on each card. + const Element = insideCard ? 'div' : 'section'; + + return React.createElement( + Element, + { + className: 'content-tags content-tags--card', + 'aria-label': Drupal.t('Tags', {}, { context: 'Label for screen reader software users explaining that this is a list of tags related to this page.' }), + role: insideCard ? 'group' : undefined, // When inside a card, use role="group" to group the tags together. + }, + ); } diff --git a/templates/component/card.twig b/templates/component/card.twig index ab4adfd5b..0fd285313 100644 --- a/templates/component/card.twig +++ b/templates/component/card.twig @@ -99,7 +99,7 @@ {% if card_tags %}
- {% embed '@hdbt/misc/tag-list.twig' with { tags: card_tags, addSpan: true, tag_container_class: ' content-tags--card' } %} + {% embed '@hdbt/misc/tag-list.twig' with { tags: card_tags, addSpan: true, tag_container_class: ' content-tags--card', inside_card: true } %} {% endembed %}
{% endif %} diff --git a/templates/misc/tag-list.twig b/templates/misc/tag-list.twig index 102177c1e..773f54948 100644 --- a/templates/misc/tag-list.twig +++ b/templates/misc/tag-list.twig @@ -1,3 +1,10 @@ +{% set element = 'section' %} +{% set role = '' %} +{% if inside_card %} + {% set element = 'div' %} + {% set role = 'role="group"' %} +{% endif %} + {% if type == 'interactive' %} {% set type_class = 'content-tags__tags--interactive' %} {% else %} @@ -6,7 +13,7 @@ {% set content_tags_id = 'hdbt-content_tags_id-' ~ random() %} - +