Skip to content

Commit 8df90f4

Browse files
committed
can ovewrite default head links
1 parent bc61229 commit 8df90f4

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

docs/tutorialkit.dev/src/content/docs/guides/overriding-components.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,15 @@ interface Props {
8989
/** Content of the dialog */
9090
children: ReactNode;
9191
}
92+
```
93+
94+
### HeadLinks
95+
96+
Component for overriding links in the head tag.
97+
98+
When overriding `HeadLinks` you can place TutorialKit's default links using the `default-links` [Astro slot](https://docs.astro.build/en/basics/astro-components/#named-slots).
99+
100+
```jsx title="src/components/CustomHeadLinks.astro"
101+
<slot name="default-links" />
102+
<link rel="sitemap" href="/sitemap-index.xml" />
92103
```

e2e/configs/override-components.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default defineConfig({
1010
components: {
1111
Dialog: './src/components/Dialog.tsx',
1212
TopBar: './src/components/TopBar.astro',
13+
HeadLinks: './src/components/HeadLinks.astro',
1314
},
1415
}),
1516
],

packages/astro/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"./default/pages/index.astro": "./dist/default/pages/index.astro",
2121
"./default/pages/[...slug].astro": "./dist/default/pages/[...slug].astro",
2222
"./default/components/TopBar.astro": "./dist/default/components/TopBar.astro",
23+
"./default/components/HeadLinks.astro": "./dist/default/components/HeadLinks.astro",
2324
"./package.json": "./package.json"
2425
},
2526
"files": [
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<slot name="default-links" />

packages/astro/src/default/env-default.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ interface WebContainerConfig {
99

1010
declare module 'tutorialkit:override-components' {
1111
const topBar: typeof import('./src/default/components/TopBar.astro').default;
12+
const headLinks: typeof import('./src/default/components/HeadLinks.astro').default;
1213
const dialog: typeof import('@tutorialkit/react/dialog').default;
1314

14-
export { topBar as TopBar, dialog as Dialog };
15+
export { topBar as TopBar, dialog as Dialog, headLinks as HeadLinks };
1516
}
1617

1718
declare const __ENTERPRISE__: boolean;

packages/astro/src/default/layouts/Layout.astro

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
import { HeadLinks } from 'tutorialkit:override-components';
23
import { ViewTransitions } from 'astro:transitions';
34
import type { MetaTagsConfig } from '@tutorialkit/types';
45
import MetaTags from '../components/MetaTags.astro';
@@ -18,10 +19,17 @@ const faviconUrl = readPublicAsset('favicon.svg');
1819
<title>{title}</title>
1920
{faviconUrl ? <link rel="icon" type="image/svg+xml" href={faviconUrl} /> : null}
2021
<MetaTags meta={meta} />
21-
<link rel="preconnect" href="https://fonts.googleapis.com" />
22-
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
23-
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
24-
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;700&display=swap" rel="stylesheet" />
22+
<HeadLinks>
23+
<Fragment slot="default-links">
24+
<link rel="preconnect" href="https://fonts.googleapis.com" />
25+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
26+
<link
27+
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap"
28+
rel="stylesheet"
29+
/>
30+
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;700&display=swap" rel="stylesheet" />
31+
</Fragment>
32+
</HeadLinks>
2533
<ViewTransitions />
2634
<script is:inline>
2735
setTutorialKitTheme();

packages/astro/src/vite-plugins/override-components.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* components: {
1919
* TopBar: './CustomTopBar.astro',
2020
* Dialog: './CustomDialog.tsx',
21+
* HeadLinks: './CustomHeadLinks.astro',
2122
* },
2223
* }),
2324
* ],
@@ -54,6 +55,18 @@ export interface OverrideComponentsOptions {
5455
* It will receive same props that `@tutorialkit/react/dialog` supports.
5556
*/
5657
Dialog?: string;
58+
59+
/**
60+
* Component for overriding links in the head tag.
61+
*
62+
* It will receive TutorialKit default links within the "default-links" slot.
63+
*
64+
* ```jsx
65+
* <slot name="default-links"></slot>
66+
* <link rel="sitemap" href="/sitemap-index.xml" />
67+
* ```
68+
*/
69+
HeadLinks: string;
5770
}
5871

5972
interface Options {
@@ -77,11 +90,13 @@ export function overrideComponents({ components, defaultRoutes }: Options): Vite
7790
async load(id) {
7891
if (id === resolvedId) {
7992
const topBar = components?.TopBar || resolveDefaultTopBar(defaultRoutes);
93+
const headLinks = components?.HeadLinks || resolveDefaultHeadLinks(defaultRoutes);
8094
const dialog = components?.Dialog || '@tutorialkit/react/dialog';
8195

8296
return `
8397
export { default as TopBar } from '${topBar}';
8498
export { default as Dialog } from '${dialog}';
99+
export { default as HeadLinks } from '${headLinks}';
85100
`;
86101
}
87102

@@ -98,3 +113,12 @@ function resolveDefaultTopBar(defaultRoutes: boolean) {
98113
// default `TopBar` is used from local file when `defaultRoutes` is disabled
99114
return './src/components/TopBar.astro';
100115
}
116+
117+
function resolveDefaultHeadLinks(defaultRoutes: boolean) {
118+
if (defaultRoutes) {
119+
return '@tutorialkit/astro/default/components/HeadLinks.astro';
120+
}
121+
122+
// default `HeadLinks` is used from local file when `defaultRoutes` is disabled
123+
return './src/components/HeadLinks.astro';
124+
}

0 commit comments

Comments
 (0)