#How can I make my own custom Icon component that uses my own SVGs?
5 messages · Page 1 of 1 (latest)
Based on the information provided in the extracts, there isn't a direct example of creating a custom Icon component using your own SVGs. However, you can create custom components in Astro and use them in your .mdx or .astro files.
Here's a general approach based on the information available:
-
Create a new Astro component for your SVG icon. For example, you might create a file called
MyIcon.astroin your components directory. -
In this file, you can define your SVG markup. For example:
<svg viewBox="0 0 24 24" width="24" height="24">
<path d="..." />
</svg>
- You can then import this component into your
.mdxor.astrofile and use it like any other component. For example:
---
import MyIcon from '../components/MyIcon.astro';
---
<ul>
<li><MyIcon /> List item 1</li>
<li><MyIcon /> List item 2</li>
</ul>
Please note that this is a general approach and might need to be adjusted based on your specific needs and the structure of your project.
For more detailed information on creating and using components in Astro, you can refer to the Astro documentation (https://docs.astro.build).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@calm plover kapa.ai is still learning and improving, please let us know how it did by reacting below
Let me check the knowledge base for you. One moment! :books:
I apologize for the confusion. You're right, the previous example doesn't follow the pattern of overriding HTML elements with custom components as shown in the Astro documentation.
To create a custom Icon component and use it within a Markdown file, you can follow these steps:
- Create a new Astro component for your SVG icon. For example, you might create a file called
Icon.astroin your components directory.
---
const props = Astro.props;
---
<svg {...props} viewBox="0 0 24 24" width="24" height="24">
<path d="..." />
</svg>
- Import your custom component into your
.mdxfile, then export acomponentsobject that maps the standard HTML element to your custom component. In this case, you might want to map it to an element likespanordivthat you can use in your Markdown content.
---
import Icon from '../components/Icon.astro';
export const components = {span: Icon};
---
- <span></span> This list item will use the custom Icon component
- <span></span> So will this one
In this example, every span element in the Markdown content will be replaced with your custom Icon component.