Skip to content

Allow Dropdown to close when something other than its button is clicked #444

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

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-numbers-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crowdstrike/glide-core': patch
---

Dropdown now opens and closes when any part of it is clicked. Previously, it would only close from a click outside of itself or one on the button with the caret icon.
4 changes: 2 additions & 2 deletions src/dropdown.test.interactions.multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ it('does not set Select All as indeterminate when all options are selected', asy
expect(selectAll?.privateIndeterminate).to.be.false;
});

it('remains open when a tag is clicked', async () => {
it('closes when a tag is clicked', async () => {
const component = await fixture<GlideCoreDropdown>(
html`<glide-core-dropdown open multiple>
<glide-core-dropdown-option
Expand Down Expand Up @@ -1463,7 +1463,7 @@ it('remains open when a tag is clicked', async () => {

await elementUpdated(component);

expect(component.open).to.be.true;
expect(component.open).to.be.false;
});

it('cannot be tabbed to when `disabled`', async () => {
Expand Down
28 changes: 0 additions & 28 deletions src/dropdown.test.interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,34 +702,6 @@ it('opens when something other than the button is clicked', async () => {
expect(component.open).to.be.true;
});

it('remains open when something other than the button is clicked', async () => {
const component = await fixture<GlideCoreDropdown>(
html`<glide-core-dropdown label="Label" placeholder="Placeholder" open>
<glide-core-dropdown-option
label="Label"
value="value"
></glide-core-dropdown-option>
</glide-core-dropdown>`,
);

const internalLabel = component.shadowRoot?.querySelector(
'[data-test="internal-label"]',
);

assert(internalLabel);

const { x, y } = internalLabel.getBoundingClientRect();

// A simple `option.click()` won't do because we need a "mousedown" so that
// `#onDropdownMousedown` gets covered.
await sendMouse({
type: 'click',
position: [Math.ceil(x), Math.ceil(y)],
});

expect(component.open).to.be.true;
});

it('hides the tooltip of the active option when opened via click', async () => {
// The period is arbitrary. 500 of them ensures we exceed the maximum
// width even if it's increased.
Expand Down
7 changes: 1 addition & 6 deletions src/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,12 +1186,7 @@ export default class GlideCoreDropdown extends LitElement {
return;
}

if (
event.target instanceof Node &&
this.#buttonElementRef.value?.contains(event.target) &&
!this.#isSelectionViaSpaceOrEnter &&
this.open
) {
if (!this.#isSelectionViaSpaceOrEnter && this.open) {
this.open = false;

// `event.detail` is an integer set to the number of clicks. When it's zero,
Expand Down