-
Notifications
You must be signed in to change notification settings - Fork 184
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some jerk (me) wrote in a dependency on the presence of |
||
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(); | ||
}); | ||
|
||
|
@@ -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}`), | ||
}; | ||
|
@@ -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'); | ||
|
@@ -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'); | ||
|
@@ -207,7 +218,6 @@ describe('triggerMode = focus (default)', () => { | |
tooltipText: 'Click me!', | ||
}); | ||
|
||
document.body.dataset.awsuiFocusVisible = 'true'; | ||
dragHandle.focus(); | ||
|
||
expect(getDirectionButton('block-start')).toBeInTheDocument(); | ||
|
@@ -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'); | ||
|
@@ -240,7 +251,6 @@ describe('triggerMode = keyboard-activate', () => { | |
triggerMode: 'keyboard-activate', | ||
}); | ||
|
||
document.body.dataset.awsuiFocusVisible = 'true'; | ||
dragHandle.focus(); | ||
expectDirectionButtonToBeHidden('block-start'); | ||
expectDirectionButtonToBeHidden('block-end'); | ||
|
@@ -261,7 +271,6 @@ describe('triggerMode = keyboard-activate', () => { | |
triggerMode: 'keyboard-activate', | ||
}); | ||
|
||
document.body.dataset.awsuiFocusVisible = 'true'; | ||
dragHandle.focus(); | ||
expectDirectionButtonToBeHidden('block-start'); | ||
expectDirectionButtonToBeHidden('block-end'); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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' }, | ||
|
@@ -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'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.