-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): create new Tag component (#15441)
closes RSQ-84
- Loading branch information
Showing
7 changed files
with
225 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as React from 'react' | ||
|
||
import { Flex } from '../../primitives' | ||
import { SPACING, VIEWPORT } from '../../ui-style-constants' | ||
import { Tag as TagComponent } from './index' | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
|
||
const meta: Meta<typeof TagComponent> = { | ||
title: 'Library/Atoms/Tag', | ||
argTypes: { | ||
type: { | ||
options: ['default', 'interactive', 'branded'], | ||
control: { | ||
type: 'select', | ||
}, | ||
}, | ||
// TODO(jr, 6/18/24): make iconName and iconPosition selectable when we have real examples | ||
// used in the app | ||
iconName: { | ||
table: { | ||
disable: true, | ||
}, | ||
}, | ||
iconPosition: { | ||
table: { | ||
disable: true, | ||
}, | ||
}, | ||
}, | ||
component: TagComponent, | ||
parameters: VIEWPORT.touchScreenViewport, | ||
decorators: [ | ||
Story => ( | ||
<Flex padding={SPACING.spacing16} width="59rem"> | ||
<Story /> | ||
</Flex> | ||
), | ||
], | ||
} | ||
|
||
export default meta | ||
type Story = StoryObj<typeof TagComponent> | ||
|
||
export const Tag: Story = { | ||
args: { | ||
type: 'default', | ||
text: 'Text', | ||
iconPosition: undefined, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as React from 'react' | ||
import { describe, it, expect } from 'vitest' | ||
import { screen } from '@testing-library/react' | ||
import { COLORS } from '../../../helix-design-system' | ||
import { renderWithProviders } from '../../../testing/utils' | ||
import { Tag } from '../index' | ||
|
||
const render = (props: React.ComponentProps<typeof Tag>) => { | ||
return renderWithProviders(<Tag {...props} />) | ||
} | ||
|
||
describe('Tag', () => { | ||
let props: React.ComponentProps<typeof Tag> | ||
|
||
it('should render text, icon with default', () => { | ||
props = { | ||
text: 'mockDefault', | ||
type: 'default', | ||
iconName: 'ot-alert', | ||
iconPosition: 'left', | ||
} | ||
render(props) | ||
const tag = screen.getByTestId('Tag_default') | ||
screen.getByText('mockDefault') | ||
expect(tag).toHaveStyle( | ||
`background-color: ${COLORS.black90}${COLORS.opacity20HexCode}` | ||
) | ||
screen.getByLabelText('icon_left_mockDefault') | ||
}) | ||
it('should render text, right icon with branded', () => { | ||
props = { | ||
text: 'mockBranded', | ||
type: 'branded', | ||
iconName: 'ot-alert', | ||
iconPosition: 'right', | ||
} | ||
render(props) | ||
const tag = screen.getByTestId('Tag_branded') | ||
screen.getByText('mockBranded') | ||
expect(tag).toHaveStyle(`background-color: ${COLORS.blue50}`) | ||
screen.getByLabelText('icon_right_mockBranded') | ||
}) | ||
it('should render text with interactive', () => { | ||
props = { | ||
text: 'mockInteractive', | ||
type: 'interactive', | ||
} | ||
render(props) | ||
const tag = screen.getByTestId('Tag_interactive') | ||
screen.getByText('mockInteractive') | ||
expect(tag).toHaveStyle( | ||
`background-color: ${COLORS.black90}${COLORS.opacity20HexCode}` | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import * as React from 'react' | ||
import { css } from 'styled-components' | ||
import { BORDERS, COLORS } from '../../helix-design-system' | ||
import { Flex } from '../../primitives' | ||
import { ALIGN_CENTER, DIRECTION_ROW } from '../../styles' | ||
import { RESPONSIVENESS, SPACING, TYPOGRAPHY } from '../../ui-style-constants' | ||
import { Icon } from '../../icons' | ||
import { StyledText } from '../StyledText' | ||
|
||
import type { IconName } from '../../icons' | ||
|
||
export type TagType = 'default' | 'interactive' | 'branded' | ||
|
||
interface TagProps { | ||
/** Tag content */ | ||
text: string | ||
/** name constant of the text color and the icon color to display */ | ||
type: TagType | ||
/** iconLocation */ | ||
iconPosition?: 'left' | 'right' | ||
/** Tagicon */ | ||
iconName?: IconName | ||
} | ||
|
||
const defaultColors = { | ||
backgroundColor: `${COLORS.black90}${COLORS.opacity20HexCode}`, | ||
color: COLORS.black90, | ||
} | ||
|
||
const TAG_PROPS_BY_TYPE: Record< | ||
TagType, | ||
{ | ||
backgroundColor: string | ||
color: string | ||
} | ||
> = { | ||
default: defaultColors, | ||
interactive: defaultColors, | ||
branded: { | ||
backgroundColor: COLORS.blue50, | ||
color: COLORS.white, | ||
}, | ||
} | ||
|
||
export function Tag(props: TagProps): JSX.Element { | ||
const { iconName, type, text, iconPosition } = props | ||
|
||
const DEFAULT_CONTAINER_STYLE = css` | ||
padding: ${SPACING.spacing2} ${SPACING.spacing8}; | ||
border-radius: ${BORDERS.borderRadius4}; | ||
@media ${RESPONSIVENESS.touchscreenMediaQuerySpecs} { | ||
border-radius: ${BORDERS.borderRadius8}; | ||
padding: ${SPACING.spacing8} ${SPACING.spacing12}; | ||
} | ||
` | ||
|
||
const INTERACTIVE_CONTAINER_STYLE = css` | ||
${DEFAULT_CONTAINER_STYLE} | ||
&:hover { | ||
background-color: ${COLORS.black90}${COLORS.opacity40HexCode}; | ||
} | ||
&:focus-visible { | ||
box-shadow: 0 0 0 3px ${COLORS.blue50}; | ||
outline: none; | ||
} | ||
` | ||
|
||
const ICON_STYLE = css` | ||
width: 0.75rem; | ||
height: 0.875rem; | ||
@media ${RESPONSIVENESS.touchscreenMediaQuerySpecs} { | ||
width: 1.5rem; | ||
height: 1.5rem; | ||
} | ||
` | ||
|
||
const TEXT_STYLE = css` | ||
${TYPOGRAPHY.h3Regular} | ||
@media ${RESPONSIVENESS.touchscreenMediaQuerySpecs} { | ||
${TYPOGRAPHY.bodyTextRegular} | ||
} | ||
` | ||
|
||
return ( | ||
<Flex | ||
alignItems={ALIGN_CENTER} | ||
flexDirection={DIRECTION_ROW} | ||
color={TAG_PROPS_BY_TYPE[type].color} | ||
backgroundColor={TAG_PROPS_BY_TYPE[type].backgroundColor} | ||
css={ | ||
type === 'interactive' | ||
? INTERACTIVE_CONTAINER_STYLE | ||
: DEFAULT_CONTAINER_STYLE | ||
} | ||
gridGap={SPACING.spacing4} | ||
data-testid={`Tag_${type}`} | ||
> | ||
{iconName != null && iconPosition === 'left' ? ( | ||
<Icon | ||
name={iconName} | ||
aria-label={`icon_left_${text}`} | ||
css={ICON_STYLE} | ||
/> | ||
) : null} | ||
<StyledText css={TEXT_STYLE}>{text}</StyledText> | ||
{iconName != null && iconPosition === 'right' ? ( | ||
<Icon | ||
name={iconName} | ||
aria-label={`icon_right_${text}`} | ||
css={ICON_STYLE} | ||
/> | ||
) : null} | ||
</Flex> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters