-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(fab): 悬浮按钮支持拖拽 #2374 * style(fab): 移动实现逻辑改成touch,兼容webview和skyline框架 * fix(fab): 水平移动或垂直移动异常修复 * style(fab): 代码优化 * docs(fab): 文档更新
- Loading branch information
Showing
23 changed files
with
234 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Component({ | ||
methods: { | ||
handleClick(e) { | ||
console.log(e); | ||
}, | ||
handleMove(e) { | ||
console.log(e); | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"component": true, | ||
"usingComponents": { | ||
"t-fab": "tdesign-miniprogram/fab/fab" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<t-fab | ||
icon="gesture-press" | ||
text="拖我" | ||
bind:click="handleClick" | ||
bind:move="handleMove" | ||
aria-label="增加" | ||
draggable="{{true}}" | ||
></t-fab> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"component": true, | ||
"usingComponents": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@import '../../common/style/index.less'; | ||
|
||
.@{prefix}-draggable { | ||
position: absolute; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { SuperComponent, wxComponent } from '../../common/src/index'; | ||
import config from '../../common/config'; | ||
import props from './props'; | ||
import { getRect } from '../../common/utils'; | ||
import type { TdDraggableProps } from './type'; | ||
|
||
const systemInfo = wx.getSystemInfoSync(); | ||
const { prefix } = config; | ||
const name = `${prefix}-draggable`; | ||
|
||
export interface DraggableProps extends TdDraggableProps {} | ||
|
||
@wxComponent() | ||
export default class Draggable extends SuperComponent { | ||
properties = props; | ||
|
||
externalClasses = [`${prefix}-class`]; | ||
|
||
data = { | ||
prefix, | ||
classPrefix: name, | ||
}; | ||
|
||
lifetimes = { | ||
ready() { | ||
this.computedRect(); | ||
}, | ||
}; | ||
|
||
methods = { | ||
onTouchStart(e) { | ||
if (this.properties.direction === 'none') return; | ||
this.startX = e.touches[0].clientX + systemInfo.windowWidth - this.rect.right; | ||
this.startY = e.touches[0].clientY + systemInfo.windowHeight - this.rect.bottom; | ||
this.triggerEvent('start', { startX: this.startX, startY: this.startY, rect: this.rect, e }); | ||
}, | ||
|
||
onTouchMove(e) { | ||
if (this.properties.direction === 'none') return; | ||
let x = this.startX - e.touches[0].clientX; // x轴移动偏移量 | ||
let y = this.startY - e.touches[0].clientY; // y轴移动偏移量 | ||
|
||
if (this.properties.direction === 'vertical') { | ||
x = systemInfo.windowWidth - this.rect.right; | ||
} | ||
if (this.properties.direction === 'horizontal') { | ||
y = systemInfo.windowHeight - this.rect.bottom; | ||
} | ||
|
||
this.triggerEvent('move', { x, y, rect: this.rect, e }); | ||
}, | ||
|
||
async onTouchEnd(e) { | ||
if (this.properties.direction === 'none') return; | ||
await this.computedRect(); | ||
this.triggerEvent('end', { rect: this.rect, e }); | ||
}, | ||
|
||
async computedRect() { | ||
this.rect = { right: 0, bottom: 0, width: 0, height: 0 }; | ||
this.rect = await getRect(this, `.${this.data.classPrefix}`); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<wxs src="../../common/utils.wxs" module="_" /> | ||
|
||
<view | ||
class="{{classPrefix}} class {{prefix}}-class" | ||
style="{{_._style([style, customStyle])}}" | ||
bind:touchstart="onTouchStart" | ||
bind:touchmove="onTouchMove" | ||
bind:touchend="onTouchEnd" | ||
> | ||
<slot></slot> | ||
</view> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './props'; | ||
export * from './type'; | ||
export * from './draggable'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { TdDraggableProps } from './type'; | ||
|
||
const props: TdDraggableProps = { | ||
/** 移动方向 */ | ||
direction: { | ||
type: String, | ||
value: 'all', | ||
}, | ||
}; | ||
|
||
export default props; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export interface TdDraggableProps { | ||
/** | ||
* 移动方向 | ||
* @default 'all' | ||
*/ | ||
direction?: { | ||
type: StringConstructor; | ||
value?: 'all' | 'vertical' | 'horizontal' | 'none'; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"component": true, | ||
"usingComponents": { | ||
"t-button": "../button/button" | ||
"t-button": "../button/button", | ||
"t-draggable": "./draggable/draggable" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,8 @@ | |
&__button { | ||
box-shadow: @fab-shadow; | ||
} | ||
|
||
&__draggable { | ||
position: fixed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,7 @@ | ||
<import src="../common/template/button.wxml" /> | ||
<wxs src="../common/utils.wxs" module="_" /> | ||
<import src="./template/view.wxml" /> | ||
<import src="./template/draggable.wxml" /> | ||
|
||
<view | ||
class="{{classPrefix}} class {{prefix}}-class" | ||
style="right: 16px; bottom: 32px; {{_._style([style, customStyle])}}" | ||
> | ||
<template | ||
is="button" | ||
data="{{ ...baseButtonProps, icon, ...buttonProps, externalClass: prefix + '-fab__button', content: text, ariaLabel}}" | ||
/> | ||
</view> | ||
<template | ||
is="{{draggable ? 'draggable' : 'view'}}" | ||
data="{{prefix, classPrefix, style, customStyle, moveStyle, draggable, buttonData}}" | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<import src="../../common/template/button.wxml" /> | ||
<wxs src="../../common/utils.wxs" module="_" /> | ||
|
||
<template name="draggable"> | ||
<t-draggable | ||
id="draggable" | ||
style="right: 16px; bottom: 32px; {{_._style([style, customStyle, moveStyle])}}" | ||
t-class="{{prefix}}}-fab__button" | ||
direction="{{draggable === true ? 'all' : draggable}}" | ||
bind:move="onMove" | ||
> | ||
<template is="button" data="{{...buttonData}}" /> | ||
</t-draggable> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<import src="../../common/template/button.wxml" /> | ||
<wxs src="../../common/utils.wxs" module="_" /> | ||
|
||
<template name="view"> | ||
<view | ||
class="{{classPrefix}} class {{prefix}}-class" | ||
style="right: 16px; bottom: 32px; {{_._style([style, customStyle])}}" | ||
> | ||
<template is="button" data="{{...buttonData}}" /> | ||
</view> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters