Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(web): copy Icon component from classic #455

Merged
merged 9 commits into from
Jun 1, 2023
4 changes: 4 additions & 0 deletions web/src/beta/components/Icon/Icons/fileIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions web/src/beta/components/Icon/icons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable import/order */
atnagata marked this conversation as resolved.
Show resolved Hide resolved
keiya01 marked this conversation as resolved.
Show resolved Hide resolved

// Dataset
import File from "./Icons/fileIcon.svg";

export default {
file: File,
plugin: Plugin,
keiya01 marked this conversation as resolved.
Show resolved Hide resolved
};
19 changes: 19 additions & 0 deletions web/src/beta/components/Icon/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Meta } from "@storybook/react";

import Icon from ".";

const icon =
'<svg stroke="white" fill="#00A0E8" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="css-1t9xarj" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"></path><circle cx="12" cy="10" r="3"></circle></svg>';

export default {
title: "Icon",
component: Icon,
} as Meta;

export const Default = () => <Icon icon="file" alt="icon" size={20} />;
export const Color = () => <Icon icon="file" color="red" alt="icon" size={20} />;
export const Image = () => <Icon icon={`/sample.svg`} alt="icon" size={20} />;
export const Svg = () => <Icon icon={icon} alt="icon" size={20} />;
export const Wrapped = () => (
<Icon icon={icon} alt="icon" size={20} style={{ background: "green" }} />
);
106 changes: 106 additions & 0 deletions web/src/beta/components/Icon/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import svgToMiniDataURI from "mini-svg-data-uri";
import React, { AriaAttributes, AriaRole, CSSProperties, memo, useMemo } from "react";
import SVG from "react-inlinesvg";

import { ariaProps } from "@reearth/classic/util/aria";
import { styled } from "@reearth/services/theme";

import Icons from "./icons";

export type Icons = keyof typeof Icons;

export type Props = {
className?: string;
icon?: string;
size?: string | number;
alt?: string;
color?: string;
stroke?: string;
style?: CSSProperties;
role?: AriaRole;
notransition?: boolean;
transitionDuration?: string;
onClick?: () => void;
} & AriaAttributes;

const Icon: React.FC<Props> = ({
className,
icon,
alt,
style,
color,
stroke,
size,
role,
notransition,
transitionDuration,
onClick,
...props
}) => {
const src = useMemo(
() => (icon?.startsWith("<svg ") ? svgToMiniDataURI(icon) : Icons[icon as Icons]),
[icon],
);
if (!icon) return null;

const aria = ariaProps(props);
const sizeStr = typeof size === "number" ? `${size}px` : size;
if (!src) {
return (
<StyledImg
className={className}
src={icon}
alt={alt}
style={style}
role={role}
size={sizeStr}
notransition={notransition}
onClick={onClick}
{...aria}
/>
);
}

return (
<StyledSvg
className={className}
src={src}
role={role}
color={color}
stroke={stroke}
size={sizeStr}
onClick={onClick}
style={{
...style,
// To prevent annoying errors from being output to the console, specify transitions without Emotion.
transitionDuration:
!notransition && !style?.transitionDuration
? transitionDuration || "0.3s"
: style?.transitionDuration,
}}
{...aria}
/>
);
};

const StyledImg = styled.img<{ size?: string; notransition?: boolean }>`
width: ${({ size }) => size};
height: ${({ size }) => size};
${({ notransition }) => !notransition && "transition: all 0.3s;"}
`;

const StyledSvg = styled(SVG)<{
color?: string;
stroke?: string;
size?: string;
}>`
font-size: 0;
display: inline-block;
width: ${({ size }) => size};
height: ${({ size }) => size};
color: ${({ color }) => color};
${({ stroke }) => `stroke: ${stroke};`}
transition-property: color, background;
`;

export default memo(Icon);