Skip to content

Commit

Permalink
refactor: use StarlightIcon type
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo committed Jan 14, 2025
1 parent b324eca commit ba5bc64
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions docs/src/components/icons-list.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import { Icon } from '@astrojs/starlight/components';
import { Icons } from '../../../packages/starlight/components/Icons';
import { Icons, type StarlightIcon } from '../../../packages/starlight/components/Icons';
interface Props {
labels?: {
Expand All @@ -10,7 +10,7 @@ interface Props {
const { copied = 'Copied!' } = Astro.props.labels ?? {};
const icons = Object.keys(Icons) as (keyof typeof Icons)[];
const icons = Object.keys(Icons) as StarlightIcon[];
---

<div class="icons-grid" data-label-copied={copied}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';
import { processFileTree } from '../../user-components/rehype-file-tree';
import { Icons } from '../../components/Icons';
import { Icons, type StarlightIcon } from '../../components/Icons';

describe('validation', () => {
test('throws an error with no content', () => {
Expand Down Expand Up @@ -176,6 +176,6 @@ function extractFileTree(html: string, stripIcons = true) {
return tree;
}

function expectHtmlToIncludeIcon(html: string, icon: (typeof Icons)[keyof typeof Icons]) {
function expectHtmlToIncludeIcon(html: string, icon: (typeof Icons)[StarlightIcon]) {
return expect(extractFileTree(html, false)).toContain(icon.replace('/>', '>'));
}
4 changes: 2 additions & 2 deletions packages/starlight/components/ContentNotice.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
import { Icons } from '../components/Icons';
import type { StarlightIcon } from '../components/Icons';
import Icon from '../user-components/Icon.astro';
interface Props {
icon: keyof typeof Icons;
icon: StarlightIcon;
label: string;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/starlight/schemas/hero.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { z } from 'astro/zod';
import type { SchemaContext } from 'astro:content';
import { Icons } from '../components/Icons';
import { Icons, type StarlightIcon } from '../components/Icons';

type IconName = keyof typeof Icons;
const iconNames = Object.keys(Icons) as [IconName, ...IconName[]];
const iconNames = Object.keys(Icons) as [StarlightIcon, ...StarlightIcon[]];

export const HeroSchema = ({ image }: SchemaContext) =>
z.object({
Expand Down
4 changes: 2 additions & 2 deletions packages/starlight/user-components/Card.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
import Icon from './Icon.astro';
import type { Icons } from '../components/Icons';
import type { StarlightIcon } from '../components/Icons';
interface Props {
icon?: keyof typeof Icons;
icon?: StarlightIcon;
title: string;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/starlight/user-components/LinkButton.astro
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
import type { HTMLAttributes } from 'astro/types';
import { Icons } from '../components/Icons';
import type { StarlightIcon } from '../components/Icons';
import Icon from './Icon.astro';
interface Props extends Omit<HTMLAttributes<'a'>, 'href'> {
href: string | URL;
icon?: keyof typeof Icons | undefined;
icon?: StarlightIcon | undefined;
iconPlacement?: 'start' | 'end' | undefined;
variant?: 'primary' | 'secondary' | 'minimal';
}
Expand Down
4 changes: 2 additions & 2 deletions packages/starlight/user-components/TabItem.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
import { TabItemTagname } from './rehype-tabs';
import type { Icons } from '../components/Icons';
import type { StarlightIcon } from '../components/Icons';
interface Props {
icon?: keyof typeof Icons;
icon?: StarlightIcon;
label: string;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/starlight/user-components/rehype-file-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fromHtml } from 'hast-util-from-html';
import { toString } from 'hast-util-to-string';
import { rehype } from 'rehype';
import { CONTINUE, SKIP, visit } from 'unist-util-visit';
import { Icons } from '../components/Icons';
import { Icons, type StarlightIcon } from '../components/Icons';
import { definitions } from './file-tree-icons';

declare module 'vfile' {
Expand Down Expand Up @@ -160,7 +160,7 @@ function getFileIcon(fileName: string) {
const name = getFileIconName(fileName);
if (!name) return defaultFileIcon;
if (name in Icons) {
const path = Icons[name as keyof typeof Icons];
const path = Icons[name as StarlightIcon];
return makeSVGIcon(path);
}
return defaultFileIcon;
Expand Down
6 changes: 3 additions & 3 deletions packages/starlight/user-components/rehype-tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { Element } from 'hast';
import { select } from 'hast-util-select';
import { rehype } from 'rehype';
import { CONTINUE, SKIP, visit } from 'unist-util-visit';
import { Icons } from '../components/Icons';
import type { StarlightIcon } from '../components/Icons';

interface Panel {
panelId: string;
tabId: string;
label: string;
icon?: keyof typeof Icons;
icon?: StarlightIcon;
}

declare module 'vfile' {
Expand Down Expand Up @@ -67,7 +67,7 @@ const tabsProcessor = rehype()
...ids,
label: String(dataLabel),
};
if (dataIcon) panel.icon = String(dataIcon) as keyof typeof Icons;
if (dataIcon) panel.icon = String(dataIcon) as StarlightIcon;
file.data.panels?.push(panel);

// Remove `<TabItem>` props
Expand Down

0 comments on commit ba5bc64

Please sign in to comment.