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

Conversation

openscript
Copy link
Contributor

Adds a IsVisible component, which shows it's children only when the component is in the users viewport.

This is especially useful for iframe, for example a youtube player, which should be unloaded when you leave a slide

@John98Zakaria
Copy link
Contributor

slidev already includes vue-use which includes the same concept

https://vueuse.org/core/useElementVisibility/

@openscript
Copy link
Contributor Author

How to use vue-use inside markdown without having a component?

@antfu
Copy link
Member

antfu commented Feb 2, 2024

Instead of a new component, maybe we could extend
https://github.com/slidevjs/slidev/blob/aa0d7141c53d7baf1da1911318f3ffc0de4c2f58/packages/client/builtin/RenderWhen.vue

to have <RenderWhen context="visible">...</RenderWhen>?

@openscript
Copy link
Contributor Author

I've been thinking about this too, but <RenderWhen /> is related to Slidevs context. For this component we don't depend on the context and also need another template. The component <RenderWhen /> would become multi purpose or non-atomic. What do you think @antfu? <RenderWhen /> or separate one?

@antfu
Copy link
Member

antfu commented Feb 2, 2024

I am okay with it being multi-purpose. And the name doesn't limit it to being context-specific. I think having fewer components would help in learning and discovering.

@openscript
Copy link
Contributor Author

Alright, I'll change the PR and also use vue-use as suggested by @John98Zakaria.

@openscript openscript changed the title feat: add IsVisible component feat: add visible to RenderWhen component Feb 3, 2024
@John98Zakaria
Copy link
Contributor

@openscript Sorry for talking so long

You can pnpm install @vueuse/core @vueuse/components to you project.

I think the secret lies in the fact that slidev slides are just vue components :)


---


Slide 1


---

<div v-element-visibility="onElementVisibility">
Slide 2
</div>

{{someData}}


<script setup lang="ts">
import {Ref, ref} from 'vue'
import {vElementVisibility} from '@vueuse/components'



const image: Ref<HTMLElement> = ref();

const someData = "someDatafromScript"

const clicks = $clicks;

function onElementVisibility(visible: boolean) {
    console.log(visible)
    console.log("data")
}


</script>

@openscript
Copy link
Contributor Author

Thank you @John98Zakaria for explaining. I think to integrate it into the <RenderWhen /> component is a more handy and clean solution.

@antfu antfu merged commit fc6769f into slidevjs:main Feb 6, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants