chore: add getZIndexStyle util (#8254)

This commit is contained in:
neverland 2021-03-03 16:45:32 +08:00 committed by GitHub
parent 202edb24c7
commit 2d8914c2c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 40 additions and 36 deletions

View File

@ -7,7 +7,7 @@ import {
} from 'vue'; } from 'vue';
// Utils // Utils
import { createNamespace, UnknownProp } from '../utils'; import { createNamespace, getZIndexStyle, UnknownProp } from '../utils';
import { DROPDOWN_KEY, DropdownMenuProvide } from '../dropdown-menu'; import { DROPDOWN_KEY, DropdownMenuProvide } from '../dropdown-menu';
// Composables // Composables
@ -153,9 +153,7 @@ export default createComponent({
closeOnClickOverlay, closeOnClickOverlay,
} = parent.props; } = parent.props;
const style: CSSProperties = { const style: CSSProperties = getZIndexStyle(zIndex);
zIndex: zIndex !== undefined ? +zIndex : undefined,
};
if (direction === 'down') { if (direction === 'down') {
style.top = `${offset.value}px`; style.top = `${offset.value}px`;

View File

@ -70,11 +70,11 @@ export default createComponent({
children.some((item) => item.state.showWrapper) children.some((item) => item.state.showWrapper)
); );
const barStyle = computed(() => { const barStyle = computed<CSSProperties | undefined>(() => {
if (opened.value && isDef(props.zIndex)) { if (opened.value && isDef(props.zIndex)) {
return { return {
zIndex: +props.zIndex + 1, zIndex: +props.zIndex + 1,
} as CSSProperties; };
} }
}); });

View File

@ -1,7 +1,7 @@
import { ref, reactive, computed, CSSProperties } from 'vue'; import { ref, reactive, computed, CSSProperties } from 'vue';
// Utils // Utils
import { createNamespace } from '../utils'; import { createNamespace, getZIndexStyle } from '../utils';
import { BORDER_BOTTOM } from '../utils/constant'; import { BORDER_BOTTOM } from '../utils/constant';
import { INDEX_BAR_KEY, IndexBarProvide } from '../index-bar'; import { INDEX_BAR_KEY, IndexBarProvide } from '../index-bar';
import { getScrollTop, getRootScrollTop } from '../utils/dom/scroll'; import { getScrollTop, getRootScrollTop } from '../utils/dom/scroll';
@ -40,17 +40,19 @@ export default createComponent({
const isSticky = () => state.active && parent.props.sticky; const isSticky = () => state.active && parent.props.sticky;
const anchorStyle = computed(() => { const anchorStyle = computed<CSSProperties | undefined>(() => {
const { zIndex, highlightColor } = parent.props; const { zIndex, highlightColor } = parent.props;
if (isSticky()) { if (isSticky()) {
return { return {
zIndex: `${zIndex}`, ...getZIndexStyle(zIndex),
left: state.left ? `${state.left}px` : null, left: state.left ? `${state.left}px` : undefined,
width: state.width ? `${state.width}px` : null, width: state.width ? `${state.width}px` : undefined,
transform: state.top ? `translate3d(0, ${state.top}px, 0)` : null, transform: state.top
? `translate3d(0, ${state.top}px, 0)`
: undefined,
color: highlightColor, color: highlightColor,
} as CSSProperties; };
} }
}); });

View File

@ -85,19 +85,19 @@ export default createComponent({
linkChildren({ props }); linkChildren({ props });
const sidebarStyle = computed(() => { const sidebarStyle = computed<CSSProperties | undefined>(() => {
if (isDef(props.zIndex)) { if (isDef(props.zIndex)) {
return { return {
zIndex: +props.zIndex + 1, zIndex: +props.zIndex + 1,
} as CSSProperties; };
} }
}); });
const highlightStyle = computed(() => { const highlightStyle = computed<CSSProperties | undefined>(() => {
if (props.highlightColor) { if (props.highlightColor) {
return { return {
color: props.highlightColor, color: props.highlightColor,
} as CSSProperties; };
} }
}); });

View File

@ -870,9 +870,7 @@ exports[`should render demo and match snapshot 1`] = `
</div> </div>
<div> <div>
<div style="height: 0px;"> <div style="height: 0px;">
<div class="van-index-anchor van-index-anchor--sticky van-hairline--bottom" <div class="van-index-anchor van-index-anchor--sticky van-hairline--bottom">
style="z-index: undefined;"
>
Z Z
</div> </div>
</div> </div>

View File

@ -300,9 +300,7 @@ exports[`should update active anchor after page scroll 2`] = `
<div data-index="0" <div data-index="0"
style="height: 10px;" style="height: 10px;"
> >
<div class="van-index-anchor van-index-anchor--sticky van-hairline--bottom" <div class="van-index-anchor van-index-anchor--sticky van-hairline--bottom">
style="z-index: undefined;"
>
1 1
</div> </div>
</div> </div>
@ -310,7 +308,7 @@ exports[`should update active anchor after page scroll 2`] = `
style="height: 10px;" style="height: 10px;"
> >
<div class="van-index-anchor van-index-anchor--sticky van-hairline--bottom" <div class="van-index-anchor van-index-anchor--sticky van-hairline--bottom"
style="z-index: undefined; transform: translate3d(0, 10px, 0);" style="transform: translate3d(0, 10px, 0);"
> >
2 2
</div> </div>

View File

@ -1,7 +1,7 @@
import { ref, CSSProperties } from 'vue'; import { ref, CSSProperties } from 'vue';
// Utils // Utils
import { createNamespace } from '../utils'; import { createNamespace, getZIndexStyle } from '../utils';
import { BORDER_BOTTOM } from '../utils/constant'; import { BORDER_BOTTOM } from '../utils/constant';
// Composables // Composables
@ -58,9 +58,7 @@ export default createComponent({
const renderNavBar = () => { const renderNavBar = () => {
const { title, fixed, border, zIndex } = props; const { title, fixed, border, zIndex } = props;
const style: CSSProperties = { const style: CSSProperties = getZIndexStyle(zIndex);
zIndex: zIndex !== undefined ? +zIndex : undefined,
};
const hasLeft = props.leftArrow || props.leftText || slots.left; const hasLeft = props.leftArrow || props.leftText || slots.left;
const hasRight = props.rightText || slots.right; const hasRight = props.rightText || slots.right;

View File

@ -8,7 +8,7 @@ import {
Transition, Transition,
TeleportProps, TeleportProps,
} from 'vue'; } from 'vue';
import { createNamespace, stopPropagation } from '../utils'; import { createNamespace, getZIndexStyle, stopPropagation } from '../utils';
import { useClickAway } from '@vant/use'; import { useClickAway } from '@vant/use';
import Key, { KeyType } from './Key'; import Key, { KeyType } from './Key';
@ -263,9 +263,7 @@ export default createComponent({
<div <div
v-show={props.show} v-show={props.show}
ref={root} ref={root}
style={{ style={getZIndexStyle(props.zIndex)}
zIndex: props.zIndex !== undefined ? +props.zIndex : undefined,
}}
class={bem({ class={bem({
unfit: !props.safeAreaInsetBottom, unfit: !props.safeAreaInsetBottom,
'with-title': !!Title, 'with-title': !!Title,

View File

@ -5,6 +5,7 @@ import {
UnknownProp, UnknownProp,
preventDefault, preventDefault,
createNamespace, createNamespace,
getZIndexStyle,
} from '../utils'; } from '../utils';
import { useLazyRender } from '../composables/use-lazy-render'; import { useLazyRender } from '../composables/use-lazy-render';
@ -32,7 +33,7 @@ export default createComponent({
const renderOverlay = lazyRender(() => { const renderOverlay = lazyRender(() => {
const style: CSSProperties = { const style: CSSProperties = {
zIndex: props.zIndex !== undefined ? +props.zIndex : undefined, ...getZIndexStyle(props.zIndex),
...props.customStyle, ...props.customStyle,
}; };

View File

@ -1,7 +1,7 @@
import { ref, PropType } from 'vue'; import { ref, PropType } from 'vue';
// Utils // Utils
import { createNamespace } from '../utils'; import { createNamespace, getZIndexStyle } from '../utils';
import { BORDER_TOP_BOTTOM } from '../utils/constant'; import { BORDER_TOP_BOTTOM } from '../utils/constant';
import { callInterceptor, Interceptor } from '../utils/interceptor'; import { callInterceptor, Interceptor } from '../utils/interceptor';
@ -69,7 +69,7 @@ export default createComponent({
return ( return (
<div <div
ref={root} ref={root}
style={{ zIndex: zIndex !== undefined ? +zIndex : undefined }} style={getZIndexStyle(zIndex)}
class={[ class={[
bem({ unfit: isUnfit(), fixed }), bem({ unfit: isUnfit(), fixed }),
{ [BORDER_TOP_BOTTOM]: border }, { [BORDER_TOP_BOTTOM]: border },

View File

@ -1,3 +1,4 @@
import { CSSProperties } from 'vue';
import { isDef, inBrowser } from '../base'; import { isDef, inBrowser } from '../base';
import { isNumeric } from '../validate/number'; import { isNumeric } from '../validate/number';
@ -9,7 +10,9 @@ export function addUnit(value?: string | number): string | undefined {
return isNumeric(value) ? `${value}px` : String(value); return isNumeric(value) ? `${value}px` : String(value);
} }
export function getSizeStyle(originSize?: string | number) { export function getSizeStyle(
originSize?: string | number
): CSSProperties | undefined {
if (isDef(originSize)) { if (isDef(originSize)) {
const size = addUnit(originSize); const size = addUnit(originSize);
return { return {
@ -19,6 +22,14 @@ export function getSizeStyle(originSize?: string | number) {
} }
} }
export function getZIndexStyle(zIndex?: string | number) {
const style: CSSProperties = {};
if (zIndex !== undefined) {
style.zIndex = +zIndex;
}
return style;
}
// cache // cache
let rootFontSize: number; let rootFontSize: number;