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 15 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% !important;
height: 100% !important;
}

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

.AvatarContainer[data-size='small'] {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.AvatarContainer[data-size='small'] {
.AvatarContainer:where([data-size='small']) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi @jonrohan, I'll get these changes in. Quick question though, why is :where preferred over the bare attribute selector?

Copy link
Member

Choose a reason for hiding this comment

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

Where will reduce the specificity on the attribute selector and make it easier to override the styles in dotcom. We try and add it whenever the attribute lands on the same dom node where we add the passed in className

width: calc(16px - var(--spacing));
height: calc(16px - var(--spacing));
}

.AvatarContainer[data-size='medium'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
width: calc(20px - var(--spacing));
height: calc(20px - var(--spacing));
}

.AvatarContainer[data-size='large'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
width: calc(24px - var(--spacing));
height: calc(24px - var(--spacing));
}

.AvatarContainer[data-size='xlarge'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
width: calc(32px - var(--spacing));
height: calc(32px - var(--spacing));
}
43 changes: 35 additions & 8 deletions packages/react/src/Token/AvatarToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,50 @@ 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'

// 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, ...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={classes.Token}
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
{...rest}
ref={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[data-has-remove-button='true'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
padding-right: 0;
}

.RemoveButton[data-has-multiple-action-targets='true'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
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[data-interactive='true']:hover {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
color: var(--fgColor-default);
background-color: var(--bgColor-neutral-muted);
box-shadow: var(--shadow-resting-medium);
}

.Token[data-is-selected='true'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
color: var(--fgColor-default);
}

.Token[data-is-remove-btn='true'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
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[data-cursor-is-interactive='true'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
cursor: pointer;
}

.TokenBase[data-cursor-is-interactive='false'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
cursor: auto;
}

.TokenBase[data-size='small'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
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[data-size='medium'] {
randall-krauskopf marked this conversation as resolved.
Show resolved Hide resolved
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