Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Backtop): refactor backtop using hoverbutton and v15 adaption #2866

Merged
merged 8 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions migrate-from-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,9 @@ plugins: [

#### BackTop

- `elId` 重命名为 `target`
- 移除 `right`、`bottom`,通过 style 传入,增加支持 `left`、`top`
- `distance` 重命名为 `threshold`
- 移除 `isAnimation`,通过 `duration` 设置 0 实现无动画效果
- 使用 `HoverButton` 重构 `BackTop`
- 新增 `icon` 字段,可直接修改图标
- 继续支持自定义节点

#### Dialog

Expand Down
1 change: 1 addition & 0 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@
"tarodoc": true,
"show": true,
"taro": true,
"v15": true,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

需要更新文档和 CHANGELOG

根据验证结果,虽然在 config.json 中添加了 v15 版本的配置,但是相关的文档和 CHANGELOG 尚未更新。为确保代码变更的完整性,请:

  • 在 CHANGELOG 中添加 v15 版本支持的说明
  • 更新组件文档,包含 v15 版本的使用说明和示例
  • 如有必要,更新组件的演示示例
🔗 Analysis chain

版本标记更新

添加了 "v15": true 配置,表明组件已适配 v15 版本。建议:

  1. 确保相关文档已更新
  2. 在 CHANGELOG 中说明版本变更
  3. 更新组件使用示例
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查文档和示例是否已更新
fd -e md -e mdx . | xargs rg "BackTop.*v15"

Length of output: 45


Script:

#!/bin/bash
# 检查 CHANGELOG 和文档中是否包含 v15 相关更新
rg -i "v15|version.*15" -g "CHANGELOG*" -g "*.md" -g "*.mdx"

# 检查组件使用示例中是否包含 v15 相关内容
rg -i "backtop.*v15|v15.*backtop" -g "*.md" -g "*.mdx" -g "demo*" -g "example*"

# 检查是否存在其他版本相关的配置文件
fd "version|config" -e json -e js | xargs rg "v15"

Length of output: 584

"author": "vickyYe"
},
{
Expand Down
43 changes: 24 additions & 19 deletions src/packages/backtop/__test__/__snapshots__/backtop.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,38 @@
exports[`backtop custom test 1`] = `
<div>
<div
class="nut-backtop custom-class"
class="nut-hoverbutton-container nut-backtop custom-class"
style="z-index: 900; bottom: 110px; right: 10px;"
>
<div
class="backtop-demo"
style="display: flex; flex-direction: column; align-items: center;"
class="nut-hoverbutton"
>
<svg
aria-labelledby="Top"
class="nut-icon nut-icon-Top nut-backtop-main"
role="presentation"
style="width: 12px; height: 12px;"
viewBox="0 0 1024 1024"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M128 0a42.67 42.67 0 1 0 0 85.33h768A42.67 42.67 0 1 0 896 0zm391.21 173.42a10.88 10.88 0 0 0-14.42 0L3.61 619.67a10.71 10.71 0 0 0 7.21 18.67h241.34v257.11c0 71 58.05 128.55 129.64 128.55h260.4c71.59 0 129.64-57.56 129.64-128.55V638.34h241.34a10.71 10.71 0 0 0 7.21-18.67zM338.6 895.45V552.64H208.21L512 282.15l303.77 270.49H685.42v342.83c0 23.64-19.37 42.84-43.22 42.83H381.8c-23.85 0-43.2-19.2-43.2-42.85"
fill="currentColor"
fill-opacity="0.9"
/>
</svg>
<div
style="font-size: 12px;"
class="nut-hoverbutton-item-container"
>
顶部
<svg
aria-labelledby="Top"
class="nut-icon nut-icon-Top "
role="presentation"
viewBox="0 0 1024 1024"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M128 0a42.67 42.67 0 1 0 0 85.33h768A42.67 42.67 0 1 0 896 0zm391.21 173.42a10.88 10.88 0 0 0-14.42 0L3.61 619.67a10.71 10.71 0 0 0 7.21 18.67h241.34v257.11c0 71 58.05 128.55 129.64 128.55h260.4c71.59 0 129.64-57.56 129.64-128.55V638.34h241.34a10.71 10.71 0 0 0 7.21-18.67zM338.6 895.45V552.64H208.21L512 282.15l303.77 270.49H685.42v342.83c0 23.64-19.37 42.84-43.22 42.83H381.8c-23.85 0-43.2-19.2-43.2-42.85"
fill="currentColor"
fill-opacity="0.9"
/>
</svg>
<div
style="font-size: 12px;"
>
顶部
</div>
</div>
</div>
<div
class="nut-safe-area nut-safe-area-position-bottom"
/>
</div>
</div>
`;
45 changes: 26 additions & 19 deletions src/packages/backtop/__test__/backtop.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as React from 'react'
import '@testing-library/jest-dom'
import { Top } from '@nutui/icons-react'
import { render, fireEvent } from '@testing-library/react'
import { render, fireEvent, waitFor, act } from '@testing-library/react'
import BackTop from '@/packages/backtop'

test('backtop props test', () => {
Expand Down Expand Up @@ -37,31 +37,38 @@ test('backtop custom test', () => {
}}
onClick={handleClick}
>
<div
className="backtop-demo"
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}
>
<Top width="12px" height="12px" className="nut-backtop-main" />
<div style={{ fontSize: '12px' }}>顶部</div>
</div>
<Top />
<div style={{ fontSize: '12px' }}>顶部</div>
</BackTop>
)
expect(container.querySelector('.nut-backtop')).toHaveAttribute(
'style',
'z-index: 900; bottom: 110px; right: 10px;'
)
expect(container.querySelector('.backtop-demo')).toHaveAttribute(
'style',
'display: flex; flex-direction: column; align-items: center;'
)
expect(container.querySelector('.nut-icon-Top')).toHaveClass(
'nut-backtop-main'
)
fireEvent.click(container)
expect(handleClick).toBeCalled
Alex-huxiyang marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

少了()

expect(container).toMatchSnapshot()
})

