Skip to content

feat(astro): override components to support HeadTags #375

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

Merged
merged 13 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,22 @@ interface Props {
/** Content of the dialog */
children: ReactNode;
}
```

### HeadTags

Component for overriding links in the head tag.

When overriding `HeadTags` you can place TutorialKit's default components using following [Astro slots](https://docs.astro.build/en/basics/astro-components/#named-slots):

- `title`: The page title
- `links`: Links for the favicon, fonts and other assets
- `meta`: Metadata and Open Graph tags

```jsx title="src/components/CustomHeadLinks.astro"
<slot name="title" />
<slot name="links" />
<slot name="meta" />
{/** Add your own tags */}
<link rel="sitemap" href="/sitemap-index.xml" />
```
1 change: 1 addition & 0 deletions e2e/configs/override-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineConfig({
components: {
Dialog: './src/components/Dialog.tsx',
TopBar: './src/components/TopBar.astro',
HeadTags: './src/components/CustomHeadTags.astro',
},
}),
],
Expand Down
4 changes: 4 additions & 0 deletions e2e/src/components/CustomHeadTags.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<slot name="title" />
<slot name="links" />
<slot name="meta" />
<link rel="sitemap" href="/sitemap-index.xml" />
20 changes: 20 additions & 0 deletions e2e/test/headtags.override-components.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test, expect } from '@playwright/test';

test('developer can override HeadTags', async ({ page }) => {
await page.goto('/');

const defaultElems = [
page.locator('title'),
page.locator('meta[name="og:title"]'),
page.locator('link[rel="stylesheet"]').first(),
];
const customElems = [page.locator('link[rel="sitemap"]')];

for (const e of defaultElems) {
await expect(e).toBeAttached();
}

for (const e of customElems) {
await expect(e).toBeAttached();
}
});
1 change: 1 addition & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"./default/pages/index.astro": "./dist/default/pages/index.astro",
"./default/pages/[...slug].astro": "./dist/default/pages/[...slug].astro",
"./default/components/TopBar.astro": "./dist/default/components/TopBar.astro",
"./default/components/HeadTags.astro": "./dist/default/components/HeadTags.astro",
"./package.json": "./package.json"
},
"files": [
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/default/components/HeadTags.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<slot name="title" />
<slot name="links" />
<slot name="meta" />
3 changes: 2 additions & 1 deletion packages/astro/src/default/env-default.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ interface WebContainerConfig {

declare module 'tutorialkit:override-components' {
const topBar: typeof import('./src/default/components/TopBar.astro').default;
const headLinks: typeof import('./src/default/components/HeadTags.astro').default;
const dialog: typeof import('@tutorialkit/react/dialog').default;

export { topBar as TopBar, dialog as Dialog };
export { topBar as TopBar, dialog as Dialog, headLinks as HeadTags };
}

declare const __ENTERPRISE__: boolean;
Expand Down
22 changes: 15 additions & 7 deletions packages/astro/src/default/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import { HeadTags } from 'tutorialkit:override-components';
import { ViewTransitions } from 'astro:transitions';
import type { MetaTagsConfig } from '@tutorialkit/types';
import MetaTags from '../components/MetaTags.astro';
Expand All @@ -15,13 +16,20 @@ const faviconUrl = readPublicAsset('favicon.svg');
<!doctype html>
<html lang="en" transition:animate="none" class="h-full overflow-hidden">
<head>
<title>{title}</title>
{faviconUrl ? <link rel="icon" type="image/svg+xml" href={faviconUrl} /> : null}
<MetaTags meta={meta} />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;700&display=swap" rel="stylesheet" />
<HeadTags>
<title slot="title">{title}</title>
<Fragment slot="links">
{faviconUrl ? <link rel="icon" type="image/svg+xml" href={faviconUrl} /> : null}
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap"
rel="stylesheet"
/>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;700&display=swap" rel="stylesheet" />
</Fragment>
<MetaTags slot="meta" meta={meta} />
</HeadTags>
<ViewTransitions />
<script is:inline>
setTutorialKitTheme();
Expand Down
24 changes: 24 additions & 0 deletions packages/astro/src/vite-plugins/override-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* components: {
* TopBar: './CustomTopBar.astro',
* Dialog: './CustomDialog.tsx',
* HeadTags: './CustomHeadLinks.astro',
* },
* }),
* ],
Expand Down Expand Up @@ -54,6 +55,18 @@ export interface OverrideComponentsOptions {
* It will receive same props that `@tutorialkit/react/dialog` supports.
*/
Dialog?: string;

/**
* Component for overriding links in the head tag.
*
* It will receive TutorialKit default links within the "default-links" slot.
*
* ```jsx
* <slot name="default-links"></slot>
* <link rel="sitemap" href="/sitemap-index.xml" />
* ```
*/
HeadTags: string;
}

interface Options {
Expand All @@ -77,11 +90,13 @@ export function overrideComponents({ components, defaultRoutes }: Options): Vite
async load(id) {
if (id === resolvedId) {
const topBar = components?.TopBar || resolveDefaultTopBar(defaultRoutes);
const headLinks = components?.HeadTags || resolveDefaultHeadLinks(defaultRoutes);
const dialog = components?.Dialog || '@tutorialkit/react/dialog';

return `
export { default as TopBar } from '${topBar}';
export { default as Dialog } from '${dialog}';
export { default as HeadTags } from '${headLinks}';
`;
}

Expand All @@ -98,3 +113,12 @@ function resolveDefaultTopBar(defaultRoutes: boolean) {
// default `TopBar` is used from local file when `defaultRoutes` is disabled
return './src/components/TopBar.astro';
}

function resolveDefaultHeadLinks(defaultRoutes: boolean) {
if (defaultRoutes) {
return '@tutorialkit/astro/default/components/HeadTags.astro';
}

// default `HeadTags` is used from local file when `defaultRoutes` is disabled
return './src/components/HeadTags.astro';
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ exports[`create and eject a project 1`] = `
"public/logo.svg",
"src",
"src/components",
"src/components/HeadTags.astro",
"src/components/LoginButton.tsx",
"src/components/Logo.astro",
"src/components/MainContainer.astro",
Expand Down
Loading