docs: add DocsVersion component to navigate through different docs versions

This commit is contained in:
Yassine Doghri 2024-05-29 17:28:24 +00:00
parent fe676590f2
commit 083a766e4e
4 changed files with 163 additions and 0 deletions

View File

@ -15,6 +15,10 @@ export default defineConfig({
title: "Castopod Docs",
description:
"Check out the Castopod documentation! Install your own free & open-source podcast host, help make it better by contributing, or simply learn more about Castopod!",
components: {
Header: "./src/components/Header.astro",
MobileMenuFooter: "./src/components/MobileMenuFooter.astro",
},
logo: {
src: "./src/assets/castopod-logo-inline.svg",
replacesTitle: true,

View File

@ -0,0 +1,34 @@
---
import Select from '@astrojs/starlight/components/Select.astro';
import type { Props } from '@astrojs/starlight/props';
---
<docs-version-select>
<Select
icon="starlight"
label="version"
value="develop"
options={[
{ label: 'develop', selected: import.meta.env.BASE_URL === '/develop', value: '/develop' },
{ label: 'next', selected: import.meta.env.BASE_URL === '/next', value: '/next' },
]}
width="7em"
/>
</docs-version-select>
<script>
class DocsVersionSelect extends HTMLElement {
constructor() {
super();
const select = this.querySelector('select');
if (select) {
select.addEventListener('change', (e) => {
if (e.currentTarget instanceof HTMLSelectElement) {
window.location.pathname = e.currentTarget.value;
}
});
}
}
}
customElements.define('docs-version-select', DocsVersionSelect);
</script>

View File

@ -0,0 +1,90 @@
---
import config from 'virtual:starlight/user-config';
import type { Props } from '../props';
import LanguageSelect from 'virtual:starlight/components/LanguageSelect';
import Search from 'virtual:starlight/components/Search';
import SiteTitle from 'virtual:starlight/components/SiteTitle';
import SocialIcons from 'virtual:starlight/components/SocialIcons';
import ThemeSelect from 'virtual:starlight/components/ThemeSelect';
import VersionSelect from './DocsVersionSelect.astro';
/**
* Render the `Search` component if Pagefind is enabled or the default search component has been overridden.
*/
const shouldRenderSearch =
config.pagefind || config.components.Search !== '@astrojs/starlight/components/Search.astro';
---
<div class="header sl-flex">
<div class="title-wrapper sl-flex">
<SiteTitle {...Astro.props} />
</div>
<div class="sl-flex">
{shouldRenderSearch && <Search {...Astro.props} />}
</div>
<div class="sl-hidden md:sl-flex right-group">
<div class="sl-flex social-icons">
<SocialIcons {...Astro.props} />
</div>
<ThemeSelect {...Astro.props} />
<VersionSelect {...Astro.props} />
<LanguageSelect {...Astro.props} />
</div>
</div>
<style>
.header {
gap: var(--sl-nav-gap);
justify-content: space-between;
align-items: center;
height: 100%;
}
.title-wrapper {
/* Prevent long titles overflowing and covering the search and menu buttons on narrow viewports. */
overflow: hidden;
}
.right-group,
.social-icons {
gap: 1rem;
align-items: center;
}
.social-icons::after {
content: '';
height: 2rem;
border-inline-end: 1px solid var(--sl-color-gray-5);
}
@media (min-width: 50rem) {
:global(:root[data-has-sidebar]) {
--__sidebar-pad: calc(2 * var(--sl-nav-pad-x));
}
:global(:root:not([data-has-toc])) {
--__toc-width: 0rem;
}
.header {
--__sidebar-width: max(0rem, var(--sl-content-inline-start, 0rem) - var(--sl-nav-pad-x));
--__main-column-fr: calc(
(
100% + var(--__sidebar-pad, 0rem) - var(--__toc-width, var(--sl-sidebar-width)) -
(2 * var(--__toc-width, var(--sl-nav-pad-x))) - var(--sl-content-inline-start, 0rem) -
var(--sl-content-width)
) / 2
);
display: grid;
grid-template-columns:
/* 1 (site title): runs up until the main content columns left edge or the width of the title, whichever is the largest */
minmax(
calc(var(--__sidebar-width) + max(0rem, var(--__main-column-fr) - var(--sl-nav-gap))),
auto
)
/* 2 (search box): all free space that is available. */
1fr
/* 3 (right items): use the space that these need. */
auto;
align-content: center;
}
}
</style>

View File

@ -0,0 +1,35 @@
---
import LanguageSelect from 'virtual:starlight/components/LanguageSelect';
import SocialIcons from 'virtual:starlight/components/SocialIcons';
import ThemeSelect from 'virtual:starlight/components/ThemeSelect';
import type { Props } from '../props';
import VersionSelect from './DocsVersionSelect.astro';
---
<div class="mobile-preferences sl-flex">
<div class="sl-flex social-icons">
<SocialIcons {...Astro.props} />
</div>
<ThemeSelect {...Astro.props} />
<VersionSelect {...Astro.props} />
<LanguageSelect {...Astro.props} />
</div>
<style>
.social-icons {
margin-inline-end: auto;
gap: 1rem;
align-items: center;
padding-block: 1rem;
}
.social-icons:empty {
display: none;
}
.mobile-preferences {
justify-content: space-between;
flex-wrap: wrap;
border-top: 1px solid var(--sl-color-gray-6);
column-gap: 1rem;
padding: 0.5rem 0;
}
</style>