Skip to content
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

feat(Token): Migrate to CSS modules behind feature flag Pt 2 #5271

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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/slow-spoons-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Update `Token`, `IssueLabelToken`, `AvatarToken` components to use CSS Modules
37 changes: 37 additions & 0 deletions packages/react/src/Token/AvatarToken.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
:root {
--spacing: calc(var(--base-size-4) * 2);
}

.AvatarContainer {
display: block;
}

/* TODO: Remove this once the avatar component is converted to CSS modules */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avatar is converted #5221

.Avatar {
width: 100%;
height: 100%;
}

.Token {
padding-left: var(--base-size-4) !important;
}

.AvatarContainer:where([data-size='small']) {
width: calc(16px - var(--spacing));
height: calc(16px - var(--spacing));
}

.AvatarContainer:where([data-size='medium']) {
width: calc(20px - var(--spacing));
height: calc(20px - var(--spacing));
}

.AvatarContainer:where([data-size='large']) {
width: calc(24px - var(--spacing));
height: calc(24px - var(--spacing));
}

.AvatarContainer:where([data-size='xlarge']) {
width: calc(32px - var(--spacing));
height: calc(32px - var(--spacing));
}
46 changes: 37 additions & 9 deletions packages/react/src/Token/AvatarToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,51 @@ import {defaultTokenSize, tokenSizes} from './TokenBase'
import Token from './Token'
import Avatar from '../Avatar'
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
import {toggleStyledComponent} from '../internal/utils/toggleStyledComponent'
import {useFeatureFlag} from '../FeatureFlags'
import classes from './AvatarToken.module.css'
import {clsx} from 'clsx'

// TODO: update props to only accept 'large' and 'xlarge' on the next breaking change
export interface AvatarTokenProps extends TokenBaseProps {
avatarSrc: string
}

const AvatarContainer = styled.span<{avatarSize: TokenSizeKeys}>`
// 'space.1' is used because to match space from the left of the token to the left of the avatar
// '* 2' is done to account for the top and bottom
--spacing: calc(${get('space.1')} * 2);
const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_team'

display: block;
height: ${props => `calc(${tokenSizes[props.avatarSize]} - var(--spacing))`};
width: ${props => `calc(${tokenSizes[props.avatarSize]} - var(--spacing))`};
`
const AvatarContainer = toggleStyledComponent(
CSS_MODULES_FEATURE_FLAG,
'span',
styled.span<{avatarSize: TokenSizeKeys}>`
// 'space.1' is used because to match space from the left of the token to the left of the avatar
// '* 2' is done to account for the top and bottom
--spacing: calc(${get('space.1')} * 2);

display: block;
height: ${props => `calc(${tokenSizes[props.avatarSize]} - var(--spacing))`};
width: ${props => `calc(${tokenSizes[props.avatarSize]} - var(--spacing))`};
`,
)

