Skip to content

feat(icon): add ItIconRegistryService for custom SVG icons#685

Open
giulio-leone wants to merge 2 commits into
italia:mainfrom
giulio-leone:feat/563-custom-svg-icons
Open

feat(icon): add ItIconRegistryService for custom SVG icons#685
giulio-leone wants to merge 2 commits into
italia:mainfrom
giulio-leone:feat/563-custom-svg-icons

Conversation

@giulio-leone

Copy link
Copy Markdown
Contributor

Closes #563

What

Adds ItIconRegistryService — a root-provided service that allows registering custom SVG icons by name, so they can be rendered using <it-icon name="my-custom"> alongside built-in Bootstrap Italia sprite icons.

How it works

Three registration methods:

  1. Inline SVGregisterIcon(name, svgContent) / registerIcons(record)

    const registry = inject(ItIconRegistryService);
    registry.registerIcon('custom-home', '<path d="M10 20v-6h4v6..."/>');
  2. External spriteregisterIconFromSprite(name, url, fragmentId)

    registry.registerIconFromSprite('custom-star', '/assets/sprites.svg', 'star');
  3. BulkregisterIcons(icons)

Resolution priority: Custom inline SVG → Custom sprite → Built-in BI sprite

Component changes:

  • ItIconComponent now injects ItIconRegistryService and checks it before falling back to the built-in sprite
  • Template uses @if (isCustomInlineSvg) to conditionally render <g [innerHTML]> for inline SVGs vs <use> for sprites
  • name input type widened to IconName | string to accept custom names

API surface

@Injectable({ providedIn: 'root' })
export class ItIconRegistryService {
  registerIcon(name: string, svgContent: string): void;
  registerIcons(icons: Record<string, string>): void;
  registerIconFromSprite(name: string, spriteUrl: string, fragmentId: string): void;
  hasIcon(name: string): boolean;
  getIconSvg(name: string): SafeHtml | undefined;
  getIconSpriteHref(name: string): string | undefined;
  removeIcon(name: string): boolean;
  clear(): void;
}

Test coverage

27 new tests covering:

  • Built-in sprite rendering (href, no <g>)
  • CSS class propagation (size, color, padded, svgClass)
  • Accessibility (aria-hidden, title, labelWaria)
  • Custom inline SVG rendering (correct DOM, no <use>)
  • Custom sprite rendering (custom href in <use>)
  • Fallback to built-in sprite when no custom icon registered
  • Name collision: custom takes precedence over built-in
  • Bulk registration, removal, clear
  • Registry unit operations (hasIcon, getIconSvg, getIconSpriteHref, removeIcon)

136/136 tests passing, 0 lint errors (only pre-existing warnings).

⚠️ This reopens #641 which was accidentally closed due to fork deletion.

giulio-leone and others added 2 commits March 25, 2026 11:19
- Create ItIconRegistryService (providedIn: 'root') for registering
  custom SVG icons by name (inline SVG content or external sprite refs)
- Modify ItIconComponent to check registry first, then fall back to
  built-in Bootstrap Italia sprite
- Widen name input type to accept IconName | string for custom names
- Support three registration methods: registerIcon (inline SVG),
  registerIcons (bulk), registerIconFromSprite (external sprite)
- Custom icons take precedence over built-in sprites on name collision
- Add 27 comprehensive tests covering: inline SVG rendering, sprite
  rendering, CSS class propagation, accessibility, collision handling,
  bulk registration, removal, and registry unit operations
- Export ItIconRegistryService from public_api.ts

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Show inline SVG registration, batch registration, and external
sprite registration with ItIconRegistryService.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vercel

vercel Bot commented Mar 25, 2026

Copy link
Copy Markdown

@giulio-leone is attempting to deploy a commit to the dip-trasformazione-digitale Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Mar 27, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
design-angular-kit Error Error Mar 27, 2026 3:50pm

Request Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feat] Possibilità di aggiungere icone (SVG) personalizzate

1 participant