-
Notifications
You must be signed in to change notification settings - Fork 84
feat: support overriding TopBar
#112
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3c5b40d
refactor: rename `Header` to `TopBar`
AriPerkkio dcb48a9
feat: support overriding `TopBar`
AriPerkkio 0bc4836
fix: code review
AriPerkkio 01dd492
fix: code review
AriPerkkio b8e62b7
fix: code review
AriPerkkio c73dcf5
fix: consistent import path on windows
AriPerkkio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions
64
docs/tutorialkit.dev/src/content/docs/guides/overriding-components.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
title: 'Overriding Components' | ||
description: "Override TutorialKit's default components to fit your needs." | ||
--- | ||
import { Image } from 'astro:assets'; | ||
import uiTopBar from './images/ui-top-bar.png'; | ||
|
||
TutorialKit's default components are customizable with [theming](/reference/theming/) options. | ||
In cases where theming is not enough you can override some built-in components with your own ones. | ||
|
||
## Configuration | ||
|
||
Define `components` option in your `astro.config.ts`: | ||
|
||
```ts | ||
import tutorialkit from '@tutorialkit/astro'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
integrations: [ | ||
tutorialkit({ | ||
components: { | ||
// Component overrides go here | ||
}, | ||
}), | ||
], | ||
}); | ||
``` | ||
|
||
For example to override `TopBar`, provide a path to the new Astro component: | ||
|
||
```ts | ||
tutorialkit({ | ||
components: { | ||
TopBar: './src/components/CustomTopBar.astro', | ||
}, | ||
}), | ||
``` | ||
|
||
### Top Bar | ||
|
||
<Image src={uiTopBar} alt="TutorialKit's Top bar" /> | ||
|
||
When overriding `TopBar` you can place TutorialKit's default components using following [Astro slots](https://docs.astro.build/en/basics/astro-components/#named-slots): | ||
|
||
- `logo`: Logo of the application | ||
- `theme-switch`: Switch for changing the theme | ||
- `login-button`: For StackBlitz Enterprise user, the login button | ||
|
||
```jsx title="src/components/CustomTopBar.astro" | ||
<nav> | ||
<slot name="logo" /> | ||
|
||
<a href="https://tutorialkit.dev/"> | ||
Home page | ||
</a> | ||
|
||
<slot name="theme-switch" /> | ||
|
||
<LanguageSelect /> | ||
|
||
<slot name="login-button" /> | ||
</nav> | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
"demo": "pnpm run --filter=demo.tutorialkit.dev dev", | ||
"demo:build": "pnpm run --filter=demo.tutorialkit.dev build", | ||
"lint": "eslint \"{packages,docs}/**/*\"", | ||
"test": "pnpm run --filter=@tutorialkit/* --filter=tutorialkit test" | ||
"test": "pnpm run --stream --filter=@tutorialkit/* --filter=tutorialkit test --run" | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"license": "MIT", | ||
"packageManager": "[email protected]", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<nav | ||
class="bg-tk-elements-topBar-backgroundColor border-b border-tk-elements-app-borderColor flex max-w-full items-center p-3 px-4 min-h-[56px]" | ||
> | ||
<div class="flex flex-1"> | ||
<slot name="logo" /> | ||
</div> | ||
<div> | ||
<slot name="theme-switch" /> | ||
</div> | ||
<div> | ||
<slot name="login-button" /> | ||
</div> | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</nav> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
import { TopBar } from 'tutorialkit:override-components'; | ||
import { ThemeSwitch } from './ThemeSwitch'; | ||
import { LoginButton } from './LoginButton'; | ||
import Logo from './Logo.astro'; | ||
import { useAuth } from './setup'; | ||
|
||
interface Props { | ||
logoLink: string; | ||
} | ||
|
||
const { logoLink } = Astro.props; | ||
--- | ||
|
||
<TopBar> | ||
<Logo slot="logo" logoLink={logoLink ?? '/'} /> | ||
|
||
<ThemeSwitch client:load transition:persist slot="theme-switch" /> | ||
|
||
{useAuth && <LoginButton client:load transition:persist slot="login-button" />} | ||
</TopBar> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* A plugin that lets users override TutorialKit's components. | ||
* | ||
* The virtual module can be imported as: | ||
* | ||
* ```ts | ||
* import { TopBar } from 'tutorialkit:override-components'; | ||
* | ||
* <TopBar /> | ||
* ``` | ||
* | ||
* User can override the components in `astro.config.ts`: | ||
* | ||
* ```ts | ||
* export default defineConfig({ | ||
* integrations: [ | ||
* tutorialkit({ | ||
* components: { | ||
* TopBar: './CustomTopBar.astro', | ||
* }, | ||
* }), | ||
* ], | ||
* }); | ||
* ``` | ||
*/ | ||
import type { VitePlugin } from '../types.js'; | ||
|
||
export interface OverrideComponentsOptions { | ||
/** | ||
* Component for overriding the top bar. | ||
* | ||
* This component has 3 slots that are used to pass TutorialKit's default components: | ||
* - `logo`: Logo of the application | ||
* - `theme-switch`: Switch for changing the theme | ||
* - `login-button`: For StackBlitz Enterprise user, the login button | ||
* | ||
* Usage: | ||
* | ||
* ```jsx | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* <slot name="logo" /> | ||
* <slot name="theme-switch" /> | ||
* <slot name="login-button" /> | ||
* ``` | ||
*/ | ||
TopBar?: string; | ||
} | ||
|
||
interface Options { | ||
components?: OverrideComponentsOptions; | ||
defaultRoutes: boolean; | ||
} | ||
|
||
const virtualModuleId = 'tutorialkit:override-components'; | ||
const resolvedId = `\0${virtualModuleId}`; | ||
|
||
export function overrideComponents({ components, defaultRoutes }: Options): VitePlugin { | ||
return { | ||
name: 'tutorialkit-override-components-plugin', | ||
resolveId(id) { | ||
if (id === virtualModuleId) { | ||
return resolvedId; | ||
} | ||
|
||
return undefined; | ||
}, | ||
async load(id) { | ||
if (id === resolvedId) { | ||
const topBar = components?.TopBar || resolveDefaultTopBar(defaultRoutes); | ||
|
||
return ` | ||
export { default as TopBar } from '${topBar}'; | ||
`; | ||
} | ||
|
||
return undefined; | ||
}, | ||
}; | ||
} | ||
|
||
function resolveDefaultTopBar(defaultRoutes: boolean) { | ||
if (defaultRoutes) { | ||
return '@tutorialkit/astro/default/components/TopBar.astro'; | ||
} | ||
|
||
// default `TopBar` is used from local file when `defaultRoutes` is disabled | ||
return './src/components/TopBar.astro'; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These docs are highly inspired by Starlight: https://starlight.astro.build/guides/overriding-components/