How it works
When you hover over an SVG element in your code, Better SVG displays:- A rendered preview of the SVG
- The file size in bytes or KB
- A link to optimize the SVG directly from the hover tooltip
The hover preview uses a 5-second cache to improve performance and reduce rendering overhead.
Supported languages
Hover preview works in the following file types:- Astro (
.astro) - EJS (
.ejs) - ERB (
.erb) - HTML (
.html) - JavaScript (
.js) - JavaScript React (
.jsx) - Liquid (
.liquid) - PHP (
.php) - Svelte (
.svelte) - SVG (
.svg) - TypeScript (
.ts) - TypeScript React (
.tsx) - Vue (
.vue) - XML (
.xml)
Configuration
Enable or disable hover preview:- Default:
true - Type:
boolean - Description: Enable hover preview for SVG elements in supported files
JSX and framework support
The hover preview intelligently handles JSX and framework-specific syntax:React/JSX transformation
For.jsx and .tsx files, the extension automatically:
- Converts
classNametoclass - Transforms camelCase attributes to kebab-case (e.g.,
strokeWidth→stroke-width) - Handles JSX expression values like
{2}by encoding them for preview - Preserves event handlers and dynamic attributes
Framework directives
The extension recognizes and preserves:- Vue directives:
v-,:,@ - Svelte directives:
on:,bind:,class:,use:, etc. - Astro directives:
client:
Theme-aware rendering
The hover preview adapts to your VS Code theme:- Dark themes:
currentColoris replaced with#ffffff(white) - Light themes:
currentColoris replaced with#000000(black) - High contrast: Uses white for better visibility
Size display
The hover preview shows the file size of the SVG:- < 1 KB: Displayed in bytes (e.g., “342 bytes”)
- ≥ 1 KB: Displayed in kilobytes with 2 decimal places (e.g., “2.45 KB”)
The size calculation uses
Buffer.byteLength to accurately measure the UTF-8 encoded size of the SVG content.Inline optimization
Each hover preview includes an “Optimizar SVG” link that allows you to optimize the SVG without leaving your current file. When clicked, the optimization:- Extracts the exact SVG element at the cursor position
- Runs SVGO optimization with framework-aware settings
- Replaces the original SVG with the optimized version in place
- Shows a notification with the size reduction achieved
Minimum size enforcement
To ensure visibility in the hover tooltip, the extension enforces a minimum size of 128px:- If the SVG has no
widthorheight, dimensions are calculated from theviewBox - If the SVG is smaller than 128px, it’s scaled up proportionally
- The aspect ratio is always preserved during scaling
Stroke and fill propagation
The extension intelligently propagates stroke and fill attributes from parent SVG elements to children:Technical implementation
The hover provider is implemented insvgGutterPreview.ts:32:
- Provider class:
SvgHoverProvider - Cache duration: 5 seconds
- Encoding: Base64 data URIs for reliable rendering
- Validation: Skips SVGs with unresolved JSX expressions
vscode.languages.registerHoverProvider API to inject hover previews into all supported language modes.
Best practices
- Hover over SVGs to check their visual appearance before committing changes
- Use the inline optimize link to reduce file size of embedded SVGs
- If a preview doesn’t appear, check that your SVG has valid closing tags
- For dynamic SVGs with expressions, test with the preview panel instead
Performance considerations
The hover preview includes several optimizations:- Debouncing: 5-second cache prevents redundant rendering
- Lazy rendering: Previews only generate when you actually hover
- Validation: Invalid or unrenderable SVGs are skipped
- Base64 encoding: Reduces rendering issues with special characters