Skip to content

Commit 003a610

Browse files
authored
Merge pull request #259 from hashicorp/dropdown-fix-focus-part1
"Dropdown" component - Fix issues with focus - Part 1
2 parents b538e71 + c2a8f70 commit 003a610

File tree

4 files changed

+40
-58
lines changed

4 files changed

+40
-58
lines changed

.changeset/famous-singers-admire.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@hashicorp/design-system-components": patch
3+
---
4+
5+
- removed autofocus on first item for `Disclosure` component (and as a result also for `Breadcrumb` and `Dropdown` components)
6+
- updated focus state treatment for `Dropdown` component

packages/components/addon/components/hds/disclosure/index.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class="hds-disclosure__content"
88
{{focus-trap
99
isActive=this.isActive
10-
shouldSelfFocus=false
10+
shouldSelfFocus=true
1111
focusTrapOptions=(hash clickOutsideDeactivates=this.clickOutsideDeactivates onDeactivate=this.onDeactivate)
1212
}}
1313
>

packages/components/app/styles/components/dropdown.scss

+33-40
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,18 @@ $hds-dropdown-toggle-border-radius: 5px;
274274
// Notice: to avoid too much duplication we define two local CSS variables
275275
// and define their values in the color variants below
276276

277+
&:hover,
278+
&.is-hover {
279+
color: var(--current-color-hover);
280+
&::before {
281+
background-color: currentColor;
282+
}
283+
}
284+
277285
// default focus for browsers that still rely on ":focus"
278286
&:focus,
279287
&.is-focus {
288+
color: var(--current-color-focus);
280289
&::after {
281290
background-color: var(--current-background-color);
282291
box-shadow: var(--current-focus-ring-box-shadow);
@@ -286,22 +295,39 @@ $hds-dropdown-toggle-border-radius: 5px;
286295
// undo the previous declaration for browsers that support ":focus-visible" but wouldn't normally show default focus styles
287296
&:focus:not(:focus-visible) {
288297
&::after {
298+
background-color: transparent;
289299
box-shadow: none;
290300
}
291301
}
292302
// set focus for browsers that support ":focus-visible"
293303
&:focus-visible {
304+
color: var(--current-color-focus);
294305
&::after {
295306
background-color: var(--current-background-color);
296307
box-shadow: var(--current-focus-ring-box-shadow);
297308
left: 4px;
298309
}
299310
}
311+
300312
// remove the focus ring on "active + focused" state (by design)
301313
&:focus:active,
314+
&:focus-visible:active,
302315
&.is-focus.is-active {
303316
&::after {
317+
background-color: var(--current-background-color);
304318
box-shadow: none;
319+
left: 10px;
320+
}
321+
}
322+
323+
&:active,
324+
&.is-active {
325+
color: var(--current-color-active);
326+
&::before {
327+
background-color: currentColor;
328+
}
329+
&::after {
330+
background-color: var(--current-background-color);
305331
}
306332
}
307333
}
@@ -320,30 +346,12 @@ $hds-dropdown-toggle-border-radius: 5px;
320346
color: var(--token-color-foreground-primary);
321347

322348
// assign the values to the local CSS variables used above
349+
--current-color-hover: var(--token-color-foreground-action-hover);
350+
--current-color-focus: var(--token-color-foreground-action-active);
351+
--current-color-active: var(--token-color-foreground-action-active);
323352
&::after {
324353
--current-background-color: var(--token-color-surface-action);
325-
--current-focus-ring-box-shadow: var(
326-
--token-focus-ring-action-box-shadow
327-
);
328-
}
329-
330-
&:hover,
331-
&.is-hover {
332-
color: var(--token-color-foreground-action-hover);
333-
&::before {
334-
background-color: currentColor;
335-
}
336-
}
337-
338-
&:active,
339-
&.is-active {
340-
color: var(--token-color-foreground-action-active);
341-
&::before {
342-
background-color: currentColor;
343-
}
344-
&::after {
345-
background-color: var(--token-color-surface-action);
346-
}
354+
--current-focus-ring-box-shadow: var(--token-focus-ring-action-box-shadow);
347355
}
348356
}
349357
}
@@ -353,28 +361,13 @@ $hds-dropdown-toggle-border-radius: 5px;
353361
color: var(--token-color-foreground-critical);
354362

355363
// assign the values to the local CSS variables used above
364+
--current-color-hover: var(--token-color-palette-red-300);
365+
--current-color-focus: var(--token-color-palette-red-400);
366+
--current-color-active: var(--token-color-palette-red-400);
356367
&::after {
357368
--current-background-color: var(--token-color-surface-critical);
358369
--current-focus-ring-box-shadow: var(--token-focus-ring-critical-box-shadow);
359370
}
360-
361-
&:hover,
362-
&.is-hover {
363-
color: var(--token-color-palette-red-300);
364-
&::before {
365-
background-color: currentColor;
366-
}
367-
}
368-
&:active,
369-
&.is-active {
370-
color: var(--token-color-palette-red-400);
371-
&::before {
372-
background-color: currentColor;
373-
}
374-
&::after {
375-
background-color: var(--token-color-surface-critical);
376-
}
377-
}
378371
}
379372
}
380373

packages/components/tests/integration/components/hds/disclosure/index-test.js

-17
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,6 @@ module('Integration | Component | hds/disclosure/index', function (hooks) {
8181

8282
// FOCUS
8383

84-
test('it should focus the first item in the "content" when the "toggle" is clicked', async function (assert) {
85-
await render(hbs`
86-
<Hds::Disclosure>
87-
<:toggle as |t|>
88-
<button type="button" id="test-disclosure-button" {{on "click" t.onClickToggle}} />
89-
</:toggle>
90-
<:content>
91-
<a id="test-disclosure-link-1" href="#">test1</a>
92-
<a id="test-disclosure-link-2" href="#">test2</a>
93-
</:content>
94-
</Hds::Disclosure>
95-
`);
96-
await click('button#test-disclosure-button');
97-
await waitNextExecutionFrame();
98-
assert.dom('a#test-disclosure-link-1').isFocused();
99-
});
100-
10184
// TODO this doesn't work
10285
// see https://github.com/emberjs/ember-test-helpers/issues/738
10386
// https://discord.com/channels/480462759797063690/480523424121356298/842578755633545276

0 commit comments

Comments
 (0)