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

feat(tag): introduce size prop to Tag component #49

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/apsara-ui/src/Tag/Tag.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export const Profile = Icons["profile"];
type: { summary: "ReactNode" },
},
},
size: {
description: "Size of the tag",
control: { type: "select", options: ["small", "medium", "large"] },
table: {
type: { summary: ["small", "medium", "large"] },
defaultValue: { summary: "medium" },
},
}
}}
/>

Expand Down
17 changes: 15 additions & 2 deletions packages/apsara-ui/src/Tag/Tag.styles.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import styled from "styled-components";
import { textStyles } from "../mixin";
import { TagSize } from "./Tag";

export const StyledTag = styled("div")<{ type: "round" | "rect"; color: string; closable: boolean; icon: boolean }>`
const fontSizeMap: Record<TagSize, string> = {
small: "9px",
medium: "11px",
large: "14px",
};

export const StyledTag = styled("div") <{
type: "round" | "rect";
color: string;
closable: boolean;
icon: boolean;
size: TagSize;
}>`
box-sizing: border-box;
display: inline-flex;
align-items: center;
${({ theme }) => textStyles("11px", theme?.tag?.text, "0.11px", "normal")}
${({ theme, size }) => textStyles(fontSizeMap[size], theme?.tag?.text, "0.11px", "normal")}
border-radius: ${({ type }) => (type === "round" ? "10.5px" : "2px")};
border-color: ${({ color }) => color};
border-width: 1px;
Expand Down
6 changes: 5 additions & 1 deletion packages/apsara-ui/src/Tag/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Colors from "../Colors";

const Cross = Icons["cross"];

export type TagSize = "small" | "medium" | "large";

type TagProps = {
children?: ReactNode;
type?: "round" | "rect";
Expand All @@ -14,6 +16,7 @@ type TagProps = {
icon?: ReactNode;
className?: string;
style?: React.CSSProperties;
size: TagSize;
};

const Tag = ({
Expand All @@ -23,13 +26,14 @@ const Tag = ({
closable = false,
closeIcon,
icon,
size = "medium",
...props
}: TagProps) => {
const [visible, setVisible] = useState(true);
if (!visible) return null;
return (
<TagWrapper>
<StyledTag {...props} type={type} color={color} closable={closable} icon={icon ? true : false}>
<StyledTag {...props} type={type} color={color} closable={closable} icon={icon ? true : false} size={size}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are only changing the font-size using this prop then let's rename it to fontSize to avoid confusion?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this new prop is to let user customize the Tag sizing using small | medium | large value without having to care about font sizing or other styling. And apparently, this only needs to change the font size because the wrapper box size will also adapt based on the font size.

on the other hand, if user need to customize this component's font size they can use props.style

<span className="tag_icon">{icon}</span>
<span className="tag_content">{children}</span>
{closable && (
Expand Down