Skip to content

Commit

Permalink
feat(menu): Profile avatar (#159)
Browse files Browse the repository at this point in the history
* feat(menu): Profile avatar

* refactor(menu): Moved no profile logic out of Avatar

* chore(menu): Add aria label
  • Loading branch information
hachiojidev authored Feb 2, 2021
1 parent 3e7b991 commit 2087ada
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 271 deletions.
66 changes: 35 additions & 31 deletions src/__tests__/widgets/menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ it("renders correctly", () => {
expect(asFragment()).toMatchInlineSnapshot(`
<DocumentFragment>
<div
class="sc-lmoMRL bBKjWM"
class="sc-iJuUWI hDEHSw"
>
<nav
class="sc-iJuUWI cpqwLz"
class="sc-giIncl kXYSnc"
>
<div
class="sc-dlfnbm fcyuqz"
class="sc-gsTCUz dwpdGQ"
>
<button
aria-label="Toggle menu"
class="sc-hKgILt cgarvn sc-eCssSg hYyuPy"
type="button"
>
<svg
class="sc-gsTCUz ePKWQB"
class="sc-dlfnbm esrYrS"
color="textSubtle"
viewBox="0 0 24 24"
width="24px"
Expand All @@ -76,7 +76,7 @@ it("renders correctly", () => {
href="/"
>
<svg
class="sc-gsTCUz eqJhFw mobile-icon"
class="sc-dlfnbm cqTLJf mobile-icon"
color="text"
viewBox="0 0 32 32"
width="20px"
Expand Down Expand Up @@ -108,7 +108,7 @@ it("renders correctly", () => {
/>
</svg>
<svg
class="sc-gsTCUz eqJhFw desktop-icon"
class="sc-dlfnbm cqTLJf desktop-icon"
color="text"
viewBox="0 0 160 26"
width="20px"
Expand Down Expand Up @@ -185,17 +185,21 @@ it("renders correctly", () => {
</svg>
</a>
</div>
<div>
<button
class="sc-hKgILt lePvmD"
type="button"
>
0xbd...c980
</button>
<div
class="sc-gsTCUz dwpdGQ"
>
<div>
<button
class="sc-hKgILt lePvmD"
type="button"
>
0xbd...c980
</button>
</div>
</div>
</nav>
<div
class="sc-giIncl kfFqkk"
class="sc-ezrdKe qgmWt"
>
<div
class="sc-hHftDr kbPZub"
Expand All @@ -213,7 +217,7 @@ it("renders correctly", () => {
href="/"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand All @@ -238,7 +242,7 @@ it("renders correctly", () => {
role="button"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand All @@ -256,7 +260,7 @@ it("renders correctly", () => {
Trade
</div>
<svg
class="sc-gsTCUz eqJhFw"
class="sc-dlfnbm cqTLJf"
color="text"
viewBox="0 0 24 24"
width="20px"
Expand All @@ -279,7 +283,7 @@ it("renders correctly", () => {
href="/farms"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand Down Expand Up @@ -317,7 +321,7 @@ it("renders correctly", () => {
href="/syrup"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand Down Expand Up @@ -347,7 +351,7 @@ it("renders correctly", () => {
href="/lottery"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand All @@ -374,7 +378,7 @@ it("renders correctly", () => {
href="/nft"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand All @@ -399,7 +403,7 @@ it("renders correctly", () => {
href="/profile"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand All @@ -424,7 +428,7 @@ it("renders correctly", () => {
role="button"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand All @@ -445,7 +449,7 @@ it("renders correctly", () => {
Info
</div>
<svg
class="sc-gsTCUz eqJhFw"
class="sc-dlfnbm cqTLJf"
color="text"
viewBox="0 0 24 24"
width="20px"
Expand All @@ -468,7 +472,7 @@ it("renders correctly", () => {
role="button"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand All @@ -484,7 +488,7 @@ it("renders correctly", () => {
IFO
</div>
<svg
class="sc-gsTCUz eqJhFw"
class="sc-dlfnbm cqTLJf"
color="text"
viewBox="0 0 24 24"
width="20px"
Expand All @@ -507,7 +511,7 @@ it("renders correctly", () => {
role="button"
>
<svg
class="sc-gsTCUz ldLGgy"
class="sc-dlfnbm fDSbid"
color="text"
viewBox="0 0 24 24"
width="24px"
Expand All @@ -523,7 +527,7 @@ it("renders correctly", () => {
More
</div>
<svg
class="sc-gsTCUz eqJhFw"
class="sc-dlfnbm cqTLJf"
color="text"
viewBox="0 0 24 24"
width="20px"
Expand All @@ -547,7 +551,7 @@ it("renders correctly", () => {
type="button"
>
<svg
class="sc-gsTCUz eqJhFw"
class="sc-dlfnbm cqTLJf"
color="text"
viewBox="0 0 24 24"
width="20px"
Expand All @@ -561,12 +565,12 @@ it("renders correctly", () => {
</div>
</div>
<div
class="sc-ezrdKe hUsMni"
class="sc-bYEvPH ieIXYJ"
>
body
</div>
<div
class="sc-bdfBwQ sc-bYEvPH bQcnNj dNBFRM"
class="sc-bdfBwQ sc-kLgntA bQcnNj hBpZTL"
role="presentation"
/>
</div>
Expand Down
49 changes: 49 additions & 0 deletions src/widgets/Menu/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from "react";
import styled from "styled-components";
import { Link } from "react-router-dom";
import { Profile } from "./types";
import NoProfileAvatar from "./icons/NoProfileAvatar";

interface AvatarProps {
profile: Profile;
}

const StyledAvatar = styled.div`
margin-left: 8px;
img {
border-radius: 50%;
}
`;

const Avatar: React.FC<AvatarProps> = ({ profile }) => {
const { username = "Bunny", image, profileLink, noProfileLink } = profile;
const link = profile.username ? profileLink : noProfileLink;
const isExternal = link.startsWith("http");
const ariaLabel = "Link to profile";
const icon = image ? (
<img src={image} alt="profile avatar" height="32px" width="32px" />
) : (
<NoProfileAvatar width="32px" height="32px" />
);

if (isExternal) {
return (
<StyledAvatar title={username}>
<a href={link} aria-label={ariaLabel}>
{icon}
</a>
</StyledAvatar>
);
}

return (
<StyledAvatar title={username}>
<Link to={link} aria-label={ariaLabel}>
{icon}
</Link>
</StyledAvatar>
);
};

export default Avatar;
8 changes: 7 additions & 1 deletion src/widgets/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React, { useState, useEffect, useRef } from "react";
import styled from "styled-components";
import throttle from "lodash/throttle";
import Overlay from "../../components/Overlay/Overlay";
import { Flex } from "../../components/Flex";
import { useMatchBreakpoints } from "../../hooks";
import Logo from "./Logo";
import Panel from "./Panel";
import UserBlock from "./UserBlock";
import { NavProps } from "./types";
import { MENU_HEIGHT, SIDEBAR_WIDTH_REDUCED, SIDEBAR_WIDTH_FULL } from "./config";
import Avatar from "./Avatar";

const Wrapper = styled.div`
position: relative;
Expand Down Expand Up @@ -67,6 +69,7 @@ const Menu: React.FC<NavProps> = ({
currentLang,
cakePriceUsd,
links,
profile,
children,
}) => {
const { isXl } = useMatchBreakpoints();
Expand Down Expand Up @@ -116,7 +119,10 @@ const Menu: React.FC<NavProps> = ({
isDark={isDark}
href={homeLink?.href ?? "/"}
/>
<UserBlock account={account} login={login} logout={logout} />
<Flex>
<UserBlock account={account} login={login} logout={logout} />
{profile && <Avatar profile={profile} />}
</Flex>
</StyledNav>
<BodyWrapper>
<Panel
Expand Down
34 changes: 34 additions & 0 deletions src/widgets/Menu/icons/NoProfileAvatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from "react";
import { useTheme } from "styled-components";
import Svg from "../../../components/Svg/Svg";
import { SvgProps } from "../../../components/Svg/types";

const Icon: React.FC<SvgProps> = (props) => {
const theme = useTheme();
const primaryColor = theme.isDark ? "#3C3742" : "#e9eaeb";
const secondaryColor = theme.isDark ? "#666171" : "#bdc2c4";

return (
<Svg viewBox="0 0 32 32" {...props}>
<path d="M32 16c0 8.837-7.163 16-16 16S0 24.837 0 16 7.163 0 16 0s16 7.163 16 16z" fill={primaryColor} />
<mask id="A" mask-type="alpha" maskUnits="userSpaceOnUse" x="0" y="0" width="32" height="32">
<path d="M32 16c0 8.837-7.163 16-16 16S0 24.837 0 16 7.163 0 16 0s16 7.163 16 16z" fill="#c4c4c4" />
</mask>
<g mask="url(#A)">
<path
d="M25.128 16.436c0 3.115-4.133 5.641-9.231 5.641s-9.231-2.526-9.231-5.641V15h18.461v1.436zm2.205 13.806c0-3.815-5.074-6.908-11.333-6.908S4.667 26.426 4.667 30.242V32h22.667v-1.759z"
fill={secondaryColor}
/>
<path
fillRule="evenodd"
d="M10.234 5.601C9.942 4.264 10.96 3 12.328 3c1.184 0 2.143.959 2.143 2.143v3.873l1.427-.067c.589 0 1.166.034 1.724.098V5.143c0-1.184.959-2.143 2.143-2.143 1.368 0 2.386 1.264 2.093 2.601l-.931 4.258c2.529 1.006 4.201 2.749 4.201 4.731 0 3.115-4.133 5.641-9.231 5.641s-9.231-2.526-9.231-5.641c0-2.053 1.794-3.849 4.476-4.836l-.908-4.153z"
fill={secondaryColor}
/>
<ellipse cx="12.308" cy="14.846" rx="1.026" ry="1.538" fill={primaryColor} />
<ellipse cx="19.385" cy="14.846" rx="1.026" ry="1.538" fill={primaryColor} />
</g>
</Svg>
);
};

export default Icon;
Loading

0 comments on commit 2087ada

Please sign in to comment.