active link style

This commit is contained in:
itsMapleLeaf
2023-03-12 16:14:46 -05:00
parent 6da6008d2c
commit 84348d287f
5 changed files with 46 additions and 32 deletions

View File

@@ -0,0 +1,14 @@
---
export type Props = astroHTML.JSX.AnchorHTMLAttributes & {
href: string
}
const url = Astro.url
const linkUrl = new URL(Astro.props.href, url)
---
<a
{...Astro.props}
data-active={url.pathname === linkUrl.pathname ? true : undefined}
>
<slot />
</a>

View File

@@ -4,6 +4,7 @@ import { getCollection, type CollectionEntry } from "astro:content"
import AppFooter from "~/components/app-footer.astro"
import Layout from "~/components/layout.astro"
import MainNavigation from "~/components/main-navigation.astro"
import NavLink from "~/components/nav-link.astro"
export type Props = {
guide: CollectionEntry<"guides">
@@ -37,9 +38,13 @@ const { Content } = await Astro.props.guide.render()
{
guides.map((guide) => (
<li>
<a class="link" href={`/guides/${guide.slug}`} rel="prefetch">
<NavLink
class="link data-[active]:link-active"
href={`/guides/${guide.slug}`}
rel="prefetch"
>
{guide.data.title}
</a>
</NavLink>
</li>
))
}

View File

@@ -24,13 +24,13 @@
@apply font-medium inline-block relative opacity-60 hover:opacity-100 transition-opacity;
}
.link::after {
@apply content-[''] absolute block w-full h-px bg-current translate-y-[3px] opacity-0 transition;
@apply content-[''] bottom-[-2px] absolute block w-full h-px bg-current translate-y-[3px] opacity-0 transition;
}
.link:hover::after {
@apply -translate-y-px opacity-50;
}
.link-active {
@apply text-emerald-500;
@apply text-emerald-500 opacity-100;
}
.button {