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

Small Tag variant #2398

Merged
merged 5 commits into from
Oct 28, 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
52 changes: 34 additions & 18 deletions packages/odyssey-react-mui/src/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,33 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

import { memo, ReactElement, useCallback, useContext } from "react";
import { Chip as MuiChip, ChipProps as MuiChipProps } from "@mui/material";
import { memo, ReactElement, useCallback, useContext } from "react";
import styled from "@emotion/styled";

import { useContrastModeContext, ContrastMode } from "./useContrastMode";
import { HtmlProps } from "./HtmlProps";
import { CloseCircleFilledIcon } from "./icons.generated";
import { HtmlProps } from "./HtmlProps";
import { MuiPropsContext, MuiPropsContextType } from "./MuiPropsContext";
import {
DesignTokens,
useOdysseyDesignTokens,
} from "./OdysseyDesignTokensContext";
import { TagListContext } from "./TagListContext";
import { ContrastMode, useContrastModeContext } from "./useContrastMode";

const tagSizeValues = ["medium", "small"] as const;

export const tagColorVariants = [
"default",
"info",
"accentFour",
"accentOne",
"accentTwo",
"accentThree",
"accentFour",
"accentTwo",
"default",
"info",
] as const;

type TagColorVariant = (typeof tagColorVariants)[number];
type TagSize = (typeof tagSizeValues)[number];

export type TagProps = {
icon?: ReactElement;
Expand All @@ -54,6 +57,10 @@ export type TagProps = {
* Color variant of the Tag, affecting its appearance
*/
colorVariant?: TagColorVariant;
/*
* Size variant of the Tag
*/
size?: TagSize;
} & Pick<HtmlProps, "testId" | "translate">;

const getChipColors = ({
Expand Down Expand Up @@ -187,16 +194,17 @@ const getChipColors = ({

const StyledTag = styled(MuiChip, {
shouldForwardProp: (prop) =>
!["colorVariant", "contrastMode", "odysseyDesignTokens"].includes(
!["colorVariant", "contrastMode", "odysseyDesignTokens", "size"].includes(
prop as string,
),
})<{
as?: React.ElementType; // Allow the 'as' prop to be forwarded
clickable?: boolean;
colorVariant: TagColorVariant;
contrastMode: ContrastMode;
odysseyDesignTokens: DesignTokens;
as?: React.ElementType; // Allow the 'as' prop to be forwarded
clickable?: boolean;
}>(({ colorVariant, contrastMode, odysseyDesignTokens, clickable }) => {
size?: TagSize;
}>(({ colorVariant, contrastMode, odysseyDesignTokens, clickable, size }) => {
const colors = getChipColors({
colorVariant,
odysseyDesignTokens,
Expand All @@ -215,6 +223,10 @@ const StyledTag = styled(MuiChip, {
borderColor: "transparent",
}),

...(size === "small" && {
paddingBlock: `calc(${odysseyDesignTokens.Spacing1})`,
}),

"&.MuiChip-clickable:hover": {
backgroundColor: colors.hover,
},
Expand All @@ -234,6 +246,7 @@ const StyledTag = styled(MuiChip, {
"& .MuiChip-icon": {
color: colors.icon,
},

"& .MuiChip-deleteIcon": {
color: colors.deleteIcon,
"&:hover": {
Expand All @@ -250,6 +263,7 @@ const Tag = ({
label,
onClick,
onRemove,
size = "medium",
testId,
translate,
}: TagProps) => {
Expand All @@ -261,34 +275,36 @@ const Tag = ({
(muiProps: MuiPropsContextType) => (
<StyledTag
{...muiProps}
as={chipElementType}
aria-disabled={isDisabled}
as={chipElementType}
clickable={Boolean(onClick)}
data-se={testId}
colorVariant={colorVariant}
odysseyDesignTokens={odysseyDesignTokens}
contrastMode={contrastMode}
data-se={testId}
deleteIcon={<CloseCircleFilledIcon />}
disabled={isDisabled}
icon={icon}
label={label}
odysseyDesignTokens={odysseyDesignTokens}
onClick={onClick}
onDelete={onRemove}
deleteIcon={<CloseCircleFilledIcon />}
size={size}
translate={translate}
/>
),
[
chipElementType,
colorVariant,
contrastMode,
icon,
isDisabled,
label,
odysseyDesignTokens,
onClick,
onRemove,
size,
testId,
translate,
colorVariant,
odysseyDesignTokens,
contrastMode,
],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,26 @@ const storybookMeta: Meta<TagProps> = {
},
},
},
size: {
control: {
type: "select",
},
options: ["default", "small"],
description: "The size of the tag",
table: {
type: {
summary: "string",
},
defaultValue: {
summary: "default",
},
},
},
},
args: {
label: "Starship",
colorVariant: "default",
size: "medium",
},
decorators: [MuiThemeDecorator],
tags: ["autodocs"],
Expand Down Expand Up @@ -177,6 +193,13 @@ export const List: StoryObj<TagProps> = {
},
};

export const Small: StoryObj<TagProps> = {
args: {
label: "Starship",
size: "small",
},
};

export const Icon: StoryObj<TagProps> = {
args: {
label: "Crew",
Expand Down