-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve focus behavior on terms
- Loading branch information
1 parent
c81ad73
commit 195adca
Showing
3 changed files
with
128 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,50 @@ | ||
import {getEventTarget, isCustom} from '../utils'; | ||
import { | ||
Selector, | ||
closeDefinition, | ||
createDefinitionElement, | ||
openClass, | ||
openDefinition, | ||
openDefinitionClass, | ||
setDefinitionId, | ||
setDefinitionPosition, | ||
setDefinitonAriaLive, | ||
} from './utils'; | ||
import {getEventTarget, isCustom} from '../utils'; | ||
|
||
if (typeof document !== 'undefined') { | ||
document.addEventListener('click', (event) => { | ||
const openDefinition = document.getElementsByClassName( | ||
openDefinitionClass, | ||
)[0] as HTMLElement; | ||
const target = getEventTarget(event) as HTMLElement; | ||
|
||
const termId = target.getAttribute('id'); | ||
const termKey = target.getAttribute('term-key'); | ||
let definitionElement = document.getElementById(termKey + '_element'); | ||
|
||
if (termKey && !definitionElement) { | ||
definitionElement = createDefinitionElement(target); | ||
} | ||
|
||
const isSameTerm = openDefinition && termId === openDefinition.getAttribute('term-id'); | ||
if (isSameTerm) { | ||
closeDefinition(openDefinition); | ||
return; | ||
} | ||
|
||
const isTargetDefinitionContent = target.closest( | ||
[Selector.CONTENT.replace(' ', ''), openClass].join('.'), | ||
); | ||
|
||
if (openDefinition && !isTargetDefinitionContent) { | ||
closeDefinition(openDefinition); | ||
} | ||
|
||
if (isCustom(event) || !target.matches(Selector.TITLE) || !definitionElement) { | ||
return; | ||
if (getEventTarget(event) || !isCustom(event)) { | ||
openDefinition(getEventTarget(event) as HTMLElement); | ||
} | ||
|
||
setDefinitionId(definitionElement, target); | ||
setDefinitonAriaLive(definitionElement, target); | ||
setDefinitionPosition(definitionElement, target); | ||
|
||
definitionElement.classList.toggle(openClass); | ||
}); | ||
|
||
document.addEventListener('keydown', (event) => { | ||
const openDefinition = document.getElementsByClassName( | ||
const openedDefinition = document.getElementsByClassName( | ||
openDefinitionClass, | ||
)[0] as HTMLElement; | ||
if (event.key === 'Escape' && openDefinition) { | ||
closeDefinition(openDefinition); | ||
|
||
if (event.key === 'Enter' && document.activeElement) { | ||
openDefinition(document.activeElement as HTMLElement); | ||
} | ||
|
||
if (event.key === 'Escape' && openedDefinition) { | ||
closeDefinition(openedDefinition); | ||
} | ||
}); | ||
|
||
window.addEventListener('resize', () => { | ||
const openDefinition = document.getElementsByClassName( | ||
const openedDefinition = document.getElementsByClassName( | ||
openDefinitionClass, | ||
)[0] as HTMLElement; | ||
|
||
if (!openDefinition) { | ||
if (!openedDefinition) { | ||
return; | ||
} | ||
|
||
const termId = openDefinition.getAttribute('term-id') || ''; | ||
const termId = openedDefinition.getAttribute('term-id') || ''; | ||
const termElement = document.getElementById(termId); | ||
|
||
if (!termElement) { | ||
openDefinition.classList.toggle(openClass); | ||
openedDefinition.classList.toggle(openClass); | ||
return; | ||
} | ||
|
||
setDefinitionPosition(openDefinition, termElement); | ||
setDefinitionPosition(openedDefinition, termElement); | ||
}); | ||
} |
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