Skip to content

Commit

Permalink
feat(tabs): render all tabs when printing. (#50)
Browse files Browse the repository at this point in the history
see: #49

---------

Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
  • Loading branch information
kwesterfeld2 and sapphi-red authored Feb 1, 2025
1 parent ac04097 commit 971574e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-eyes-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vitepress-plugin-tabs': minor
---

render all tabs when printing
5 changes: 4 additions & 1 deletion packages/vitepress-plugin-tabs/src/client/PluginTabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { useTabsSelectedState } from './useTabsSelectedState'
import { useUid } from './useUid'
import { useTabLabels } from './useTabLabels'
import { provideTabsSingleState } from './useTabsSingleState'
import { useIsPrint } from './useIsPrint'
const props = defineProps<{ sharedStateKey?: string }>()
const isPrint = useIsPrint()
const tabLabels = useTabLabels()
const { selected, select } = useTabsSelectedState(
Expand Down Expand Up @@ -59,7 +62,7 @@ provideTabsSingleState({ uid, selected })
:key="tabLabel"
role="tab"
class="plugin-tabs--tab"
:aria-selected="tabLabel === selected"
:aria-selected="tabLabel === selected && !isPrint"
:aria-controls="`panel-${tabLabel}-${uid}`"
:tabindex="tabLabel === selected ? 0 : -1"
@click="() => selectStable(tabLabel)"
Expand Down
10 changes: 9 additions & 1 deletion packages/vitepress-plugin-tabs/src/client/PluginTabsTab.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<script setup lang="ts">
import { useTabsSingleState } from './useTabsSingleState'
import { useIsPrint } from './useIsPrint'
defineProps<{ label: string }>()
const { uid, selected } = useTabsSingleState()
const isPrint = useIsPrint()
</script>

<template>
<div
v-if="selected === label"
v-if="selected === label || isPrint"
:id="`panel-${label}-${uid}`"
class="plugin-tabs--content"
role="tabpanel"
tabindex="0"
:aria-labelledby="`tab-${label}-${uid}`"
:data-is-print="isPrint"
>
<slot />
</div>
Expand All @@ -24,6 +28,10 @@ const { uid, selected } = useTabsSingleState()
padding: 16px;
}
.plugin-tabs--content[data-is-print='true']:not(:last-child) {
border-bottom: 2px solid var(--vp-plugin-tabs-tab-divider);
}
.plugin-tabs--content > :first-child:first-child {
margin-top: 0;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/vitepress-plugin-tabs/src/client/useIsPrint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { onBeforeMount, onUnmounted, ref } from 'vue'

export const useIsPrint = () => {
const matchMedia =
typeof window !== 'undefined' ? window.matchMedia('print') : undefined
const value = ref(matchMedia?.matches)

const listener = () => {
value.value = matchMedia?.matches
}
onBeforeMount(() => {
matchMedia?.addEventListener('change', listener)
})
onUnmounted(() => {
matchMedia?.removeEventListener('change', listener)
})

return value
}

0 comments on commit 971574e

Please sign in to comment.