Skip to content

Commit

Permalink
fix(comp:drawer): translate transition error when container is set (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sallerli1 committed Aug 11, 2023
1 parent e7895a1 commit a4dc760
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
26 changes: 14 additions & 12 deletions packages/components/drawer/__tests__/drawer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MountingOptions, VueWrapper, flushPromises, mount } from '@vue/test-utils'
import { h } from 'vue'
import { h, nextTick } from 'vue'

import { isElementVisible, renderWork } from '@tests'

Expand Down Expand Up @@ -40,6 +40,8 @@ describe('Drawer', () => {
expect(isElementVisible(document.querySelector('.ix-drawer-wrapper'))).toBe(false)

await wrapper.setProps({ visible: true })
await nextTick()
await flushPromises()

const drawerWrapper = wrapper.getComponent(DrawerWrapper)

Expand Down Expand Up @@ -195,17 +197,17 @@ describe('Drawer', () => {
test('height work', async () => {
const wrapper = DrawerMount({ props: { height: 400 } })
const drawerWrapper = wrapper.getComponent(DrawerWrapper)
const contentDom = drawerWrapper.find('.ix-drawer').element as HTMLElement
const getDom = () => drawerWrapper.find('.ix-drawer').element as HTMLElement

expect(contentDom.style.height).toBe('400px')
expect(getDom().style.height).toBe('400px')

await wrapper.setProps({ height: '200px' })

expect(contentDom.style.height).toBe('200px')
expect(getDom().style.height).toBe('200px')

await wrapper.setProps({ height: '20%' })

expect(contentDom.style.height).toBe('20%')
expect(getDom().style.height).toBe('20%')
})

test('mask work', async () => {
Expand Down Expand Up @@ -237,13 +239,13 @@ describe('Drawer', () => {
const wrapper = DrawerMount({ props: { offset: 256 } })
const drawerWrapper = wrapper.getComponent(DrawerWrapper)

const contentDom = drawerWrapper.find('.ix-drawer').element as HTMLElement
const getDom = () => drawerWrapper.find('.ix-drawer').element as HTMLElement

expect(contentDom.style.top).toBe('256px')
expect(getDom().style.top).toBe('256px')

await wrapper.setProps({ offset: '30%' })

expect(contentDom.style.top).toBe('30%')
expect(getDom().style.top).toBe('30%')
})

test('placement work', async () => {
Expand Down Expand Up @@ -274,17 +276,17 @@ describe('Drawer', () => {
test('width work', async () => {
const wrapper = DrawerMount({ props: { width: 400 } })
const drawerWrapper = wrapper.getComponent(DrawerWrapper)
const contentDom = drawerWrapper.find('.ix-drawer').element as HTMLElement
const getDom = () => drawerWrapper.find('.ix-drawer').element as HTMLElement

expect(contentDom.style.width).toBe('400px')
expect(getDom().style.width).toBe('400px')

await wrapper.setProps({ width: '200px' })

expect(contentDom.style.width).toBe('200px')
expect(getDom().style.width).toBe('200px')

await wrapper.setProps({ width: '20%' })

expect(contentDom.style.width).toBe('20%')
expect(getDom().style.width).toBe('20%')
})

test('zIndex work', async () => {
Expand Down
30 changes: 27 additions & 3 deletions packages/components/drawer/src/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
computed,
defineComponent,
inject,
nextTick,
onBeforeUnmount,
onDeactivated,
onMounted,
Expand Down Expand Up @@ -43,7 +44,7 @@ export default defineComponent({

const mask = computed(() => props.mask ?? config.mask)

const { visible, setVisible, animatedVisible, mergedVisible } = useVisible(props)
const { loaded, visible, setVisible, animatedVisible, mergedVisible } = useVisible(props)
const currentZIndex = useZIndex(toRef(props, 'zIndex'), toRef(common, 'overlayZIndex'), visible)

const { open, close } = useTrigger(props, setVisible)
Expand Down Expand Up @@ -76,7 +77,7 @@ export default defineComponent({
return null
}
return (
<CdkPortal target={mergedPortalTarget.value} load={visible.value}>
<CdkPortal target={mergedPortalTarget.value} load={loaded.value}>
<ɵMask
class={`${mergedPrefixCls.value}-mask`}
mask={mask.value}
Expand All @@ -92,6 +93,29 @@ export default defineComponent({

function useVisible(props: DrawerProps) {
const [visible, setVisible] = useControlledProp(props, 'visible', false)

// because portal is lazy loaded, actual visible at the first time should be delayed
// or else transition animation will behave unexpectedly
const loaded = ref<boolean>(false)
const delayedVisible = ref<boolean>(false)
watch(
visible,
v => {
if (v && !loaded.value) {
loaded.value = true

nextTick(() => {
delayedVisible.value = true
})
} else {
delayedVisible.value = v
}
},
{
immediate: true,
},
)

const animatedVisible = ref<boolean>()

const mergedVisible = computed(() => {
Expand All @@ -109,7 +133,7 @@ function useVisible(props: DrawerProps) {
}
})

return { visible, setVisible, animatedVisible, mergedVisible }
return { loaded, visible: delayedVisible, setVisible, animatedVisible, mergedVisible }
}

function useScrollStrategy(props: DrawerProps, mask: ComputedRef<boolean>, mergedVisible: ComputedRef<boolean>) {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/drawer/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface DrawerContext {
common: CommonConfig
config: DrawerConfig
mergedPrefixCls: ComputedRef<string>
visible: ComputedRef<boolean>
visible: Ref<boolean>
animatedVisible: Ref<boolean | undefined>
mergedVisible: ComputedRef<boolean>
currentZIndex: ComputedRef<number>
Expand Down
15 changes: 8 additions & 7 deletions packages/components/drawer/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
}
}

&-opened&-push {
transition: transform var(--ix-transition-duration-medium) var(--ix-ease-out);
transition-delay: var(--ix-transition-duration-fast);
}

&-opened&-pull {
transition: transform var(--ix-transition-duration-medium) var(--ix-ease-in);
&-opened {
&.@{drawer-prefix}-push {
transition: transform var(--ix-transition-duration-medium) var(--ix-ease-out);
transition-delay: var(--ix-transition-duration-fast);
}
&.@{drawer-prefix}-pull {
transition: transform var(--ix-transition-duration-medium) var(--ix-ease-in);
}
}

&-sentinel {
Expand Down

0 comments on commit a4dc760

Please sign in to comment.