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 support custom position #506

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/tzfeng-handsome.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@alauda/ui": patch
---

feat: backtop support custom position
4 changes: 3 additions & 1 deletion src/back-top/back-top.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div
class="aui-back-top"
[class.show]="isDisplayed$ | async"
[style.display]="display$ | async"
[style.position]="'fixed'"
Copy link
Member

Choose a reason for hiding this comment

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

可以写到 scss 里吧

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

跟left, bottom一起放在inline里感觉比较舒服

[style]="position"
>
<button
aui-button="text"
Expand Down
8 changes: 0 additions & 8 deletions src/back-top/back-top.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@
$block: 'aui-back-top';

.#{$block} {
position: fixed;
right: 12px;
bottom: 12px;
@include flex-center;

height: use-var(inline-height-m);
width: use-var(inline-height-m);
border-radius: use-var(border-radius-m);
overflow: hidden;
visibility: hidden;
cursor: pointer;
@include popper-shadow;

background-color: use-rgb(n-10);

&.show {
visibility: visible;
}
}
13 changes: 12 additions & 1 deletion src/back-top/back-top.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
map,
switchMap,
throttleTime,
startWith,
} from 'rxjs';

type TargetType = Element | Window | string;
Expand All @@ -39,6 +40,14 @@ export class BackTopComponent {
return this._visibilityHeight;
}

@Input()
position: {
left?: string;
right?: string;
top?: string;
bottom?: string;
} = { right: '12px', bottom: '12px' };

@Input()
set target(val: TargetType) {
this._target = val;
Expand All @@ -63,7 +72,7 @@ export class BackTopComponent {

constructor(@Optional() private readonly cdkScrollable: CdkScrollable) {}

isDisplayed$ = combineLatest([
display$ = combineLatest([
this.target$$.asObservable().pipe(
map(target => this.getTarget(target)),
switchMap(target =>
Expand All @@ -77,6 +86,8 @@ export class BackTopComponent {
this.visibilityHeight$$,
]).pipe(
map(([scrollTop, visibilityHeight]) => scrollTop >= visibilityHeight),
map(visible => (visible ? 'flex' : 'none')),
startWith('none'),
distinctUntilChanged(),
);

Expand Down
9 changes: 5 additions & 4 deletions stories/back-top/back-top.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ import * as useSelector from './use-selector.stories';

## Backtop Inputs

| 名称 | 类型 | 默认值 | 描述 |
| ---------------- | --------------------------- | ------ | -------------------------------------------------------------------------------------------------------------------------- |
| visibilityHeight | number | 400 | 目标容器滚动到多高的距离,展示 backtop 按钮 |
| target | Element \| Window \| string | - | 指定目标容器,可以指定 ElementRef,selector 来选择目标组件,不指定时优先选择外部的 cdkScrollable 组件,若没有则选择 window |
| 名称 | 类型 | 默认值 | 描述 |
| ---------------- | ------------------------------------------------------------------- | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| visibilityHeight | number | 400 | 目标容器滚动到多高的距离,展示 backtop 按钮 |
| position | \{ left?: string; right?: string; top?: string; bottom?: string; \} | \{ right: '12px', bottom: '12px' \} | 设置按钮在浏览器窗口中的位置 |
| target | Element \| Window \| string | - | 指定目标容器,可以指定 ElementRef,selector 来选择目标组件,不指定时优先选择外部的 cdkScrollable 组件,若没有则选择 window |

## Backtop Outputs

Expand Down