mirror of
				https://gitee.com/vant-contrib/vant.git
				synced 2025-11-04 21:02:09 +08:00 
			
		
		
		
	feat(Overlay): add lock-scroll prop (#6082)
This commit is contained in:
		
							parent
							
								
									2abc2f939e
								
							
						
					
					
						commit
						8f3baa3449
					
				@ -64,6 +64,7 @@ export default {
 | 
				
			|||||||
| duration | Animation duration | _number \| string_ | `0.3` |
 | 
					| duration | Animation duration | _number \| string_ | `0.3` |
 | 
				
			||||||
| class-name | ClassName | _string_ | - |
 | 
					| class-name | ClassName | _string_ | - |
 | 
				
			||||||
| custom-class `v2.2.5` | Custom style | _object_ | - |
 | 
					| custom-class `v2.2.5` | Custom style | _object_ | - |
 | 
				
			||||||
 | 
					| lock-scroll `v2.6.2` | Whether to lock background scroll | _boolean_ | `true` |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Events
 | 
					### Events
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -64,12 +64,13 @@ export default {
 | 
				
			|||||||
### Props
 | 
					### Props
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 参数 | 说明 | 类型 | 默认值 |
 | 
					| 参数 | 说明 | 类型 | 默认值 |
 | 
				
			||||||
| --------------------- | ---------------- | ------------------ | ------- |
 | 
					| --- | --- | --- | --- |
 | 
				
			||||||
| show | 是否展示遮罩层 | _boolean_ | `false` |
 | 
					| show | 是否展示遮罩层 | _boolean_ | `false` |
 | 
				
			||||||
| z-index | z-index 层级 | _number \| string_ | `1` |
 | 
					| z-index | z-index 层级 | _number \| string_ | `1` |
 | 
				
			||||||
| duration | 动画时长,单位秒 | _number \| string_ | `0.3` |
 | 
					| duration | 动画时长,单位秒 | _number \| string_ | `0.3` |
 | 
				
			||||||
| class-name | 自定义类名 | _string_ | - |
 | 
					| class-name | 自定义类名 | _string_ | - |
 | 
				
			||||||
| custom-style `v2.2.5` | 自定义样式 | _object_ | - |
 | 
					| custom-style `v2.2.5` | 自定义样式 | _object_ | - |
 | 
				
			||||||
 | 
					| lock-scroll `v2.6.2` | 是否锁定背景滚动,锁定时蒙层里的内容也将无法滚动 | _boolean_ | `true` |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Events
 | 
					### Events
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,5 @@
 | 
				
			|||||||
// Utils
 | 
					// Utils
 | 
				
			||||||
import { createNamespace, isDef } from '../utils';
 | 
					import { createNamespace, isDef, noop } from '../utils';
 | 
				
			||||||
import { inherit } from '../utils/functional';
 | 
					import { inherit } from '../utils/functional';
 | 
				
			||||||
import { preventDefault } from '../utils/dom/event';
 | 
					import { preventDefault } from '../utils/dom/event';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -12,6 +12,7 @@ export type OverlayProps = {
 | 
				
			|||||||
  zIndex?: number | string;
 | 
					  zIndex?: number | string;
 | 
				
			||||||
  duration: number | string | null;
 | 
					  duration: number | string | null;
 | 
				
			||||||
  className?: any;
 | 
					  className?: any;
 | 
				
			||||||
 | 
					  lockScroll?: boolean;
 | 
				
			||||||
  customStyle?: object;
 | 
					  customStyle?: object;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -46,10 +47,10 @@ function Overlay(
 | 
				
			|||||||
        vShow={props.show}
 | 
					        vShow={props.show}
 | 
				
			||||||
        style={style}
 | 
					        style={style}
 | 
				
			||||||
        class={[bem(), props.className]}
 | 
					        class={[bem(), props.className]}
 | 
				
			||||||
        onTouchmove={preventTouchMove}
 | 
					        onTouchmove={props.lockScroll ? preventTouchMove : noop}
 | 
				
			||||||
        {...inherit(ctx, true)}
 | 
					        {...inherit(ctx, true)}
 | 
				
			||||||
      >
 | 
					      >
 | 
				
			||||||
        {slots.default && slots.default()}
 | 
					        {slots.default?.()}
 | 
				
			||||||
      </div>
 | 
					      </div>
 | 
				
			||||||
    </transition>
 | 
					    </transition>
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
@ -61,6 +62,10 @@ Overlay.props = {
 | 
				
			|||||||
  duration: [Number, String],
 | 
					  duration: [Number, String],
 | 
				
			||||||
  className: null as any,
 | 
					  className: null as any,
 | 
				
			||||||
  customStyle: Object,
 | 
					  customStyle: Object,
 | 
				
			||||||
 | 
					  lockScroll: {
 | 
				
			||||||
 | 
					    type: Boolean,
 | 
				
			||||||
 | 
					    default: true,
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default createComponent<OverlayProps, OverlayEvents>(Overlay);
 | 
					export default createComponent<OverlayProps, OverlayEvents>(Overlay);
 | 
				
			||||||
 | 
				
			|||||||
@ -70,3 +70,29 @@ test('default slot', () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  expect(wrapper).toMatchSnapshot();
 | 
					  expect(wrapper).toMatchSnapshot();
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test('lock-scroll prop', () => {
 | 
				
			||||||
 | 
					  const onTouchMove = jest.fn();
 | 
				
			||||||
 | 
					  const wrapper = mount({
 | 
				
			||||||
 | 
					    template: `
 | 
				
			||||||
 | 
					      <div @touchmove="onTouchMove">
 | 
				
			||||||
 | 
					        <van-overlay :lock-scroll="lockScroll" />
 | 
				
			||||||
 | 
					      </div>
 | 
				
			||||||
 | 
					    `,
 | 
				
			||||||
 | 
					    data() {
 | 
				
			||||||
 | 
					      return {
 | 
				
			||||||
 | 
					        lockScroll: true,
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    methods: {
 | 
				
			||||||
 | 
					      onTouchMove,
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  wrapper.find('.van-overlay').trigger('touchmove');
 | 
				
			||||||
 | 
					  expect(onTouchMove).toHaveBeenCalledTimes(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  wrapper.setData({ lockScroll: false });
 | 
				
			||||||
 | 
					  wrapper.find('.van-overlay').trigger('touchmove');
 | 
				
			||||||
 | 
					  expect(onTouchMove).toHaveBeenCalledTimes(1);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user