mirror of
https://github.com/element-plus/element-plus.git
synced 2025-01-18 10:59:10 +08:00
feat(components): el-teleport (#4186)
This commit is contained in:
parent
ece91f0c25
commit
5a0ac482ce
40
packages/components/teleport/__tests__/teleport.spec.ts
Normal file
40
packages/components/teleport/__tests__/teleport.spec.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { mount } from '@vue/test-utils'
|
||||
import Teleport from '../src/teleport.vue'
|
||||
|
||||
const AXIOM = 'rem is the best girl'
|
||||
|
||||
describe('<el-teleport />', () => {
|
||||
let wrapper
|
||||
beforeEach(() => {
|
||||
wrapper = mount(Teleport, {
|
||||
slots: { default: () => AXIOM },
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('should render slot correctly', () => {
|
||||
expect(wrapper.text()).toBe('')
|
||||
expect(document.body.textContent).toBe(AXIOM)
|
||||
expect(wrapper.vm.containerRef).toBeDefined()
|
||||
})
|
||||
|
||||
describe('props', () => {
|
||||
it('should be able to set customized style', async () => {
|
||||
const style = {
|
||||
color: 'red',
|
||||
}
|
||||
|
||||
await wrapper.setProps({ style })
|
||||
expect(getComputedStyle(wrapper.vm?.containerRef).color).toBe(style.color)
|
||||
})
|
||||
|
||||
it('should be able to set z-index', async () => {
|
||||
const zIndex = '10000'
|
||||
await wrapper.setProps({ zIndex })
|
||||
expect(getComputedStyle(wrapper.vm?.containerRef).zIndex).toBe(zIndex)
|
||||
})
|
||||
})
|
||||
})
|
8
packages/components/teleport/index.ts
Normal file
8
packages/components/teleport/index.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { withInstall } from '@element-plus/utils/with-install'
|
||||
import Teleport from './src/teleport.vue'
|
||||
|
||||
export const ElTeleport = withInstall(Teleport)
|
||||
|
||||
export default ElTeleport
|
||||
|
||||
export * from './src/teleport'
|
19
packages/components/teleport/src/teleport.ts
Normal file
19
packages/components/teleport/src/teleport.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { buildProps, definePropType } from '@element-plus/utils/props'
|
||||
|
||||
import type { ExtractPropTypes, StyleValue } from 'vue'
|
||||
|
||||
export const elTeleportProps = buildProps({
|
||||
container: {
|
||||
type: definePropType<string | HTMLElement>([String, Object]),
|
||||
default: 'body',
|
||||
},
|
||||
style: {
|
||||
type: definePropType<StyleValue>([String, Array, Object]),
|
||||
},
|
||||
zIndex: {
|
||||
type: String,
|
||||
default: '2000',
|
||||
},
|
||||
})
|
||||
|
||||
export type ElTeleportProps = ExtractPropTypes<typeof elTeleportProps>
|
36
packages/components/teleport/src/teleport.vue
Normal file
36
packages/components/teleport/src/teleport.vue
Normal file
@ -0,0 +1,36 @@
|
||||
<template>
|
||||
<teleport v-if="container" :to="container">
|
||||
<div ref="containerRef" class="el-teleport" :style="containerStyle">
|
||||
<slot />
|
||||
</div>
|
||||
</teleport>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { computed, defineComponent, ref } from 'vue'
|
||||
import { elTeleportProps } from './teleport'
|
||||
|
||||
export default defineComponent({
|
||||
props: elTeleportProps,
|
||||
setup(props) {
|
||||
const containerRef = ref<HTMLElement>()
|
||||
const containerStyle = computed(() => {
|
||||
return props.container === 'body'
|
||||
? [
|
||||
props.style,
|
||||
{
|
||||
position: 'absolute',
|
||||
top: `0px`,
|
||||
left: `0px`,
|
||||
zIndex: props.zIndex,
|
||||
},
|
||||
]
|
||||
: {}
|
||||
})
|
||||
return {
|
||||
containerRef,
|
||||
containerStyle,
|
||||
}
|
||||
},
|
||||
})
|
||||
</script>
|
1
packages/components/teleport/style/css.ts
Normal file
1
packages/components/teleport/style/css.ts
Normal file
@ -0,0 +1 @@
|
||||
/* Empty file for avoiding plugin error */
|
1
packages/components/teleport/style/index.ts
Normal file
1
packages/components/teleport/style/index.ts
Normal file
@ -0,0 +1 @@
|
||||
/* Empty file for avoiding plugin error */
|
@ -134,8 +134,8 @@ export const usePopperProps = {
|
||||
}
|
||||
|
||||
export const usePopperHook = () => {
|
||||
const vm = getCurrentInstance()
|
||||
const props: ExtractPropTypes<typeof usePopperProps> = vm.props as any
|
||||
const vm = getCurrentInstance()!
|
||||
const props: ExtractPropTypes<typeof usePopperProps> = vm.proxy?.$props as any
|
||||
const { slots } = vm
|
||||
|
||||
const arrowRef = ref<RefElement>(null)
|
||||
|
Loading…
Reference in New Issue
Block a user