Skip to content

StateLabel: Add open and closed states for no icon cases #5803

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

Merged
merged 8 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/nine-apes-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

StateLabel: Add open and closed states for no icon cases
36 changes: 36 additions & 0 deletions e2e/components/StateLabel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,40 @@ test.describe('StateLabel', () => {
})
}
})

test.describe('Open', () => {
for (const theme of themes) {
test.describe(theme, () => {
test('default @vrt', async ({page}) => {
await visit(page, {
id: 'components-statelabel-features--open',
globals: {
colorScheme: theme,
},
})

// Default state
expect(await page.screenshot()).toMatchSnapshot(`StateLabel.Open.${theme}.png`)
})
})
}
})

test.describe('Closed', () => {
for (const theme of themes) {
test.describe(theme, () => {
test('default @vrt', async ({page}) => {
await visit(page, {
id: 'components-statelabel-features--closed',
globals: {
colorScheme: theme,
},
})

// Default state
expect(await page.screenshot()).toMatchSnapshot(`StateLabel.Closed.${theme}.png`)
})
})
}
})
})
8 changes: 7 additions & 1 deletion packages/react/src/StateLabel/StateLabel.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
},
{
"id": "components-statelabel-features--small"
},
{
"id": "components-statelabel-features--open"
},
{
"id": "components-statelabel-features--closed"
}
],
"importPath": "@primer/react",
Expand All @@ -50,7 +56,7 @@
},
{
"name": "status",
"type": "'issueOpened' | 'issueClosed' | 'issueClosedNotPlanned' | 'pullOpened' | 'pullClosed' | 'pullMerged' | 'draft' | 'issueDraft' | 'unavailable'",
"type": "'issueOpened' | 'issueClosed' | 'issueClosedNotPlanned' | 'pullOpened' | 'pullClosed' | 'pullMerged' | 'draft' | 'issueDraft' | 'unavailable' | 'open' | 'closed'",
"required": true
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import type {Meta} from '@storybook/react'
import type {ComponentProps} from '../utils/types'
import StateLabel from './StateLabel'
import VisuallyHidden from '../_VisuallyHidden'

export default {
title: 'Components/StateLabel/Features',
Expand All @@ -19,6 +20,14 @@ export const PullMerged = () => <StateLabel status="pullMerged">Merged</StateLab
export const Queued = () => <StateLabel status="pullQueued">Queued</StateLabel>
export const Draft = () => <StateLabel status="draft">Draft</StateLabel>
export const Unavailable = () => <StateLabel status="unavailable">Unavailable</StateLabel>
export const Open = () => (
<StateLabel status="open">
{/* Because open is a generic status, a visually hidden text could be added to specify the type of the artifact */}
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 is optional - I am happy to remove it if you think it is unnecessary or confusing.

<VisuallyHidden>Milestone</VisuallyHidden>
Open
</StateLabel>
)
export const Closed = () => <StateLabel status="closed">Closed</StateLabel>

export const Small = () => (
<StateLabel status="issueOpened" variant="small">
Expand Down
18 changes: 17 additions & 1 deletion packages/react/src/StateLabel/StateLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const octiconMap = {
issueDraft: IssueDraftIcon,
pullQueued: GitMergeQueueIcon,
unavailable: AlertIcon,
open: null,
closed: null,
}

const labelMap: Record<keyof typeof octiconMap, 'Issue' | 'Issue, not planned' | 'Pull request' | ''> = {
Expand All @@ -44,6 +46,8 @@ const labelMap: Record<keyof typeof octiconMap, 'Issue' | 'Issue, not planned' |
issueDraft: 'Issue',
pullQueued: 'Pull request',
unavailable: '',
open: '',
closed: '',
}

const colorVariants = variant({
Expand Down Expand Up @@ -99,6 +103,16 @@ const colorVariants = variant({
color: 'fg.onEmphasis',
boxShadow: 'var(--boxShadow-thin, inset 0 0 0 1px) var(--borderColor-neutral-emphasis, transparent)',
},
open: {
backgroundColor: 'open.emphasis',
color: 'fg.onEmphasis',
boxShadow: 'var(--boxShadow-thin, inset 0 0 0 1px) var(--borderColor-open-emphasis, transparent)',
},
closed: {
backgroundColor: 'done.emphasis',
color: 'fg.onEmphasis',
boxShadow: 'var(--boxShadow-thin, inset 0 0 0 1px) var(--borderColor-done-emphasis, transparent)',
},
},
})

Expand Down Expand Up @@ -140,10 +154,12 @@ export type StateLabelProps = ComponentProps<typeof StateLabelBase>

function StateLabel({children, status, variant: variantProp = 'normal', ...rest}: StateLabelProps) {
const octiconProps = variantProp === 'small' ? {width: '1em'} : {}
// Open and closed statuses, we don't want to show an icon
const noIconStatus = status === 'open' || status === 'closed'
return (
<StateLabelBase {...rest} variant={variantProp} status={status}>
{/* eslint-disable-next-line @typescript-eslint/no-unnecessary-condition */}
{status && (
{status && !noIconStatus && (
<Octicon
{...octiconProps}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
Expand Down
10 changes: 10 additions & 0 deletions packages/react/src/StateLabel/__tests__/StateLabel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ describe('StateLabel', () => {
expect(screen2.getByLabelText('Pull request')).toBeInTheDocument() // svg
expect(screen2.getByText('Merged')).toBeInTheDocument() // text
})
it('renders open status without an icon', () => {
const screen = HTMLRender(<StateLabel status="open">Open</StateLabel>)
expect(screen.queryByRole('img')).not.toBeInTheDocument() // svg
expect(screen.getByText('Open')).toBeInTheDocument() // text
})
it('renders closed status without an icon', () => {
const screen = HTMLRender(<StateLabel status="closed">Closed</StateLabel>)
expect(screen.queryByRole('img')).not.toBeInTheDocument() // svg
expect(screen.getByText('Closed')).toBeInTheDocument() // text
})
})
8 changes: 8 additions & 0 deletions script/generate-e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,14 @@ const components = new Map([
id: 'components-statelabel-features--small',
name: 'Small',
},
{
id: 'components-statelabel-features--open',
name: 'Open',
},
{
id: 'components-statelabel-features--closed',
name: 'Closed',
},
],
},
],
Expand Down
Loading