Skip to content

Commit

Permalink
perf: refactor snackbar
Browse files Browse the repository at this point in the history
  • Loading branch information
BeADre committed Dec 9, 2020
1 parent 75e3df6 commit fd58373
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 169 deletions.
122 changes: 122 additions & 0 deletions packages/varlet-ui/src/snackbar-core/SnackbarCore.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<template>
<div
class="var-snackbar"
:style="{ alignItems, pointerEvents: forbidClick ? 'auto' : 'none' }"
v-show="show"
>
<div
:class="`var-snackbar__wrapper var-elevation--4 ${
vertical ? 'var-snackbar__vertical' : ''
}`"
:style="snackbarStyle"
>
<div :class="`var-snackbar__content ${contentClass}`">
<var-loading
:type="loadingType"
:size="loadingSize"
v-if="type === 'loading'"
/>
<slot>
{{ content }}
</slot>
</div>
<div class="var-snackbar__action">
<var-button
type="primary"
size="small"
v-if="['success', 'error', 'info', 'warning'].includes(type)"
>
{{ type }}
</var-button>
<slot name="action"></slot>
</div>
</div>
</div>
</template>

<script lang="ts">
import {
defineComponent,
reactive,
computed,
watch,
ref,
Ref,
onMounted,
} from 'vue'
import Loading from '../loading'
import Button from '../button'
import { useZIndex } from '../context/zIndex'
import { useTeleport } from '../utils/teleport'
import { props, emits } from './propsEmits'
import { useLock } from '../context/lock'
export default defineComponent({
name: 'VarSnackbarCore',
components: {
[Loading.name]: Loading,
[Button.name]: Button,
},
props,
emits,
setup(props, ctx) {
const timer: Ref<any> = ref(null)
const { disabled } = useTeleport()
const { zIndex } = useZIndex(props, 'show', 1)
useLock(props, 'show', 'lockScroll')
const alignItems = computed(() => {
if (props.position === 'top') return 'flex-start'
if (props.position === 'center') return 'center'
if (props.position === 'bottom') return 'flex-end'
})
const snackbarStyle = reactive({
backgroundColor: props.color,
width: typeof props.width === 'string' ? props.width : props.width + 'px',
height:
typeof props.height === 'string' ? props.height : props.height + 'px',
zIndex,
})
watch(
() => props.show,
(show) => {
if (show) {
props.onOpen && props.onOpen()
timer.value = setTimeout(() => {
ctx.emit('update:show', false)
}, props.duration)
} else if (show === false) {
props.onClose && props.onClose()
}
}
)
watch(
() => props._update,
() => {
clearTimeout(timer.value)
timer.value = setTimeout(() => {
ctx.emit('update:show', false)
}, props.duration)
}
)
onMounted(() => {
if (props.show) {
props.onOpen && props.onOpen()
timer.value = setTimeout(() => {
ctx.emit('update:show', false)
}, props.duration)
}
})
return {
snackbarStyle,
alignItems,
disabled,
}
},
})
</script>

<style lang="less">
@import 'snackbarCore';
</style>
7 changes: 7 additions & 0 deletions packages/varlet-ui/src/snackbar-core/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Snackbar = require('../../../cjs/snackbar-core').default
const { render } = require('@testing-library/vue')

test('test snackbar-core', async () => {
const wrapper = render(Snackbar)
console.log(wrapper)
})
7 changes: 7 additions & 0 deletions packages/varlet-ui/src/snackbar-core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import SnackbarCore from './SnackbarCore.vue'

SnackbarCore.install = function (app: any) {
app.component(SnackbarCore.name, SnackbarCore)
}

export default SnackbarCore
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ export const props = {
_update: {
type: String,
},
_isDeclarative: {
type: Boolean,
default: true,
},
}

export const emits = {
'update:show': null,
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@import "../styles/elevation";
@import "../styles/common";

.var-transition-group {
position: absolute;
left: 0;
Expand All @@ -7,9 +9,11 @@
bottom: 0;
pointer-events: none;
}
.var-pointer-auto{

.var-pointer-auto {
pointer-events: auto
}

.var-snackbar {
display: flex;
justify-content: center;
Expand Down
173 changes: 24 additions & 149 deletions packages/varlet-ui/src/snackbar/Snackbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,166 +3,41 @@
name="var-snackbar-fade"
@after-enter="onOpened"
@after-leave="onClosed"
v-if="_isDeclarative"
>
<div
class="var-snackbar"
:style="{
alignItems,
pointerEvents: forbidClick ? 'auto' : 'none',
position: 'absolute',
}"
v-show="show"
<var-snackbar-core
v-bind="$props"
v-on="$event"
@update:show="update"
class="var--absolute"
>
<div
:class="`var-snackbar__wrapper var-elevation--4 ${
vertical ? 'var-snackbar__vertical' : ''
}`"
:style="snackbarStyle"
>
<div :class="`var-snackbar__content ${contentClass}`">
<var-loading
:type="loadingType"
:size="loadingSize"
v-if="type === 'loading'"
/>
<slot>
{{ content }}
</slot>
</div>
<div class="var-snackbar__action">
<var-button
type="primary"
size="small"
v-if="['success', 'error', 'info', 'warning'].includes(type)"
>
{{ type }}
</var-button>
<slot name="action"></slot>
</div>
</div>
</div>
</transition>

<div
class="var-snackbar"
:style="{ alignItems, pointerEvents: forbidClick ? 'auto' : 'none' }"
v-show="show"
v-else
>
<div
:class="`var-snackbar__wrapper var-elevation--4 ${
vertical ? 'var-snackbar__vertical' : ''
}`"
:style="snackbarStyle"
>
<div :class="`var-snackbar__content ${contentClass}`">
<var-loading
:type="loadingType"
:size="loadingSize"
v-if="type === 'loading'"
/>
<slot>
{{ content }}
</slot>
</div>
<div class="var-snackbar__action">
<var-button
type="primary"
size="small"
v-if="['success', 'error', 'info', 'warning'].includes(type)"
>
{{ type }}
</var-button>
<slot>
{{ content }}
</slot>
<template #action>
<slot name="action"></slot>
</div>
</div>
</div>
</template>
</var-snackbar-core>
</transition>
</template>

<script lang="ts">
import {
defineComponent,
reactive,
computed,
watch,
ref,
Ref,
onMounted,
} from 'vue'
import Loading from '../loading'
import Button from '../button'
import { useZIndex } from '../context/zIndex'
import { useTeleport } from '../utils/teleport'
import { props } from './propsEmits'
import { useLock } from '../context/lock'
<script>
import SnackbarCore from '../snackbar-core'
import { props, emits } from '../snackbar-core/propsEmits'
export default defineComponent({
export default {
name: 'VarSnackbar',
components: {
[Loading.name]: Loading,
[Button.name]: Button,
[SnackbarCore.name]: SnackbarCore,
},
props,
emits: ['update:show'],
setup(props, ctx) {
const timer: Ref<any> = ref(null)
const { disabled } = useTeleport()
const { zIndex } = useZIndex(props, 'show', 2)
useLock(props, 'show', 'lockScroll')
const alignItems = computed(() => {
if (props.position === 'top') return 'flex-start'
if (props.position === 'center') return 'center'
if (props.position === 'bottom') return 'flex-end'
})
const snackbarStyle = reactive({
backgroundColor: props.color,
width: typeof props.width === 'string' ? props.width : props.width + 'px',
height:
typeof props.height === 'string' ? props.height : props.height + 'px',
zIndex,
})
watch(
() => props.show,
(show) => {
if (show) {
props.onOpen && props.onOpen()
timer.value = setTimeout(() => {
ctx.emit('update:show', false)
}, props.duration)
} else if (show === false) {
props.onClose && props.onClose()
}
}
)
watch(
() => props._update,
() => {
clearTimeout(timer.value)
timer.value = setTimeout(() => {
ctx.emit('update:show', false)
}, props.duration)
}
)
onMounted(() => {
if (props.show) {
props.onOpen && props.onOpen()
timer.value = setTimeout(() => {
ctx.emit('update:show', false)
}, props.duration)
}
})
emits,
setup(props, { emit }) {
const update = (value) => {
emit('update:show', value)
}
return {
snackbarStyle,
alignItems,
disabled,
update,
}
},
})
}
</script>

<style lang="less">
@import './snackbar';
</style>
7 changes: 4 additions & 3 deletions packages/varlet-ui/src/snackbar/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const Snackbar = require('../../../cjs/snackbar').default
const SnackbarTransition = require('../../../cjs/snackbar-core-transition')
.default
const { render } = require('@testing-library/vue')

test('test snackbar', async () => {
const wrapper = render(Snackbar)
test('test snackbarTransition', async () => {
const wrapper = render(SnackbarTransition)
console.log(wrapper)
})
Loading

0 comments on commit fd58373

Please sign in to comment.