File size: 1,377 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import React, { useState } from 'react';
import styled from 'styled-components';
const EMOJIS_COUNT = 50;
const EMOJIS_COLUMNS = 11;
const ITEM_SIZE = 22;
const StyledEmojiPickerButton = styled.button`
background: 0;
padding: 0;
margin: 0;
border: 0;
outline: 0;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
width: 34px;
height: 44px;
flex: 0 0 auto;
:hover .sprite {
filter: grayscale(0%);
opacity: 1;
transform: scale(1.275);
}
.sprite {
width: 22px;
height: 22px;
background-image: url('https://i.imgur.com/GCsoD7z.png');
background-repeat: no-repeat;
background-size: 242px 110px;
transition: filter 0.1s ease-in-out, opacity 0.1s ease-in-out, transform 0.1s ease-in-out;
filter: grayscale(100%);
opacity: 0.3;
}
`;
const EmojiPickerButton = () => {
const [index, setIndex] = useState(0);
const pickRandomIndex = () => {
setIndex(Math.floor(Math.random() * EMOJIS_COUNT));
};
const x = (index % EMOJIS_COLUMNS) * ITEM_SIZE;
const y = Math.floor(index / EMOJIS_COLUMNS) * ITEM_SIZE;
const backgroundPosition = `-${x}px -${y}px`;
return (
<StyledEmojiPickerButton onMouseEnter={pickRandomIndex}>
<div className="sprite" style={{ backgroundPosition }} />
</StyledEmojiPickerButton>
);
};
export default EmojiPickerButton;
|