Skip to content

Commit

Permalink
Fix: [DS-395] Switch badge's data-variant to a class (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicky-comeau authored Oct 10, 2024
2 parents 4d831b7 + 7e2cd4c commit 45bb6f6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-schools-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hopper-ui/components": patch
---

In the Badge component, we were using data-variant to be able to add styles in other components based on its variant. Now that we have className render props, we can switch back to using a class for Badge's variant prop.
24 changes: 12 additions & 12 deletions packages/components/src/Badge/src/Badge.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,62 +66,62 @@
border-radius: var(--border-radius);
}

.hop-Badge[data-variant="primary"] {
.hop-Badge--primary {
--background-color: var(--hop-Badge-primary-background-color);
--text-color: var(--hop-Badge-primary-text-color);
}

.hop-Badge[data-hovered][data-variant="primary"] {
.hop-Badge--primary[data-hovered] {
--background-color: var(--hop-Badge-primary-background-color-hovered);
--text-color: var(--hop-Badge-primary-text-color-hovered);
}

.hop-Badge[data-pressed][data-variant="primary"] {
.hop-Badge--primary[data-pressed] {
--background-color: var(--hop-Badge-primary-background-color-pressed);
--text-color: var(--hop-Badge-primary-text-color-pressed);
}

.hop-Badge[data-selected][data-variant="primary"] {
.hop-Badge--primary[data-selected] {
--background-color: var(--hop-Badge-primary-background-color-selected);
--text-color: var(--hop-Badge-primary-text-color-selected);
}

.hop-Badge[data-variant="secondary"] {
.hop-Badge--secondary {
--background-color: var(--hop-Badge-secondary-background-color);
--text-color: var(--hop-Badge-secondary-text-color);
}

.hop-Badge[data-hovered][data-variant="secondary"] {
.hop-Badge--secondary[data-hovered] {
--background-color: var(--hop-Badge-secondary-background-color-hovered);
--text-color: var(--hop-Badge-secondary-text-color-hovered);
}

.hop-Badge[data-pressed][data-variant="secondary"] {
.hop-Badge--secondary[data-pressed] {
--background-color: var(--hop-Badge-secondary-background-color-pressed);
--text-color: var(--hop-Badge-secondary-text-color-pressed);
}

.hop-Badge[data-selected][data-variant="secondary"] {
.hop-Badge--secondary[data-selected] {
--background-color: var(--hop-Badge-secondary-background-color-selected);
--text-color: var(--hop-Badge-secondary-text-color-selected);
}

.hop-Badge[data-variant="subdued"] {
.hop-Badge--subdued {
--background-color: var(--hop-Badge-subdued-background-color);
--text-color: var(--hop-Badge-subdued-text-color);
}

.hop-Badge[data-hovered][data-variant="subdued"] {
.hop-Badge--subdued[data-hovered] {
--background-color: var(--hop-Badge-subdued-background-color-hovered);
--text-color: var(--hop-Badge-subdued-text-color-hovered);
}

.hop-Badge[data-pressed][data-variant="subdued"] {
.hop-Badge--subdued[data-pressed] {
--background-color: var(--hop-Badge-subdued-background-color-pressed);
--text-color: var(--hop-Badge-subdued-text-color-pressed);
}

.hop-Badge[data-selected][data-variant="subdued"] {
.hop-Badge--subdued[data-selected] {
--background-color: var(--hop-Badge-subdued-background-color-selected);
--text-color: var(--hop-Badge-subdued-text-color-selected);
}
Expand Down
12 changes: 9 additions & 3 deletions packages/components/src/Badge/src/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ interface BadgeRenderProps {
* Whether or not the badge is selected.
*/
isSelected?: boolean;
/**
* The visual style of the badge.
* @default "primary"
*/
variant?: BadgeVariant;
}

export interface BadgeProps extends StyledSystemProps, AccessibleSlotProps, RenderProps<BadgeRenderProps> {
Expand Down Expand Up @@ -75,7 +80,8 @@ function Badge(props: BadgeProps, ref: ForwardedRef<HTMLSpanElement>) {
GlobalBadgeCssSelector,
cssModule(
styles,
"hop-Badge"
"hop-Badge",
mapOrbiterToHopperVariants(variant)
),
stylingProps.className
);
Expand All @@ -96,7 +102,8 @@ function Badge(props: BadgeProps, ref: ForwardedRef<HTMLSpanElement>) {
isIndeterminate: isIndeterminate || false,
isHovered: isHovered || false,
isPressed: isPressed || false,
isSelected: isSelected || false
isSelected: isSelected || false,
variant: mapOrbiterToHopperVariants(variant)
}
});

Expand All @@ -115,7 +122,6 @@ function Badge(props: BadgeProps, ref: ForwardedRef<HTMLSpanElement>) {
data-hovered={isHovered || undefined}
data-pressed={isPressed || undefined}
data-selected={isSelected || undefined}
data-variant={mapOrbiterToHopperVariants(variant)}
>
{!isDot && renderProps.children}
</OverlineText>
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/tag/src/Tag.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@
margin-inline-end: var(--badge-margin-inline-end);
}

.hop-Tag .hop-Tag__badge[data-variant="subdued"] {
.hop-Tag .hop-Tag__badge--subdued {
margin-inline-start: var(--badge-secondary-margin-inline-start);
color: inherit;
background: none;
Expand Down
8 changes: 7 additions & 1 deletion packages/components/src/tag/src/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ function Tag(props: TagProps, ref: ForwardedRef<HTMLDivElement>) {
size: "sm"
}],
[BadgeContext, {
className: styles["hop-Tag__badge"],
className: ({ variant: badgeVariant }) => {
return cssModule(
styles,
"hop-Tag__badge",
badgeVariant
);
},
isDisabled: isDisabled,
isHovered: isHovered,
isPressed: isPressed,
Expand Down

0 comments on commit 45bb6f6

Please sign in to comment.