Skip to content

Instantly share code, notes, and snippets.

@blocka
Created November 8, 2017 16:49
Show Gist options
  • Save blocka/362721a9ebb60c66669efacd27852628 to your computer and use it in GitHub Desktop.
Save blocka/362721a9ebb60c66669efacd27852628 to your computer and use it in GitHub Desktop.
Example modal
<template>
<div>
<div class="modal-mask" v-if="show" @click="requestClose && requestClose()">
<div class="modal-wrapper">
<div class="modal-container" @click.stop :style="{width: width + 'px'}">
<slot></slot>
</div>
</div>
</div>
</div>
</template>
<style scoped>
.modal-mask {
position: fixed;
z-index: 9998;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, .8);
display: block;
transition: opacity .3s ease;
overflow: auto;
padding-top: 100px;
}
.modal-wrapper {
display: block;
vertical-align: middle;
}
.modal-container {
width: 500px;
margin: 0px auto;
padding: 0;
background-color: #fff;
border-radius: 2px;
box-shadow: 0 2px 8px rgba(0, 0, 0, .2);
transition: all .3s ease;
}
</style>
<script>
export default {
name: 'Modal',
watch: {
show (show) {
if (show) {
document.body.parentElement.classList.add('noscroll');
}
else {
document.body.parentElement.classList.remove('noscroll');
}
}
},
props: {
show: {
type: Boolean
},
title: {
type: String
},
requestClose: {
type: Function
},
width: {
type: String
}
}
};
</script>
@M-Zuber
Copy link

M-Zuber commented Nov 9, 2017

A few small changes, mainly actually using the title, and passing requestClose into the slot

<template>
<div>
	<div class="modal-mask" v-if="show" @click="requestClose && requestClose()">
		<div class="modal-wrapper">
			<div class="modal-container" @click.stop :style="{width: width + 'px', height: height + 'px'}">
				<div v-if="title" class="modal-header">
                    <h3>{{title}}</h3>
                </div>
				<div class="modal-body">
					<slot :requestClose="requestClose"></slot>
				</div>
			</div>
		</div>
	</div>
</div>
</template>
<script>
export default {
	name: 'Modal',
	props: {
		show: {
			type: Boolean
		},
		title: {
			type: String
		},
		requestClose: {
			type: Function
		},
		width: {
			type: String
		},
		height: {
			type: String
		}
	},
	watch: {
		show (show) {
			if (show) {
				document.body.parentElement.classList.add('noscroll');
			}
			else {
				document.body.parentElement.classList.remove('noscroll');
			}
		}
	}
};
</script>
<style scoped>
.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, .8);
  display: block;
  transition: opacity .3s ease;
	overflow: auto;
	padding-top: 100px;
}
.modal-wrapper {
  display: block;
  vertical-align: middle;
}
.modal-container {
  width: 500px;
  margin: 0px auto;
  padding: 0;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .2);
  transition: all .3s ease;
}
.modal-header h3 {
    margin-top: 0;
	vertical-align: middle;
}

.modal-body {
    margin: 20px 0;
}
</style>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment