{#
/**
 * @file
 * Theme override for a form element label.
 *
 * Available variables:
 * - title: The label's text.
 * - title_display: Elements title_display setting.
 * - required: An indicator for whether the associated form element is required.
 * - attributes: A list of HTML attributes for the label.
 *
 * @see template_preprocess_form_element_label()
 */
#}
{%-
  set classes = [
  is_checkbox ? 'usa-checkbox__label',
  is_radio ? 'usa-radio__label',
  'control-label',
  title_display == 'after' ? 'option',
  title_display == 'invisible' and not (is_checkbox or is_radio) ? 'usa-sr-only',
  required ? 'js-form-required',
  required ? 'form-required',
  required ? 'usa-label-required',
]
-%}
{% if title is not empty and title_display == 'invisible' and (is_checkbox or is_radio) -%}
  {#
  Clear but preserve label text as attribute (e.g. for screen readers) for
  checkboxes/radio buttons when it actually should be invisible.
  #}
  {%- set attributes = attributes.setAttribute('title', title) -%}
  {# Add display inline since title will be empty #}
  {%- set attributes = attributes.addClass('display-inline') -%}

  {% set sr_only_title %}
    <p class="usa-sr-only">{{ title }}</p>
  {% endset %}

  {% set title = title|merge({'#markup': sr_only_title}) %}

{%- endif -%}

{#
Labels for single checkboxes/radios contain the element itself and thus have
always to be rendered regardless of whether they have a title or not.
#}
{%- if title is not empty or is_checkbox or is_radio -%}
  {{ include('ui_suite_uswds:label', {
    label: title,
    attributes: attributes.addClass(classes),
  }, with_context = false) }}
{%- endif -%}
