Skip to content

Commit

Permalink
feat: add data-active to variant in route hash
Browse files Browse the repository at this point in the history
  • Loading branch information
timhanlon committed Jan 24, 2025
1 parent 562eb80 commit 53747cc
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/runtime/components/Variant.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div
:id="`variant-${variantDetails?.slug}`"
ref="variantContainer"
class="variant-container"
:class="tvVariant().base({ class: [classes?.container, variantClasses.container] })"
>
Expand Down Expand Up @@ -54,15 +55,15 @@
</template>

<script setup lang="ts">
import { computed, inject, ref } from 'vue'
import { computed, inject, ref, watch } from 'vue'
import { tv } from 'tailwind-variants'
import { useStory } from '../composables/useStory'
import type { ComponentSlotClasses } from '../../types/module'
import CodeButton from './CodeButton.vue'
import CopyButton from './CopyButton.vue'
import TemplateView from './TemplateView.vue'
// @ts-expect-error resolved at runtime
import { useRuntimeConfig } from '#imports'
import { useRoute, useRuntimeConfig } from '#imports'
const tvVariant = tv({
slots: {
Expand Down Expand Up @@ -100,6 +101,19 @@ const variantDetails = computed(() =>
)
const showTemplate = ref(props.showTemplate)
const route = useRoute()
const variantContainer = ref<HTMLElement | null>(null)
watch(() => route.hash, (newHash) => {
if (newHash) {
if (newHash === `#variant-${variantDetails?.value?.slug}`) {
variantContainer.value?.setAttribute('data-active', 'true')
}
else {
variantContainer.value?.removeAttribute('data-active')
}
}
})
</script>

<style scoped>
Expand Down

0 comments on commit 53747cc

Please sign in to comment.