Utilities
SVG
Create SVG utilities for fill, stroke, and stroke-width with full type safety.
Overview
SVG utilities help you style SVG elements including fill colors, stroke colors, and stroke widths. These utilities work with inline SVG elements and SVG icon components.
Why Use SVG Utilities?
SVG utilities help you:
- Style icons consistently: Apply colors from your design system to SVG icons
- Support theming: SVG colors can automatically adapt to theme changes
- Create interactive states: Combine with modifiers for hover and focus colors
- Control stroke appearance: Adjust stroke width for different visual weights
useFillUtility
The useFillUtility() function creates utility classes for setting SVG fill colors.
styleframe.config.ts
import { styleframe } from "styleframe";
import { useColor } from "@styleframe/theme";
import { useFillUtility } from "@styleframe/theme";
const s = styleframe();
const { ref } = s;
const { colorPrimary, colorGray500 } = useColor(s, {
primary: '#006cff',
gray500: '#6b7280',
} as const);
useFillUtility(s, {
none: 'none',
current: 'currentColor',
primary: ref(colorPrimary),
gray: ref(colorGray500),
white: '#fff',
black: '#000',
});
export default s;
styleframe/index.css
:root {
--color--primary: oklch(0.5749 0.233917 259.9541 / 1);
--color--gray-500: oklch(0.5513 0.0169 264.36 / 1);
}
._fill\:none { fill: none; }
._fill\:current { fill: currentColor; }
._fill\:primary { fill: var(--color--primary); }
._fill\:gray { fill: var(--color--gray-500); }
._fill\:white { fill: #fff; }
._fill\:black { fill: #000; }
<svg class="_fill:primary"><!-- Primary colored icon --></svg>
<svg class="_fill:current"><!-- Inherits text color --></svg>
<svg class="_fill:none"><!-- No fill (for stroke icons) --></svg>
Pro tip: Use
fill: currentColor to make SVG icons inherit their color from the parent element's color property.useStrokeUtility
The useStrokeUtility() function creates utility classes for setting SVG stroke colors.
styleframe.config.ts
import { styleframe } from "styleframe";
import { useColor } from "@styleframe/theme";
import { useStrokeUtility } from "@styleframe/theme";
const s = styleframe();
const { ref } = s;
const { colorPrimary, colorGray500 } = useColor(s, {
primary: '#006cff',
gray500: '#6b7280',
} as const);
useStrokeUtility(s, {
none: 'none',
current: 'currentColor',
primary: ref(colorPrimary),
gray: ref(colorGray500),
white: '#fff',
black: '#000',
});
export default s;
styleframe/index.css
:root {
--color--primary: oklch(0.5749 0.233917 259.9541 / 1);
--color--gray-500: oklch(0.5513 0.0169 264.36 / 1);
}
._stroke\:none { stroke: none; }
._stroke\:current { stroke: currentColor; }
._stroke\:primary { stroke: var(--color--primary); }
._stroke\:gray { stroke: var(--color--gray-500); }
._stroke\:white { stroke: #fff; }
._stroke\:black { stroke: #000; }
<svg class="_stroke:primary _fill:none"><!-- Outlined icon --></svg>
<svg class="_stroke:current"><!-- Stroke inherits text color --></svg>
useStrokeWidthUtility
The useStrokeWidthUtility() function creates utility classes for setting SVG stroke width.
styleframe.config.ts
import { styleframe } from "styleframe";
import { useStrokeWidthUtility } from "@styleframe/theme";
const s = styleframe();
useStrokeWidthUtility(s, {
'0': '0',
'1': '1',
'2': '2',
});
export default s;
styleframe/index.css
._stroke-width\:0 { stroke-width: 0; }
._stroke-width\:1 { stroke-width: 1; }
._stroke-width\:2 { stroke-width: 2; }
<svg class="_stroke:current _stroke-width:1">Thin stroke</svg>
<svg class="_stroke:current _stroke-width:2">Bold stroke</svg>
Examples
Themed Icon Component
styleframe.config.ts
import { styleframe } from "styleframe";
import { useColor } from "@styleframe/theme";
import { useFillUtility, useStrokeUtility } from "@styleframe/theme";
const s = styleframe();
const { ref, modifier } = s;
const { colorPrimary, colorGray500 } = useColor(s, {
primary: '#006cff',
gray500: '#6b7280',
} as const);
// Base icon colors
useFillUtility(s, {
none: 'none',
current: 'currentColor',
primary: ref(colorPrimary),
gray: ref(colorGray500),
});
useStrokeUtility(s, {
current: 'currentColor',
primary: ref(colorPrimary),
gray: ref(colorGray500),
});
// Hover state modifier
const hover = modifier('hover', ({ declarations }) => ({
'&:hover': declarations,
}));
// Apply primary color on hover
useFillUtility(s, { primary: ref(colorPrimary) }, [hover]);
useStrokeUtility(s, { primary: ref(colorPrimary) }, [hover]);
export default s;
styleframe/index.css
:root {
--color--primary: oklch(0.5749 0.233917 259.9541 / 1);
--color--gray-500: oklch(0.5513 0.0169 264.36 / 1);
}
._fill\:none { fill: none; }
._fill\:current { fill: currentColor; }
._fill\:primary { fill: var(--color--primary); }
._fill\:gray { fill: var(--color--gray-500); }
._stroke\:current { stroke: currentColor; }
._stroke\:primary { stroke: var(--color--primary); }
._stroke\:gray { stroke: var(--color--gray-500); }
._hover\:fill\:primary:hover { fill: var(--color--primary); }
._hover\:stroke\:primary:hover { stroke: var(--color--primary); }
Usage in HTML:
<button>
<svg class="_fill:none _stroke:gray _hover:stroke:primary" width="24" height="24">
<path d="..." />
</svg>
</button>
Icon with Text Color Inheritance
styleframe.config.ts
import { styleframe } from "styleframe";
import { useFillUtility } from "@styleframe/theme";
const s = styleframe();
useFillUtility(s, {
current: 'currentColor',
});
export default s;
styleframe/index.css
._fill\:current { fill: currentColor; }
Usage in HTML:
<a href="#" style="color: #006cff;">
<svg class="_fill:current" width="16" height="16">
<path d="..." />
</svg>
Link text
</a>
Best Practices
- Use currentColor for icons: Makes icons inherit their parent's text color automatically
- Set fill: none for stroke icons: Stroke-based icons need
fill: noneto appear correctly - Reference design tokens: Use
ref()for consistent colors across your application - Consider accessibility: Ensure sufficient contrast for icon colors
- Use viewBox for scalability: SVGs with viewBox scale properly with width/height utilities
FAQ
Fill colors the interior of SVG shapes, while stroke colors the outline/border. Many icons use only stroke (with
fill: none) for a lighter appearance. Some icons use both fill and stroke for different parts.currentColor makes the SVG inherit the color property from its parent element. This means you can change the icon color by changing the text color, and icons automatically match their surrounding text.Stroke width in SVG is relative to the SVG's coordinate system (defined by viewBox), not CSS pixels. A stroke-width of 2 in a 24x24 viewBox SVG will appear different than in a 100x100 viewBox SVG. Icons typically use 1 or 2 for their stroke width.
Yes, fill and stroke colors can be animated with CSS transitions. This is useful for hover effects on icons. Add a
transition property to the SVG or path elements for smooth color changes.