<div class="bundled-learning-inpage-question single-choice" id="bundled-learning-inpage-question" data-inpage-question-id="2940c4853c1348489866b8af512f762e" data-behavior="bundled-learning-inpage-question">
    <button class="bundled-learning-inpage-question__close" aria-label="close Would you discourage your players from using a toe poke? question"></button>
    <div class="bundled-learning-inpage-question__questions">
        <div class="bundled-learning-inpage-question__questions--steps step1">
            <div class="question">Would you discourage your players from using a toe poke?</div>
            <ul class="option-container" data-answer='[ "Yes", "" ]'>
                <div class="options">
                    <input name="question-1" type="radio" id="quiz-1-option-1" value="Yes">
                    <label for="quiz-1-option-1">Yes</label>
                </div>
                <div class="options">
                    <input name="question-1" type="radio" id="quiz-1-option-2" value="No">
                    <label for="quiz-1-option-2">No</label>
                </div>

            </ul>
        </div>
        <div class="bundled-learning-inpage-question__questions--steps step2 hidden">
            <canvas class="right-canvas" width="72" height="72"></canvas>
            <div class="feedback">That's right.</div>
            <p>A toe poke is a useful technique for teams of any age.</p>
        </div>
        <div class="bundled-learning-inpage-question__questions--steps step3 hidden">
            <canvas class="wrong-canvas" width="72" height="72"></canvas>
            <div class="feedback">Not Quite.</div>
            <p>Actually, a toe poke is a useful technique for teams of any age. Panle hight is dynamic to contnet, this
                is an example of a longer answer</p>
        </div>
    </div>
</div>

No notes defined.

