x-govuk/govuk-form-builder

Legends should not default to using an H1

Closed this issue · 5 comments

fofr commented

Including an H1 in a legend by default is not semantically correct.

When writing:

legend: { text: "Legend text" }

The following HTML is output, defaulting to using an H1 tag:

<legend class="govuk-fieldset__legend govuk-fieldset__legend--m">
  <h1 class="govuk-fieldset__heading">Legend text</h1>
</legend>

When using the official Nunjucks macro:

fieldset: {
  legend: {
    text: "Legend text"
  }
}

The default HTML does not include an H1:

<legend class="govuk-fieldset__legend">
  Legend text
</legend>

Headings only get included with the isPageHeading flag:

https://github.com/alphagov/govuk-frontend/blob/master/src/govuk/components/fieldset/template.njk#L8-L14

Strong agree - ideally we'd have the same defaults as the design system.

I agree, it should be an optional setting called something like "isPageHeading" - link to options

Yeah, this makes sense. I originally built it based purely off the examples on this page, both of which wrap it in <h1>. As I never really looked into the Nunjucks it didn't occur to me to allow non-header legends.

It's possible to set the tag to another header level by passing in tag: 'h3' or whatever in the legend options, and also possible to set the default tag in the config:

GOVUKDesignSystemFormBuilder.configure do |conf|
  conf.default_legend_tag  = 'h3'
  conf.default_legend_size = 'l'
end

Unfortunately, it's currently not possible to omit the tag altogether, so I think once that's implemented all of the desired functionality will be present. The equivalent of isPageHeading will be achievable by passing in tag: 'h1' and the default behaviour will be no tag (tag: nil) which will just output <legend class="govuk-fieldset__legend">Legend text</legend>.

fofr commented

@peteryates Agree about the examples being misleading, I've raised this issue: alphagov/govuk-design-system#1364

Fixed by #204, will be included in v2.1.0 shortly.