diff --git a/src/components/Search/index.js b/src/components/Search/index.js
index e56bc0358..0b8403059 100644
--- a/src/components/Search/index.js
+++ b/src/components/Search/index.js
@@ -13,6 +13,8 @@ import { useMedia } from 'react-use'
import { useAllPairsInUniswap, useAllTokensInUniswap } from '../../contexts/GlobalData'
import { OVERVIEW_TOKEN_BLACKLIST, OVERVIEW_PAIR_BLACKLIST } from '../../constants'
+import { useKeyPress } from '../../hooks'
+
const Wrapper = styled.div`
display: flex;
position: relative;
@@ -68,7 +70,7 @@ const Menu = styled.div`
width: 100%;
top: 50px;
max-height: 540px;
- overflow: scroll;
+ overflow-y: scroll;
left: 0;
padding-bottom: 20px;
background: white;
@@ -89,6 +91,10 @@ const MenuItem = styled(Row)`
cursor: pointer;
background-color: #f7f8fa;
}
+ :focus {
+ background-color: #f7f8fa;
+ outline: black auto 1px;
+ }
`
const Heading = styled(Row)`
@@ -119,7 +125,13 @@ export const Search = ({ small = false }) => {
const allPairs = useAllPairsInUniswap()
const allPairData = useAllPairData()
- const [showMenu, toggleMenu] = useState(false)
+ const [cursor, setCursor] = useState(null)
+ const downKeyPressed = useKeyPress('ArrowDown')
+ const escapeKeyPressed = useKeyPress('Escape')
+ const tabKeyPressed = useKeyPress('Tab')
+ const upKeyPressed = useKeyPress('ArrowUp')
+
+ const [showMenu, setShowMenu] = useState(false)
const [value, setValue] = useState('')
const [, toggleShadow] = useState(false)
const [, toggleBottomShadow] = useState(false)
@@ -134,9 +146,9 @@ export const Search = ({ small = false }) => {
useEffect(() => {
if (value !== '') {
- toggleMenu(true)
+ setShowMenu(true)
} else {
- toggleMenu(false)
+ setShowMenu(false)
}
}, [value])
@@ -278,10 +290,50 @@ export const Search = ({ small = false }) => {
setPairsShown(Math.min(Object.keys(filteredPairList).length, 3))
}, [filteredPairList])
+ useEffect(() => {
+ if (pairsShown + tokensShown === 0) {
+ setCursor(undefined)
+ } else if (showMenu && downKeyPressed && cursor === undefined) {
+ setCursor(0)
+ } else if (showMenu && downKeyPressed && cursor < pairsShown + tokensShown) {
+ setCursor(cursor + 1)
+ }
+ }, [showMenu, downKeyPressed])
+
+ useEffect(() => {
+ if (pairsShown + tokensShown === 0) {
+ setCursor(undefined)
+ } else if (showMenu && upKeyPressed && cursor === undefined) {
+ setCursor(pairsShown + tokensShown - 1)
+ } else if (showMenu && upKeyPressed && cursor === 0) {
+ setCursor(pairsShown + tokensShown - 1)
+ } else if (showMenu && upKeyPressed && cursor > 0) {
+ setCursor(cursor - 1)
+ }
+ }, [showMenu, upKeyPressed])
+
+ useEffect(() => {
+ setCursor(undefined)
+ }, [tabKeyPressed])
+
+ useEffect(() => {
+ setCursor(undefined)
+ setShowMenu(false)
+ }, [escapeKeyPressed])
+
+ useEffect(() => {
+ const canUseCursor = Boolean(
+ Number.isInteger(cursor) && menuRef.current && menuRef.current.querySelectorAll('a')[cursor]
+ )
+ if (canUseCursor) {
+ menuRef.current.querySelectorAll('a')[cursor].focus()
+ }
+ }, [cursor])
+
function onDismiss() {
setPairsShown(3)
setTokensShown(3)
- toggleMenu(false)
+ setShowMenu(false)
setValue('')
}
@@ -296,7 +348,7 @@ export const Search = ({ small = false }) => {
) {
setPairsShown(3)
setTokensShown(3)
- toggleMenu(false)
+ setShowMenu(false)
}
}
@@ -338,7 +390,7 @@ export const Search = ({ small = false }) => {
setValue(e.target.value)
}}
onFocus={() => {
- toggleMenu(true)
+ setShowMenu(true)
}}
/>
@@ -350,68 +402,51 @@ export const Search = ({ small = false }) => {