refactor(message): code clean

This commit is contained in:
07akioni 2019-12-06 18:23:01 +08:00
parent 6b427bd229
commit 7c3f6b9aef
7 changed files with 83 additions and 110 deletions

View File

@ -1,5 +1,4 @@
# Icon
# Basic
```html
<n-button @click="emitMessage">
Hourglass Icon
@ -15,7 +14,7 @@ export default {
methods: {
emitMessage() {
this.$NMessage.warning("I never needed anybody's help in any way", {
icon: mdHourglass
icon: h => h(mdHourglass)
})
}
}

View File

@ -27,7 +27,8 @@ export default {
render (h) {
let icon = null
if (this.icon) {
icon = h(this.icon)
if (typeof this.icon === 'function') icon = this.icon(h)
else icon = h(this.icon)
} else if (this.type === 'success') {
icon = h(mdCheckmarkCircle)
} else if (this.type === 'error') {
@ -37,10 +38,12 @@ export default {
} else if (this.type === 'warning') {
icon = h(mdAlert)
}
const theme = this.option.theme
return h('div', {
staticClass: 'n-message',
class: {
[`n-message--${this.type}`]: true
[`n-message--${this.type}`]: true,
[`n-${theme}-theme`]: theme
}
}, [
h('div', {

View File

@ -1,13 +1,5 @@
import Message from './Message'
function setTheme (messageContainer) {
const themeClasses = Array.from(messageContainer.classList).filter(c =>
c.endsWith('-theme')
)
themeClasses.forEach(c => messageContainer.classList.remove(c))
if (this.theme) messageContainer.classList.add(`n-${this.theme}-theme`)
}
function attachMessageContainer () {
let messageContainer = document.querySelector('.n-message-container')
if (!messageContainer) {
@ -16,11 +8,10 @@ function attachMessageContainer () {
messageContainer.style = `z-index: ${this.options.zIndex}; top: ${this.options.top}px;`
document.body.appendChild(messageContainer)
}
setTheme.call(this, messageContainer)
return messageContainer
}
function registerMessageEl (container, el, option) {
function mountMessage (container, el, option) {
el.classList.add('n-message--enter')
container.appendChild(el)
el.getBoundingClientRect()
@ -67,15 +58,15 @@ const NMessage = {
zIndex: 6000,
top: 20
},
theme: null,
attachMessageContainer,
notice (content, option) {
// console.log('test', this)
const messageContainer = this.attachMessageContainer()
const messageCell = new this.Vue({
...Message,
propsData: { option, content }
propsData: { option: { theme: this.theme, ...option }, content }
}).$mount()
registerMessageEl(messageContainer, messageCell.$el, mixinOption(option))
mountMessage(messageContainer, messageCell.$el, mixinOption(option))
},
info (content, option) {
option = mixinOption(option)

View File

@ -1,7 +0,0 @@
import Scaffold from './src/main.vue'
Scaffold.install = function (Vue) {
Vue.component(Scaffold.name, Scaffold)
}
export default Scaffold

View File

@ -1,13 +0,0 @@
<template>
<div>example</div>
</template>
<script>
export default {
name: 'NNimbusScaffold'
}
</script>
<style lang="scss" scoped>
</style>

View File

@ -1,28 +1,27 @@
function getTheme (component) {
let cursor = component
function getTheme (componentInstance, property) {
let cursor = componentInstance
while (cursor.$parent) {
const name = cursor.$options.name
if (cursor.synthesizedTheme) {
return cursor.synthesizedTheme
}
if (name === 'NConfigProvider') {
return cursor.theme || null
return cursor.synthesizedTheme || null
}
cursor = cursor.$parent
}
return null
}
function install (Vue, Component, name) {
function install (Vue, property, name) {
const prototypeProxy = new Proxy(
function () {
return Component
return property
},
{
apply (target, thisArg, argumentsList) {
if (thisArg instanceof Vue) {
Component.theme = getTheme(thisArg)
// console.log('theme', getTheme(thisArg))
property.theme = getTheme(thisArg, property)
}
return target.bind(thisArg)(...argumentsList)
}

View File

@ -12,71 +12,72 @@
}
}
@include themes-mixin {
@include b(message-container) {
@include once {
position: fixed;
left: 0;
right: 0;
height: 0;
overflow: visible;
display: flex;
flex-direction: column;
align-items: center;
}
@include b(message) {
@include once {
display: flex;
transition: opacity .3s $default-cubic-bezier, transform .3s $default-cubic-bezier, max-height .3s $default-cubic-bezier, margin-bottom .3s $default-cubic-bezier;
max-height: 40px;
opacity: 1;
margin-bottom: 12px;
padding: 0 40px;
height: 40px;
border-radius: 20px;
flex-shrink: 0;
font-weight: 700;
overflow: hidden;
}
color: $--message-text-color;
@include once {
@include e(content) {
display: inline-block;
height: 40px;
line-height: 40px;
font-size: 15px;
}
@include e(icon) {
margin-right: 10px;
display: inline-flex;
height: 40px;
line-height: 40px;
align-items: center;
i::before {
font-size: 20px;
}
}
}
@include once {
@include m(enter) {
opacity: 0;
transform: translateY(-12px) scale(.5);
transform-origin: top center;
max-height: 0px;
margin-bottom: 0;
}
@include m(leave) {
opacity: 0;
transform-origin: top center;
transform: translateY(-12px) scale(.5);
max-height: 0px;
margin-bottom: 0;
}
}
@include message-type-mixin('info');
@include message-type-mixin('success');
@include message-type-mixin('error');
@include message-type-mixin('warning');
}
@include b(message-container) {
@include once {
position: fixed;
left: 0;
right: 0;
height: 0;
overflow: visible;
display: flex;
flex-direction: column;
align-items: center;
}
}
@include themes-mixin {
@include b(message) {
@include once {
display: flex;
transition: opacity .3s $default-cubic-bezier, transform .3s $default-cubic-bezier, max-height .3s $default-cubic-bezier, margin-bottom .3s $default-cubic-bezier;
max-height: 40px;
opacity: 1;
margin-bottom: 12px;
padding: 0 40px;
height: 40px;
border-radius: 20px;
flex-shrink: 0;
font-weight: 700;
overflow: hidden;
}
color: $--message-text-color;
@include once {
@include e(content) {
display: inline-block;
height: 40px;
line-height: 40px;
font-size: 15px;
}
@include e(icon) {
margin-right: 10px;
display: inline-flex;
height: 40px;
line-height: 40px;
align-items: center;
i::before {
font-size: 20px;
}
}
}
@include once {
@include m(enter) {
opacity: 0;
transform: translateY(-12px) scale(.5);
transform-origin: top center;
max-height: 0px;
margin-bottom: 0;
}
@include m(leave) {
opacity: 0;
transform-origin: top center;
transform: translateY(-12px) scale(.5);
max-height: 0px;
margin-bottom: 0;
}
}
@include message-type-mixin('info');
@include message-type-mixin('success');
@include message-type-mixin('error');
@include message-type-mixin('warning');
}
}