-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
94 additions
and
75 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
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,29 @@ | ||
import { isDescendantOrTheSame } from '../utils'; | ||
import { element, btn } from './value'; | ||
|
||
export const close = () => { | ||
element.style.visibility = 'hidden'; | ||
stopListening(); | ||
}; | ||
|
||
export const listenForClose = () => { | ||
document.addEventListener('click', onClick); | ||
document.addEventListener('keyup', onKeyUp); | ||
}; | ||
|
||
const stopListening = () => { | ||
document.removeEventListener('click', onClick); | ||
document.removeEventListener('keyup', onKeyUp); | ||
}; | ||
|
||
const onClick = (e: Event) => { | ||
const el = e.target as HTMLElement; | ||
if (!isDescendantOrTheSame(el, [element, btn])) close(); | ||
}; | ||
|
||
const onKeyUp = (e: KeyboardEvent) => { | ||
if (e.code === 'Escape') { | ||
close(); | ||
btn.focus(); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,41 @@ | ||
import { debounce, isDescendantOrTheSame } from '../utils'; | ||
import { debounce } from '../utils'; | ||
import { close, listenForClose } from './close'; | ||
|
||
export const set = (el: HTMLDivElement, btnEl: HTMLButtonElement) => { | ||
element = el; | ||
btn = btnEl; | ||
}; | ||
|
||
export let element = null as HTMLDivElement; | ||
export let btn = null as HTMLButtonElement; | ||
export let isOpen = false; | ||
|
||
let menuWidth = 0; | ||
export const adjustWidth = () => { | ||
menuWidth = element.getBoundingClientRect().width; | ||
}; | ||
|
||
type Focusable = { focus(): void }; | ||
let firstFocusable = null as Focusable; | ||
export const setFirstFocusable = (el: Focusable) => { | ||
firstFocusable = el; | ||
}; | ||
|
||
export const toggle = debounce(() => { | ||
isOpen = !isOpen; | ||
if (isOpen) { | ||
fixPosition(); | ||
element.style.visibility = 'visible'; | ||
listenForClose(); | ||
firstFocusable.focus(); | ||
} else { | ||
close(); | ||
} | ||
}, 100); | ||
|
||
export const value = { | ||
element: null as HTMLDivElement, | ||
btn: null as HTMLButtonElement, | ||
isOpen: false, | ||
width: 0, | ||
_closeListener: { | ||
onClick(e: Event) { | ||
const el = e.target as HTMLElement; | ||
if (!isDescendantOrTheSame(el, [value.element, value.btn])) | ||
value.toggle(); | ||
}, | ||
onKeyUp(e: KeyboardEvent) { | ||
if (e.code === 'Escape') { | ||
value._setOpen(false); | ||
value.btn.focus(); | ||
} | ||
}, | ||
add() { | ||
document.addEventListener('click', this.onClick); | ||
document.addEventListener('keyup', this.onKeyUp); | ||
}, | ||
remove() { | ||
document.removeEventListener('click', this.onClick); | ||
document.removeEventListener('keyup', this.onKeyUp); | ||
}, | ||
}, | ||
firstFocusable: null as Focusable, | ||
_setOpen(bool: boolean) { | ||
if (bool) { | ||
this.fixPosition(); | ||
this.element.style.visibility = 'visible'; | ||
this._closeListener.add(); | ||
this.firstFocusable.focus(); | ||
} else { | ||
this.element.style.visibility = 'hidden'; | ||
this._closeListener.remove(); | ||
} | ||
this.isOpen = bool; | ||
}, | ||
toggle: debounce(function () { | ||
this._setOpen(!this.isOpen); | ||
}, 100), | ||
fixPosition() { | ||
const { y, height, width, left } = this.btn.getBoundingClientRect(); | ||
this.element.style.top = y + height + 8 + 'px'; | ||
this.element.style.left = left + width - this.width + 'px'; | ||
}, | ||
export const fixPosition = () => { | ||
const { y, height, width, left } = btn.getBoundingClientRect(); | ||
element.style.top = y + height + 8 + 'px'; | ||
element.style.left = left + width - menuWidth + 'px'; | ||
console.log(element.style.left); | ||
}; |