Skip to content

Commit

Permalink
add dropdown primitive ctd
Browse files Browse the repository at this point in the history
  • Loading branch information
amk221 committed Dec 11, 2024
1 parent 9194787 commit 5b23a7b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
8 changes: 8 additions & 0 deletions addon/components/dropdown/index.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export default class Dropdown extends Component {
return;
}

event.preventDefault();

this.toggle();

this._ensureFocus();
}

@action
Expand Down Expand Up @@ -209,6 +213,10 @@ export default class Dropdown extends Component {
this.args.onClose?.(reason);
}

_ensureFocus() {
this.triggerElement.focus({ focusVisible: false });
}

@cached
get _api() {
return {
Expand Down
45 changes: 44 additions & 1 deletion tests/integration/components/dropdown/index/focus-test.gjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { module, test } from 'qunit';
import { module, test, todo } from 'qunit';
import { setupRenderingTest } from 'dummy/tests/helpers';
import { render, focus, click } from '@ember/test-helpers';
import Dropdown from '@zestia/ember-select-box/components/dropdown';
Expand Down Expand Up @@ -172,4 +172,47 @@ module('dropdown (focus)', function (hooks) {

assert.dom('.outside').isFocused();
});

test('auto focusing an element inside the dropdown content', async function (assert) {
assert.expect(1);

await render(<template>
<Dropdown as |dd|>
<dd.Trigger />
{{#if dd.isOpen}}
<dd.Content>
<input aria-label="example" class="inside" {{autoFocus}} />
</dd.Content>
{{/if}}
</Dropdown>
</template>);

await click('.dropdown__trigger');

assert.dom('.inside').isFocused();
});

test('focus-visible', async function (assert) {
assert.expect(1);

await render(<template>
{{! template-lint-disable no-forbidden-elements }}
<style>
.dropdown__trigger:focus-visible { outline: 2px solid red; }
</style>
<Dropdown as |dd|>
<dd.Trigger />
</Dropdown>
</template>);

await click('.dropdown__trigger');

assert.dom('.dropdown__trigger').hasStyle(
{ outline: 'rgb(255, 0, 0) solid 2px' },
`the dropdown's focus management does not accidentally cause
focus-visible styles to apply. (here, the trigger was clicked,
whereas the styles should only apply if the user had navigated
to the element using the keyboard`
);
});
});
4 changes: 2 additions & 2 deletions tests/integration/components/select-box/index/focus-test.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ module('select-box (focus)', function (hooks) {
);
});

todo('focus-visible', async function (assert) {
test('focus-visible', async function (assert) {
assert.expect(1);

await render(<template>
Expand All @@ -582,7 +582,7 @@ module('select-box (focus)', function (hooks) {

await click('.select-box .dropdown__trigger');

assert.dom('.select-box .dropdown__trigger').doesNotHaveStyle(
assert.dom('.select-box .dropdown__trigger').hasStyle(
{ outline: 'rgb(255, 0, 0) solid 2px' },
`the select box's focus management does not accidentally cause
focus-visible styles to apply. (here, the trigger was clicked,
Expand Down

0 comments on commit 5b23a7b

Please sign in to comment.