Skip to content

Fluent API

When you call swarm_icon() or $manager->get(), you get back an Icon object. It’s immutable: every method returns a new instance, so you can chain calls without worrying about side effects.

echo swarm_icon('tabler:home')
->size(24)
->class('text-blue-500')
->strokeWidth(1.5);

Sets both width and height at once. Accepts numbers or CSS units:

echo swarm_icon('tabler:home')->size(32); // width="32" height="32"
echo swarm_icon('tabler:home')->size('2rem'); // width="2rem" height="2rem"

Appends CSS classes. Never replaces existing ones (they accumulate):

echo swarm_icon('tabler:home')->class('text-blue-500');
echo swarm_icon('tabler:home')->class(['w-6', 'h-6', 'text-blue-500']);
// Multiple calls stack:
echo swarm_icon('tabler:home')->class('w-6')->class('text-blue');
// → class="w-6 text-blue"

Set the fill and stroke attributes:

echo swarm_icon('mdi:account')->fill('currentColor');
echo swarm_icon('tabler:home')->stroke('#3b82f6');

Sets stroke-width:

echo swarm_icon('tabler:home')->strokeWidth(1.5);

For anything else, attr() sets arbitrary attributes. By default it merges with existing ones:

echo swarm_icon('tabler:home')->attr(['data-icon' => 'home', 'id' => 'main-icon']);

Pass merge: false to replace all attributes instead of merging:

echo swarm_icon('tabler:home')->attr(['class' => 'only-this'], merge: false);

Setting a value to null removes that attribute:

echo swarm_icon('tabler:home')->attr(['aria-hidden' => null]);

This is worth emphasizing: the original icon is never modified. Every method returns a new copy:

$home = swarm_icon('tabler:home');
$large = $home->size(48);
$blue = $home->class('text-blue');
echo $home; // original: no size, no class
echo $large; // width="48" height="48"
echo $blue; // class="text-blue"

This makes icons safe to pass around, store in variables, and reuse without surprises.

Sometimes you need to inspect an icon rather than render it:

$icon = swarm_icon('tabler:home')->size(24)->class('icon');
$icon->getContent(); // inner SVG (without the <svg> wrapper)
$icon->getAttributes(); // ['viewBox' => '...', 'width' => '24', ...]
$icon->getAttribute('class'); // 'icon'
$icon->getAttribute('data-x', 'none'); // 'none' (not set, returns default)
$icon->hasAttribute('class'); // true

Echo an icon directly: it implements Stringable:

echo swarm_icon('tabler:home');

Or call toHtml() explicitly when you need the string without echoing:

$html = swarm_icon('tabler:home')->size(24)->toHtml();

Both produce the same full <svg> tag with all attributes and content.

Most of the time you’ll use swarm_icon() or $manager->get(). But you can also create icons from raw SVG data:

use Frostybee\SwarmIcons\Icon;
// From a file on disk
$icon = Icon::fromFile('/path/to/icon.svg');
// From a raw SVG string
$icon = Icon::fromString('<svg viewBox="0 0 24 24"><path d="..."/></svg>');
// From Iconify API JSON data (handles hFlip, vFlip, rotate)
$icon = Icon::fromIconifyData([
'body' => '<path d="..."/>',
'width' => 24,
'height' => 24,
]);

Icons support PHP’s native serialize() / unserialize(), which is how the file cache stores them on disk:

$serialized = serialize($icon);
$restored = unserialize($serialized);
echo $restored; // works exactly like the original

Use get_icon() to get the Icon object and chain methods in your template:

{% set star = get_icon('tabler:star') %}
{{ star.size(32).class('text-yellow-500') }}
{{ get_icon('tabler:arrow-right').fill('currentColor').strokeWidth(1.5) }}