// Tooltips
//
//  - Tooltips should not receive focus. The focus should remain on the field, button, or link the tooltip is about.
//  - Tooltips should not be revealed until a short time has passed (~1-5 seconds).
//  - Depending on the tooltip option you choose, there may be additional ARIA tags you should apply. Add `aria-describedby="example1"` on an input field and link it to a related section with a matching ID selector `id="example1"`.
//  - Whenever possible, use descriptive text on your form fields that does need to be activated by a focus or hover event. Tooltips can be problematic for some assistive technologies (AT) devices.
//
// <section data-action="aria-toggle" class="atblock"><h3 class="atblock__title"><a href="#tooltips_res">Resources</a></h3><div id="tooltips_res" class="atblock__panel"><ul><li><a href="http://heydonworks.com/practical_aria_examples/#input-tooltip" target="_blank">ARIA Examples - Tooltips</a></li><li><a href="https://github.com/scottaohara/a11y_tooltips" target="_blank">ARIA Tooltips</a></li><li><a href="https://www.w3.org/TR/wai-aria-practices/#tooltip" target="_blank">Tooltip Widget</a></li></ul></div></section>
//
//
//
// <section data-action="aria-toggle" class="atblock"><h3 class="atblock__title"><a href="#tooltips_guide">WCAG 2.1 Guidelines</a></h3><div id="tooltips_guide" class="atblock__panel guidelines"><p><a href="https://www.w3.org/WAI/WCAG21/quickref/#qr-content-structure-separation-programmatic" target="_blank">1.3.1 Info and Relationships</a> - Information, structure, and relationships conveyed through presentation can be programmatically determined or are available in text. (Level A)<br><br><a href="https://www.w3.org/WAI/WCAG21/quickref/#qr-navigation-mechanisms-descriptive" target="_blank">2.4.6 Headings and Labels</a> - Headings and labels describe topic or purpose. (Level AA)<br><br><a href="https://www.w3.org/WAI/WCAG21/quickref/#qr-minimize-error-cues" target="_blank">3.3.2 Labels or Instructions</a> - Labels or instructions are provided when content requires user input. (Level A)<br><br><a href="https://www.w3.org/WAI/WCAG21/quickref/#qr-ensure-compat-rsv" target="_blank">4.1.2 Name, Role, Value</a> - For all user interface components (including but not limited to: form elements, links and components generated by scripts), the name and role can be programmatically determined; states, properties, and values that can be set by the user can be programmatically set; and notification of changes to these items is available to user agents, including assistive technologies. (Level A)</p></div><div class="markup"></div></section>
//
//
//
// Markup: tooltips.twig
//
// Style guide: Forms.tooltips

// Import site utilities.
@import '../../global/utils/init';

.a11y-tip {
  display: table;
  position: relative;

  &__trigger {

    &[tabindex] {
      border-bottom: $tip--trigger--border-bottom;
    }

    &[aria-describedby]:hover + ,
    &[aria-describedby]:not(.a11y-tip__trigger--toggle):focus +,
    &[aria-describedby][aria-expanded='true'] {

      .a11y-tip__help,
      .a11y-tip__help::after {
        opacity: 1;
        pointer-events: auto;
        visibility: visible;
      }

      .a11y-tip__help {

        &,
        &::after {
          transform: translate(-50%, 0);
        }

        @media screen and ( min-width: $bp-sm ) {

          @if $tip--compile-left == true {
            &--left,
            &--left::after {
              transform: translate(0, -50%);
            }
          }

          @if $tip--compile-right == true {
            &--right,
            &--right::after {
              transform: translate(0, -50%);
            }
          }
        }
      }
    }

    &[aria-describedby].a11y-tip__trigger--toggle:hover + {

      .a11y-tip__help,
      .a11y-tip__help::after {
        opacity: 0;
        pointer-events: none;
        visibility: hidden;
      }
    }

    &[aria-expanded='true'].a11y-tip__trigger--toggle:hover + {

      .a11y-tip__help,
      .a11y-tip__help::after {
        opacity: 1 !important; // sass-lint:disable-line no-important
        pointer-events: auto !important; // sass-lint:disable-line no-important
        transition: opacity $tip--no-delay ease-in-out,
        transform $tip--no-delay ease-in-out;
        visibility: visible !important; // sass-lint:disable-line no-important
      }
    }
  }

  &--no-delay {

    .a11y-tip__help,
    .a11y-tip__help::after {
      transition: opacity $tip--no-delay ease-in-out,
      transform $tip--no-delay ease-in-out;
    }
  }
}


