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

fix <Shortcut /> #609

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/components/src/shortcut/shortcut.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const variants = ['primary', 'secondary', 'gray'] as const
// eslint-disable-next-line react/display-name
const renderVariant = (variant: (typeof variants)[number]) => (props: any) => (
<div className="flex items-center gap-2">
<Shortcut {...props} variant={variant} icon={CommandIcon} />
<Shortcut {...props} variant={variant} icon={<CommandIcon />} />
<Shortcut {...props} variant={variant} symbol="K" />
</div>
)
Expand Down
15 changes: 9 additions & 6 deletions packages/components/src/shortcut/shortcut.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { forwardRef } from 'react'
import { cloneElement, forwardRef } from 'react'

import { match, P } from 'ts-pattern'

import { cva } from '../utils/variants'

import type { IconElement } from '../types'
import type { VariantProps } from '../utils/variants'

const styles = cva({
base: 'flex size-4 flex-shrink-0 items-center justify-center rounded-6 border',
base: 'grid size-4 flex-shrink-0 place-items-center rounded-6 border',
variants: {
variant: {
primary: [
Expand All @@ -32,7 +33,7 @@ const styles = cva({
type Props = VariantProps<typeof styles> &
(
| {
icon: React.ComponentType<React.SVGProps<SVGSVGElement>>
icon: IconElement
symbol?: never
}
| {
Expand All @@ -42,15 +43,17 @@ type Props = VariantProps<typeof styles> &
)

const Shortcut = (props: Props, ref: React.Ref<HTMLDivElement>) => {
const { variant = 'primary', ...rest } = props
const { variant = 'primary' } = props

return (
<div className={styles({ variant })} ref={ref} {...rest}>
<div className={styles({ variant })} ref={ref}>
{match(props)
.with({ symbol: P.string }, ({ symbol }) => (
<span className="text-11 font-medium">{symbol}</span>
))
.with({ icon: P._ }, ({ icon: Icon }) => <Icon className="size-3" />)
.with({ icon: P._ }, ({ icon }) =>
cloneElement(icon, { className: 'size-3' }),
)
.exhaustive()}
</div>
)
Expand Down
Loading