From c629657bfcbe6113a30418196b9e1cb2de1e415e Mon Sep 17 00:00:00 2001 From: Joshua Pease Date: Wed, 2 Oct 2024 16:51:23 -0700 Subject: [PATCH] [#45] Block heading level accessibility --- templates/_blocks/_heading.twig | 16 +++++++++++++--- templates/_blocks/_pageHero.twig | 4 ++++ templates/_blocks/index.twig | 13 ++++++++++++- templates/_components/call-to-action.twig | 2 +- templates/_components/page-hero.twig | 13 +++++++++++-- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/templates/_blocks/_heading.twig b/templates/_blocks/_heading.twig index a1bf755..cb68fc4 100644 --- a/templates/_blocks/_heading.twig +++ b/templates/_blocks/_heading.twig @@ -1,6 +1,16 @@ {# @var \craft\elements\Entry|null block #} {% set block = block ?? null %} -
-

{{ block.heading }}

-
+{# @var bool hasPreviousBlocksWithH1 #} +{% set hasPreviousBlocksWithH1 = hasPreviousBlocksWithH1 ?? false %} + +{% set element = hasPreviousBlocksWithH1 ? 'h2' : 'h1' %} + +{% tag element with { + class: cx('container font-bold', { + 'text-4xl': not hasPreviousBlocksWithH1, + 'text-3xl': hasPreviousBlocksWithH1, + }), +} %} + {{ block.heading }} +{% endtag %} diff --git a/templates/_blocks/_pageHero.twig b/templates/_blocks/_pageHero.twig index afcf953..cf8dedc 100644 --- a/templates/_blocks/_pageHero.twig +++ b/templates/_blocks/_pageHero.twig @@ -1,8 +1,12 @@ {# @var \craft\elements\Entry|null block #} {% set block = block ?? null %} +{# @var bool hasPreviousBlocksWithH1 #} +{% set hasPreviousBlocksWithH1 = hasPreviousBlocksWithH1 ?? false %} + {{ include('_components/page-hero.twig', { image: block.image.eagerly().one(), title: block.title, description: block.richTextSimple, + headingLevel: hasPreviousBlocksWithH1 ? 2 : 1, }) }} diff --git a/templates/_blocks/index.twig b/templates/_blocks/index.twig index 8cb7e94..4fe643f 100644 --- a/templates/_blocks/index.twig +++ b/templates/_blocks/index.twig @@ -10,6 +10,16 @@ {% set nextBlock = blocks.get(loop.index0 + 1) %} {% set previousBlocks = blocks.take(loop.index0) %} + {# + Check if there are previous blocks that have a heading level of 1. This is used + to determine if the current block should use an h1 or h2 as its root heading level. + #} + {% set hasPreviousBlocksWithH1 = previousBlocks + |map(block => block.type.handle) + |intersect(['pageHero', 'heading']) + |length + %} + {% set marginBottomConfig = { default: 'mb-64', 'heading + richText': 'mb-32', @@ -32,12 +42,13 @@ |filter |first %} + {# Block wrapper #}
- {{ include("_blocks/_#{block.type.handle}.twig", { block, + hasPreviousBlocksWithH1, }) }}
{% endfor %} diff --git a/templates/_components/call-to-action.twig b/templates/_components/call-to-action.twig index e66ffad..6c25126 100644 --- a/templates/_components/call-to-action.twig +++ b/templates/_components/call-to-action.twig @@ -16,7 +16,7 @@
{% if title %} -

{{ title }}

+

{{ title }}

{% endif %} {% if text %} diff --git a/templates/_components/page-hero.twig b/templates/_components/page-hero.twig index 20d9f74..25e6665 100644 --- a/templates/_components/page-hero.twig +++ b/templates/_components/page-hero.twig @@ -7,15 +7,24 @@ {# @var \craft\elements\Asset|null image #} {% set image = image ?? null %} +{# @var int headingLevel #} +{% set headingLevel = headingLevel ?? 1 %} + +{% set element = "h#{headingLevel}" %} +
{% if title %} -

{{ title }}

+ {% tag element with { + class: 'text-4xl font-bold', + } %} + {{ title }} + {% endtag %} {% endif %} {% if description %} -
{{ description }}
+
{{ description }}
{% endif %}