-
-
Save ansarizafar/7cf1c84242461dc9ba7128a2a8b9cd1e to your computer and use it in GitHub Desktop.
Svelte + Sapper highlighted link (component that is aware of current url)
This file contains 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 is used to have a link on the page that will show highlighted if the url meets the criteria. | |
You might want to adjust the logic on line 19. | |
usage: | |
<HighlightedLink bind:segment highlight="faq" rel="prefetch" link="/faq" text="FAQ" /> | |
--> | |
<script> | |
import { stores } from '@sapper/app'; | |
const { page } = stores(); | |
export let highlight; | |
export let segment; | |
export let text = 'text here'; | |
export let link; | |
export let target; | |
let highlightPath = false; | |
$: highlightPath = | |
$page.path && highlight && ($page.path.includes(highlight) || $page.path.includes(link)); | |
</script> | |
<style> | |
.selected { | |
position: relative; | |
display: inline-block; | |
} | |
.selected::after { | |
position: absolute; | |
content: ''; | |
width: calc(100% - 1em); | |
height: 2px; | |
background-color: rgb(255, 62, 0); | |
display: block; | |
bottom: -1px; | |
} | |
a { | |
padding-left: 10px; | |
} | |
</style> | |
<a class:selected={highlightPath} href={link}>{text}</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment