Skip to content

Commit

Permalink
chore(Label): Remove CSS modules feature flag from Label component (#…
Browse files Browse the repository at this point in the history
…5060)

* Remove css modules feature flag from Label

* Remove feature flag from test

* Remove export

* Create six-colts-admire.md

* Add variants export and update Label types

* Fix tests for updated attributes

* Remove unused import
  • Loading branch information
jonrohan authored Oct 24, 2024
1 parent 07b75e7 commit 33396ea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-colts-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Remove CSS modules feature flag from Label component
62 changes: 13 additions & 49 deletions packages/react/src/Label/Label.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import {clsx} from 'clsx'
import {useFeatureFlag} from '../FeatureFlags'
import Box from '../Box'
import classes from './Label.module.css'
import React from 'react'
import styled from 'styled-components'
import {variant} from 'styled-system'
import {get} from '../constants'
import type {BetterSystemStyleObject, SxProp} from '../sx'
import sx from '../sx'
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'

export type LabelProps = {
Expand Down Expand Up @@ -72,52 +67,21 @@ export const variants: Record<LabelColorOptions, BetterSystemStyleObject> = {
},
}

const sizes: Record<LabelSizeKeys, BetterSystemStyleObject> = {
small: {
height: 'var(--base-size-20, 20px)',
padding: '0 var(--base-size-6, 6px)',
},
large: {
height: 'var(--base-size-24, 24px)',
padding: '0 var(--base-size-8, 8px)',
},
}

const StyledLabel = styled.span<LabelProps>`
align-items: center;
background-color: transparent;
border-width: 1px;
border-radius: 999px;
border-style: solid;
display: inline-flex;
font-weight: ${get('fontWeights.semibold')};
font-size: ${get('fontSizes.0')};
line-height: 1;
white-space: nowrap;
${variant({variants})};
${variant({prop: 'size', variants: sizes})};
${sx};
`

const Label = React.forwardRef(function Label({as, size = 'small', variant = 'default', className, ...rest}, ref) {
const enabled = useFeatureFlag('primer_react_css_modules_ga')
if (enabled) {
const Component = as || 'span'
if (rest.sx) {
return (
<Box
as={Component}
className={clsx(className, classes.Label)}
data-size={size}
data-variant={variant}
ref={ref}
{...rest}
/>
)
}
return <Component className={clsx(className, classes.Label)} data-size={size} data-variant={variant} {...rest} />
const Component = as || 'span'
if (rest.sx) {
return (
<Box
as={Component}
className={clsx(className, classes.Label)}
data-size={size}
data-variant={variant}
ref={ref}
{...rest}
/>
)
}
return <StyledLabel as={as} className={className} size={size} variant={variant} ref={ref} {...rest} />
return <Component className={clsx(className, classes.Label)} data-size={size} data-variant={variant} {...rest} />
}) as PolymorphicForwardRefComponent<'span', LabelProps>

export default Label
17 changes: 5 additions & 12 deletions packages/react/src/__tests__/Label.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {render} from '@testing-library/react'
import axe from 'axe-core'
import type {LabelColorOptions} from '../Label'
import Label, {variants} from '../Label'
import {renderStyles} from '../utils/testing'
import {FeatureFlags} from '../FeatureFlags'

describe('Label', () => {
Expand Down Expand Up @@ -31,21 +30,15 @@ describe('Label', () => {
expect(label.textContent).toEqual('Default')
})
it('default size is rendered as "small"', () => {
const expectedStyles = {
height: 'var(--base-size-20,20px)',
padding: '0 var(--base-size-6,6px)',
}
const defaultStyles = renderStyles(<Label />)
const {getByText} = render(<Label>Default</Label>)

expect(defaultStyles).toEqual(expect.objectContaining(expectedStyles))
// Expect the label to have the default size
expect(getByText('Default')).toHaveAttribute('data-size', 'small')
})
it('default variant is rendered as "default"', () => {
const expectedStyles = {
['border-color']: 'var(--borderColor-default,var(--color-border-default,#d0d7de))',
}
const defaultStyles = renderStyles(<Label />)
const {getByText} = render(<Label>Default</Label>)

expect(defaultStyles).toEqual(expect.objectContaining(expectedStyles))
expect(getByText('Default')).toHaveAttribute('data-variant', 'default')
})
it('should have no axe violations', async () => {
for (const variant in variants) {
Expand Down

0 comments on commit 33396ea

Please sign in to comment.