rgossiaux/svelte-headlessui

Popover not detecting content click events (links / otherwise) in web component context (with fix)

cjm771 opened this issue · 2 comments

Describe the bug

The issue is when using web components export (we use this to embed svelte in legacy pages), the mousedown event for popovers (likely modals as well) aren't checking for shadowroot target, adding check for composedPath() from event does actually cehck the correct target, where as the typical event.target defaults to the top level container as a web component. See below fix

function handleMousedown(event) {
    let target = event.target;
    if (popoverState !== PopoverStates.Open) return;
    if ($button?.contains(target)) return;
    if ($panel?.contains(target)) return;
   
   // below two lines are a fix that seems to work
    const potentialShadowRootTarget = event.composedPath()[0]; 
    if ($panel?.contains(potentialShadowRootTarget)) return;
     
     $api.closePopover();
}

To Reproduce
simply put a link in the content of a popoverPanel, and make sure the bundling uses customElement: true. (svelte dev api - web components) below is our example loader an

Snippet inside PopoverPanel

Here is a <a href="http://google.com" target="_blank"><Button link>google link.</Button></a>. Can you click it? it should not register this inside web component shadow root context

Webpack config (customElement: true)

    {
            loader: 'svelte-loader',
            options: {
              compilerOptions: {
                dev: !IS_PROD,
                customElement: true,
              },
              emitCss: true,
              hotReload: false,
              preprocess: sveltePreprocess({
                postcss: {
                  plugins: [tailwindcss],
                },
                sourceMap: false,
              }),
            },
          },

Library version
1.0.2

For the time being a workaround if you can afford it is to on:mousedown={(e) => { e.stopPropagation() } on a menu item or popoverpanel main container div

I think you can also doon:mousedown|stopPropagation={handleMouseDown}