test('scroll', async () => {
const { container } = render(
<div id="target" style={{ height: '100vh' }} className="backtop-wrapper">
{new Array(24).fill(0).map((_, index) => {
return <div key={index}>我是测试数据{index}</div>
})}
<BackTop target="target" className="backtop-button" />
</div>
)
const track = container.querySelector('.backtop-wrapper')
const element18 = container.querySelectorAll('.backtop-button')[0]
if (track) {
track.scrollTop = 200
act(() => {
track.dispatchEvent(new Event('scroll'))
})
await waitFor(() => {
expect(element18).toHaveClass('nut-backtop-show')
})
fireEvent.click(element18 as Element)
}
})
25 changes: 8 additions & 17 deletions src/packages/backtop/backtop.scss
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
.nut-backtop {
display: none;
position: fixed;

&-rn {
position: absolute;
}

&-show {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
background: $color-background-overlay;
color: $color-title;
border: 1px solid $backtop-border-color;
border-radius: 21px;
z-index: 100;

&:active,
&-active {
background: $color-background;
}
}

&-main {
width: $hoverbutton-item-size;
height: $hoverbutton-item-size;
transition: all 0.2s ease-in-out;
color: $color-title;
.nut-hoverbutton-item-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
}
}
66 changes: 24 additions & 42 deletions src/packages/backtop/backtop.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
FunctionComponent,
useCallback,
useState,
useMemo,
useEffect,
useRef,
} from 'react'
Expand All @@ -13,23 +12,24 @@
PageScrollObject,
getSystemInfo,
} from '@tarojs/taro'
import { View, ITouchEvent } from '@tarojs/components'
import { Top } from '@nutui/icons-react-taro'
import { ITouchEvent, View } from '@tarojs/components'
import classNames from 'classnames'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
import { useRtl } from '@/packages/configprovider/index.taro'
import { harmonyAndRn, rn } from '@/utils/platform-taro'
import pxTransform from '@/utils/px-transform'
import { Top } from '@nutui/icons-react-taro'
import { ComponentDefaults } from '@/utils/typings'
import HoverButton, {
HoverButtonProps,
} from '@/packages/hoverbutton/index.taro'
import { rn } from '@/utils/platform-taro'

