From 45982cff8a65a57928e21598e2234b3ecdb41be0 Mon Sep 17 00:00:00 2001 From: jeremywu <15975785+JeremyWuuuuu@users.noreply.github.com> Date: Wed, 23 Jun 2021 14:35:42 +0800 Subject: [PATCH] refactor(popper): refactor extract common props for props (#2300) - Extract some common props out of prop Co-authored-by: jeremywuuuuu --- packages/constants/index.ts | 2 + packages/constants/package.json | 13 ++++++ packages/constants/popper.constants.ts | 6 +++ packages/internal/package.json | 13 ++++++ .../props/use-popper-props.ts} | 45 +++++-------------- packages/popover/src/index.vue | 8 ++-- packages/popover/src/usePopover.ts | 7 +-- packages/popper/index.ts | 2 - packages/popper/src/index.vue | 2 +- packages/popper/src/use-popper/index.ts | 6 +-- .../popper/src/use-popper/popper-options.ts | 13 +----- packages/tooltip/src/index.ts | 2 +- packages/types/index.ts | 1 + packages/types/package.json | 13 ++++++ 14 files changed, 74 insertions(+), 59 deletions(-) create mode 100644 packages/constants/index.ts create mode 100644 packages/constants/package.json create mode 100644 packages/constants/popper.constants.ts create mode 100644 packages/internal/package.json rename packages/{popper/src/use-popper/defaults.ts => internal/props/use-popper-props.ts} (76%) create mode 100644 packages/types/index.ts create mode 100644 packages/types/package.json diff --git a/packages/constants/index.ts b/packages/constants/index.ts new file mode 100644 index 0000000000..bfeb7f3a54 --- /dev/null +++ b/packages/constants/index.ts @@ -0,0 +1,2 @@ +export * from './popper.constants' + diff --git a/packages/constants/package.json b/packages/constants/package.json new file mode 100644 index 0000000000..87476f2fab --- /dev/null +++ b/packages/constants/package.json @@ -0,0 +1,13 @@ +{ + "name": "@element-plus/constants", + "description": "Stores all commonly used constants", + "version": "0.0.0", + "license": "MIT", + "peerDependencies": { + "vue": "3.1.1" + }, + "devDependencies": { + "@vue/test-utils": "^2.0.0-beta.3" + } + } + \ No newline at end of file diff --git a/packages/constants/popper.constants.ts b/packages/constants/popper.constants.ts new file mode 100644 index 0000000000..ac318fcc5a --- /dev/null +++ b/packages/constants/popper.constants.ts @@ -0,0 +1,6 @@ +export const DARK_EFFECT = 'dark' +export const LIGHT_EFFECT = 'light' + +export const DEFAULT_TRIGGER = 'hover' +export const DEFAULT_FALLBACK_PLACEMENTS = [] + diff --git a/packages/internal/package.json b/packages/internal/package.json new file mode 100644 index 0000000000..543425f289 --- /dev/null +++ b/packages/internal/package.json @@ -0,0 +1,13 @@ +{ + "name": "@element-plus/internal", + "description": "Stores all internal objects", + "version": "0.0.0", + "license": "MIT", + "peerDependencies": { + "vue": "3.1.1" + }, + "devDependencies": { + "@vue/test-utils": "^2.0.0-beta.3" + } + } + \ No newline at end of file diff --git a/packages/popper/src/use-popper/defaults.ts b/packages/internal/props/use-popper-props.ts similarity index 76% rename from packages/popper/src/use-popper/defaults.ts rename to packages/internal/props/use-popper-props.ts index c494412262..877e9153cd 100644 --- a/packages/popper/src/use-popper/defaults.ts +++ b/packages/internal/props/use-popper-props.ts @@ -1,12 +1,11 @@ -import type { PropType } from 'vue' + +import { DEFAULT_TRIGGER, DEFAULT_FALLBACK_PLACEMENTS } from '@element-plus/constants/popper.constants' + +import type { PropType, ExtractPropTypes } from 'vue' import type { Placement, PositioningStrategy, Instance as PopperInstance, Options } from '@popperjs/core' -export enum Effect { - DARK = 'dark', - LIGHT = 'light' -} +export type Effect = 'light' | 'dark' -export type RefElement = Nullable export type Offset = [number, number] | number export type { Placement, PositioningStrategy, PopperInstance, Options } @@ -15,33 +14,7 @@ export type TriggerType = 'click' | 'hover' | 'focus' | 'manual' export type Trigger = TriggerType | TriggerType[] -export type IPopperOptions = { - arrowOffset: number - autoClose: number - boundariesPadding: number - class: string - cutoff: boolean - disabled: boolean - enterable: boolean - hideAfter: number - manualMode: boolean - offset: number - placement: Placement - popperOptions: Options - showAfter: number - showArrow: boolean - strategy: PositioningStrategy - trigger: Trigger - visible: boolean - stopPopperMouseEvent: boolean - gpuAcceleration: boolean - fallbackPlacements: Array -} - -export const DEFAULT_TRIGGER = 'hover' -const DEFAULT_FALLBACK_PLACEMENTS = [] - -export default { +const defaultProps = { // the arrow size is an equailateral triangle with 10px side length, the 3rd side length ~ 14.1px // adding a offset to the ceil of 4.1 should be 5 this resolves the problem of arrow overflowing out of popper. arrowOffset: { @@ -83,7 +56,7 @@ export default { }, effect: { type: String as PropType, - default: Effect.DARK, + default: 'dark', }, enterable: { type: Boolean, @@ -151,3 +124,7 @@ export default { default: DEFAULT_FALLBACK_PLACEMENTS, }, } + +export type IPopperOptions = ExtractPropTypes + +export default defaultProps diff --git a/packages/popover/src/index.vue b/packages/popover/src/index.vue index 709679f59d..513f1aa571 100644 --- a/packages/popover/src/index.vue +++ b/packages/popover/src/index.vue @@ -1,15 +1,17 @@