Skip to content

Commit

Permalink
Add support for is:inline directive (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Feb 2, 2024
1 parent 9a332d7 commit d7528f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-waves-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-icon": minor
---

Adds support for the `is:inline` directive, which bypasses automatic sprite optimization and directly embeds the plain SVG.
3 changes: 2 additions & 1 deletion demo/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ const icon = "adjustment";

<article>
<h2>Local Icons</h2>
<!-- This will inline your SVG directly -->
<Icon size={24} name="adjustment" />
<Icon size={24} name={icon} />
<Icon size={24} name="annotation" />
<Icon size={24} name="logos/deno" />
<Icon name="logos/deno" />
<Icon width={75} name="logos/alpine" />
<Icon name="logos/alpine-multi-color" />

<Icon is:inline size={24} name="adjustment" />
</article>

<article>
Expand Down
17 changes: 13 additions & 4 deletions packages/core/components/Icon.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { cache } from "./cache.js";
interface Props extends HTMLAttributes<"svg"> {
name: Icon;
"is:inline"?: boolean;
title?: string;
size?: number;
width?: number;
Expand All @@ -24,7 +25,7 @@ class AstroIconError extends Error {
}
const req = Astro.request;
const { name = "", title, ...props } = Astro.props;
const { name = "", title, "is:inline": inline = false, ...props } = Astro.props;
const map = cache.get(req) ?? new Map();
const i = map.get(name) ?? 0;
map.set(name, i + 1);
Expand All @@ -33,7 +34,7 @@ cache.set(req, map);
const { include = {} } = config;
const sets = Object.keys(include);
const includeSymbol = i === 0;
const includeSymbol = !inline && i === 0;
let [setName, iconName] = (name as string).split(":");
Expand Down Expand Up @@ -115,6 +116,14 @@ const normalizedBody = renderData.body;

<svg {...normalizedProps} data-icon={name}>
{title && <title>{title}</title>}
{includeSymbol && <symbol id={id} set:html={normalizedBody} />}
<use xlink:href={`#${id}`}></use>
{
inline ? (
<Fragment id={id} set:html={normalizedBody} />
) : (
<Fragment>
{includeSymbol && <symbol id={id} set:html={normalizedBody} />}
<use xlink:href={`#${id}`} />
</Fragment>
)
}
</svg>

0 comments on commit d7528f6

Please sign in to comment.