Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add symbol prop to <Icon> for <use> integration in UI frameworks #238

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twenty-pianos-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-icon": minor
---

feat: add `symbol` prop to `<Icon>` for `<use>` integration in UI frameworks
17 changes: 17 additions & 0 deletions demo/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ const icon = "adjustment";

<article>
<h2>Local Icons</h2>
<!-- SVG sprite -->
<svg width="0" height="0">
<Icon name="adjustment" symbol />
<Icon name="adjustment" symbol />
<Icon name="annotation" symbol />
<Icon name="logos/deno" symbol />
</svg>

<Icon size={24} name="adjustment" />
<Icon size={24} name={icon} />
<Icon size={24} name="annotation" />
Expand All @@ -33,6 +41,15 @@ const icon = "adjustment";
render it here!
</p>
</article>

<!-- SVG sprite -->
<svg width="0" height="0">
<Icon name="ic:baseline-account-box" symbol />
<Icon name="ic:baseline-account-box" symbol />
<Icon name="annotation" symbol />
<Icon name="ri:aliens-fill" symbol />
</svg>

<!-- Pull your icon from the default remote service -->
<Icon size={32} name="ic:baseline-account-box" />
<Icon size={64} name="ic:baseline-account-box" />
Expand Down
35 changes: 20 additions & 15 deletions packages/core/components/Icon.astro
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Props extends HTMLAttributes<"svg"> {
size?: number | string;
width?: number | string;
height?: number | string;
symbol?: boolean;
}

class AstroIconError extends Error {
Expand All @@ -25,7 +26,7 @@ class AstroIconError extends Error {
}

const req = Astro.request;
const { name = "", title, "is:inline": inline = false, ...props } = Astro.props;
const { name = "", title, "is:inline": inline = false, symbol: isSymbol, ...props } = Astro.props;
const map = cache.get(req) ?? new Map();
const i = map.get(name) ?? 0;
map.set(name, i + 1);
Expand Down Expand Up @@ -112,18 +113,22 @@ if (props.size) {
const renderData = iconToSVG(iconData);
const normalizedProps = { ...renderData.attributes, ...props };
const normalizedBody = renderData.body;
const symbolProps = { viewBox: renderData.attributes.viewBox };
---

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