{
  "question-type": "single-choice",
  "question": "Would you discourage your players from using a toe poke?",
  "quiz": [
    {
      "type": "radio",
      "name": "question-1",
      "value": "Yes",
      "id": "quiz-1-option-1"
    },
    {
      "type": "radio",
      "name": "question-1",
      "value": "No",
      "id": "quiz-1-option-2"
    }
  ],
  "correct-answer": {
    "firstAnswer": "Yes"
  }
}
  • Content:
    import { Rive, Fit, Layout } from '@rive-app/canvas';
    
    export default parentElement => {
      const closeBtn = parentElement.querySelector(
        '.bundled-learning-inpage-question__close'
      );
    
      const swithScreen = (screen, question) => {
        const allSteps = question.querySelectorAll(
          '.bundled-learning-inpage-question__questions--steps'
        );
        const activeStep = question.querySelector(`.${screen}`);
    
        allSteps.forEach(steps => {
          steps.classList.add('hidden');
        });
        activeStep.classList.remove('hidden');
      };
    
      const feedback = question => {
        const allOptions = question.querySelectorAll('input');
        const optionContainer = question.querySelector('.option-container');
        const selectedAnswers = [];
        const getCorrectAnswer = JSON.parse(optionContainer.dataset.answer);
        const correctAnswer = getCorrectAnswer.filter(item => item);
        const rightfeedbackCanvas = question.querySelector('.right-canvas');
        const wrongfeedbackCanvas = question.querySelector('.wrong-canvas');
        const lyout = new Layout({
          fit: Fit.Cover,
        });
    
        const rightAnimation = new Rive({
          src: '/assets/example-content/quiz-correct-feedback.riv',
          canvas: rightfeedbackCanvas,
          layout: lyout,
          onLoad: () => {
            rightAnimation.resizeDrawingSurfaceToCanvas();
          },
        });
        const wrongAnimation = new Rive({
          src: '/assets/example-content/quiz-incorrect-feedback.riv',
          canvas: wrongfeedbackCanvas,
          layout: lyout,
          onLoad: () => {
            wrongAnimation.resizeDrawingSurfaceToCanvas();
          },
        });
    
        allOptions.forEach(option => {
          if (option.checked) {
            selectedAnswers.push(option.value);
          }
        });
    
        if (
          selectedAnswers.length === correctAnswer.length &&
          selectedAnswers.every(answer => correctAnswer.includes(answer))
        ) {
          rightAnimation.play('Correct');
          swithScreen('step2', question);
        } else {
          wrongAnimation.play('Incorrect');
          swithScreen('step3', question);
        }
      };
    
      // Accessibility
      const updateTabIndex = () => {
        const allInputs = parentElement.querySelectorAll('input');
    
        allInputs.forEach(element => {
          if (element.type === 'radio') {
            // eslint-disable-next-line no-param-reassign
            element.nextElementSibling.tabIndex = '0';
          }
        });
      };
    
      const updateOptLabel = () => {
        const questionElement = parentElement.querySelector('.question');
        const questionText = questionElement.innerText;
        const answerOptions = parentElement.querySelectorAll('input');
        const firstOpt = answerOptions[0];
        const optLabel = firstOpt.nextElementSibling;
        const optText = optLabel.innerText;
    
        if (firstOpt.type === 'checkbox') {
          firstOpt.setAttribute('aria-label', `${questionText} ${optText}`);
        } else if (firstOpt.type === 'radio') {
          optLabel.setAttribute('aria-label', `${questionText} ${optText}`);
        }
      };
    
      window.addEventListener('click', e => {
        const clickedElement = e.target;
    
        if (clickedElement.tagName === 'INPUT') {
          const question = clickedElement.closest(
            '.bundled-learning-inpage-question'
          );
    
          if (!question) {
            return;
          }
    
          const inputs = question.querySelectorAll('input');
          const submitCta = question.querySelector('.cta.cta--efl.submit');
    
          if (clickedElement.type === 'checkbox') {
            let answered = false;
    
            inputs.forEach(element => {
              if (element.checked) {
                answered = true;
              }
            });
            if (answered === true) {
              question.setAttribute('data-answered', 'true');
              submitCta.setAttribute('aria-disabled', 'false');
            } else {
              question.setAttribute('data-answered', 'false');
              submitCta.setAttribute('aria-disabled', 'true');
            }
          } else if (clickedElement.type === 'radio') {
            question.setAttribute('data-answered', 'true');
    
            feedback(question);
          }
    
          if (submitCta) {
            submitCta.addEventListener('click', event => {
              event.preventDefault();
    
              feedback(question);
    
              const allInputs = question.querySelectorAll('input, a');
    
              allInputs.forEach(element => {
                // eslint-disable-next-line no-param-reassign
                element.tabIndex = '-1';
                if (element.type === 'radio' || element.type === 'checkbox') {
                  // eslint-disable-next-line no-param-reassign
                  element.nextElementSibling.tabIndex = '-1';
                }
              });
    
              closeBtn.focus();
            });
            submitCta.addEventListener('keydown', event => {
              if (event.keyCode === 13) {
                event.preventDefault();
                submitCta.click();
              }
            });
          }
    
          const { inpageQuestionId } = question.dataset;
          let inPageQuestion = [];
          const newQuestionId = `{"id": "${inpageQuestionId}"}`;
    
          if (localStorage.getItem('inPageQuestion')) {
            inPageQuestion = JSON.parse(localStorage.getItem('inPageQuestion'));
            const idExist = inPageQuestion.find(val => val.id === inpageQuestionId);
    
            if (!idExist) {
              inPageQuestion[inPageQuestion.length] = JSON.parse(newQuestionId);
              localStorage.setItem(
                'inPageQuestion',
                JSON.stringify(inPageQuestion)
              );
            }
          } else {
            localStorage.setItem('inPageQuestion', `[${newQuestionId}]`);
          }
        }
      });
    
      window.addEventListener('keydown', event => {
        if ((event.keyCode === 13 || event.keyCode === 32) && parentElement) {
          const { target } = event;
          const question = target.closest('.bundled-learning-inpage-question');
    
          if (!question) {
            return;
          }
    
          if (target.previousElementSibling) {
            if (target.previousElementSibling.type === 'radio') {
              target.click();
              target.focus();
            }
          } else if (target.tagName === 'BUTTON' || target.type === 'checkbox') {
            target.click();
            target.focus();
          }
        }
      });
    
      closeBtn.addEventListener('click', e => {
        e.preventDefault();
        parentElement.classList.add('hidden');
      });
    
      closeBtn.addEventListener('keydown', e => {
        if (e.keyCode === 13) {
          e.preventDefault();
          closeBtn.click();
        }
      });
    
      document.addEventListener('readystatechange', () => {
        if (localStorage.getItem('inPageQuestion')) {
          const questionIds = JSON.parse(localStorage.getItem('inPageQuestion'));
    
          questionIds.forEach(questionId => {
            if (
              document.querySelector(`[data-inpage-question-id="${questionId.id}"]`)
            ) {
              document
                .querySelector(`[data-inpage-question-id="${questionId.id}"]`)
                .classList.add('hidden');
            }
          });
        }
      });
    
      updateTabIndex();
      updateOptLabel();
    };
    
  • URL: /components/raw/bundled-learning-inpage-questions/bundled-learning-inpage-questions.js
  • Filesystem Path: src/library/components/bundled-learning-inpage-questions/bundled-learning-inpage-questions.js
  • Size: 6.9 KB
  • Content:
    .bundled-learning-inpage-question {
      position: relative;
      display: flex;
      width: 100%;
      min-width: 100%;
      background-color: $grey-light;
      border-radius: 0.8rem;
      border: 0.1rem solid $grey-light;
      padding: 2.4rem 2.1rem;
      flex-direction: column;
      max-height: initial;
      transition: max-height 0.3s ease-in-out;
      margin: 1.6rem auto;
    
      &__close {
        position: absolute;
        top: 2.4rem;
        right: 2rem;
        display: flex;
        justify-content: center;
        align-items: center;
        border: none;
        cursor: pointer;
        background-color: transparent;
        color: $blue;
        padding: 0;
    
        &::after {
          content: '';
          display: block;
          width: 1.4rem;
          height: 1.4rem;
          background: url('./assets/images/close.svg') no-repeat center;
          background-size: contain;
        }
      }
    
      &__questions {
        font-family: $ef-font;
        &--steps {
          .question {
            @extend .efl-p-large;
    
            font-weight: 500;
            margin-bottom: 1.6rem;
            margin-right: 1.6rem;
            color: $blue;
          }
          .option-container {
            .options {
              position: relative;
              margin-bottom: 0.8rem;
              &:last-child {
                margin-bottom: 0;
              }
              input[type='checkbox'] {
                position: absolute;
                width: 1.8rem;
                height: 1.8rem;
                top: 1.2rem;
                left: 1.5rem;
                border: 0.2rem solid $light-blue;
                margin: 0;
                -webkit-appearance: none;
                appearance: none;
              }
              input[type='checkbox']:checked {
                background-image: url('./assets/images/inpage-question-checked.svg');
                background-position: center;
                background-repeat: no-repeat;
                background-size: contain;
                border: none;
              }
              input[type='radio'] {
                display: none;
              }
              label {
                display: flex;
                flex-direction: row;
                justify-content: center;
                align-items: center;
                background: rgba(255, 255, 255, 0.1);
                border: 0.2rem solid $light-blue;
                color: $blue;
                border-radius: 0.4rem;
                padding: 0.8rem;
                cursor: pointer;
    
                @extend .efl-p-medium;
              }
              input[type='checkbox']:checked + label {
                border: 0.2rem solid $color-interface-light;
                background: $color-interface-light;
                color: $white;
              }
            }
            .cta.submit {
              height: 4.4rem;
              max-width: none;
              width: 100%;
              border: none;
              border-radius: 0.2rem;
              margin-top: 2.4rem;
              font-weight: 700;
    
              @extend .efl-p-small;
              &:disabled,
              &[aria-disabled='true'] {
                opacity: 0.2;
                background-color: $color-interface-light;
                color: $white;
                cursor: default;
                &:hover {
                  background-color: $color-interface-light;
                  color: $white;
                }
              }
            }
          }
          .feedback {
            text-align: center;
            margin-bottom: 0.8rem;
    
            @extend .efl-heading-3;
          }
          p {
            margin: 0 auto;
            text-align: center;
            letter-spacing: 0.02em;
            color: $color-interface-light;
    
            @extend .efl-p-medium;
          }
          .right-canvas {
            display: flex;
            width: 7.2rem;
            height: 7.2rem;
            margin: 0 auto 1.6rem auto;
          }
          .wrong-canvas {
            display: flex;
            width: 7.2rem;
            height: 7.2rem;
            margin: 0 auto 1.6rem auto;
          }
        }
      }
    
      @media screen and (min-width: $mq-medium) {
        min-width: 31.2rem;
        padding: 2.4rem 4rem;
    
        &__close {
          right: 4.2rem;
        }
      }
    
      @media screen and (max-width: $mq-small) {
        &__questions {
          &--steps {
            p {
              max-width: 22.6rem;
            }
          }
        }
      }
    }
    
  • URL: /components/raw/bundled-learning-inpage-questions/bundled-learning-inpage-questions.scss
  • Filesystem Path: src/library/components/bundled-learning-inpage-questions/bundled-learning-inpage-questions.scss
  • Size: 4.1 KB
