#Changing icons in favicon and index.mdx for Starlight
18 messages · Page 1 of 1 (latest)
Hi @tropic lichen. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To change the favicon in Starlight, you should set the favicon option in your Starlight integration configuration (astro.config.mjs). The favicon file should be placed in your public/ directory, and you can specify its path like this:
import { defineConfig } from 'astro/config';
import starlight from '@astrojs/starlight';
export default defineConfig({
integrations: [
starlight({
favicon: '/images/favicon.svg', // Path to your favicon in the public directory
}),
],
});
If you need to add additional favicon variants or fallbacks, use the head option to inject extra <link> tags:
starlight({
favicon: '/images/favicon.svg',
head: [
{
tag: 'link',
attrs: {
rel: 'icon',
href: '/images/favicon.ico',
sizes: '32x32',
},
},
],
});
Reference: Favicon configuration
For dynamically changing the icon used in the homepage (such as in index.mdx), Starlight does not provide a built-in way to dynamically change icons based on a parameter or variable at runtime. Icons in content are typically set statically using the <Icon> component, for example:
import { Icon } from '@astrojs/starlight/components';
<Icon name="star" />
Reference: Using icons in content
If you want to change the icon based on some condition, you would need to implement that logic in a custom component or use MDX expressions, but the knowledge sources do not provide explicit instructions or examples for dynamic icon switching in Starlight content files.
Summary:
- Favicon: Set statically in
astro.config.mjsusing thefaviconoption. - Homepage/content icons: Set statically with the
<Icon>component; dynamic switching is not documented in the provided sources.
If you need more advanced dynamic behavior, you may need to create a custom component, but the knowledge sources do not cover this scenario.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@tropic lichen Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@misty sleet Regarding the favicon, I also want to change it depending on dark/light mode. Also, regarding the index.mdx, I meant the entry in its frontmatter, like so:
---
title: NeuroPilot Docs
description: The operator's manual on using NeuroPilot.
template: splash
hero:
tagline: The operator's manual on using NeuroPilot.
image:
file: ../../assets/neuropilot.svg
---
How do I change the file dynamically?
Let me check the knowledge base for you again. One moment! :books:
For your two questions:
1. Dynamically Changing the Favicon for Dark/Light Mode
Starlight’s favicon configuration allows you to set a single favicon via the favicon option in your astro.config.mjs. If you want to provide additional favicon variants (such as for dark and light mode), you can add extra <link> tags using the head option. However, the knowledge sources do not document a built-in way to automatically switch favicons based on the user’s color scheme (dark/light mode). You can add multiple favicons, but switching based on theme is not natively supported in Starlight’s documented configuration:
starlight({
favicon: '/images/favicon.svg',
head: [
// Add ICO favicon fallback for Safari.
{
tag: 'link',
attrs: {
rel: 'icon',
href: '/images/favicon.ico',
sizes: '32x32',
},
},
],
});
Reference: Favicon configuration
There is no mention of using media queries (like prefers-color-scheme) for favicon switching in the provided documentation.
2. Dynamically Changing the hero.image.file in Frontmatter
Starlight’s frontmatter for the hero component supports specifying different images for dark and light mode. Instead of using the file property, you can use the dark and light properties under image in your frontmatter. Starlight will then automatically use the appropriate image based on the user’s color scheme:
---
title: NeuroPilot Docs
description: The operator's manual on using NeuroPilot.
template: splash
hero:
tagline: The operator's manual on using NeuroPilot.
image:
dark: ../../assets/neuropilot-dark.svg
light: ../../assets/neuropilot-light.svg
---
This is the documented way to provide different images for dark and light mode in Starlight’s hero component frontmatter Reference: Hero image dark/light mode.
Summary:
- Favicon: No documented way to switch favicon based on dark/light mode in Starlight.
- Hero image in frontmatter: Use
darkandlightproperties underhero.imageto switch images automatically based on color scheme.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@tropic lichen Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@misty sleet would you happen to know a workaround for question 1? I really want to have my favicon change on my Starlight site depending on site theme.
Let me check the knowledge base for you again. One moment! :books:
I'm sorry, but the knowledge sources do not provide a documented or supported workaround for dynamically changing the favicon based on dark/light mode in Starlight. The official documentation only describes setting a static favicon and adding additional variants or fallbacks via the head option, but does not mention any method (such as media queries or JavaScript) for switching the favicon according to the user's color scheme Reference: Favicon configuration.
If you need further assistance or want to explore possible custom solutions, you may want to ask in the Astro Discord community or check for related discussions in the Starlight GitHub issues as suggested in the troubleshooting section Reference: Troubleshooting Starlight.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@tropic lichen Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@misty sleet Regarding question 2, what other fields could I add?
Let me check the knowledge base for you again. One moment! :books:
You can add additional fields to the frontmatter of your Starlight pages by extending the frontmatter schema in your src/content.config.ts file. Starlight uses the docsSchema() helper, which you can extend with custom fields using Zod.
For example, you could add a required description field and an optional category field:
import { defineCollection, z } from 'astro:content';
import { docsLoader } from '@astrojs/starlight/loaders';
import { docsSchema } from '@astrojs/starlight/schema';
export const collections = {
docs: defineCollection({
loader: docsLoader(),
schema: docsSchema({
extend: z.object({
// Make a built-in field required instead of optional.
description: z.string(),
// Add a new field to the schema.
category: z.enum(['tutorial', 'guide', 'reference']).optional(),
}),
}),
}),
};
You can also add fields for images using Astro’s image() helper:
import { defineCollection, z } from 'astro:content';
import { docsLoader } from '@astrojs/starlight/loaders';
import { docsSchema } from '@astrojs/starlight/schema';
export const collections = {
docs: defineCollection({
loader: docsLoader(),
schema: docsSchema({
extend: ({ image }) => {
return z.object({
// Add a field that must resolve to a local image.
cover: image(),
});
},
}),
}),
};