Skip to content

Commit

Permalink
feat: backtop support custom position
Browse files Browse the repository at this point in the history
  • Loading branch information
fengtianze committed Sep 19, 2023
1 parent e4b4b4e commit f26a4d5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
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'"
[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

0 comments on commit f26a4d5

Please sign in to comment.