Skip to content

Commit

Permalink
fix: forceMount on scrollArea
Browse files Browse the repository at this point in the history
  • Loading branch information
zernonia committed Oct 20, 2023
1 parent 1e382f0 commit 48b2757
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 30 deletions.
3 changes: 3 additions & 0 deletions packages/radix-vue/src/Presence/Presence.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ function render() {
return h(slots.default?.()[0] as VNode, {
ref: (v) => {
const el = unrefElement(v as HTMLElement)
if (typeof el?.hasAttribute === 'undefined')
return el
// special case to handle animation for PopperContent
if (el?.hasAttribute('data-radix-popper-content-wrapper'))
node.value = el.firstChild as HTMLElement
Expand Down
1 change: 0 additions & 1 deletion packages/radix-vue/src/ScrollArea/ScrollAreaScrollbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ provideScrollAreaScrollbarContext({
v-else-if="rootContext.type.value === 'always'"
v-bind="$attrs"
data-state="visible"
:force-mount="forceMount"
>
<slot />
</ScrollAreaScrollbarVisible>
Expand Down
25 changes: 18 additions & 7 deletions packages/radix-vue/src/ScrollArea/ScrollAreaScrollbarAuto.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ import { useDebounceFn, useResizeObserver } from '@vueuse/core'
import { injectScrollAreaRootContext } from './ScrollAreaRoot.vue'
import { injectScrollAreaScrollbarContext } from './ScrollAreaScrollbar.vue'
import ScrollAreaScrollbarVisible from './ScrollAreaScrollbarVisible.vue'
import { Presence } from '@/Presence'
import { useForwardRef } from '@/shared'
export interface ScrollAreaScrollbarAutoProps {
forceMount?: boolean
}
defineProps<ScrollAreaScrollbarAutoProps>()
const rootContext = injectScrollAreaRootContext()
const scrollbarContext = injectScrollAreaScrollbarContext()
const forwardRef = useForwardRef()
const visible = ref(false)
const handleResize = useDebounceFn(() => {
Expand All @@ -32,11 +41,13 @@ useResizeObserver(rootContext.content, handleResize)
</script>

<template>
<ScrollAreaScrollbarVisible
v-if="visible"
v-bind="$attrs"
:data-state="visible ? 'visible' : 'hidden'"
>
<slot />
</ScrollAreaScrollbarVisible>
<Presence :present="forceMount || visible">
<ScrollAreaScrollbarVisible
v-bind="$attrs"
ref="forwardRef"
:data-state="visible ? 'visible' : 'hidden'"
>
<slot />
</ScrollAreaScrollbarVisible>
</Presence>
</template>
25 changes: 17 additions & 8 deletions packages/radix-vue/src/ScrollArea/ScrollAreaScrollbarHover.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import { injectScrollAreaRootContext } from './ScrollAreaRoot.vue'
import ScrollAreaScrollbarAuto from './ScrollAreaScrollbarAuto.vue'
import ScrollAreaScrollbarAuto, { type ScrollAreaScrollbarAutoProps } from './ScrollAreaScrollbarAuto.vue'
import { Presence } from '@/Presence'
import { useForwardRef } from '@/shared'
export interface ScrollAreaScrollbarHoverProps extends ScrollAreaScrollbarAutoProps {}
defineProps<ScrollAreaScrollbarHoverProps>()
const rootContext = injectScrollAreaRootContext()
const forwardRef = useForwardRef()
let timeout: ReturnType<typeof setTimeout> | undefined | number
const visible = ref(false)
Expand Down Expand Up @@ -44,11 +51,13 @@ export default {
</script>

<template>
<ScrollAreaScrollbarAuto
v-if="visible"
v-bind="$attrs"
:data-state="visible ? 'visible' : 'hidden'"
>
<slot />
</ScrollAreaScrollbarAuto>
<Presence :present="forceMount || visible">
<ScrollAreaScrollbarAuto
v-bind="$attrs"
ref="forwardRef"
:data-state="visible ? 'visible' : 'hidden'"
>
<slot />
</ScrollAreaScrollbarAuto>
</Presence>
</template>
9 changes: 7 additions & 2 deletions packages/radix-vue/src/ScrollArea/ScrollAreaScrollbarImpl.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import { type ComponentPublicInstance, onMounted, onUnmounted, ref } from 'vue'
import { useResizeObserver } from '@vueuse/core'
import { injectScrollAreaRootContext } from './ScrollAreaRoot.vue'
import { injectScrollAreaScrollbarVisibleContext } from './ScrollAreaScrollbarVisible.vue'
import { injectScrollAreaScrollbarContext } from './ScrollAreaScrollbar.vue'
import { toInt } from './utils'
import { Primitive, usePrimitiveElement } from '@/Primitive'
import { useForwardRef } from '@/shared'
const props = defineProps<ScrollAreaScrollbarImplProps>()
const emit = defineEmits<{
Expand All @@ -22,6 +23,7 @@ export interface ScrollAreaScrollbarImplProps {
}
const { primitiveElement, currentElement: scrollbar } = usePrimitiveElement()
const forwardRef = useForwardRef()
const prevWebkitUserSelectRef = ref('')
const rectRef = ref<DOMRect>()
Expand Down Expand Up @@ -117,7 +119,10 @@ useResizeObserver(rootContext.content, handleSizeChange)

<template>
<Primitive
ref="primitiveElement"
:ref="vnode => {
forwardRef(vnode)
primitiveElement = vnode as ComponentPublicInstance
}"
style="position: absolute"
data-scrollbarimpl
:as="scrollbarContext.as.value"
Expand Down
17 changes: 14 additions & 3 deletions packages/radix-vue/src/ScrollArea/ScrollAreaScrollbarScroll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import { useStateMachine } from '../shared/useStateMachine'
import { injectScrollAreaRootContext } from './ScrollAreaRoot.vue'
import { injectScrollAreaScrollbarContext } from './ScrollAreaScrollbar.vue'
import ScrollAreaScrollbarVisible from './ScrollAreaScrollbarVisible.vue'
import { Presence } from '@/Presence'
import { useForwardRef } from '@/shared'
export interface ScrollAreaScrollbarScrollProps {
forceMount?: boolean
}
defineProps<ScrollAreaScrollbarScrollProps>()
const rootContext = injectScrollAreaRootContext()
const scrollbarContext = injectScrollAreaScrollbarContext()
const forwardRef = useForwardRef()
const { state, dispatch } = useStateMachine('hidden', {
hidden: {
SCROLL: 'scrolling',
Expand Down Expand Up @@ -62,7 +71,9 @@ watchEffect(() => {
</script>

<template>
<ScrollAreaScrollbarVisible v-if="state !== 'hidden'" v-bind="$attrs">
<slot />
</ScrollAreaScrollbarVisible>
<Presence :present="forceMount || state !== 'hidden'">
<ScrollAreaScrollbarVisible v-bind="$attrs" ref="forwardRef">
<slot />
</ScrollAreaScrollbarVisible>
</Presence>
</template>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { createContext } from '@/shared'
import { createContext, useForwardRef } from '@/shared'
export interface ScrollAreaScrollbarVisibleContext {
sizes: Ref<Sizes>
Expand Down Expand Up @@ -40,6 +40,7 @@ import {
const rootContext = injectScrollAreaRootContext()
const scrollbarContext = injectScrollAreaScrollbarContext()
const forwardRef = useForwardRef()
const sizes = ref<Sizes>({
content: 0,
Expand Down Expand Up @@ -150,10 +151,10 @@ provideScrollAreaScrollbarVisibleContext({
</script>

<template>
<ScrollAreaScrollbarX v-if="isShowingScrollbarX" v-bind="$attrs">
<ScrollAreaScrollbarX v-if="isShowingScrollbarX" v-bind="$attrs" ref="forwardRef">
<slot />
</ScrollAreaScrollbarX>
<ScrollAreaScrollbarY v-else v-bind="$attrs">
<ScrollAreaScrollbarY v-else v-bind="$attrs" ref="forwardRef">
<slot />
</ScrollAreaScrollbarY>
</template>
9 changes: 7 additions & 2 deletions packages/radix-vue/src/ScrollArea/ScrollAreaScrollbarX.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { type ComponentPublicInstance, computed, onMounted } from 'vue'
import { injectScrollAreaScrollbarVisibleContext } from './ScrollAreaScrollbarVisible.vue'
import { injectScrollAreaRootContext } from './ScrollAreaRoot.vue'
import ScrollAreaScrollbarImpl from './ScrollAreaScrollbarImpl.vue'
import { getThumbSize } from './utils'
import { usePrimitiveElement } from '@/Primitive'
import { useForwardRef } from '@/shared'
const rootContext = injectScrollAreaRootContext()
const scrollbarVisibleContext = injectScrollAreaScrollbarVisibleContext()
const { primitiveElement, currentElement: scrollbarElement }
= usePrimitiveElement()
const forwardRef = useForwardRef()
onMounted(() => {
if (scrollbarElement.value)
Expand All @@ -21,7 +23,10 @@ const sizes = computed(() => scrollbarVisibleContext.sizes.value)

<template>
<ScrollAreaScrollbarImpl
ref="primitiveElement"
:ref="vnode => {
forwardRef(vnode)
primitiveElement = vnode as ComponentPublicInstance
}"
:is-horizontal="true"
data-orientation="horizontal"
:style="{
Expand Down
9 changes: 7 additions & 2 deletions packages/radix-vue/src/ScrollArea/ScrollAreaScrollbarY.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { type ComponentPublicInstance, computed, onMounted } from 'vue'
import { injectScrollAreaScrollbarVisibleContext } from './ScrollAreaScrollbarVisible.vue'
import { injectScrollAreaRootContext } from './ScrollAreaRoot.vue'
import ScrollAreaScrollbarImpl from './ScrollAreaScrollbarImpl.vue'
import { getThumbSize } from './utils'
import { usePrimitiveElement } from '@/Primitive'
import { useForwardRef } from '@/shared'
const rootContext = injectScrollAreaRootContext()
const scrollbarVisibleContext = injectScrollAreaScrollbarVisibleContext()
const { primitiveElement, currentElement: scrollbarElement }
= usePrimitiveElement()
const forwardRef = useForwardRef()
onMounted(() => {
if (scrollbarElement.value)
Expand All @@ -22,7 +24,10 @@ const sizes = computed(() => scrollbarVisibleContext.sizes.value)

<template>
<ScrollAreaScrollbarImpl
ref="primitiveElement"
:ref="vnode => {
forwardRef(vnode)
primitiveElement = vnode as ComponentPublicInstance
}"
:is-horizontal="false"
data-orientation="vertical"
:style="{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ exports[`given default ScrollArea > should render content, but not scrollbar 1`]
<p class=\\"w-[4000px]\\" style=\\"width: 50px;\\"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sit amet eros iaculis, bibendum tellus ac, lobortis odio. Aliquam bibendum elit est, in iaculis est commodo id. Donec pulvinar est libero. Proin consectetur pellentesque molestie. Fusce mi ante, ullamcorper eu ante finibus, finibus pellentesque turpis. Mauris convallis, leo in vulputate varius, sapien lectus suscipit eros, ac semper odio sapien sit amet magna. Sed mattis turpis et lacinia ultrices. Nulla a commodo mauris. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque id tempor metus. Pellentesque faucibus tortor non nisi maximus dignissim. Etiam leo nisi, molestie a porttitor at, euismod a libero. Nullam placerat tristique enim nec pulvinar. Sed eleifend dictum nulla a aliquam. Sed tempus ipsum eget urna posuere aliquam. Nulla maximus tortor dui, sed laoreet odio aliquet ac. Vestibulum dolor orci, lacinia finibus vehicula eget, posuere ac lectus. Quisque non felis at ipsum scelerisque condimentum. In pharetra semper arcu, ut hendrerit sem auctor vel. Aliquam non lacinia elit, a facilisis ante. Praesent eget eros augue. Praesent nunc orci, ullamcorper non pulvinar eu, elementum id nibh. Nam id lorem euismod, sodales augue quis, porttitor magna. Vivamus ut nisl velit. Nam ultrices maximus felis, quis ullamcorper quam luctus et. </p>
</div>
</div>
<!--v-if-->
<!---->
<!--v-if-->
</div>"
`;
Expand Down Expand Up @@ -126,7 +126,7 @@ exports[`given prop:type="scroll" ScrollArea > should render content and scrollb
<p class=\\"w-[4000px]\\" style=\\"width: 50px;\\"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sit amet eros iaculis, bibendum tellus ac, lobortis odio. Aliquam bibendum elit est, in iaculis est commodo id. Donec pulvinar est libero. Proin consectetur pellentesque molestie. Fusce mi ante, ullamcorper eu ante finibus, finibus pellentesque turpis. Mauris convallis, leo in vulputate varius, sapien lectus suscipit eros, ac semper odio sapien sit amet magna. Sed mattis turpis et lacinia ultrices. Nulla a commodo mauris. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque id tempor metus. Pellentesque faucibus tortor non nisi maximus dignissim. Etiam leo nisi, molestie a porttitor at, euismod a libero. Nullam placerat tristique enim nec pulvinar. Sed eleifend dictum nulla a aliquam. Sed tempus ipsum eget urna posuere aliquam. Nulla maximus tortor dui, sed laoreet odio aliquet ac. Vestibulum dolor orci, lacinia finibus vehicula eget, posuere ac lectus. Quisque non felis at ipsum scelerisque condimentum. In pharetra semper arcu, ut hendrerit sem auctor vel. Aliquam non lacinia elit, a facilisis ante. Praesent eget eros augue. Praesent nunc orci, ullamcorper non pulvinar eu, elementum id nibh. Nam id lorem euismod, sodales augue quis, porttitor magna. Vivamus ut nisl velit. Nam ultrices maximus felis, quis ullamcorper quam luctus et. </p>
</div>
</div>
<!--v-if-->
<!---->
<!--v-if-->
</div>"
`;

0 comments on commit 48b2757

Please sign in to comment.