Skip to content

Commit

Permalink
Add size and weight props to Text (#4834)
Browse files Browse the repository at this point in the history
* add size and weight props

* add fallbacks

* polymorphic?

* test ci

* snapshots

* test(vrt): update snapshots

* lint

* Create gold-cups-explode.md

* fix playground

* refactor(e2e): update Text e2e test

---------

Co-authored-by: langermank <langermank@users.noreply.github.com>
Co-authored-by: Josh Black <joshblack@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 12, 2024
1 parent e74e581 commit 7ce1fda
Show file tree
Hide file tree
Showing 30 changed files with 230 additions and 64 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-cups-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Add `size` and `weight` props to `Text`
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 50 additions & 50 deletions e2e/components/Text.test.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
import {test, expect} from '@playwright/test'
import {visit} from '../test-helpers/storybook'
import {themes} from '../test-helpers/themes'

test.describe('Text', () => {
test.describe('Default', () => {
for (const theme of themes) {
test.describe(theme, () => {
test('default @vrt', async ({page}) => {
await visit(page, {
id: 'components-text--default',
globals: {
colorScheme: theme,
},
})
const stories = [
{
title: 'Default',
id: 'components-text--default',
},
{
title: 'Small',
id: 'components-text-features--size-small',
},
{
title: 'Medium',
id: 'components-text-features--size-medium',
},
{
title: 'Large',
id: 'components-text-features--size-large',
},
{
title: 'LightWeight',
id: 'components-text-features--light-weight',
},
{
title: 'NormalWeight',
id: 'components-text-features--normal-weight',
},
{
title: 'MediumWeight',
id: 'components-text-features--medium-weight',
},
{
title: 'SemiboldWeight',
id: 'components-text-features--semibold-weight',
},
] as const

// Default state
expect(await page.screenshot()).toMatchSnapshot(`Text.Default.${theme}.png`)
})
// only testing light theme because this component is only concerned with text size and weight

test('axe @aat', async ({page}) => {
await visit(page, {
id: 'components-text--default',
globals: {
colorScheme: theme,
},
})
await expect(page).toHaveNoViolations()
test.describe('Text', () => {
for (const story of stories) {
test.describe(story.title, () => {
test('default @vrt', async ({page}) => {
await visit(page, {
id: story.id,
})
})
}
})

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

// Default state
expect(await page.screenshot()).toMatchSnapshot(`Text.Playground.${theme}.png`)
})
// Default state
expect(await page.screenshot()).toMatchSnapshot(`Text.${story.title}.png`)
})

test('axe @aat', async ({page}) => {
await visit(page, {
id: 'components-text--playground',
globals: {
colorScheme: theme,
},
})
await expect(page).toHaveNoViolations()
test('axe @aat', async ({page}) => {
await visit(page, {
id: story.id,
})
await expect(page).toHaveNoViolations()
})
}
})
})
}
})
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/react/src/Text/Text.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
{
"name": "sx",
"type": "SystemStyleObject"
},
{
"name": "size",
"type": "'large' | 'medium' | 'small'"
},
{
"name": "weight",
"type": "'light' | 'normal' | 'medium' | 'semibold'"
}
],
"subcomponents": []
Expand Down
44 changes: 43 additions & 1 deletion packages/react/src/Text/Text.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,49 @@ export const Polymorphism = () => (
)

export const StyledText = () => (
<Text as="p" sx={{color: 'fg.onEmphasis', bg: 'neutral.emphasis', p: 2}}>
<Text as="p" sx={{color: 'fg.onEmphasis', bg: 'neutral.emphasis', p: 2}} size="small">
Stylized text
</Text>
)

export const SizeSmall = () => (
<Text as="span" size="small">
Stylized text
</Text>
)

export const SizeMedium = () => (
<Text as="span" size="medium">
Stylized text
</Text>
)

export const SizeLarge = () => (
<Text as="span" size="large">
Stylized text
</Text>
)

export const LightWeight = () => (
<Text as="span" weight="light">
Stylized text
</Text>
)

export const NormalWeight = () => (
<Text as="span" weight="normal">
Stylized text
</Text>
)

export const MediumWeight = () => (
<Text as="span" weight="medium">
Stylized text
</Text>
)

export const SemiboldWeight = () => (
<Text as="span" weight="semibold">
Stylized text
</Text>
)
20 changes: 14 additions & 6 deletions packages/react/src/Text/Text.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const Playground: StoryFn<typeof Text> = args => <Text {...args}>{args.te
Playground.args = {
text: 'Playground',
as: 'span',
size: 'medium',
weight: 'normal',
}

Playground.argTypes = {
Expand All @@ -29,12 +31,6 @@ Playground.argTypes = {
disable: true,
},
},
theme: {
controls: false,
table: {
disable: true,
},
},
ref: {
controls: false,
table: {
Expand All @@ -47,4 +43,16 @@ Playground.argTypes = {
disable: true,
},
},
size: {
control: {
type: 'radio',
},
options: ['small', 'medium', 'large'],
},
weight: {
control: {
type: 'radio',
},
options: ['light', 'normal', 'medium', 'semibold'],
},
}
45 changes: 43 additions & 2 deletions packages/react/src/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,52 @@ import type {SxProp} from '../sx'
import sx from '../sx'
import type {ComponentProps} from '../utils/types'

const Text = styled.span<SystemTypographyProps & SystemCommonProps & SxProp>`
type StyledTextProps = {
size?: 'large' | 'medium' | 'small'
weight?: 'light' | 'normal' | 'medium' | 'semibold'
} & SystemTypographyProps &
SystemCommonProps &
SxProp

const Text = styled.span.attrs<StyledTextProps>(({size, weight}) => ({
'data-size': size,
'data-weight': weight,
}))<StyledTextProps>`
${TYPOGRAPHY};
${COMMON};
&:where([data-size='small']) {
font-size: var(--text-body-size-small, 0.75rem);
line-height: var(--text-body-lineHeight-small, 1.6666);
}
&:where([data-size='medium']) {
font-size: var(--text-body-size-medium, 0.875rem);
line-height: var(--text-body-lineHeight-medium, 1.4285);
}
&:where([data-size='large']) {
font-size: var(--text-body-size-large, 1rem);
line-height: var(--text-body-lineHeight-large, 1.5);
}
&:where([data-weight='light']) {
font-weight: var(--base-text-weight-light, 300);
}
&:where([data-weight='normal']) {
font-weight: var(--base-text-weight-normal, 400);
}
&:where([data-weight='medium']) {
font-weight: var(--base-text-weight-medium, 500);
}
&:where([data-weight='semibold']) {
font-weight: var(--base-text-weight-semibold, 600);
}
${sx};
`

export type TextProps = ComponentProps<typeof Text>
export default Text
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,37 @@ exports[`TextInputWithTokens renders a truncated set of tokens 1`] = `
color: var(--fgColor-muted,var(--color-fg-muted,#656d76));
}
.c9:where([data-size='small']) {
font-size: var(--text-body-size-small,0.75rem);
line-height: var(--text-body-lineHeight-small,1.6666);
}
.c9:where([data-size='medium']) {
font-size: var(--text-body-size-medium,0.875rem);
line-height: var(--text-body-lineHeight-medium,1.4285);
}
.c9:where([data-size='large']) {
font-size: var(--text-body-size-large,1rem);
line-height: var(--text-body-lineHeight-large,1.5);
}
.c9:where([data-weight='light']) {
font-weight: var(--base-text-weight-light,300);
}
.c9:where([data-weight='normal']) {
font-weight: var(--base-text-weight-normal,400);
}
.c9:where([data-weight='medium']) {
font-weight: var(--base-text-weight-medium,500);
}
.c9:where([data-weight='semibold']) {
font-weight: var(--base-text-weight-semibold,600);
}
.c0 {
font-size: 14px;
line-height: 20px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ exports[`has default theme 1`] = `
margin-bottom: 4px;
}
.c0:where([data-size='small']) {
font-size: var(--text-body-size-small,0.75rem);
line-height: var(--text-body-lineHeight-small,1.6666);
}
.c0:where([data-size='medium']) {
font-size: var(--text-body-size-medium,0.875rem);
line-height: var(--text-body-lineHeight-medium,1.4285);
}
.c0:where([data-size='large']) {
font-size: var(--text-body-size-large,1rem);
line-height: var(--text-body-lineHeight-large,1.5);
}
.c0:where([data-weight='light']) {
font-weight: var(--base-text-weight-light,300);
}
.c0:where([data-weight='normal']) {
font-weight: var(--base-text-weight-normal,400);
}
.c0:where([data-weight='medium']) {
font-weight: var(--base-text-weight-medium,500);
}
.c0:where([data-weight='semibold']) {
font-weight: var(--base-text-weight-semibold,600);
}
<span
class="c0"
color="fg.default"
Expand Down

0 comments on commit 7ce1fda

Please sign in to comment.