@opentiny/vue-hooks 3.20.0 → 3.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/README.zh-CN.md +3 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +24 -0
- package/{src → dist/src}/use-floating.d.ts +7 -2
- package/dist/src/use-floating.js +141 -0
- package/{src → dist/src}/use-lazy-show.d.ts +6 -3
- package/dist/src/use-lazy-show.js +10 -0
- package/dist/src/useEventListener.d.ts +15 -0
- package/dist/src/useEventListener.js +31 -0
- package/dist/src/useInstanceSlots.d.ts +6 -0
- package/dist/src/useInstanceSlots.js +14 -0
- package/dist/src/useRect.d.ts +1 -0
- package/dist/src/useRect.js +18 -0
- package/dist/src/useRelation.d.ts +31 -0
- package/dist/src/useRelation.js +54 -0
- package/dist/src/useTouch.d.ts +15 -0
- package/dist/src/useTouch.js +32 -0
- package/dist/src/useUserAgent.d.ts +3 -0
- package/dist/src/useUserAgent.js +10 -0
- package/dist/src/useWindowSize.d.ts +4 -0
- package/dist/src/useWindowSize.js +14 -0
- package/{index.d.ts → dist/src/vue-emitter.d.ts} +5 -3
- package/dist/src/vue-popper.js +85 -0
- package/dist/src/vue-popup.d.ts +18 -0
- package/dist/src/vue-popup.js +69 -0
- package/index.ts +23 -0
- package/package.json +15 -9
- package/src/use-floating.ts +409 -0
- package/src/use-lazy-show.ts +20 -0
- package/src/useEventListener.ts +65 -0
- package/src/useInstanceSlots.ts +29 -0
- package/src/useRect.ts +25 -0
- package/src/useRelation.ts +130 -0
- package/src/useTouch.ts +74 -0
- package/src/useUserAgent.ts +18 -0
- package/src/useWindowSize.ts +25 -0
- package/src/vue-emitter.ts +49 -0
- package/src/vue-popper.ts +277 -0
- package/src/vue-popup.ts +196 -0
- package/tsconfig.json +25 -0
- package/tsconfig.node.json +10 -0
- package/vite.config.ts +23 -0
- package/index.js +0 -2296
package/src/vue-popup.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2022 - present TinyVue Authors.
|
|
3
|
+
* Copyright (c) 2022 - present Huawei Cloud Computing Technologies Co., Ltd.
|
|
4
|
+
*
|
|
5
|
+
* Use of this source code is governed by an MIT-style license.
|
|
6
|
+
*
|
|
7
|
+
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
|
|
8
|
+
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
|
|
9
|
+
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { merge, PopupManager, addClass } from '@opentiny/utils'
|
|
14
|
+
|
|
15
|
+
// todo
|
|
16
|
+
import type { ISharedRenderlessFunctionParams } from 'types/shared.type'
|
|
17
|
+
import { isServer } from '@opentiny/utils'
|
|
18
|
+
|
|
19
|
+
let idSeed = 1
|
|
20
|
+
|
|
21
|
+
export interface IPopupState {
|
|
22
|
+
opened: boolean
|
|
23
|
+
rendered: boolean
|
|
24
|
+
}
|
|
25
|
+
type IPopupInputParams = ISharedRenderlessFunctionParams<never> & {
|
|
26
|
+
api: { open: Function; close: Function }
|
|
27
|
+
state: IPopupState
|
|
28
|
+
props: any
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const setWatchFn = ({
|
|
32
|
+
onMounted,
|
|
33
|
+
onBeforeUnmount,
|
|
34
|
+
watch,
|
|
35
|
+
vm,
|
|
36
|
+
api,
|
|
37
|
+
props,
|
|
38
|
+
state,
|
|
39
|
+
nextTick
|
|
40
|
+
}: Pick<
|
|
41
|
+
IPopupInputParams,
|
|
42
|
+
'onMounted' | 'onBeforeUnmount' | 'watch' | 'vm' | 'api' | 'props' | 'state' | 'nextTick'
|
|
43
|
+
>) => {
|
|
44
|
+
onMounted(() => {
|
|
45
|
+
vm._popupId = `popup-${idSeed++}`
|
|
46
|
+
PopupManager.register(vm._popupId, vm)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
onBeforeUnmount(() => {
|
|
50
|
+
PopupManager.deregister(vm._popupId)
|
|
51
|
+
PopupManager.closeModal(vm._popupId)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
watch(
|
|
55
|
+
() => props.visible,
|
|
56
|
+
(val) => {
|
|
57
|
+
if (val) {
|
|
58
|
+
if (vm._opening) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
if (state.rendered) {
|
|
62
|
+
api.open()
|
|
63
|
+
} else {
|
|
64
|
+
state.rendered = true
|
|
65
|
+
nextTick(() => {
|
|
66
|
+
api.open()
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
api.close()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const openFn =
|
|
77
|
+
({ state, vm }: Pick<IPopupInputParams, 'state' | 'vm'>) =>
|
|
78
|
+
(options: any) => {
|
|
79
|
+
if (!state.rendered) {
|
|
80
|
+
state.rendered = true
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const props: any = merge({}, vm.$props || vm, options)
|
|
84
|
+
|
|
85
|
+
if (vm._closeTimer) {
|
|
86
|
+
clearTimeout(vm._closeTimer)
|
|
87
|
+
vm._closeTimer = null
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
clearTimeout(vm._openTimer)
|
|
91
|
+
|
|
92
|
+
// 复用doOpen
|
|
93
|
+
const doOpen = () => {
|
|
94
|
+
if (isServer || state.opened) {
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
vm._opening = true
|
|
99
|
+
|
|
100
|
+
const dom = vm.$el
|
|
101
|
+
const modal = props.modal
|
|
102
|
+
const zIndex = props.zIndex
|
|
103
|
+
|
|
104
|
+
if (zIndex) {
|
|
105
|
+
PopupManager.zIndex = zIndex
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (modal) {
|
|
109
|
+
if (vm._closing) {
|
|
110
|
+
PopupManager.closeModal(vm._popupId)
|
|
111
|
+
vm._closing = false
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
PopupManager.openModal(
|
|
115
|
+
vm._popupId,
|
|
116
|
+
PopupManager.nextZIndex(),
|
|
117
|
+
props.modalAppendToBody ? undefined : dom,
|
|
118
|
+
props.modalClass,
|
|
119
|
+
props.modalFade
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
if (props.lockScroll) {
|
|
123
|
+
// 必须先计算宽度,再添加popLockClass。 下面2行不能交换
|
|
124
|
+
PopupManager.fixBodyBorder()
|
|
125
|
+
addClass(document.body, PopupManager.popLockClass)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (getComputedStyle(dom).position === 'static') {
|
|
130
|
+
dom.style.position = 'absolute'
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
dom.style.zIndex = PopupManager.nextZIndex().toString()
|
|
134
|
+
state.opened = true
|
|
135
|
+
|
|
136
|
+
vm._opening = false
|
|
137
|
+
}
|
|
138
|
+
const openDelay = Number(props.openDelay)
|
|
139
|
+
|
|
140
|
+
if (openDelay > 0) {
|
|
141
|
+
vm._openTimer = setTimeout(() => {
|
|
142
|
+
vm._openTimer = null
|
|
143
|
+
doOpen()
|
|
144
|
+
}, openDelay)
|
|
145
|
+
} else {
|
|
146
|
+
doOpen()
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const closeFn =
|
|
150
|
+
({ state, vm }: Pick<IPopupInputParams, 'state' | 'vm'>) =>
|
|
151
|
+
() => {
|
|
152
|
+
if (vm._openTimer !== null) {
|
|
153
|
+
clearTimeout(vm._openTimer)
|
|
154
|
+
vm._openTimer = null
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
clearTimeout(vm._closeTimer)
|
|
158
|
+
|
|
159
|
+
// 复用 doClose
|
|
160
|
+
const doClose = () => {
|
|
161
|
+
vm._closing = true
|
|
162
|
+
|
|
163
|
+
state.opened = false
|
|
164
|
+
PopupManager.closeModal(vm._popupId)
|
|
165
|
+
vm._closing = false
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const closeDelay = Number(vm.closeDelay)
|
|
169
|
+
|
|
170
|
+
if (closeDelay > 0) {
|
|
171
|
+
vm._closeTimer = setTimeout(() => {
|
|
172
|
+
vm._closeTimer = null
|
|
173
|
+
doClose()
|
|
174
|
+
}, closeDelay)
|
|
175
|
+
} else {
|
|
176
|
+
doClose()
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** vue-popup 只是dialog-box 自己使用的包, 封装了一些state和几个方法,处理mount,unmount 和watch。 它内部封装了 PopupManager 的调用!
|
|
181
|
+
* 计划:drawer/image 等组件均使用该函数
|
|
182
|
+
*/
|
|
183
|
+
export const usePopup = (options: IPopupInputParams) => {
|
|
184
|
+
const { api, nextTick, onBeforeUnmount, onMounted, props, reactive, toRefs, vm, watch } = options
|
|
185
|
+
const state = reactive<IPopupState>({
|
|
186
|
+
opened: false,
|
|
187
|
+
rendered: false
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
setWatchFn({ onMounted, onBeforeUnmount, watch, vm, api, props, state, nextTick })
|
|
191
|
+
|
|
192
|
+
const open = openFn({ state, vm })
|
|
193
|
+
const close = closeFn({ state, vm })
|
|
194
|
+
|
|
195
|
+
return { open, close, PopupManager, ...toRefs(state) }
|
|
196
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"useDefineForClassFields": true,
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
|
|
9
|
+
/* Bundler mode */
|
|
10
|
+
"moduleResolution": "bundler",
|
|
11
|
+
"allowImportingTsExtensions": true,
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"noEmit": true,
|
|
15
|
+
"jsx": "preserve",
|
|
16
|
+
|
|
17
|
+
/* Linting */
|
|
18
|
+
"strict": true,
|
|
19
|
+
"noUnusedLocals": true,
|
|
20
|
+
"noUnusedParameters": true,
|
|
21
|
+
"noFallthroughCasesInSwitch": true
|
|
22
|
+
},
|
|
23
|
+
"include": ["src/**/*.ts", "src/**/*.tsx", "index.ts"],
|
|
24
|
+
"references": [{ "path": "./tsconfig.node.json" }]
|
|
25
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
import dts from 'vite-plugin-dts'
|
|
3
|
+
|
|
4
|
+
// https://vitejs.dev/config/
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
plugins: [dts()],
|
|
7
|
+
build: {
|
|
8
|
+
lib: {
|
|
9
|
+
entry: ['index.ts']
|
|
10
|
+
},
|
|
11
|
+
rollupOptions: {
|
|
12
|
+
external: [/@opentiny/, /@floating-ui/],
|
|
13
|
+
input: ['index.ts'],
|
|
14
|
+
output: [
|
|
15
|
+
{
|
|
16
|
+
format: 'es',
|
|
17
|
+
entryFileNames: '[name].js',
|
|
18
|
+
preserveModules: true
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
})
|