1
- import Taro , { pxTransform , useState } from '@tarojs/taro'
2
- import { View } from " @tarojs/components" ;
1
+ import Taro , { pxTransform , useState } from '@tarojs/taro' ;
2
+ import { View } from ' @tarojs/components' ;
3
3
import utils from '../utils/index' ;
4
- import { IProps } from '../../../@types/floatButton'
5
- import ClIcon from " ../icon" ;
4
+ import { IProps } from '../../../@types/floatButton' ;
5
+ import ClIcon from ' ../icon' ;
6
6
7
- import './index.scss'
8
- import ClAnimation from " ../animation" ;
9
- import ClLayout from " ../layout" ;
7
+ import './index.scss' ;
8
+ import ClAnimation from ' ../animation' ;
9
+ import ClLayout from ' ../layout' ;
10
10
11
11
let tempPageX = 0 ;
12
12
let tempPageY = 0 ;
13
+ let pageX = 25 ;
14
+ let pageY = 100 ;
13
15
export default function ClFloatButton ( props : IProps ) {
14
16
const [ show , setShow ] = useState ( false ) ;
15
17
const [ rotate , setRotate ] = useState ( 0 ) ;
16
- const [ pageX , setPageX ] = useState ( 50 ) ;
17
- const [ pageY , setPageY ] = useState ( 200 ) ;
18
- const { move, open, icon, bgColor, iconColor, direction, shadow, shape, size, actionList, onClick, onActionClick, closeWithShadow} = props ;
19
- const dealSize = utils . model . SIZE [ size || "normal" ] ;
20
- const dealBgColor = utils . model . BG_COLOR_LIST [ bgColor || "blue" ] ;
18
+ const [ animation , setAnimation ] = useState ( { } ) ;
19
+ const {
20
+ move,
21
+ open,
22
+ icon,
23
+ bgColor,
24
+ iconColor,
25
+ direction,
26
+ shadow,
27
+ shape,
28
+ size,
29
+ actionList,
30
+ onClick,
31
+ onActionClick,
32
+ closeWithShadow
33
+ } = props ;
34
+ const dealSize = utils . model . SIZE [ size || 'normal' ] ;
35
+ const dealBgColor = utils . model . BG_COLOR_LIST [ bgColor || 'blue' ] ;
21
36
const dealShadow = shadow ? 'shadow' : '' ;
22
37
const dealIconColor = iconColor || '' ;
23
- const dealActionList = actionList || [ ] ;
38
+ let dealActionList = actionList || [ ] ;
24
39
const len = dealActionList . length ;
40
+ const type = ( ) => {
41
+ if ( direction === 'vertical' ) {
42
+ return show ? 'slide-bottom' : 'slide-top' ;
43
+ } else {
44
+ return show ? 'slide-right' : 'slide-left' ;
45
+ }
46
+ } ;
25
47
const actionListComponent = dealActionList . map ( ( item , index ) => (
26
- < ClAnimation type = { direction === "vertical" ? "slide-bottom" : "slide-right" } key = { index }
27
- delay = { ( len - index - 1 ) / 10 } >
28
- < ClLayout padding = { "small" } paddingDirection = { direction === "vertical" ? "bottom" : "right" } >
29
- < View
30
- className = { `cu-avatar ${ shape } ${ dealSize } ${ item . bgColor ? utils . model . BG_COLOR_LIST [ item . bgColor ] : dealBgColor } ${ dealShadow } ` }
31
- onClick = { ( e ) => {
32
- e . stopPropagation ( ) ;
33
- clickButton ( ) ;
34
- onActionClick && onActionClick ( index )
35
- } } >
36
- < View className = { `${ item . iconColor || dealIconColor } ` } >
37
- < ClIcon iconName = { item . icon } size = { size } />
48
+ < View key = { index } style = { { position : show ? 'relative' : 'absolute' } } >
49
+ < ClAnimation
50
+ type = { type ( ) }
51
+ delay = { show ? ( len - index - 1 ) / 10 : 0 }
52
+ duration = { 0.2 }
53
+ >
54
+ < ClLayout
55
+ padding = 'small'
56
+ paddingDirection = { direction === 'vertical' ? 'bottom' : 'right' }
57
+ >
58
+ < View
59
+ className = { `cu-avatar ${ shape } ${ dealSize } ${
60
+ item . bgColor
61
+ ? utils . model . BG_COLOR_LIST [ item . bgColor ]
62
+ : dealBgColor
63
+ } ${ dealShadow } `}
64
+ onClick = { e => {
65
+ e . stopPropagation ( ) ;
66
+ clickButton ( ) ;
67
+ onActionClick && onActionClick ( index ) ;
68
+ } }
69
+ >
70
+ < View className = { `${ item . iconColor || dealIconColor } ` } >
71
+ < ClIcon iconName = { item . icon } size = { size } />
72
+ </ View >
38
73
</ View >
39
- </ View >
40
- </ ClLayout >
41
- </ ClAnimation >
74
+ </ ClLayout >
75
+ </ ClAnimation >
76
+ </ View >
42
77
) ) ;
43
- const directionClass = direction === " vertical" ? '' : 'flex' ;
78
+ const directionClass = direction === ' vertical' ? '' : 'flex' ;
44
79
const clickButton = ( ) => {
45
80
setShow ( ! show ) ;
46
81
open && setRotate ( rotate ? 0 : 45 ) ;
47
82
} ;
48
83
return (
49
- < View className = { `${ show ? 'float_button__mask' : '' } ` } onClick = { ( e ) => {
50
- e . stopPropagation ( ) ;
51
- closeWithShadow && clickButton ( )
52
- } } >
53
- < View className = { 'float_button__fixed' }
54
- onTouchStart = { ( e ) => {
55
- if ( ! move ) return ;
56
- const touchs = e . touches [ 0 ] ;
57
- tempPageX = touchs . pageX ;
58
- tempPageY = touchs . pageY ;
59
- } }
60
- onTouchMove = { ( e ) => {
61
- if ( ! move ) return ;
62
- const touchs = e . touches [ 0 ] ;
63
- setPageX ( ( pageX - ( touchs . pageX - tempPageX ) * 2 ) ) ;
64
- setPageY ( ( pageY - ( touchs . pageY - tempPageY ) * 2 ) ) ;
65
- tempPageX = touchs . pageX ;
66
- tempPageY = touchs . pageY ;
67
- } }
68
- onTouchCancel = { ( e ) => {
69
- } }
70
- style = { { right : pxTransform ( pageX ) , bottom : pxTransform ( pageY ) } }
84
+ < View
85
+ className = { `${ show ? 'float_button__mask' : '' } ` }
86
+ onClick = { e => {
87
+ closeWithShadow && clickButton ( ) ;
88
+ } }
89
+ >
90
+ < View
91
+ className = 'float_button__fixed'
92
+ animation = { animation }
93
+ onTouchStart = { e => {
94
+ if ( ! move ) return ;
95
+ const touchs = e . touches [ 0 ] ;
96
+ tempPageX = touchs . pageX ;
97
+ tempPageY = touchs . pageY ;
98
+ } }
99
+ onTouchMove = { e => {
100
+ e . stopPropagation ( ) ;
101
+ if ( ! move ) return ;
102
+ const touchs = e . touches [ 0 ] ;
103
+ let newAnimation = Taro . createAnimation ( animation ) ;
104
+ pageX = pageX - ( touchs . pageX - tempPageX ) ;
105
+ pageY = pageY - ( touchs . pageY - tempPageY ) ;
106
+ const length = Math . sqrt ( Math . pow ( pageX , 2 ) + Math . pow ( pageY , 2 ) )
107
+ newAnimation . right ( pageX ) ;
108
+ newAnimation . bottom ( pageY ) . step ( {
109
+ duration : 13 * length / 100
110
+ } ) ;
111
+ setAnimation ( newAnimation . export ( ) ) ;
112
+ tempPageX = touchs . pageX ;
113
+ tempPageY = touchs . pageY ;
114
+ } }
115
+ onTouchCancel = { e => {
116
+ e . stopPropagation ( ) ;
117
+ } }
71
118
>
72
119
< View className = { `float_button__content ${ directionClass } ` } >
73
- {
74
- show ? actionListComponent : ''
75
- }
76
- < View className = { `cu-avatar ${ shape } ${ dealSize } ${ dealBgColor } ${ dealShadow } ` }
77
- onClick = { ( ) => {
78
- clickButton ( ) ;
79
- onClick && onClick ( )
80
- } } >
81
- < View className = { `${ dealIconColor } ` }
82
- style = { { transform : `rotate(${ rotate } deg)` , transition : 'all 0.15s linear' } } >
83
- < ClIcon iconName = { icon } size = { size } />
120
+ { actionListComponent }
121
+ < View
122
+ className = { `cu-avatar ${ shape } ${ dealSize } ${ dealBgColor } ${ dealShadow } ` }
123
+ onClick = { e => {
124
+ e . stopPropagation ( ) ;
125
+ clickButton ( ) ;
126
+ onClick && onClick ( ) ;
127
+ } }
128
+ >
129
+ < View
130
+ className = { `${ dealIconColor } ` }
131
+ style = { {
132
+ transform : `rotate(${ rotate } deg)` ,
133
+ transition : 'all 0.15s linear'
134
+ } }
135
+ >
136
+ < ClIcon iconName = { icon } size = { size } />
84
137
</ View >
85
138
</ View >
86
139
</ View >
87
140
</ View >
88
141
</ View >
89
- )
142
+ ) ;
90
143
}
91
144
92
145
ClFloatButton . options = {
@@ -96,17 +149,15 @@ ClFloatButton.options = {
96
149
ClFloatButton . defaultProps = {
97
150
move : false ,
98
151
open : true ,
99
- icon : " add" ,
100
- bgColor : " blue" ,
152
+ icon : ' add' ,
153
+ bgColor : ' blue' ,
101
154
iconColor : undefined ,
102
- direction : "vertical" ,
103
- onClick : ( ) => {
104
- } ,
155
+ direction : 'vertical' ,
156
+ onClick : ( ) => { } ,
105
157
shadow : true ,
106
- onActionClick : index => {
107
- } ,
158
+ onActionClick : index => { } ,
108
159
actionList : [ ] ,
109
- size : " normal" ,
110
- shape : " round" ,
160
+ size : ' normal' ,
161
+ shape : ' round' ,
111
162
closeWithShadow : false
112
163
} as IProps ;
0 commit comments