export interface BackTopProps extends BasicComponent {
export interface BackTopProps extends HoverButtonProps {
threshold: number
zIndex: number
duration: number
/**
* 容器滚动时的回调参数,主要用于 rn、鸿蒙端
*/
scrollRes?: PageScrollObject
onClick?: (event: React.MouseEvent<Element, MouseEvent> | ITouchEvent) => void
// onClick?: (event: React.MouseEvent<Element, MouseEvent> | ITouchEvent) => void
}

const defaultProps = {
Expand All @@ -39,18 +39,16 @@
duration: 1000,
} as BackTopProps

const isNative = harmonyAndRn()

export const BackTop: FunctionComponent<
Partial<BackTopProps> & Omit<React.HTMLAttributes<HTMLDivElement>, 'onClick'>
> = (props) => {
const rtl = useRtl()
const {
children,
threshold,
zIndex,
className,
duration,
icon,
style,
scrollRes,
onClick,
Expand All @@ -60,12 +58,10 @@
}
const classPrefix = 'nut-backtop'
const [backTop, setBackTop] = useState(false)
const [isTouchStart, setTouchStart] = useState(false)
const cls = classNames(
classPrefix,
{
[`${classPrefix}-show`]: backTop,
[`${classPrefix}-show-active`]: isNative && isTouchStart,
[`${classPrefix}-rn`]: rn(),
},
className
Expand All @@ -77,14 +73,6 @@
})
}, [])

const handleActiveStart = useCallback(() => {
isNative && setTouchStart(true)
}, [])

const handleActiveEnd = useCallback(() => {
isNative && setTouchStart(false)
}, [])

const onScroll = useCallback(
(res: PageScrollObject) => {
const { scrollTop } = res
Expand All @@ -104,7 +92,7 @@
// 返回顶部点击事件
const goTop = useCallback(
(e: React.MouseEvent<Element, MouseEvent> | ITouchEvent) => {
onClick?.(e)

Check failure on line 95 in src/packages/backtop/backtop.taro.tsx

View workflow job for this annotation

GitHub Actions / build

Argument of type 'ITouchEvent | MouseEvent<Element, MouseEvent<Element, MouseEvent>>' is not assignable to parameter of type 'ITouchEvent | MouseEvent<Element, MouseEvent>'.
pageScrollTo({
scrollTop: 0,
duration: duration > 0 ? duration : 0,
Expand All @@ -113,32 +101,26 @@
[duration, onClick]
)

const styles = useMemo(() => {
return Object.keys(style || {}).length !== 0
? {
zIndex,
...style,
}
: {
[rtl ? 'left' : 'right']: pxTransform(10),
bottom: pxTransform(20),
zIndex,
}
}, [rtl, style, zIndex])

return (
<View
<HoverButton
className={cls}
style={styles}
style={{ zIndex, ...style }}
icon={!children && (icon || <Top />)}
onClick={(e) => {
goTop(e)

Check failure on line 110 in src/packages/backtop/backtop.taro.tsx

View workflow job for this annotation

GitHub Actions / build

Argument of type 'ITouchEvent | MouseEvent<HTMLDivElement, MouseEvent> | MouseEvent<Element, MouseEvent>' is not assignable to parameter of type 'ITouchEvent | MouseEvent<Element, MouseEvent<Element, MouseEvent>>'.
}}
onTouchStart={handleActiveStart}
onTouchEnd={handleActiveEnd}
onTouchCancel={handleActiveEnd}
>
{children || <Top size={19} className="nut-backtop-main" />}
</View>
{children && (
<View
className="nut-hoverbutton-item-container"
onClick={(e) => {
goTop(e)
}}
>
{children}
</View>
)}
</HoverButton>
)
}

Expand Down
Loading
Loading