-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a39cd2
commit 332aa6a
Showing
4 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<script lang="ts" setup> | ||
import { useToggle } from '@vueuse/core' | ||
const props = defineProps<{ | ||
maxHeightInRem: number | ||
}>() | ||
const [isExpanded, toggleExpanded] = useToggle() | ||
const contentRef = ref<HTMLElement | null>(null) | ||
function convertRemToPixels(rem: number): number { | ||
return rem * parseFloat(getComputedStyle(document.documentElement).fontSize) | ||
} | ||
const isContentTallerThanMaxHeight = computed(() => { | ||
if (!contentRef.value) { | ||
return false | ||
} | ||
return contentRef.value.scrollHeight > convertRemToPixels(props.maxHeightInRem) | ||
}) | ||
const shouldBeExpanded = computed(() => { | ||
return isContentTallerThanMaxHeight.value && !isExpanded.value | ||
}) | ||
</script> | ||
|
||
<template> | ||
<!-- TODO: Animate max height --> | ||
<div | ||
:style="{ maxHeight: shouldBeExpanded ? `${props.maxHeightInRem}rem` : 'none' }" | ||
class="relative overflow-y-hidden" | ||
> | ||
<!-- Overlay--> | ||
<div | ||
v-if="shouldBeExpanded" | ||
class="pointer-events-none absolute inset-0 z-10 bg-gradient-to-t from-base-1000 via-transparent" | ||
/> | ||
|
||
<!-- Content --> | ||
<div ref="contentRef"> | ||
<slot /> | ||
</div> | ||
</div> | ||
|
||
<!-- Show more/less button --> | ||
<div | ||
v-if="isContentTallerThanMaxHeight" | ||
class="flex justify-center pt-4" | ||
> | ||
<button | ||
class="focus-visible:focus-outline-util hover:hover-bg-util hover:hover-text-util rounded-md bg-base-1000/60 px-2 py-1 text-sm ring-1 ring-base-0/20" | ||
type="button" | ||
@click="toggleExpanded()" | ||
> | ||
{{ isExpanded ? 'Show less' : 'Show more' }} | ||
</button> | ||
</div> | ||
</template> |
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