Skip to content

Instantly share code, notes, and snippets.

@stoplion
Created January 8, 2021 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoplion/f4984e10a0cb4dfd4d184b0aaceb1ee4 to your computer and use it in GitHub Desktop.
Save stoplion/f4984e10a0cb4dfd4d184b0aaceb1ee4 to your computer and use it in GitHub Desktop.
MWjBvjv
<div id="root"></div>
const tokensUrl = "https://raw.githubusercontent.com/Uniswap/token-lists/master/test/schema/bigexample.tokenlist.json";
function iconURL(address) {
return `https://github.com/trustwallet/assets/blob/master/blockchains/ethereum/assets/${address}/logo.png?raw=true`
}
function Dropdown() {
const [dropDownOpen, setDropDownOpen] = React.useState(false);
const [selectedCoin, setSelectedCoin] = React.useState(null);
const [coins, setCoins] = React.useState([]);
React.useEffect(() => {
fetch(tokensUrl)
.then(data => {
return data;
})
.then(data => {
return data.json()
})
.then(data => {
setCoins(data.tokens)
})
}, [])
return (
<>
<h1>
You're selected coin is: {selectedCoin}
</h1>
<div className="dropdown">
<button
onClick={() => {
setDropDownOpen(!dropDownOpen)
}}
className="dropdown-button"
>
Select a coin
</button>
{dropDownOpen &&
<div className="dropdown-list">
{coins.map(token => {
return (
<div
className="dropdown-item"
onClick={() => {
setSelectedCoin(token.symbol)
}}
>
<img className="dropdown-logo" src={iconURL(token.address)} />
{token.symbol}
</div>
)
})}
</div>
}
</div>
</>
)
}
ReactDOM.render(<Dropdown />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
body {
display: flex;
justify-content: center;
padding: 10px;
}
.dropdown {
width: 500px;
position: relative;
}
.dropdown-list {
position: absolute;
width: 100%;
top: 100%;
transform: translateY(2px);
border: 1px solid gray;
border-radius: 10px;
overflow: hidden;
max-height: 300px;
overflow: scroll;
}
.dropdown-item {
padding: 6px 10px;
display: flex;
align-items: center;
cursor: pointer;
}
.dropdown-item:hover {
background: #eee;
}
.dropdown-logo {
width: 1.5rem;
margin-right: 5px;
}
.dropdown-button {
background: black;
color: white;
border: 0;
width: 100%;
margin: 0;
padding: 13px;
cursor: pointer;
border-radius: 10px;
font-size: 1.2rem;
outline: none;
}
.dropdown-button:hover {
background: #313131;
transition: all;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment