Skip to content
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

It has been done #14

Closed
wants to merge 10 commits into from
Closed
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,8 @@ startKeyUX(window, [
hiddenKeyUX()
])
```

### data-keyux-ignore-hotkeys attribute


### data-keyux-hotkeys attribute
35 changes: 34 additions & 1 deletion hotkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,43 @@ function ignoreHotkeysIn(target) {
)
}

function choseTheElement(current, selector) {
let list = current.querySelectorAll(selector)

for (let el of list) {
let closestElement = el.closest('data-keyux-ignore-hotkeys')
if (closestElement === current || !closestElement) {
return el
}
}
return null
}

function checkHotkey(where, code, overrides) {
let codeOverride = overrides[code]
if (Object.values(overrides).includes(code) && !codeOverride) return false
return where.querySelector(`[aria-keyshortcuts="${codeOverride || code}" i]`)
let current = where.activeElement
let cssSelector = `[aria-keyshortcuts="${codeOverride || code}" i]`
let elementWithHotKey = null

if (current.hasAttribute('data-keyux-hotkeys')) {
let id = current.getAttribute('data-keyux-hotkeys')
let newCurrent = where.getElementById(id)
slavamukhin marked this conversation as resolved.
Show resolved Hide resolved

if (newCurrent) {
elementWithHotKey = choseTheElement(newCurrent, cssSelector)
console.log(newCurrent)
if (!elementWithHotKey) {
elementWithHotKey = choseTheElement(where, cssSelector)
}
}
} else if (current.hasAttribute('data-keyux-ignore-hotkeys')) {
elementWithHotKey = choseTheElement(current, cssSelector)
slavamukhin marked this conversation as resolved.
Show resolved Hide resolved
} else {
elementWithHotKey = choseTheElement(where, cssSelector)
}

return elementWithHotKey
}

function findHotKey(event, where, overrides) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"import": {
"./index.js": "{ startKeyUX, hotkeyKeyUX, pressKeyUX, menuKeyUX, jumpKeyUX, hiddenKeyUX, likelyWithKeyboard, getHotKeyHint }"
},
"limit": "1599 B"
"limit": "1740 B"
}
],
"clean-publish": {
Expand Down
26 changes: 26 additions & 0 deletions test/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,32 @@ const App: FC = () => {
setUpdate({})
}}
/>

<ul>
<li tabIndex={0}>
Item 1
<div>
<button aria-keyshortcuts="v">Validate</button>
<HotKeyHint hotkey="v" />
</div>
</li>

<li data-keyux-hotkeys="item3" data-keyux-ignore-hotkeys tabIndex={0}>
Item 2
<div>
<button aria-keyshortcuts="v">Validate</button>
<HotKeyHint hotkey="v" />
</div>
</li>

<li data-keyux-ignore-hotkeys id="item3" tabIndex={0}>
Item 3
<div>
{/* <button aria-keyshortcuts="v" >Validate</button>
<HotKeyHint hotkey="v" /> */}
</div>
</li>
</ul >
</>
)
}
Expand Down
60 changes: 60 additions & 0 deletions test/hotkey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,63 @@ test('allows to override hotkeys', () => {
press(window, { code: 'KeyQ', key: 'й' })
equal(clicked, 'bqb')
})

// test('dont serching in data-keyux-ignore-hotkeys', () => {
// let window = new JSDOM().window
// startKeyUX(window, [hotkeyKeyUX()])
// window.document.body.innerHTML = `
// <ul>
// <li data-keyux-ignore-hotkeys tabindex="0">
// Item 1
// <button aria-keyshortcuts="v">1</button>
// </li>
// <li tabindex="0">
// Item 2
// <button aria-keyshortcuts="v">2</button>
// </li>
// </ul>
// `

// let result = 0
// let buttons = window.document.querySelectorAll('button')
// for (let button of buttons) {
// button.addEventListener('click', () => {
// result += parseInt(button.textContent!)
// })
// }
// press(window, { key: 'v' })
// equal(result, 2)

// press(window, { code: 'KeyV', key: 'м' })
// equal(result, 2)
// })

// test('ignore data-keyux-ignore-hotkeys, with data-keyux-hotkeys', () => {
// let window = new JSDOM().window
// startKeyUX(window, [hotkeyKeyUX()])
// window.document.body.innerHTML = `
// <ul>
// <li data-keyux-hotkeys data-keyux-ignore-hotkeys tabindex="0">
// Item 1
// <button aria-keyshortcuts="v">1</button>
// </li>
// <li tabindex="0">
// Item 2
// <button aria-keyshortcuts="v">2</button>
// </li>
// </ul>
// `

// let result = 0
// let buttons = window.document.querySelectorAll('button')
// for (let button of buttons) {
// button.addEventListener('click', () => {
// result += parseInt(button.textContent!)
// })
// }
// press(window, { key: 'v' })
// equal(result, 1)

// press(window, { code: 'KeyV', key: 'м' })
// equal(result, 1)
// })