const AvatarToken = forwardRef(({avatarSrc, id, size = defaultTokenSize, className, ...rest}, forwardedRef) => {
const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG)
if (enabled) {
return (
<Token
leadingVisual={() => (
<AvatarContainer avatarSize={size} className={classes.AvatarContainer} data-size={size}>
<Avatar src={avatarSrc} size={parseInt(tokenSizes[size], 10)} className={classes.Avatar} />
</AvatarContainer>
)}
size={size}
id={id?.toString()}
className={clsx(classes.Token, className)}
{...rest}
ref={forwardedRef}
/>
)
}

const AvatarToken = forwardRef(({avatarSrc, id, size = defaultTokenSize, ...rest}, forwardedRef) => {
return (
<Token
leadingVisual={() => (
Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/Token/IssueLabelToken.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.IssueLabel:where([data-has-remove-button='true']) {
padding-right: 0;
}

.RemoveButton:where([data-has-multiple-action-targets='true']) {
position: relative;
z-index: 1;
}
39 changes: 39 additions & 0 deletions packages/react/src/Token/IssueLabelToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {parseToHsla, parseToRgba} from 'color2k'
import {useTheme} from '../ThemeProvider'
import TokenTextContainer from './_TokenTextContainer'
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
import classes from './IssueLabelToken.module.css'
import {useFeatureFlag} from '../FeatureFlags'
import {clsx} from 'clsx'

export interface IssueLabelTokenProps extends TokenBaseProps {
/**
Expand All @@ -16,6 +19,7 @@ export interface IssueLabelTokenProps extends TokenBaseProps {
fillColor?: string
}

const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_team'
const tokenBorderWidthPx = 1

const lightModeStyles = {
Expand Down Expand Up @@ -43,6 +47,8 @@ const darkModeStyles = {
}

const IssueLabelToken = forwardRef((props, forwardedRef) => {
const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG)

const {
as,
fillColor = '#999',
Expand All @@ -54,13 +60,15 @@ const IssueLabelToken = forwardRef((props, forwardedRef) => {
hideRemoveButton,
href,
onClick,
className,
...rest
} = props
const interactiveTokenProps = {
as,
href,
onClick,
}

const {resolvedColorScheme} = useTheme()
const hasMultipleActionTargets = isTokenInteractive(props) && Boolean(onRemove) && !hideRemoveButton
const onRemoveClick: MouseEventHandler = e => {
Expand Down Expand Up @@ -133,6 +141,37 @@ const IssueLabelToken = forwardRef((props, forwardedRef) => {
}
}, [fillColor, resolvedColorScheme, hideRemoveButton, onRemove, isSelected, props])

if (enabled) {
return (
<TokenBase
onRemove={onRemove}
id={id?.toString()}
isSelected={isSelected}
className={clsx(classes.IssueLabel, className)}
text={text}
size={size}
style={labelStyles}
data-has-remove-button={!hideRemoveButton && !!onRemove}
{...(!hasMultipleActionTargets ? interactiveTokenProps : {})}
{...rest}
ref={forwardedRef}
>
<TokenTextContainer {...(hasMultipleActionTargets ? interactiveTokenProps : {})}>{text}</TokenTextContainer>
{!hideRemoveButton && onRemove ? (
<RemoveTokenButton
borderOffset={tokenBorderWidthPx}
onClick={onRemoveClick}
size={size}
aria-hidden={hasMultipleActionTargets ? 'true' : 'false'}
isParentInteractive={isTokenInteractive(props)}
data-has-multiple-action-targets={hasMultipleActionTargets}
className={classes.RemoveButton}
/>
) : null}
</TokenBase>
)
}

return (
<TokenBase
onRemove={onRemove}
Expand Down
21 changes: 21 additions & 0 deletions packages/react/src/Token/Token.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.Token {
max-width: 100%;
color: var(--fgColor-muted);
background-color: var(--bgColor-neutral-muted);
border-color: var(--borderColor-muted);
border-style: solid;
}

.Token:where([data-interactive='true']):hover {
color: var(--fgColor-default);
background-color: var(--bgColor-neutral-muted);
box-shadow: var(--shadow-resting-medium);
}

.Token:where([data-is-selected='true']) {
color: var(--fgColor-default);
}

.Token:where([data-is-remove-btn='true']) {
padding-right: 0;
}
58 changes: 54 additions & 4 deletions packages/react/src/Token/Token.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import type {MouseEventHandler} from 'react'
import React, {forwardRef} from 'react'
import Box from '../Box'
import type {BetterSystemStyleObject, SxProp} from '../sx'
import {merge} from '../sx'
import {merge, type BetterSystemStyleObject, type SxProp} from '../sx'
import {defaultSxProp} from '../utils/defaultSxProp'
import type {TokenBaseProps} from './TokenBase'
import TokenBase, {defaultTokenSize, isTokenInteractive} from './TokenBase'
import RemoveTokenButton from './_RemoveTokenButton'
import TokenTextContainer from './_TokenTextContainer'
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
import VisuallyHidden from '../_VisuallyHidden'
import {useFeatureFlag} from '../FeatureFlags'

import classes from './Token.module.css'
import {clsx} from 'clsx'

// Omitting onResize and onResizeCapture because seems like React 18 types includes these menthod in the expansion but React 17 doesn't.
// TODO: This is a temporary solution until we figure out why these methods are causing type errors.
Expand All @@ -20,6 +23,8 @@ export interface TokenProps extends TokenBaseProps, SxProp {
leadingVisual?: React.ElementType
}

const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_team'

const tokenBorderWidthPx = 1

const LeadingVisualContainer: React.FC<React.PropsWithChildren<Pick<TokenBaseProps, 'size'>>> = ({children, size}) => (
Expand All @@ -35,6 +40,8 @@ const LeadingVisualContainer: React.FC<React.PropsWithChildren<Pick<TokenBasePro
)

const Token = forwardRef((props, forwardedRef) => {
const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG)

const {
as,
onRemove,
Expand All @@ -46,6 +53,8 @@ const Token = forwardRef((props, forwardedRef) => {
href,
onClick,
sx: sxProp = defaultSxProp,
className,
style,
...rest
} = props
const hasMultipleActionTargets = isTokenInteractive(props) && Boolean(onRemove) && !hideRemoveButton
Expand All @@ -58,7 +67,8 @@ const Token = forwardRef((props, forwardedRef) => {
href,
onClick,
}
const sx = merge<BetterSystemStyleObject>(

const mergedSx = merge<BetterSystemStyleObject>(
{
backgroundColor: 'neutral.subtle',
borderColor: props.isSelected ? 'fg.default' : 'border.subtle',
Expand All @@ -80,16 +90,56 @@ const Token = forwardRef((props, forwardedRef) => {
sxProp,
)

if (enabled) {
return (
<TokenBase
onRemove={onRemove}
id={id?.toString()}
className={clsx(className, classes.Token)}
text={text}
size={size}
sx={sxProp}
data-is-selected={props.isSelected}
data-is-remove-btn={!(hideRemoveButton || !onRemove)}
{...(!hasMultipleActionTargets ? interactiveTokenProps : {})}
{...rest}
ref={forwardedRef}
style={{borderWidth: `${tokenBorderWidthPx}px`, ...style}}
>
{LeadingVisual ? (
<LeadingVisualContainer size={size}>
<LeadingVisual />
</LeadingVisualContainer>
) : null}
<TokenTextContainer {...(hasMultipleActionTargets ? interactiveTokenProps : {})}>
{text}
{onRemove && <VisuallyHidden> (press backspace or delete to remove)</VisuallyHidden>}
</TokenTextContainer>

{!hideRemoveButton && onRemove ? (
<RemoveTokenButton
borderOffset={tokenBorderWidthPx}
onClick={onRemoveClick}
size={size}
isParentInteractive={isTokenInteractive(props)}
aria-hidden={hasMultipleActionTargets ? 'true' : 'false'}
/>
) : null}
</TokenBase>
)
}

return (
<TokenBase
onRemove={onRemove}
id={id?.toString()}
text={text}
size={size}
sx={sx}
sx={mergedSx}
{...(!hasMultipleActionTargets ? interactiveTokenProps : {})}
{...rest}
ref={forwardedRef}
style={style}
>
{LeadingVisual ? (
<LeadingVisualContainer size={size}>
Expand Down
60 changes: 60 additions & 0 deletions packages/react/src/Token/TokenBase.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
.TokenBase {
position: relative;
display: inline-flex;
font-family: inherit;
font-weight: var(--base-text-weight-semibold);
text-decoration: none;
white-space: nowrap;
border-radius: var(--borderRadius-full);
align-items: center;
}

.TokenBase:where([data-cursor-is-interactive='true']) {
cursor: pointer;
}

.TokenBase:where([data-cursor-is-interactive='false']) {
cursor: auto;
}

.TokenBase:where([data-size='small']) {
width: auto;
height: 16px;
padding-right: var(--base-size-4);
padding-left: var(--base-size-4);
font-size: var(--text-body-size-small);
/* stylelint-disable-next-line primer/typography */
line-height: 16px;
}

.TokenBase:where([data-size='medium']) {
width: auto;
height: 20px;
padding-right: var(--base-size-8);
padding-left: var(--base-size-8);
font-size: var(--text-body-size-small);
/* stylelint-disable-next-line primer/typography */
line-height: 20px;
}

.TokenBase[data-size='large'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
width: auto;
height: 24px;
padding-right: var(--base-size-8);
padding-left: var(--base-size-8);
font-size: var(--text-body-size-small);
/* stylelint-disable-next-line primer/typography */
line-height: 24px;
}

.TokenBase[data-size='xlarge'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
width: auto;
height: 32px;
padding-top: 0;
padding-right: var(--base-size-16);
padding-bottom: 0;
padding-left: var(--base-size-16);
font-size: var(--text-body-size-medium);
/* stylelint-disable-next-line primer/typography */
line-height: 32px;
}
Loading
Loading