-
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, anchors, clipboard button (#486)
* feat: improve focus behavior on terms * refactor * feat: change anchor opacity on focus * feat: use button tag for clipboard button * ref * update snapshots * snapshots for terms * update snap * fix ts erorr
- Loading branch information
1 parent
0e7a080
commit 4323b50
Showing
9 changed files
with
222 additions
and
146 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 |
---|---|---|
@@ -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
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
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
Oops, something went wrong.