Skip to content
This repository was archived by the owner on Oct 27, 2021. It is now read-only.

Commit 9b85868

Browse files
author
pengshanglong
committed
feat: add FloatLayout
1 parent 9a826bb commit 9b85868

File tree

4 files changed

+215
-3
lines changed

4 files changed

+215
-3
lines changed

src/components/float-layout/index.jsx

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import classNames from 'classnames'
2+
import { handleTouchScroll } from '../../utils/common'
3+
4+
export default {
5+
name: 'FloatLayout',
6+
props: {
7+
title: {
8+
type: String,
9+
default: '',
10+
},
11+
isOpened: {
12+
type: Boolean,
13+
default: false,
14+
},
15+
scrollY: {
16+
type: Boolean,
17+
default: true,
18+
},
19+
scrollX: {
20+
type: Boolean,
21+
default: false,
22+
},
23+
scrollWithAnimation: {
24+
type: Boolean,
25+
default: false,
26+
},
27+
onClose: {
28+
type: Function,
29+
default: () => () => {},
30+
},
31+
onScroll: {
32+
type: Function,
33+
default: () => () => {},
34+
},
35+
onScrollToLower: {
36+
type: Function,
37+
default: () => () => {},
38+
},
39+
onScrollToUpper: {
40+
type: Function,
41+
default: () => () => {},
42+
},
43+
scrollTop: {
44+
type: Number,
45+
default: 0,
46+
},
47+
scrollLeft: {
48+
type: Number,
49+
default: 0,
50+
},
51+
upperThreshold: {
52+
type: Number,
53+
default: 0,
54+
},
55+
lowerThreshold: {
56+
type: Number,
57+
default: 0,
58+
},
59+
className: {
60+
type: [String, Array],
61+
default: '',
62+
},
63+
},
64+
data() {
65+
const { isOpened } = this
66+
return {
67+
state: {
68+
_isOpened: isOpened,
69+
},
70+
}
71+
},
72+
watch: {
73+
isOpened(val, oldVal) {
74+
if (val !== oldVal) {
75+
handleTouchScroll(val)
76+
}
77+
if (val !== this.state._isOpened) {
78+
this.setState({
79+
_isOpened: val,
80+
})
81+
}
82+
},
83+
},
84+
methods: {
85+
setState(newState, fn) {
86+
const ks = Object.keys(newState)
87+
if (Array.isArray(ks)) {
88+
ks.forEach((k) => {
89+
if (k in this.state) {
90+
this.state[k] = newState[k]
91+
}
92+
})
93+
}
94+
this.$nextTick(() => {
95+
typeof fn === 'function' && fn.call(this)
96+
})
97+
},
98+
handleClose() {
99+
if (typeof this.onClose === 'function') {
100+
this.onClose()
101+
}
102+
},
103+
close() {
104+
this.setState(
105+
{
106+
_isOpened: false,
107+
},
108+
this.handleClose
109+
)
110+
},
111+
/**
112+
*
113+
* @param {event} e
114+
*/
115+
handleTouchMove(e) {
116+
e.stopPropagation()
117+
},
118+
},
119+
render() {
120+
const { _isOpened } = this.state
121+
const {
122+
title,
123+
124+
scrollY,
125+
scrollX,
126+
scrollTop,
127+
scrollLeft,
128+
upperThreshold,
129+
lowerThreshold,
130+
scrollWithAnimation,
131+
} = this
132+
133+
const rootClass = classNames(
134+
'at-float-layout',
135+
{
136+
'at-float-layout--active': _isOpened,
137+
},
138+
this.className
139+
)
140+
141+
return (
142+
<view class={rootClass} onTouchMove={this.handleTouchMove}>
143+
<view onTap={this.close} class="at-float-layout__overlay" />
144+
<view class="at-float-layout__container layout">
145+
{title ? (
146+
<view class="layout-header">
147+
<view class="layout-header__title">{title}</view>
148+
<view class="layout-header__btn-close" onTap={this.close} />
149+
</view>
150+
) : null}
151+
<view class="layout-body">
152+
<scroll-view
153+
scrollY={scrollY}
154+
scrollX={scrollX}
155+
scrollTop={scrollTop}
156+
scrollLeft={scrollLeft}
157+
upperThreshold={upperThreshold}
158+
lowerThreshold={lowerThreshold}
159+
scrollWithAnimation={scrollWithAnimation}
160+
// @ts-ignore // TODO: Fix typings
161+
onScroll={this.onScroll}
162+
// @ts-ignore // TODO: Fix typings
163+
onScrollToLower={this.onScrollToLower}
164+
// @ts-ignore // TODO: Fix typings
165+
onScrollToUpper={this.onScrollToUpper}
166+
class="layout-body__content">
167+
{this.$slots.default}
168+
</scroll-view>
169+
</view>
170+
</view>
171+
</view>
172+
)
173+
},
174+
}

src/components/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import Grid from './grid/index'
22
import List from './list/index'
33
import ListItem from './list/item/index'
44
import Card from './card/index'
5+
import FloatLayout from './float-layout/index'
56

6-
export { Grid, List, ListItem, Card }
7+
export { Grid, List, ListItem, Card, FloatLayout }

src/pages/index/index.vue

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
<view
33
class="index"
44
>
5+
<FloatLayout
6+
title="这是个标题"
7+
>
8+
这是内容区 随你怎么写这是内容区 随你怎么写这是内容区 随你怎么写这是内容区
9+
随你怎么写这是内容区 随你怎么写这是内容区 随你怎么写
10+
</FloatLayout>
511
<Card
612
note="小Tips"
713
extra="额外信息"
@@ -211,7 +217,7 @@ import ModalAction from '../../components/modal/action/index.jsx'
211217
import Toast from '../../components/toast/index.jsx'
212218
import SwipeAction from '../../components/swipe-action/index.jsx'
213219
import Message from '../../components/message/index.jsx'
214-
import { Grid, List, ListItem, Card } from '../../components/index'
220+
import { Grid, List, ListItem, Card, FloatLayout } from '../../components/index'
215221
216222
export default {
217223
name: 'Index',
@@ -242,6 +248,7 @@ export default {
242248
List,
243249
ListItem,
244250
Card,
251+
FloatLayout,
245252
},
246253
data() {
247254
return {

src/utils/common.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,34 @@ function isTest(): boolean {
110110
return process.env.NODE_ENV === 'test'
111111
}
112112

113-
export { getEnvs, delayGetScrollOffset, delayGetClientRect, delayQuerySelector, uuid, isTest }
113+
let scrollTop = 0
114+
115+
function handleTouchScroll(flag: any): void {
116+
if (ENV !== Taro.ENV_TYPE.WEB) {
117+
return
118+
}
119+
if (flag) {
120+
scrollTop = document.documentElement.scrollTop
121+
122+
// 使body脱离文档流
123+
document.body.classList.add('at-frozen')
124+
125+
// 把脱离文档流的body拉上去!否则页面会回到顶部!
126+
document.body.style.top = `${-scrollTop}px`
127+
} else {
128+
document.body.style.top = null
129+
document.body.classList.remove('at-frozen')
130+
131+
document.documentElement.scrollTop = scrollTop
132+
}
133+
}
134+
135+
export {
136+
getEnvs,
137+
delayGetScrollOffset,
138+
delayGetClientRect,
139+
delayQuerySelector,
140+
uuid,
141+
isTest,
142+
handleTouchScroll,
143+
}

0 commit comments

Comments
 (0)