Skip to content

Icon Stacking

Sometimes you need to combine icons: a checkmark on top of a circle for a badge, or an exclamation mark over a triangle for a warning. IconStack layers multiple icons into one <svg>.

use Frostybee\SwarmIcons\IconStack;
$stack = (new IconStack())
->push(swarm_icon('tabler:circle'))
->push(swarm_icon('tabler:check'));
echo $stack;

This renders a single <svg> with each icon wrapped in a <g> layer.

IconManager::stack() creates a stack and pushes icons in one call:

$stack = $manager->stack('tabler:circle', 'tabler:check');
echo $stack->size(48)->class('badge-icon');

Pass attributes to individual layers to control their appearance:

$stack = (new IconStack())
->push(swarm_icon('tabler:circle'), ['fill' => '#3b82f6'])
->push(swarm_icon('tabler:check'), [
'fill' => 'white',
'transform' => 'scale(0.6) translate(8,8)',
]);

Layer attributes are applied to the <g> element wrapping that icon’s content.

The stack supports fluent methods for the outer <svg> container:

$stack = $manager->stack('tabler:circle', 'tabler:check')
->size(48)
->class('badge-icon')
->attr(['data-stack' => 'composite']);
MethodDescription
push(icon, attributes)Add an icon layer
size(value)Set width and height
class(classes)Add CSS classes
attr(attributes)Set arbitrary attributes
count()Number of layers
toHtml()Render as SVG string

All methods return new instances (immutable).

The viewBox is taken from the first icon pushed. For best results, use icons from the same set or with matching viewBoxes.