Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
16e847e
update token and token base to css modules
randall-krauskopf Nov 13, 2024
7f5e55c
add changeset and update snapshot
randall-krauskopf Nov 13, 2024
7073d90
fix css
randall-krauskopf Nov 13, 2024
6db120e
properly merge in style prop
randall-krauskopf Nov 13, 2024
3fc609f
key sx and style prop correctly off of feature flag
randall-krauskopf Nov 13, 2024
a8084b3
update snapshot
randall-krauskopf Nov 13, 2024
0278c87
update avatartoken
randall-krauskopf Nov 13, 2024
54afea0
update IssueLabelToken to css modules
randall-krauskopf Nov 13, 2024
50e9708
fix remove btn logic
randall-krauskopf Nov 13, 2024
98f100c
fix render logic
randall-krauskopf Nov 13, 2024
0e308b4
update snapshots
randall-krauskopf Nov 13, 2024
b311b15
add missing title attribute
randall-krauskopf Nov 13, 2024
d61bc5c
remove unneeded prop
randall-krauskopf Nov 13, 2024
8f62eb2
small refactor
randall-krauskopf Nov 18, 2024
523452e
Merge branch 'main' into token-css-modules-base-and-token
randall-krauskopf Nov 18, 2024
58c6ea7
Merge branch 'main' into token-css-modules-base-and-token
randall-krauskopf Nov 19, 2024
e1c0aae
refactor
randall-krauskopf Nov 19, 2024
4fdc37b
remove :where to add specificity to a css rule
randall-krauskopf Nov 20, 2024
635be65
remove accidental check in
randall-krauskopf Nov 20, 2024
fba0acd
Merge branch 'main' into token-css-modules-base-and-token
randall-krauskopf Nov 21, 2024
f7f4392
Merge branch 'main' into token-css-modules-base-and-token
randall-krauskopf Dec 2, 2024
3b94118
remove unneeded css
randall-krauskopf Dec 3, 2024
5853210
re-add CSS
randall-krauskopf Dec 3, 2024
fa175a2
Merge branch 'main' into token-css-modules-base-and-token
randall-krauskopf Dec 3, 2024
8cd93d7
Merge branch 'main' into token-css-modules-base-and-token
randall-krauskopf Dec 4, 2024
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
36 changes: 36 additions & 0 deletions packages/react/src/Token/AvatarToken.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
:root {
--spacing: calc(var(--base-size-4) * 2);
}

.AvatarContainer {
display: block;
}

.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[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'] {
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'] {
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