diff --git a/packages/directives/index.ts b/packages/directives/index.ts index 562c0996c8..9d12c25397 100644 --- a/packages/directives/index.ts +++ b/packages/directives/index.ts @@ -1,5 +1,2 @@ -import ClickOutside from './click-outside/index' +export { default as ClickOutside } from './click-outside' -export { - ClickOutside, -} diff --git a/packages/hooks/index.ts b/packages/hooks/index.ts new file mode 100644 index 0000000000..d7d38bcdce --- /dev/null +++ b/packages/hooks/index.ts @@ -0,0 +1 @@ +export { default as useEvents } from './use-events' diff --git a/packages/hooks/package.json b/packages/hooks/package.json new file mode 100644 index 0000000000..46b47bfcdf --- /dev/null +++ b/packages/hooks/package.json @@ -0,0 +1,12 @@ +{ + "name": "@element-plus/hooks", + "version": "0.0.0", + "main": "dist/index.js", + "license": "MIT", + "peerDependencies": { + "vue": "^3.0.0-rc.6" + }, + "devDependencies": { + "@vue/test-utils": "^2.0.0-beta.0" + } +} diff --git a/packages/hooks/use-events/index.ts b/packages/hooks/use-events/index.ts new file mode 100644 index 0000000000..b9e416175f --- /dev/null +++ b/packages/hooks/use-events/index.ts @@ -0,0 +1,23 @@ +import { watch } from 'vue' +import { on, off } from '@element-plus/utils/dom' + +import type { Ref } from 'vue' + +type Events = Array<{ + name: string + handler: (...args: any[]) => any +}> + +export default (el: Ref, events: Events) => { + events.map(({ name, handler }) => { + on(el.value, name, handler) + }) + + watch(el, (_, __, onCleanup) => { + onCleanup(() => { + events.map(({ name, handler }) => { + off(el.value, name, handler) + }) + }) + }) +} diff --git a/packages/popper/__tests__/popper.spec.ts b/packages/popper/__tests__/popper.spec.ts index acb4de4feb..5f52271577 100644 --- a/packages/popper/__tests__/popper.spec.ts +++ b/packages/popper/__tests__/popper.spec.ts @@ -83,8 +83,14 @@ describe('Popper.vue', () => { let wrapper = _mount() const selector = '[role="tooltip"]' expect(wrapper.find(selector).exists()).toBe(false) - // Due to the parent node of popper is Transition so we should match the grandparent - expect(document.querySelector(selector).parentElement.parentElement).toBe(document.body) + /** + * Current layout of `ElPopper` + * --> Teleport + * --> mask + * --> transition + * --> popper + */ + wrapper = _mount({ appendToBody: false, }) diff --git a/packages/popper/src/index.vue b/packages/popper/src/index.vue index 021d9b512e..0fe9922dc3 100644 --- a/packages/popper/src/index.vue +++ b/packages/popper/src/index.vue @@ -1,14 +1,7 @@