-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…n-accesible [READY_TO_REVIEW]accessible dropdown component
- Loading branch information
Showing
8 changed files
with
175 additions
and
60 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export const handleFocus = ( | ||
previousFocusedElement: React.MutableRefObject<Element | null>, | ||
containerRef: React.RefObject<HTMLUListElement> | ||
) => { | ||
previousFocusedElement.current = document.activeElement; | ||
containerRef.current?.focus(); | ||
|
||
if (containerRef.current) { | ||
containerRef.current.tabIndex = -1; | ||
} | ||
|
||
const focusableElementsInside = | ||
containerRef.current?.querySelectorAll('li[role="option"]'); | ||
|
||
if (focusableElementsInside && focusableElementsInside.length > 0) { | ||
const firstElement = focusableElementsInside[0]; | ||
if (firstElement instanceof HTMLElement) { | ||
firstElement.setAttribute('tabindex', '0'); | ||
firstElement.focus(); | ||
} | ||
|
||
focusableElementsInside.forEach((element: Element) => { | ||
if (element.getAttribute('aria-selected') === 'true') { | ||
const selectedElement = element; | ||
if (selectedElement instanceof HTMLElement) { | ||
selectedElement.setAttribute('tabindex', '0'); | ||
selectedElement.focus(); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
export const handleNextFocus = ( | ||
previousFocusedElement: React.MutableRefObject<Element | null> | ||
) => { | ||
const focusableElements = document.querySelectorAll( | ||
'a[href], button, textarea, input[type="text"], input[type="radio"], input[type="checkbox"]' | ||
); | ||
focusableElements.forEach((element: Element) => { | ||
element.removeAttribute('tabindex'); | ||
}); | ||
const currentElement = previousFocusedElement.current as HTMLElement; | ||
if (currentElement) { | ||
currentElement.focus(); | ||
currentElement.tabIndex = 0; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
} | ||
|
||
.select-container { | ||
position: relative; | ||
width: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
|
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