axa-ch-webhub-cloud / pattern-library

AXA CH UI component library. Please share, comment, create issues and work with us!
https://axa-ch-webhub-cloud.github.io/plib-feature/develop
126 stars 17 forks source link

Modal does not close in setting "open" to false #2210

Closed LucaMele closed 3 years ago

LucaMele commented 3 years ago

Having following code, the modal does not close if setAttribute is set to false. It closes only using X or ESC

<axa-modal class="js--modal"></axa-modal>
<axa-button variant="secondary" class="js--trigger-modal">Toggle Debug Modal (or Press "d")</axa-button>
<script>
    const modal = document.querySelector('.js--modal');
    const btn = document.querySelector('.js--trigger-modal');
    let open = false;

    const toggleModal = () => {
      open = !open;
      modal.setAttribute('open', open);
    }

    document.addEventListener('keyup', (e) => {
       if (e.code === 'KeyD') {
         toggleModal(e);
       }
    });

    btn.addEventListener('click', toggleModal);
</script>

image

marcwelz commented 3 years ago

Thanks for reporting this bug! I will have a look.

Therefore you can use removeAttribute() to close the Modal

LucaMele commented 3 years ago

@marcwelz thank you, actually in HTML attributes are considered as "false" if not there and "true" if there empty or there with value same as name. So removeAttribute is actually the correct way, so i guess no bug. thanks! Feel free to close it