Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Autofocus fix #51

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

Merged
merged 4 commits into from
Jul 21, 2021
Merged
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
16 changes: 16 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
</style>
</head>
<body>
<h1>Base examples</h1>

<details>
<summary>Best robot: <span data-menu-button>Unknown</span></summary>
<details-menu>
Expand Down Expand Up @@ -60,6 +62,20 @@
</details-menu>
</details>

<h1>Autofocus example</h1>
<p><code>summary</code> may have <code>autofocus</code> so it's the initially focused element in the page.</p>
<p>Then any focusable element inside the popup can declare autofocus too, so it gets focus when the popup is opened.</p>

<details>
<summary data-menu-button autofocus>Autofocus picker</summary>
<details-menu>
<input type="text" autofocus />
<button type="submit" name="robot" value="Hubot" role="menuitemradio" data-menu-button-text>Hubot</button>
<button type="submit" name="robot" value="Bender" role="menuitemradio" data-menu-button-text>Bender</button>
<button type="submit" name="robot" value="BB-8" role="menuitemradio" data-menu-button-text>BB-8</button>
</details-menu>
</details>

<script type="text/javascript">
document.addEventListener('details-menu-selected', e => console.log(e))
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function closeCurrentMenu(details: Element) {

function autofocus(details: Element): boolean {
if (!details.hasAttribute('open')) return false
const input = details.querySelector<HTMLElement>('[autofocus]')
const input = details.querySelector<HTMLElement>('details-menu [autofocus]')
if (input) {
input.focus()
return true
Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,22 @@ describe('details-menu element', function () {

assert.equal(input, document.activeElement, 'toggle open focuses on [autofocus]')
})

it('summary autofocus should not impact with inner autofocus element', function () {
const details = document.querySelector('details')
const summary = details.querySelector('summary')
const input = details.querySelector('input')

// Summary is the initial element of the entire page, while input is the initial element in the popup
summary.setAttribute('autofocus', '')

summary.focus()
details.open = true
summary.dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter', bubbles: true}))
details.dispatchEvent(new CustomEvent('toggle'))

assert.equal(document.activeElement, input, 'toggle open focuses on [autofocus]')
})
})

describe('closing the menu', function () {
Expand Down