[role='tooltip'].a11y-tip__help {
  background: $tip--bg;
  border-radius: $border-rd-sm;
  color: $tip--color-white;
  font-size: $tip--base-size + 2;
  left: 50%;
  margin-top: 6px + $tip--arrow-size;
  max-width: 400px;
  opacity: 0;
  padding: $padding-xsm $padding-sm;
  pointer-events: none;
  position: absolute;
  text-align: left;
  top: 100%;
  transform: translate(-50%, 10px);
  transform-style: preserve-3d;
  transition: opacity 0.2s $tip--delay ease-in-out;
  visibility: hidden;
  width: 100%;
  z-index: 10;

  &::after {
    @include triangle($tip--arrow-size, $tip--bg, bottom);
    bottom: 100%;
    content: '';
    left: 50%;
    margin-top: 6px;
    opacity: 0;
    pointer-events: none;
    position: absolute;
    transform: translate(-50%, 10px);
    transform-style: preserve-3d;
    transition: opacity 0.2s $tip--delay ease-in-out;
    visibility: hidden;
    z-index: 10;
  }

  @if $tip--compile-bottom == true {
    &--top {
      bottom: 100%;
      left: 50%;
      margin-bottom: 6px + $tip--arrow-size;
      margin-top: 0;
      top: auto;
      transform-origin: center top;
      transform: translate(-50%, 10px);

      &::after {
        @include triangle($tip--arrow-size, $tip--bg, top);
        left: 50%;
        margin-bottom: 6px;
        margin-top: 0;
        top: 100%;
        transform-origin: center top;
        transform: translate(-50%, -10px);
      }
    }
  }

  @media screen and ( min-width: $bp-sm ) {

    @if $tip--compile-left == true {
      &--left {
        left: auto;
        margin-right: 5px + $tip--arrow-size;
        margin-top: 0;
        right: 100%;
        top: 50%;
        transform: translate(10px, -50%);

        &::after {
          @include triangle($tip--arrow-size, $tip--bg, left);
          left: 100%;
          margin-right: 5px;
          margin-top: 0;
          top: 50%;
          transform: translate(-10px, -50%);
        }
      }
    }

    @if $tip--compile-right == true {
      &--right {
        left: 100%;
        margin-left: 5px + $tip--arrow-size;
        margin-top: 0; // cancel bottom default styling
        top: 50%;
        transform: translate(10px, -50%);

        &::after {
          @include triangle($tip--arrow-size, $tip--bg, right);
          bottom: auto; // cancel bottom default styling
          left: auto; // cancel bottom default styling
          margin-left: 5px;
          margin-top: 0;  // cancel bottom default styling
          right: 100%;
          top: 50%;
          transform: translate(10px, -50%);
        }
      }
    }
  }
}


// hide tool tips if the ESC key is hit
.a11y-tip--hide .a11y-tip__help {
  display: none;
}


.no-js .a11y-tip__help:not([role]) {
  font-size: 0.85em;

  &::before {
    content: '(';
  }

  &::after {
    content: ')';
  }
}

/* set up hidden tooltip */
.no-js-tooltip {

  [role='tooltip'] {
    display: none;
    padding: $padding-sm;
    margin: 0;
    color: $color-white;
    background: $color-cherry;
    width: 100%;
    z-index: 2;
  }

  /* reveal associated tooltip on focus of preceeding input */
  input:focus + [role='tooltip'] {
    display: block;
    position: relative;
    margin-top: -19px;
    margin-left: -2px;
  }
}
