feat(BackTop): support css variable and prop of z-index (#11582)

* feat(BackTop): support css variable and prop of z-index

* docs: update README
This commit is contained in:
Gavin 2023-02-18 14:54:44 +08:00 committed by GitHub
parent 388e76b54c
commit d6213a7b4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 4 deletions

View File

@ -13,10 +13,12 @@ import {
// Utils // Utils
import { import {
extend,
addUnit, addUnit,
inBrowser, inBrowser,
numericProp, numericProp,
getScrollTop, getScrollTop,
getZIndexStyle,
createNamespace, createNamespace,
makeNumericProp, makeNumericProp,
} from '../utils'; } from '../utils';
@ -33,6 +35,7 @@ const [name, bem] = createNamespace('back-top');
export const backTopProps = { export const backTopProps = {
right: numericProp, right: numericProp,
bottom: numericProp, bottom: numericProp,
zIndex: numericProp,
target: [String, Object] as PropType<TeleportProps['to']>, target: [String, Object] as PropType<TeleportProps['to']>,
offset: makeNumericProp(200), offset: makeNumericProp(200),
immediate: Boolean, immediate: Boolean,
@ -58,10 +61,12 @@ export default defineComponent({
const root = ref<HTMLElement>(); const root = ref<HTMLElement>();
const scrollParent = ref<Window | Element>(); const scrollParent = ref<Window | Element>();
const style = computed(() => ({ const style = computed(() =>
extend(getZIndexStyle(props.zIndex), {
right: addUnit(props.right), right: addUnit(props.right),
bottom: addUnit(props.bottom), bottom: addUnit(props.bottom),
})); })
);
const onClick = (event: MouseEvent) => { const onClick = (event: MouseEvent) => {
emit('click', event); emit('click', event);

View File

@ -126,6 +126,7 @@ Add `immediate` prop to scroll to top immediately.
| offset | The component will not display until the scroll offset reaches this value | _number_ | `200` | | offset | The component will not display until the scroll offset reaches this value | _number_ | `200` |
| teleport | Specifies a target element where BackTop will be mounted | _string \| Element_ | `body` | | teleport | Specifies a target element where BackTop will be mounted | _string \| Element_ | `body` |
| immediate `v4.0.9` | Whether to scroll to top immediately | _boolean_ | `false` | | immediate `v4.0.9` | Whether to scroll to top immediately | _boolean_ | `false` |
| z-index | Set the z-index to a fixed value | _number \| string_ | `100` |
### Events ### Events
@ -157,5 +158,8 @@ The component provides the following CSS variables, which can be used to customi
| ------------------------- | ----------------- | ----------- | | ------------------------- | ----------------- | ----------- |
| --van-back-top-size | _40px_ | - | | --van-back-top-size | _40px_ | - |
| --van-back-top-icon-size | _20px_ | - | | --van-back-top-icon-size | _20px_ | - |
| --van-back-top-right | _30px_ | - |
| --van-back-top-bottom | _40px_ | - |
| --van-back-top-z-index | _100_ | - |
| --van-back-top-text-color | _#fff_ | - | | --van-back-top-text-color | _#fff_ | - |
| --van-back-top-background | _var(--van-blue)_ | - | | --van-back-top-background | _var(--van-blue)_ | - |

View File

@ -128,6 +128,7 @@ export default {
| offset | 滚动高度达到此参数值时才显示组件 | _number_ | `200` | | offset | 滚动高度达到此参数值时才显示组件 | _number_ | `200` |
| teleport | 指定挂载的节点,等同于 Teleport 组件的 [to 属性](https://v3.cn.vuejs.org/api/built-in-components.html#teleport) | _string \| Element_ | `body` | | teleport | 指定挂载的节点,等同于 Teleport 组件的 [to 属性](https://v3.cn.vuejs.org/api/built-in-components.html#teleport) | _string \| Element_ | `body` |
| immediate `v4.0.9` | 是否瞬间滚动到顶部 | _boolean_ | `false` | | immediate `v4.0.9` | 是否瞬间滚动到顶部 | _boolean_ | `false` |
| z-index | 设置组件的 z-index 层级 | _number \| string_ | `100` |
### Events ### Events
@ -161,5 +162,6 @@ import type { BackTopProps, BackTopThemeVars } from 'vant';
| --van-back-top-icon-size | _20px_ | - | | --van-back-top-icon-size | _20px_ | - |
| --van-back-top-right | _30px_ | - | | --van-back-top-right | _30px_ | - |
| --van-back-top-bottom | _40px_ | - | | --van-back-top-bottom | _40px_ | - |
| --van-back-top-z-index | _100_ | - |
| --van-back-top-text-color | _#fff_ | - | | --van-back-top-text-color | _#fff_ | - |
| --van-back-top-background | _var(--van-blue)_ | - | | --van-back-top-background | _var(--van-blue)_ | - |

View File

@ -2,6 +2,7 @@
--van-back-top-size: 40px; --van-back-top-size: 40px;
--van-back-top-right: 30px; --van-back-top-right: 30px;
--van-back-top-bottom: 40px; --van-back-top-bottom: 40px;
--van-back-top-z-index: 100;
--van-back-top-icon-size: 20px; --van-back-top-icon-size: 20px;
--van-back-top-text-color: #fff; --van-back-top-text-color: #fff;
--van-back-top-background: var(--van-blue); --van-back-top-background: var(--van-blue);
@ -16,6 +17,7 @@
height: var(--van-back-top-size); height: var(--van-back-top-size);
right: var(--van-back-top-right); right: var(--van-back-top-right);
bottom: var(--van-back-top-bottom); bottom: var(--van-back-top-bottom);
z-index: var(--van-back-top-z-index);
cursor: pointer; cursor: pointer;
color: var(--van-back-top-text-color); color: var(--van-back-top-text-color);
border-radius: var(--van-radius-max); border-radius: var(--van-radius-max);

View File

@ -8,12 +8,14 @@ test('should allow to custom position by position prop', async () => {
props: { props: {
right: 30, right: 30,
bottom: 100, bottom: 100,
zIndex: 200,
teleport: root, teleport: root,
}, },
}); });
const backTopEl = root.querySelector<HTMLDivElement>('.van-back-top')!; const backTopEl = root.querySelector<HTMLDivElement>('.van-back-top')!;
expect(backTopEl.style.right).toBe('30px'); expect(backTopEl.style.right).toBe('30px');
expect(backTopEl.style.bottom).toBe('100px'); expect(backTopEl.style.bottom).toBe('100px');
expect(backTopEl.style.zIndex).toBe('200');
}); });
test('should allow position prop to contain unit', async () => { test('should allow position prop to contain unit', async () => {

View File

@ -1,5 +1,8 @@
export type BackTopThemeVars = { export type BackTopThemeVars = {
backTopSize?: string; backTopSize?: string;
backTopRight?: string;
backTopBottom?: string;
backTopZIndex?: number | string;
backTopIconSize?: string; backTopIconSize?: string;
backTopTextColor?: string; backTopTextColor?: string;
backTopBackground?: string; backTopBackground?: string;