Skip to content

Commit

Permalink
TokenIcon + TokenIcon.Group
Browse files Browse the repository at this point in the history
  • Loading branch information
bpierre committed Apr 26, 2024
1 parent c58216a commit d3dff5f
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 0 deletions.
7 changes: 7 additions & 0 deletions frontend/uikit-gallery/src/TokenIcon/1. Single.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use client";

import { TokenIconFixture } from "./shared";

export default function Fixture() {
return <TokenIconFixture defaultMode="single" />;
}
7 changes: 7 additions & 0 deletions frontend/uikit-gallery/src/TokenIcon/2. Group.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use client";

import { TokenIconFixture } from "./shared";

export default function Fixture() {
return <TokenIconFixture defaultMode="group" />;
}
54 changes: 54 additions & 0 deletions frontend/uikit-gallery/src/TokenIcon/shared.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use client";

import { TokenIcon } from "@liquity2/uikit";
import { useFixtureSelect } from "react-cosmos/client";

const options = [
"BOLD" as const,
"ETH" as const,
"OSETH" as const,
"RETH" as const,
"SWETH" as const,
"WSTETH" as const,
];

const emptyOption = "−";

function isNotEmptyOption(
value: (typeof options)[number] | typeof emptyOption,
): value is Exclude<typeof value, typeof emptyOption> {
return value !== emptyOption;
}

export function TokenIconFixture({
defaultMode,
}: {
defaultMode: "single" | "group";
}) {
const symbols = options.map((name, index) => {
return useFixtureSelect(`token ${index + 1}`, {
options: [emptyOption, ...options],
defaultValue: defaultMode === "single" && index > 0 ? emptyOption : name,
})[0];
});

const validSymbols = symbols.filter(isNotEmptyOption);

return (
<div
style={{
display: "flex",
justifyContent: "center",
}}
>
<TokenIcon.Group>
{validSymbols.map((symbol, index) => (
<TokenIcon
key={symbol + index}
symbol={symbol}
/>
))}
</TokenIcon.Group>
</div>
);
}
65 changes: 65 additions & 0 deletions frontend/uikit/src/TokenIcon/TokenIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { ComponentProps, ReactElement, ReactNode } from "react";

import { Children } from "react";
import { css } from "../../styled-system/css";
import tokenBold from "./icons/BOLD.svg";
import tokenEth from "./icons/ETH.svg";
import tokenOseth from "./icons/OSETH.svg";
import tokenReth from "./icons/RETH.svg";
import tokenSweth from "./icons/SWETH.svg";
import tokenWsteth from "./icons/WSTETH.svg";

export type IconName = "BOLD" | "ETH" | "OSETH" | "RETH" | "SWETH" | "WSTETH";

function srcFromName(name: IconName) {
if (name === "BOLD") return tokenBold;
if (name === "ETH") return tokenEth;
if (name === "OSETH") return tokenOseth;
if (name === "RETH") return tokenReth;
if (name === "SWETH") return tokenSweth;
if (name === "WSTETH") return tokenWsteth;
throw new Error(`Unsupported token icon: ${name}`);
}

export function TokenIcon({ symbol }: { symbol: IconName }) {
return (
<img
title={symbol}
alt={symbol}
height={24}
src={srcFromName(symbol)}
width={24}
/>
);
}

TokenIcon.Group = function TokenIconGroup<
C extends ReactElement<ComponentProps<typeof TokenIcon>>,
>({
children,
}: {
children: C | C[];
}) {
return (
<div
style={{
display: "flex",
justifyContent: "center",
gap: -8,
}}
>
{Children.map(children, (child) => (
<div
className={css({
marginLeft: -4,
_firstOfType: {
marginLeft: 0,
},
})}
>
{child}
</div>
))}
</div>
);
};
1 change: 1 addition & 0 deletions frontend/uikit/src/TokenIcon/icons/BOLD.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions frontend/uikit/src/TokenIcon/icons/ETH.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions frontend/uikit/src/TokenIcon/icons/OSETH.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions frontend/uikit/src/TokenIcon/icons/RETH.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d3dff5f

Please sign in to comment.