Skip to content

Commit d7528f6

Browse files
authored
Add support for is:inline directive (#200)
1 parent 9a332d7 commit d7528f6

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

.changeset/nine-waves-film.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro-icon": minor
3+
---
4+
5+
Adds support for the `is:inline` directive, which bypasses automatic sprite optimization and directly embeds the plain SVG.

demo/src/pages/index.astro

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ const icon = "adjustment";
1515

1616
<article>
1717
<h2>Local Icons</h2>
18-
<!-- This will inline your SVG directly -->
1918
<Icon size={24} name="adjustment" />
2019
<Icon size={24} name={icon} />
2120
<Icon size={24} name="annotation" />
2221
<Icon size={24} name="logos/deno" />
2322
<Icon name="logos/deno" />
2423
<Icon width={75} name="logos/alpine" />
2524
<Icon name="logos/alpine-multi-color" />
25+
26+
<Icon is:inline size={24} name="adjustment" />
2627
</article>
2728

2829
<article>

packages/core/components/Icon.astro

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { cache } from "./cache.js";
99
1010
interface Props extends HTMLAttributes<"svg"> {
1111
name: Icon;
12+
"is:inline"?: boolean;
1213
title?: string;
1314
size?: number;
1415
width?: number;
@@ -24,7 +25,7 @@ class AstroIconError extends Error {
2425
}
2526
2627
const req = Astro.request;
27-
const { name = "", title, ...props } = Astro.props;
28+
const { name = "", title, "is:inline": inline = false, ...props } = Astro.props;
2829
const map = cache.get(req) ?? new Map();
2930
const i = map.get(name) ?? 0;
3031
map.set(name, i + 1);
@@ -33,7 +34,7 @@ cache.set(req, map);
3334
const { include = {} } = config;
3435
const sets = Object.keys(include);
3536
36-
const includeSymbol = i === 0;
37+
const includeSymbol = !inline && i === 0;
3738
3839
let [setName, iconName] = (name as string).split(":");
3940
@@ -115,6 +116,14 @@ const normalizedBody = renderData.body;
115116

116117
<svg {...normalizedProps} data-icon={name}>
117118
{title && <title>{title}</title>}
118-
{includeSymbol && <symbol id={id} set:html={normalizedBody} />}
119-
<use xlink:href={`#${id}`}></use>
119+
{
120+
inline ? (
121+
<Fragment id={id} set:html={normalizedBody} />
122+
) : (
123+
<Fragment>
124+
{includeSymbol && <symbol id={id} set:html={normalizedBody} />}
125+
<use xlink:href={`#${id}`} />
126+
</Fragment>
127+
)
128+
}
120129
</svg>

0 commit comments

Comments
 (0)