-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: focus visible polyfill should be initialised/disposed correctly (#…
…29564) * fix: focus visible polyfill should be initialised/disposed correctly * Disposing focus visible polyfill should clear state and remove the focus visible attribute. * Initializing focus visible polyfill should try to apply the focus visible attribute Fixes #29402 * changefile * refactor
- Loading branch information
Showing
3 changed files
with
93 additions
and
25 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-tabster-79940260-a8ef-4fb7-b958-0ab00886d98a.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "fix: focus visible polyfill should be initialised/disposed correctly", | ||
"packageName": "@fluentui/react-tabster", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/react-components/react-tabster/src/focus/focusVisiblePolyfill.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createKeyborg, disposeKeyborg, Keyborg } from 'keyborg'; | ||
import { FOCUS_VISIBLE_ATTR } from './constants'; | ||
import { applyFocusVisiblePolyfill } from './focusVisiblePolyfill'; | ||
import { fireEvent } from '@testing-library/dom'; | ||
|
||
describe('focus visible polyfill', () => { | ||
let keyborg: Keyborg; | ||
beforeEach(() => { | ||
keyborg = createKeyborg(window); | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
afterEach(() => { | ||
if (keyborg) { | ||
disposeKeyborg(keyborg); | ||
} | ||
}); | ||
|
||
it('should set focus visible attribute on initialization if in keyboard navigation mode', () => { | ||
const scope = document.createElement('div'); | ||
const button = document.createElement('button'); | ||
scope.append(button); | ||
document.body.append(scope); | ||
|
||
button.focus(); | ||
fireEvent.keyDown(window); | ||
const dispose = applyFocusVisiblePolyfill(scope, window); | ||
|
||
expect(button.hasAttribute(FOCUS_VISIBLE_ATTR)).toBe(true); | ||
|
||
dispose(); | ||
}); | ||
|
||
it('should not set focus visible attribute on initialization if not in keyboard navigation mode', () => { | ||
const scope = document.createElement('div'); | ||
const button = document.createElement('button'); | ||
scope.append(button); | ||
document.body.append(scope); | ||
|
||
button.focus(); | ||
const dispose = applyFocusVisiblePolyfill(scope, window); | ||
|
||
expect(button.hasAttribute(FOCUS_VISIBLE_ATTR)).toBe(false); | ||
|
||
dispose(); | ||
}); | ||
|
||
it('should remove focus visible attribute on dispose', () => { | ||
const scope = document.createElement('div'); | ||
const button = document.createElement('button'); | ||
scope.append(button); | ||
document.body.append(scope); | ||
|
||
button.focus(); | ||
fireEvent.keyDown(window); | ||
const dispose = applyFocusVisiblePolyfill(scope, window); | ||
|
||
expect(button.hasAttribute(FOCUS_VISIBLE_ATTR)).toBe(true); | ||
dispose(); | ||
expect(button.hasAttribute(FOCUS_VISIBLE_ATTR)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters