-
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
1 parent
ef57bfd
commit b063d9f
Showing
28 changed files
with
235 additions
and
200 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 |
---|---|---|
@@ -1,15 +1,14 @@ | ||
import { modalStore } from "./store.js"; | ||
|
||
export default function openModal(component, layer = "alert") { | ||
modalStore.changeModal(component, layer); | ||
return new Promise( (resolve)=>{ | ||
function observe() | ||
{ | ||
if(modalStore.getSnapshot(layer) !== component) { | ||
modalStore.changeModal(component, layer); | ||
return new Promise((resolve) => { | ||
function observe() { | ||
if (modalStore.getSnapshot(layer) !== component) { | ||
resolve(); | ||
clear(); | ||
} | ||
} | ||
const clear = modalStore.subscribe( observe ); | ||
} ); | ||
const clear = modalStore.subscribe(observe); | ||
}); | ||
} |
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,67 +1,64 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
function getEndPointChild(element) | ||
{ | ||
const focusableElements = [...element.querySelectorAll( | ||
"a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]" | ||
)].filter( elem=>elem.tabIndex >= 0 ); | ||
if(focusableElements.length === 0) return [null, null]; | ||
return [focusableElements[0], focusableElements[focusableElements.length - 1]]; | ||
function getEndPointChild(element) { | ||
const focusableElements = [ | ||
...element.querySelectorAll( | ||
"a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]", | ||
), | ||
].filter((elem) => elem.tabIndex >= 0); | ||
if (focusableElements.length === 0) return [null, null]; | ||
return [focusableElements[0], focusableElements[focusableElements.length - 1]]; | ||
} | ||
|
||
function useFocusTrap(active) | ||
{ | ||
const prevRef = useRef(null); | ||
const ref = useRef(null); | ||
const endPointChild = useRef([null, null]); | ||
useEffect( ()=>{ | ||
if(!active || ref.current === null) return; | ||
function useFocusTrap(active) { | ||
const prevRef = useRef(null); | ||
const ref = useRef(null); | ||
const endPointChild = useRef([null, null]); | ||
useEffect(() => { | ||
if (!active || ref.current === null) return; | ||
|
||
function renewEndPointChild() | ||
{ | ||
if(ref.current === null) return; | ||
endPointChild.current = getEndPointChild(ref.current); | ||
} | ||
function renewEndPointChild() { | ||
if (ref.current === null) return; | ||
endPointChild.current = getEndPointChild(ref.current); | ||
} | ||
|
||
function handleTabKey(e) { | ||
if(e.key !== "Tab") return; | ||
function handleTabKey(e) { | ||
if (e.key !== "Tab") return; | ||
|
||
const [first, last] = endPointChild.current; | ||
const [first, last] = endPointChild.current; | ||
|
||
if(document.activeElement === prevRef.current) { | ||
if(e.shiftKey) last?.focus(); | ||
else first?.focus(); | ||
e.preventDefault(); | ||
return; | ||
} | ||
if (document.activeElement === prevRef.current) { | ||
if (e.shiftKey) last?.focus(); | ||
else first?.focus(); | ||
e.preventDefault(); | ||
return; | ||
} | ||
|
||
if(first === null || last === null) return; | ||
if(document.activeElement === last && !e.shiftKey) { | ||
first.focus(); | ||
e.preventDefault(); | ||
} | ||
else if(document.activeElement === first && e.shiftKey) { | ||
last.focus(); | ||
e.preventDefault(); | ||
} | ||
} | ||
if (first === null || last === null) return; | ||
if (document.activeElement === last && !e.shiftKey) { | ||
first.focus(); | ||
e.preventDefault(); | ||
} else if (document.activeElement === first && e.shiftKey) { | ||
last.focus(); | ||
e.preventDefault(); | ||
} | ||
} | ||
|
||
renewEndPointChild(); | ||
prevRef.current = document.activeElement; | ||
document.addEventListener("keydown", handleTabKey); | ||
const config = { subtree: true, childList: true, attributeFilter: ["disabled", "tabindex"] }; | ||
const observer = new MutationObserver( renewEndPointChild ); | ||
observer.observe(ref.current, config); | ||
renewEndPointChild(); | ||
prevRef.current = document.activeElement; | ||
document.addEventListener("keydown", handleTabKey); | ||
const config = { subtree: true, childList: true, attributeFilter: ["disabled", "tabindex"] }; | ||
const observer = new MutationObserver(renewEndPointChild); | ||
observer.observe(ref.current, config); | ||
|
||
return ()=>{ | ||
document.removeEventListener("keydown", handleTabKey); | ||
observer.disconnect(); | ||
prevRef.current.focus(); | ||
} | ||
return () => { | ||
document.removeEventListener("keydown", handleTabKey); | ||
observer.disconnect(); | ||
prevRef.current.focus(); | ||
}; | ||
}, [active]); | ||
|
||
}, [active] ); | ||
|
||
return ref; | ||
return ref; | ||
} | ||
|
||
export default useFocusTrap; | ||
export default useFocusTrap; |
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
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
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.