Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clicking on label for a select component opens it #1108

Open
olegpolin opened this issue May 22, 2024 · 7 comments
Open

Clicking on label for a select component opens it #1108

olegpolin opened this issue May 22, 2024 · 7 comments

Comments

@olegpolin
Copy link
Contributor

olegpolin commented May 22, 2024

Describe the bug

Clicking on a label for a select component triggers it, clicking it again doesn't close it. Is this intended behavior?

Reproduction

https://stackblitz.com/edit/shadcn-svelte-select-label

<script lang="ts">
  import { Label } from "$lib/components/ui/label";
  import * as Select from "$lib/components/ui/select";
</script>

<div class="flex flex-col items-center gap-2">
  <Label for="options" class="w-[180px]">Options:</Label>
  <Select.Root>
    <Select.Trigger id="options" class="w-[180px]">
      <Select.Value placeholder="Select" />
    </Select.Trigger>
    <Select.Content>
      <Select.Item value="option1">Option 1</Select.Item>
      <Select.Item value="option2">Option 2</Select.Item>
      <Select.Item value="option3">Option 3</Select.Item>
    </Select.Content>
  </Select.Root>
</div>

Logs

No response

System Info

Latest versions of everything
Tested on Chrome on Windows
On Android Chrome and IOS Safari, clicking the label still opens the select, but clicking the label again closes it.

Severity

annoyance

@shyakadavis
Copy link
Contributor

Hmm... isn't this the native/expected behaviour?

When a user clicks or touches/taps a label, the browser passes the focus to its associated input (the resulting event is also raised for the input). That increased hit area for focusing the input provides an advantage to anyone trying to activate it — including those using a touch-screen device.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label

If you don't want this behaviour, remove the for="options" but that just leads to poor U/X in my opinion. 🤷‍♂️

@olegpolin
Copy link
Contributor Author

Native behavior with a <label> associated with a <select> doesn't actually open the select, it should just pass the focus to the select element, as far as I can tell.

It seems like the <Select> component in shadcn-svelte is actually a button with a popover, which helps explain the abnormal behavior.

@shyakadavis
Copy link
Contributor

Hunh, you're right.

TIL: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select

But still, removing the for in your attached repro rids one of this behaviour.

Also, re-reading your issue, I can finally see the "not closing" it part. And I agree, it feels odd/wrong.

Maybe Hunter will provide some context.

@huntabyte
Copy link
Owner

So, when a label is associated with a button, the only option when clicking said label is to "click" the button.

I tried researching to see if there is a way to determine if the 'click' event came from a select and came up empty-handed.

@olegpolin
Copy link
Contributor Author

The weirder issue is that clicking the label again doesn't close the select, but closes it and reopens it, as if it is being clicked twice (at least on chrome on windows)

@huntabyte
Copy link
Owner

@olegpolin Yeah, this is because it is being registered as an "outside click" followed by a "click." Clicking the label is outside the content, causing the content to close, and then the label triggers a click on the button, which reopens it.

I'll look into getting this resolved in the Svelte 5 version of Bits I'm currently working on.

@olegpolin
Copy link
Contributor Author

Got it, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants