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(tag): add "surface" intent #2279

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 12 additions & 5 deletions packages/components/tag/src/Tag.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const intents: TagProps['intent'][] = [
'danger',
'info',
'neutral',
'surface',
]
const designs: TagProps['design'][] = ['filled', 'outlined', 'tinted']

Expand All @@ -43,11 +44,17 @@ export const Intent: StoryFn = _args => (
<div className="flex flex-col gap-md">
{designs.map(design => (
<div key={design} className="flex flex-row gap-md">
{intents.map(intent => (
<Tag key={intent} design={design} intent={intent}>
{intent} tag
</Tag>
))}
{intents.map(intent => {
if (design !== 'filled' && intent === 'surface') {
return <span className="self-center text-small">N/A</span>
}

return (
<Tag key={intent} design={design} intent={intent as any}>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about the any 😅, but without it, TS will complain because it can't infer that due to my previous if block, we cannot have a surface of intent, with a design of outlined or tinted when we end up here

Copy link
Contributor

Choose a reason for hiding this comment

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

You should be ashamed!

{intent} tag
</Tag>
)
})}
</div>
))}
</div>
Expand Down
14 changes: 13 additions & 1 deletion packages/components/tag/src/Tag.styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ export const tagStyles = cva(
*/
intent: makeVariants<
'intent',
['main', 'support', 'accent', 'basic', 'success', 'alert', 'info', 'neutral', 'danger']
[
'main',
'support',
'accent',
'basic',
'success',
'alert',
'info',
'neutral',
'danger',
'surface',
]
>({
main: [],
support: [],
Expand All @@ -39,6 +50,7 @@ export const tagStyles = cva(
danger: [],
info: [],
neutral: [],
surface: [],
}),
},
compoundVariants: [...filledVariants, ...outlinedVariants, ...tintedVariants],
Expand Down
17 changes: 16 additions & 1 deletion packages/components/tag/src/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { forwardRef, type PropsWithChildren } from 'react'

import { tagStyles, type TagStylesProps } from './Tag.styles'

export interface TagProps
interface BaseTagProps
extends PropsWithChildren<React.ButtonHTMLAttributes<HTMLSpanElement>>,
TagStylesProps {
/**
Expand All @@ -12,6 +12,21 @@ export interface TagProps
asChild?: boolean
}

interface FilteredDesignIntent<
Design extends TagProps['design'],
K extends TagStylesProps['intent'] | never = never,
> {
design?: Design
intent?: Exclude<TagStylesProps['intent'], K>
}

type ValidTagDesignIntent =
| FilteredDesignIntent<'tinted', 'surface'>
| FilteredDesignIntent<'outlined', 'surface'>
| FilteredDesignIntent<'filled'>
Comment on lines +23 to +26
Copy link
Contributor Author

@acd02 acd02 Jun 28, 2024

Choose a reason for hiding this comment

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

What is being done here is to prevent unwanted combinations.
The surface intent can only be applied if the design is set to filled.

<Tag design="tinted" intent="surface"> // ❌ TS will raise an error
<Tag design="outlined" intent="surface"> // ❌ TS will raise an error
<Tag design="filled" intent="surface"> // ✅


export type TagProps = BaseTagProps & ValidTagDesignIntent

export const Tag = forwardRef<HTMLButtonElement, TagProps>(
({ design = 'filled', intent = 'basic', asChild, className, ...others }, ref) => {
const Component = asChild ? Slot : 'span'
Expand Down
5 changes: 5 additions & 0 deletions packages/components/tag/src/variants/filled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ export const filledVariants = [
design: 'filled',
class: tw(['bg-neutral', 'text-on-neutral']),
},
{
intent: 'surface',
design: 'filled',
class: tw(['bg-surface', 'text-on-surface']),
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need this tw() util?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, yes. Otherwise, we lose the autocompletion / intellisense. This is due to a limitation of the Tailwind ESLint plugin's typings, which cannot handle very large objects, if I recall correctly

},
] as const
Loading