-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathicon.tsx
More file actions
78 lines (73 loc) · 1.74 KB
/
icon.tsx
File metadata and controls
78 lines (73 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { type SVGProps } from 'react'
import { cn } from '#app/utils/misc.tsx'
//@ts-ignore
import href from './icons/sprite.svg'
import { type IconName } from '@/icon-name'
export { href }
export { IconName }
const sizeClassName = {
font: 'w-[1em] h-[1em]',
xs: 'w-3 h-3',
sm: 'w-4 h-4',
md: 'w-5 h-5',
lg: 'w-6 h-6',
xl: 'w-7 h-7',
} as const
type Size = keyof typeof sizeClassName
const childrenSizeClassName = {
font: 'gap-1.5',
xs: 'gap-1.5',
sm: 'gap-1.5',
md: 'gap-2',
lg: 'gap-2',
xl: 'gap-3',
} satisfies Record<Size, string>
/**
* Renders an SVG icon. The icon defaults to the size of the font. To make it
* align vertically with neighboring text, you can pass the text as a child of
* the icon and it will be automatically aligned.
* Alternatively, if you're not ok with the icon being to the left of the text,
* you need to wrap the icon and text in a common parent and set the parent to
* display "flex" (or "inline-flex") with "items-center" and a reasonable gap.
*
* Pass `title` prop to the `Icon` component to get `<title>` element rendered
* in the SVG container, providing this way for accessibility.
*/
export function Icon({
name,
size = 'font',
className,
title,
children,
...props
}: SVGProps<SVGSVGElement> & {
name: IconName
size?: Size
title?: string
}) {
if (children) {
return (
<span
className={`inline-flex items-center ${childrenSizeClassName[size]}`}
>
<Icon
name={name}
size={size}
className={className}
title={title}
{...props}
/>
{children}
</span>
)
}
return (
<svg
{...props}
className={cn(sizeClassName[size], 'inline self-center', className)}
>
{title ? <title>{title}</title> : null}
<use href={`${href}#${name}`} />
</svg>
)
}