I'm building a site navigation using litelement. It will have a dropdown menu. I am trying to figure out how to add an event so that if a user clicks anywhere outside the dropdown menu itself or even outside of the custom element, the dropdown menu will close. I think that's the natural expectation.
I thought of adding a property to my custom element that can be used as a "state". And then added an event listener to the document through connectedCallback
lifecycle method. But, it seems I can't really access the property as I had expected. The property is accessible through any other elements that has an event attached to it.
Below is a very generic codepen. Clicking on anywhere on the document should open up a popup that shows the value of the property is undefined. However, if I click on the button that's inside the custom element, which has an event attached to it, that event handler is able to access the property successfully.
https://codepen.io/aver-mimas/pen/ExjZXMq
What's going wrong in this example?