awesome-uses/src/components/Topics.js

124 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-01-07 20:23:53 +00:00
import React, { useContext } from 'react';
2020-01-07 22:58:15 +00:00
import styled from 'styled-components';
2020-01-07 20:23:53 +00:00
import FilterContext from '../context/FilterContext';
2020-01-08 16:06:32 +00:00
import * as icons from '../util/icons';
2020-01-07 20:23:53 +00:00
export default function Topics() {
2020-01-08 15:53:02 +00:00
const { countries, tags, devices, currentTag, setCurrentTag } = useContext(
FilterContext
);
2020-01-08 11:49:41 +00:00
2020-01-07 20:23:53 +00:00
return (
2020-01-07 22:58:15 +00:00
<Tags>
2020-01-07 20:23:53 +00:00
{tags.map(tag => (
<Tag
currentTag={tag.name === currentTag}
2020-01-07 20:23:53 +00:00
htmlFor={`filter-${tag.name}`}
key={`filter-${tag.name}`}
clickable
2020-01-07 20:23:53 +00:00
>
<input
type="radio"
name="tag"
id={`filter-${tag.name}`}
value={tag.name}
checked={tag.name === currentTag}
onChange={e => setCurrentTag(e.currentTarget.value)}
/>
{tag.name}
2020-01-07 22:58:15 +00:00
<TagCount>{tag.count}</TagCount>
</Tag>
2020-01-07 20:23:53 +00:00
))}
{countries.map(tag => (
<Tag
currentTag={tag.emoji === currentTag}
2020-01-07 20:23:53 +00:00
htmlFor={`filter-${tag.name}`}
key={`filter-${tag.name}`}
title={tag.name}
clickable
2020-01-07 20:23:53 +00:00
>
<input
type="radio"
name="tag"
id={`filter-${tag.name}`}
value={tag.emoji}
checked={tag.emoji === currentTag}
onChange={e => setCurrentTag(e.currentTarget.value)}
/>
2020-01-07 22:58:15 +00:00
<TagEmoji>{tag.emoji}</TagEmoji>
<TagCount>{tag.count}</TagCount>
</Tag>
2020-01-07 20:23:53 +00:00
))}
2020-01-08 15:53:02 +00:00
{devices.map(tag => (
<Tag
currentTag={tag.name === currentTag}
2020-01-08 11:44:52 +00:00
htmlFor={`filter-${tag.name}`}
key={`filter-${tag.name}`}
title={tag.name}
clickable
2020-01-08 11:44:52 +00:00
>
<input
type="radio"
name="computer"
id={`filter-${tag.name}`}
value={tag.name}
checked={tag.name === currentTag}
onChange={e => setCurrentTag(e.currentTarget.value)}
/>
2020-01-08 16:06:32 +00:00
<img height="20px" src={icons[tag.name]} alt={tag.name} />
2020-01-08 15:53:02 +00:00
<TagCount>{tag.count}</TagCount>
</Tag>
2020-01-08 11:44:52 +00:00
))}
2020-01-07 22:58:15 +00:00
</Tags>
2020-01-07 20:23:53 +00:00
);
}
2020-01-07 22:58:15 +00:00
// Component Styles
const Tags = styled.div`
list-style-type: none;
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
`;
const Tag = styled.label`
2020-01-07 22:58:15 +00:00
background: var(--pink);
margin: 2px;
border-radius: 3px;
font-size: ${props => (props.small ? `1.2rem;` : `1.7rem;`)};
2020-01-07 22:58:15 +00:00
padding: 5px;
color: hsla(0, 100%, 100%, 0.8);
transition: background-color 0.2s;
cursor: ${props => (props.clickable? "pointer" : "default")};
2020-01-07 22:58:15 +00:00
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
input {
display: none;
}
${props =>
props.currentTag &&
`
2020-01-07 22:58:15 +00:00
background: var(--yellow);
color: hsla(0, 100%, 0%, 0.8);
`}
2020-01-07 22:58:15 +00:00
`;
const TagEmoji = styled.span`
transform: scale(1.45);
`;
const TagCount = styled.span`
background: var(--blue);
font-size: 1rem;
color: white;
padding: 2px;
border-radius: 2px;
margin-left: 5px;
`;
export { Tag, Tags };