Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add visible to RenderWhen component #1260

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions packages/client/builtin/RenderWhen.vue
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
<script setup lang="ts">
import { computed, inject } from 'vue'
import type { RenderContext } from '@slidev/types'

import { computed, inject, ref } from 'vue'
import { useElementVisibility } from '@vueuse/core'
import { injectionRenderContext } from '../constants'

type Context = 'main' | RenderContext
type Context = 'main' | 'visible' | RenderContext

const props = defineProps<{
context: Context | Context[]
}>()
const { context } = props
const target = ref(null)
const targetVisible = useElementVisibility(target)

const currentContext = inject(injectionRenderContext)
// When context has `visible`, we need to wrap the content with a div to track the visibility
const needsDomWrapper = Array.isArray(context) ? context.includes('visible') : context === 'visible'

const shouldRender = computed(() => Array.isArray(context) ? context.some(contextMatch) : contextMatch(context))
const currentContext = inject(injectionRenderContext)
const shouldRender = computed(() => {
const anyContext = Array.isArray(context) ? context.some(contextMatch) : contextMatch(context)
const allConditions = Array.isArray(context) ? context.every(conditionsMatch) : conditionsMatch(context)
return anyContext && allConditions
})

function contextMatch(context: Context) {
if (context === currentContext?.value)
return true
if (context === 'main' && (currentContext?.value === 'slide' || currentContext?.value === 'presenter'))
return true
if (context === 'visible')
return true
return false
}

function conditionsMatch(context: Context) {
if (context === 'visible')
return targetVisible.value
return true
}
</script>

<template>
<slot v-if="shouldRender" />
<div v-if="needsDomWrapper" ref="target">
<slot v-if="shouldRender" />
</div>
<slot v-else-if="shouldRender" />
</template>
Loading