Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (core): support shadow roots in useActiveElement
Assuming you have a DOM structure like so: ``` body my-element #shadow-root input ``` When the `input` becomes active, the outer document's `activeElement` will actually be `my-element`, _not_ the input. Each shadow root has its own `activeElement` in case you want to get hold of the inner element in these cases. This adds support to `useActiveElement` such that you can pass a shadow root as the document to observe the active element of.
- Loading branch information
Showing
3 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
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
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,61 @@ | ||
import { useActiveElement } from '.' | ||
|
||
describe('useActiveElement', () => { | ||
let shadowHost: HTMLElement | ||
let input: HTMLInputElement | ||
let shadowInput: HTMLInputElement | ||
let shadowRoot: ShadowRoot | ||
|
||
beforeEach(() => { | ||
shadowHost = document.createElement('div') | ||
shadowRoot = shadowHost.attachShadow({ mode: 'open' }) | ||
input = document.createElement('input') | ||
shadowInput = input.cloneNode() as HTMLInputElement | ||
shadowRoot.appendChild(shadowInput) | ||
document.body.appendChild(input) | ||
document.body.appendChild(shadowHost) | ||
}) | ||
|
||
afterEach(() => { | ||
shadowHost.remove() | ||
input.remove() | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(useActiveElement).toBeDefined() | ||
}) | ||
|
||
it('should initialise correctly', () => { | ||
const activeElement = useActiveElement() | ||
|
||
expect(activeElement.value).to.equal(document.body) | ||
}) | ||
|
||
it('should initialise with already-active element', () => { | ||
input.focus() | ||
|
||
const activeElement = useActiveElement() | ||
|
||
expect(activeElement.value).to.equal(input) | ||
}) | ||
|
||
it('should accept custom document', () => { | ||
const activeElement = useActiveElement({ document: shadowRoot }) | ||
|
||
shadowInput.focus() | ||
|
||
expect(activeElement.value).to.equal(shadowInput) | ||
}) | ||
|
||
it('should observe focus/blur events', () => { | ||
const activeElement = useActiveElement() | ||
|
||
input.focus() | ||
|
||
expect(activeElement.value).to.equal(input) | ||
|
||
input.blur() | ||
|
||
expect(activeElement.value).to.equal(document.body) | ||
}) | ||
}) |
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