# Icon Component

A wrapper around the HubSpot CMS Icon component that provides flexible sizing, customizable colors via CSS variables, and accessibility options.

## Import path
```tsx
import Icon from '@hubspot/cms-component-library/Icon';
```

## Purpose

The Icon component wraps the native HubSpot CMS Icon component to provide a consistent interface for rendering icons with customizable styling and accessibility options. It simplifies icon usage in HubSpot CMS projects by handling common configuration patterns like sizing, color theming through CSS variables, and proper accessibility attributes (SEMANTIC vs DECORATIVE purpose).

## Component Structure

```
Icon/
├── index.tsx                     # Main component with render logic
├── types.ts                      # TypeScript type definitions
├── ContentFields.tsx             # HubSpot field definitions for icon content
├── index.module.scss             # CSS module with design tokens
└── stories/
    ├── Icon.stories.tsx          # Component usage examples
    ├── IconDecorator.tsx         # Storybook decorator
    └── IconDecorator.module.css  # Decorator styles
```

## Components

### Icon (Main Component)

**Purpose:** Renders an icon with configurable size, color, and accessibility properties.

**Props:**
```tsx
{
  fieldPath?: string;                              // Path to icon in HubSpot fields
  size?: number;                                   // Icon size in pixels, sets both width and height (default: 12)
  fill?: string;                                   // Icon fill color (overrides CSS variable)
  className?: string;                              // Additional CSS classes
  style?: React.CSSProperties;                     // Inline styles (including CSS variables)
  showIcon?: boolean;                              // Toggle icon visibility (default: true)
  purpose?: 'SEMANTIC' | 'DECORATIVE';            // Icon accessibility role (default: 'DECORATIVE')
  title?: string;                                  // Icon title for screen readers (use with SEMANTIC)
}
```

## Usage Examples

### Basic Icon

```tsx
import Icon from '@hubspot/cms-component-library/Icon';

<Icon
  fieldPath="icon"
  size={24}
/>
```

### Semantic Icon with Accessibility

```tsx
<Icon
  fieldPath="icon"
  size={24}
  purpose="SEMANTIC"
  title="Download file"
/>
```

### Icon with Custom Color

```tsx
<Icon
  fieldPath="icon"
  size={32}
  fill="#FF7A59"
/>
```

### Conditional Icon Display

```tsx
<Icon
  fieldPath="icon"
  size={20}
  showIcon={shouldShowIcon}
/>
```

## HubSpot CMS Integration

### Field Definitions

The Icon component provides field definitions for easy integration with HubSpot CMS modules.

#### ContentFields

Configurable props for icon selection and visibility:

```tsx
<Icon.ContentFields
  iconLabel="Icon"
  iconName="icon"
  iconDefault={{ name: 'angle-right', unicode: 'f105', type: 'SOLID' }}
  addIconToggle={true}
  showIconLabel="Show Icon"
  showIconName="showIcon"
  showIconDefault={false}
/>
```

**Fields:**
- `icon`: IconField for selecting the icon
- `showIcon`: BooleanField toggle for icon visibility (optional, enabled with `addIconToggle={true}`)

#### Style Fields

**Note:** There are no style fields included with this component. Do not try to import them.

### Module Usage Example

```tsx
import Icon from '@hubspot/cms-component-library/Icon';

export default function IconModule({ fieldValues }) {
  return (
    <Icon
      fieldPath="icon"
      size={fieldValues.iconSize || 24}
      showIcon={fieldValues.showIcon}
      purpose="DECORATIVE"
    />
  );
}
```

### Module Fields Example

```tsx
import { ModuleFields } from '@hubspot/cms-components/fields';
import Icon from '@hubspot/cms-component-library/Icon';

export const fields = (
  <ModuleFields>
    <Icon.ContentFields
      addIconToggle={true}
    />
  </ModuleFields>
);
```

## Styling

### CSS Variables

The Icon component uses CSS variables for theming and customization:

**Base Styles:**
- `--hscl-icon-fill`: Icon fill color (default: currentColor)
- `--hscl-icon-size`: Icon width and height (set automatically from the `size` prop)


## Accessibility

The Icon component follows accessibility best practices:

- **Icon Purpose**:
  - `DECORATIVE` (default): Icon is purely visual, hidden from screen readers with `aria-hidden`
  - `SEMANTIC`: Icon conveys meaning, includes accessible title for screen readers
- **Title Attribute**: Use `title` prop with `purpose="SEMANTIC"` to provide screen reader description
- **Conditional Rendering**: The `showIcon` prop allows dynamic icon visibility without affecting layout

## Best Practices

- **Choose the right purpose**: Use `SEMANTIC` when the icon conveys important meaning, `DECORATIVE` when it's purely visual
- **Always provide title with SEMANTIC**: When using `purpose="SEMANTIC"`, always include a meaningful `title` for screen readers
- **Use CSS variables for theming**: Override `--hscl-icon-fill` for consistent color theming across your application
- **Size appropriately**: Common sizes are 12px (small), 16px (default inline), 24px (medium), 32px (large), 48px (extra large)
- **Field path**: The `fieldPath` prop should match the field name defined in ContentFields
- **Style fields**: There are no style fields in this component. Do not try to import them.
