Skip to content

Feature request: SVG should conform to ClassAttributeModifier #30

@mynona

Description

@mynona

Feature request: SVG should conform to ClassAttributeModifier

Package: swift-web-standards
Type: Missing conformance / feature request
Severity: Moderate — workaround required for common use case

Description

The SVG struct (in the SVG module) does not conform to ClassAttributeModifier, so the fluent .class() method is unavailable on SVG values.

This is very common in practice — SVG icons embedded in HTML almost always need a CSS class for sizing, colour, or visibility control:

Svg.icon(.eye)
    .setAttribute(name: ":class", value: "{ hidden: !show }")
    .class("w-4 h-4 text-gray-500")  // ❌ error: value of type 'SVG' has no member 'class'

Expected behaviour

SVG should support .class() just like HTML elements do, since SVG is embedded directly in HTML and styled with the same CSS class system.

Workaround

Declare a retroactive conformance in the consuming package:

import SVG
import HTML

extension SVG: @retroactive ClassAttributeModifier {}

Suggested fix

In the SVG module, either:

  1. Add ClassAttributeModifier to SVGTag's protocol requirements, or
  2. Provide a concrete .class() implementation on SVGTag directly using setAttribute(name: "class", value: ...).

Option 2 avoids importing HTML from the SVG module:

// In SVGTag extension
public func `class`(_ value: String) -> Self {
    setAttribute(name: "class", value: value)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions