Skip to content

chore: Use CSS-native :focus-visible to track visible focus state (batch 3) #3607

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion src/app-layout/drawer/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
@use '../../internal/styles/tokens' as awsui;
@use '../../internal/styles' as styles;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;
@use '../constants' as constants;

.toggle {
Expand Down
3 changes: 1 addition & 2 deletions src/app-layout/toggles/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@use '../../internal/styles/tokens' as awsui;
@use '../../internal/styles' as styles;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;

.toggle-button {
cursor: pointer;
Expand All @@ -22,7 +21,7 @@
outline: none;
text-decoration: none;
}
@include focus-visible.when-visible {
&:focus-visible {
@include styles.focus-highlight(awsui.$space-button-inline-icon-focus-outline-gutter);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@use '../../../../internal/styles' as styles;
@use '../../../../internal/styles/tokens' as awsui;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;

.trigger {
// reset native <button> tag styles
Expand Down
3 changes: 1 addition & 2 deletions src/app-layout/visual-refresh/trigger-button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@use '../../internal/styles' as styles;
@use '../../internal/styles/tokens' as awsui;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;

@mixin trigger-selected-styles {
background: awsui.$color-background-layout-toggle-selected-default;
Expand Down Expand Up @@ -69,7 +68,7 @@ handleSplitPanelMaxWidth function in the context.
pointer-events: auto;
position: relative;

@include focus-visible.when-visible {
&:focus-visible {
@include styles.focus-highlight(3px);
}

Expand Down
26 changes: 12 additions & 14 deletions src/file-input/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
@use '../internal/styles/tokens' as awsui;
@use '../internal/styles' as styles;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;

.root,
.file-input {
Expand All @@ -16,20 +15,19 @@
}

.file-input-button {
@include focus-visible.when-visible-unfocused {
&.force-focus-outline {
&-icon {
@include styles.focus-highlight(
(
'vertical': awsui.$space-button-icon-focus-outline-gutter-vertical,
'horizontal': awsui.$space-button-focus-outline-gutter,
)
);
}
// stylelint-disable-next-line selector-combinator-disallowed-list
Copy link
Member Author

@avinashbot avinashbot Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This utility is used four times in the entirety of the package (and all @cloudscape-design packages). You'll see the other three in this PR too. I don't think it's worth pulling in component toolkit just for a tiny little mixin that isn't even particularly Cloudscape-specific to begin with.

body:has(:focus-visible) &.force-focus-outline {
&-icon {
@include styles.focus-highlight(
(
'vertical': awsui.$space-button-icon-focus-outline-gutter-vertical,
'horizontal': awsui.$space-button-focus-outline-gutter,
)
);
}

&-button {
@include styles.focus-highlight(awsui.$space-button-focus-outline-gutter);
}
&-button {
@include styles.focus-highlight(awsui.$space-button-focus-outline-gutter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ import { PointerEventMock } from '../../../../../lib/components/internal/utils/p
import styles from '../../../../../lib/components/internal/components/drag-handle-wrapper/styles.css.js';
import tooltipStyles from '../../../../../lib/components/internal/components/tooltip/styles.css.js';

function mockFocusVisible(value: boolean) {
Copy link
Member Author

@avinashbot avinashbot Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some jerk (me) wrote in a dependency on the presence of data-awsui-focus-visible="true" as part of the component logic. Rewrote that to just check CSS instead (using document.body.matches()). And we can mock that just inside this file.

jest.spyOn(document.body, 'matches').mockReturnValue(value);
}

beforeAll(() => {
(window as any).PointerEvent ??= PointerEventMock;
});

beforeEach(() => {
mockFocusVisible(false);
// Mock behavior of handle onBlur events so that they're not considered as
// triggered by the user switching to another tab or window.
jest.spyOn(document, 'hasFocus').mockReturnValue(true);
});

afterEach(() => {
delete document.body.dataset.awsuiFocusVisible;
jest.restoreAllMocks();
});

Expand Down Expand Up @@ -68,11 +78,12 @@ function renderDragHandle(props: Partial<Omit<DragHandleWrapperProps, 'children'
</DragHandleWrapper>
);

const dragHandle = container.querySelector<HTMLButtonElement>('#drag-button')!;
return {
dragHandle: container.querySelector<HTMLButtonElement>('#drag-button')!,
dragHandle,
showButtons: () => {
document.body.dataset.awsuiFocusVisible = 'true';
container.querySelector<HTMLButtonElement>('#drag-button')!.focus();
fireEvent.pointerDown(dragHandle);
fireEvent.pointerUp(dragHandle);
},
getTooltip: () => document.querySelector(`.${tooltipStyles.root}`),
};
Expand Down Expand Up @@ -167,12 +178,13 @@ test("doesn't show direction buttons by default", () => {
});

describe('triggerMode = focus (default)', () => {
beforeEach(() => mockFocusVisible(true));

test('shows direction buttons when focus enters the button', () => {
const { dragHandle } = renderDragHandle({
directions: { 'block-start': 'active', 'block-end': 'active' },
});

document.body.dataset.awsuiFocusVisible = 'true';
dragHandle.focus();
expectDirectionButtonToBeVisible('block-start');
expectDirectionButtonToBeVisible('block-end');
Expand All @@ -184,7 +196,6 @@ describe('triggerMode = focus (default)', () => {
directions: { 'block-start': 'active', 'block-end': 'active' },
});

document.body.dataset.awsuiFocusVisible = 'true';
dragHandle.focus();
expectDirectionButtonToBeVisible('block-start');
expectDirectionButtonToBeVisible('block-end');
Expand All @@ -207,7 +218,6 @@ describe('triggerMode = focus (default)', () => {
tooltipText: 'Click me!',
});

document.body.dataset.awsuiFocusVisible = 'true';
dragHandle.focus();

expect(getDirectionButton('block-start')).toBeInTheDocument();
Expand All @@ -220,13 +230,14 @@ describe('triggerMode = focus (default)', () => {
});

describe('triggerMode = keyboard-activate', () => {
beforeEach(() => mockFocusVisible(true));

test('does not show direction buttons when focus enters the button', () => {
const { dragHandle } = renderDragHandle({
directions: { 'block-start': 'active', 'block-end': 'active' },
triggerMode: 'keyboard-activate',
});

document.body.dataset.awsuiFocusVisible = 'true';
dragHandle.focus();
expectDirectionButtonToBeHidden('block-start');
expectDirectionButtonToBeHidden('block-end');
Expand All @@ -240,7 +251,6 @@ describe('triggerMode = keyboard-activate', () => {
triggerMode: 'keyboard-activate',
});

document.body.dataset.awsuiFocusVisible = 'true';
dragHandle.focus();
expectDirectionButtonToBeHidden('block-start');
expectDirectionButtonToBeHidden('block-end');
Expand All @@ -261,7 +271,6 @@ describe('triggerMode = keyboard-activate', () => {
triggerMode: 'keyboard-activate',
});

document.body.dataset.awsuiFocusVisible = 'true';
dragHandle.focus();
expectDirectionButtonToBeHidden('block-start');
expectDirectionButtonToBeHidden('block-end');
Expand All @@ -279,8 +288,6 @@ describe('triggerMode = keyboard-activate', () => {
triggerMode: 'keyboard-activate',
});

document.body.dataset.awsuiFocusVisible = 'true';

dragHandle.focus();
fireEvent.keyDown(dragHandle, { key: 'Enter' });
expect(getDirectionButton('block-start')).toBeInTheDocument();
Expand All @@ -299,8 +306,6 @@ describe('triggerMode = keyboard-activate', () => {
triggerMode: 'keyboard-activate',
});

document.body.dataset.awsuiFocusVisible = 'true';

fireEvent.keyDown(dragHandle, { key });

expect(getDirectionButton('block-start')).toBeInTheDocument();
Expand Down Expand Up @@ -487,6 +492,8 @@ test("doesn't call onDirectionClick when disabled direction button is pressed",
});

describe('initialinitialShowButtons property', () => {
beforeEach(() => mockFocusVisible(false));

test('shows direction buttons initially when initialShowButtons=true', () => {
renderDragHandle({
directions: { 'block-start': 'active', 'block-end': 'active' },
Expand Down Expand Up @@ -534,8 +541,8 @@ describe('initialinitialShowButtons property', () => {
expectDirectionButtonToBeVisible('block-end');
expect(getDirectionButton('inline-start')).not.toBeInTheDocument();
expect(getDirectionButton('inline-end')).not.toBeInTheDocument();
dragHandle.focus();

fireEvent.focus(dragHandle);
fireEvent.blur(dragHandle);
expectDirectionButtonToBeHidden('block-start');
expectDirectionButtonToBeHidden('block-end');
Expand Down
2 changes: 1 addition & 1 deletion src/internal/components/drag-handle-wrapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function DragHandleWrapper({
// handled in the "pointerup" listener instead. In cases where focus is moved
// to the button (by manually calling `.focus()`, the buttons should only appear)
// if the action that triggered the focus move was the result of a keypress.
if (document.body.dataset.awsuiFocusVisible && !nodeContains(wrapperRef.current, event.relatedTarget)) {
if (document.body.matches('body:has(:focus-visible)') && !nodeContains(wrapperRef.current, event.relatedTarget)) {
setShowTooltip(false);
if (triggerMode === 'focus') {
setShowButtons(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import React from 'react';
import { render } from '@testing-library/react';
import { fireEvent, render } from '@testing-library/react';

import InternalDragHandle from '../../../../../lib/components/internal/components/drag-handle/index.js';
import InternalDragHandleWrapper from '../../../../../lib/components/test-utils/dom/internal/drag-handle';
Expand All @@ -25,8 +25,8 @@ describe('test util selectors', () => {
const handle = container.querySelector<HTMLButtonElement>(`.${InternalDragHandleWrapper.rootSelector}`)!;

if (showUapActions) {
document.body.dataset.awsuiFocusVisible = 'true';
handle.focus();
fireEvent.pointerDown(handle);
fireEvent.pointerUp(handle);
Copy link
Member Author

@avinashbot avinashbot Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buttons show up either on click (pointer down + no drag + pointer up) or on keyboard focus. The pointer method of doing this is easier and more straightforward to implement than faking keyboard focus.

}

return {
Expand Down
3 changes: 1 addition & 2 deletions src/internal/components/drag-handle/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@use '../../styles' as styles;
@use '../../styles/tokens' as awsui;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;

.handle {
appearance: none;
Expand Down Expand Up @@ -66,7 +65,7 @@
text-decoration: none;
}

@include focus-visible.when-visible {
&:focus-visible {
&:not(.hide-focus) {
@include styles.focus-highlight(0px);
}
Expand Down
4 changes: 2 additions & 2 deletions src/internal/components/sortable-area/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@use '../../styles' as styles;
@use '../../styles/tokens' as awsui;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;

@mixin animated {
@include styles.with-motion {
Expand All @@ -19,7 +18,8 @@
border-end-start-radius: $border-radius;
border-end-end-radius: $border-radius;

@include focus-visible.when-visible-unfocused {
// stylelint-disable-next-line selector-combinator-disallowed-list
body:has(:focus-visible) & {
@include styles.focus-highlight(0px, $border-radius);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/split-panel/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@use '../internal/styles' as styles;
@use '../internal/styles/tokens' as awsui;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;
@use '../app-layout/constants' as constants;

$slider-width: 16px;
Expand Down
5 changes: 2 additions & 3 deletions src/table/body-cell/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@use '../../internal/styles' as styles;
@use '../../internal/styles/tokens' as awsui;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;

$cell-vertical-padding: awsui.$space-scaled-xs;
// Calculate padding to prevent a shift in content after selection due to the difference
Expand Down Expand Up @@ -404,7 +403,7 @@ $cell-negative-space-vertical: 2px;
}

&-focusable {
@include focus-visible.when-visible {
&:focus-visible {
// Making focus outline slightly smaller to not intersect with the success indicator.
@include safe-focus-highlight(-1px);
}
Expand Down Expand Up @@ -546,7 +545,7 @@ $cell-negative-space-vertical: 2px;
}
}

@include focus-visible.when-visible {
&:focus-visible {
@include cell-focus-outline;
}
}
3 changes: 1 addition & 2 deletions src/table/expandable-rows/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@use '../../internal/styles/index' as styles;
@use '../../internal/styles/tokens' as awsui;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;

@use './motion';

Expand Down Expand Up @@ -40,7 +39,7 @@
outline: 0;
color: awsui.$color-text-interactive-default;

@include focus-visible.when-visible {
&:focus-visible {
@include styles.focus-highlight(awsui.$space-button-inline-icon-focus-outline-gutter);
}

Expand Down
10 changes: 4 additions & 6 deletions src/table/header-cell/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
SPDX-License-Identifier: Apache-2.0
*/

@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;
@use '../../internal/styles/tokens' as awsui;
@use '../../internal/styles' as styles;

$cell-horizontal-padding: awsui.$space-scaled-l;

@mixin when-focus-visible-or-fake {
@include focus-visible.when-visible {
&:focus-visible {
@content;
}
&.header-cell-fake-focus {
@include focus-visible.when-visible-unfocused {
@content;
}
// stylelint-disable-next-line selector-combinator-disallowed-list
body:has(:focus-visible) &.header-cell-fake-focus {
@content;
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/table/resizer/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@use '../../internal/styles/index' as styles;
@use '../../internal/styles/tokens' as awsui;
@use '@cloudscape-design/component-toolkit/internal/focus-visible' as focus-visible;

//stylelint-disable-next-line selector-combinator-disallowed-list,selector-max-universal
.resize-active:not(.resize-active-with-focus) * {
Expand Down Expand Up @@ -66,12 +65,13 @@ th:not(:last-child) > .divider {
&:hover + .divider {
border-inline-start: $active-separator-width solid awsui.$color-border-divider-active;
}
&.has-focus {
@include focus-visible.when-visible-unfocused {
@include styles.focus-highlight(calc(#{awsui.$space-table-header-focus-outline-gutter} - 2px));
& {
position: absolute;
}
// stylelint-disable-next-line selector-combinator-disallowed-list
body:has(:focus-visible) &.has-focus {
@include styles.focus-highlight(calc(#{awsui.$space-table-header-focus-outline-gutter} - 2px));
// Required to avoid SASS mixed declarations warning (https://sass-lang.com/d/mixed-decls)
// stylelint-disable-next-line no-duplicate-selectors
& {
position: absolute;
}
}
}
Expand Down
Loading
Loading