<div class="bundled-learning-inpage-question {{question-type}}" id="bundled-learning-inpage-question" data-inpage-question-id="2940c4853c1348489866b8af512f762e" data-behavior="bundled-learning-inpage-question">
    <button class="bundled-learning-inpage-question__close" aria-label="close {{question}} question"></button>
    <div class="bundled-learning-inpage-question__questions">
        <div class="bundled-learning-inpage-question__questions--steps step1">
            <div class="question">{{question}}</div>
            <ul class="option-container"
                data-answer='[ "{{correct-answer.firstAnswer}}", "{{correct-answer.secondAnswer}}" ]'>
                {{#each quiz}}
                <div class="options">
                    <input name="{{name}}" type="{{type}}" id="{{id}}" value="{{value}}">
                    <label for="{{id}}">{{value}}</label>
                </div>
                {{/each}}

                {{#if submitcta}}
                <button class="cta cta--efl submit" id="submit-cta" aria-disabled="true" aria-label="submit question"
                    data-cta-text="submit">Submit</button>
                {{/if}}
            </ul>
        </div>
        <div class="bundled-learning-inpage-question__questions--steps step2 hidden">
            <canvas class="right-canvas" width="72" height="72"></canvas>
            <div class="feedback">That's right.</div>
            <p>A toe poke is a useful technique for teams of any age.</p>
        </div>
        <div class="bundled-learning-inpage-question__questions--steps step3 hidden">
            <canvas class="wrong-canvas" width="72" height="72"></canvas>
            <div class="feedback">Not Quite.</div>
            <p>Actually, a toe poke is a useful technique for teams of any age. Panle hight is dynamic to contnet, this
                is an example of a longer answer</p>
        </div>
    </div>
</div>