Skip to content
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
74 changes: 74 additions & 0 deletions apps/web/src/components/ui/Icon/Icon.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@layer components {
.ui-icon {
color: inherit;
stroke-width: var(--icon-stroke-default);

display: inline-block;
flex: none;
vertical-align: middle;

/*
* Icon itself is presentation-only. Interactive parents such as
* Button, IconButton, links, and cards own hover/active states.
*/
transition:
color var(--duration-fast) var(--ease-standard),
stroke-width var(--duration-fast) var(--ease-standard);
}

/* Color tones */

.ui-icon--inherit {
color: inherit;
}

.ui-icon--default {
color: var(--color-icon-primary);
}

.ui-icon--secondary {
color: var(--color-icon-secondary);
}

.ui-icon--muted {
color: var(--color-icon-muted);
}

.ui-icon--brand {
color: var(--color-brand-primary);
}

.ui-icon--success {
color: var(--color-feedback-success);
}

.ui-icon--warning {
color: var(--color-feedback-warning);
}

.ui-icon--error {
color: var(--color-feedback-error);
}

.ui-icon--info {
color: var(--color-feedback-info);
}

.ui-icon--inverse {
color: var(--color-text-inverse);
}

/* Stroke weights */

.ui-icon--stroke-thin {
stroke-width: var(--icon-stroke-thin);
}

.ui-icon--stroke-default {
stroke-width: var(--icon-stroke-default);
}

.ui-icon--stroke-strong {
stroke-width: var(--icon-stroke-strong);
}
}
151 changes: 151 additions & 0 deletions apps/web/src/components/ui/Icon/Icon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import type { ComponentType, CSSProperties } from 'react';

import type { IconProps as SvgIconProps } from '../../../icons/types';

import './Icon.css';

export type IconTone =
| 'inherit'
| 'default'
| 'secondary'
| 'muted'
| 'brand'
| 'success'
| 'warning'
| 'error'
| 'info'
| 'inverse';

export type IconStroke = 'thin' | 'default' | 'strong';

export type UiIconSize = 'sm' | 'md' | 'lg' | 'xl' | SvgIconProps['size'];

type IconSpecificProps<P extends SvgIconProps> = Omit<P, keyof SvgIconProps>;

export interface IconProps<P extends SvgIconProps = SvgIconProps>
extends Omit<SvgIconProps, 'size' | 'color' | 'strokeWidth'> {
/**
* Raw SVG icon component to render.
*/
icon: ComponentType<P>;

/**
* Design-system size or an explicit CSS/SVG size.
*/
size?: UiIconSize;

/**
* Semantic icon color.
*
* "inherit" is the default so interactive parents such as
* Button, IconButton, and links can control hover/active color.
*/
tone?: IconTone;

/**
* Explicit CSS color override.
*
* Prefer tone for normal design-system usage.
*/
color?: string;

/**
* Design-system stroke weight.
*/
stroke?: IconStroke;

/**
* Escape hatch for an explicit numeric SVG stroke width.
*
* Prefer stroke="thin" | "default" | "strong" normally.
*/
strokeWidth?: number;

/**
* Props specific to the chosen raw icon.
*
* Examples:
* { filled: true }
* { variant: "half" }
*/
iconProps?: IconSpecificProps<P>;
}

const SIZE_TOKENS: Record<'sm' | 'md' | 'lg' | 'xl', string> = {
sm: 'var(--icon-size-sm)',
md: 'var(--icon-size-md)',
lg: 'var(--icon-size-lg)',
xl: 'var(--icon-size-xl)',
};

function resolveSize(size: UiIconSize | undefined): SvgIconProps['size'] {
if (size === undefined) {
return SIZE_TOKENS.md;
}

if (typeof size === 'string' && size in SIZE_TOKENS) {
return SIZE_TOKENS[size as keyof typeof SIZE_TOKENS];
}

return size;
}

/**
* Design-system wrapper around the raw SVG icons.
*
* Icon intentionally does not define hover/active states itself.
* It inherits color by default so the interactive parent owns
* those states.
*
* Examples:
*
* <Icon icon={SearchIcon} />
*
* <Icon
* icon={AlertTriangleIcon}
* tone="warning"
* />
*
* <Icon
* icon={BookmarkIcon}
* tone="brand"
* iconProps={{ filled: true }}
* />
*/
export function Icon<P extends SvgIconProps>({
icon: Component,
iconProps,
size = 'md',
tone = 'inherit',
color,
stroke = 'default',
strokeWidth,
className,
style,
...svgProps
}: IconProps<P>) {
const classes = [
'ui-icon',
`ui-icon--${tone}`,
`ui-icon--stroke-${stroke}`,
className,
]
.filter(Boolean)
.join(' ');

const iconStyle: CSSProperties = {
...style,
...(strokeWidth !== undefined ? { strokeWidth } : {}),
};

return (
<Component
{...(svgProps as P)}
{...(iconProps as Partial<P>)}
size={resolveSize(size)}
color={color}
className={classes}
style={iconStyle}
/>
);
}
Loading