ui/src/drawer/index.js

171 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-03-27 19:34:30 +08:00
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2023/03/27 10:39:29
*/
2023-05-25 17:00:17 +08:00
import { css, html, Component, nextTick, styleMap } from 'wkit'
2023-03-27 19:34:30 +08:00
2023-03-29 16:16:58 +08:00
const ANIMATION = {
left: {
custom: [
{ transform: ' translateX(-100%)' },
{ transform: ' translateX(0)' }
]
},
right: {
custom: [
{ transform: ' translateX(100%)' },
{ transform: ' translateX(0)' }
]
},
top: {
custom: [
{ transform: ' translateY(-100%)' },
{ transform: ' translateY(0)' }
]
},
bottom: {
custom: [
{ transform: ' translateY(100%)' },
{ transform: ' translateY(0)' }
]
}
2023-03-27 19:34:30 +08:00
}
class Drawer extends Component {
2023-03-28 19:40:39 +08:00
static animation = {}
2023-03-27 19:34:30 +08:00
static props = {
2023-03-28 19:40:39 +08:00
title: '',
2023-03-27 19:34:30 +08:00
from: 'right',
2023-03-28 19:40:39 +08:00
visible: {
type: Boolean,
default: false,
observer(v) {
2023-04-04 12:10:40 +08:00
this.$animate(!v)
this.$refs.drawer.$animate(!v)
2023-03-28 19:40:39 +08:00
}
},
2023-03-29 16:16:58 +08:00
width: '',
height: '',
2023-05-24 11:54:26 +08:00
maskClose: false
2023-03-27 19:34:30 +08:00
}
static styles = [
css`
:host {
position: fixed;
left: 0;
top: 0;
z-index: 9;
2023-03-27 19:34:30 +08:00
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3);
}
2023-03-28 19:40:39 +08:00
2023-03-27 19:34:30 +08:00
.drawer {
display: flex;
flex-direction: column;
position: absolute;
width: 50%;
height: 100%;
font-size: 14px;
background: #fff;
box-shadow: 0 0 24px rgba(0, 0, 0, 0.2);
}
.header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px;
font-size: 16px;
color: var(--color-dark-2);
user-select: none;
wc-icon {
--size: 16px;
margin-right: 8px;
color: var(--color-grey-3);
cursor: pointer;
}
}
.wrapper {
flex: 1;
padding: 16px;
}
`,
css`
:host([from='left']),
:host([from='right']) {
.drawer {
top: 0;
2023-03-27 19:34:30 +08:00
width: 50%;
height: 100%;
}
}
:host([from='top']),
:host([from='bottom']) {
.drawer {
left: 0;
2023-03-27 19:34:30 +08:00
width: 100%;
height: 50%;
}
}
:host([from='left']) .drawer {
left: 0;
}
:host([from='right']) .drawer {
right: 0;
}
:host([from='top']) .drawer {
2023-03-27 19:34:30 +08:00
top: 0;
}
:host([from='bottom']) .drawer {
bottom: 0;
}
`
]
closeDrawer() {
this.$emit('close')
}
mounted() {
2023-03-28 19:40:39 +08:00
this.$on('click', ev => {
2023-03-29 16:16:58 +08:00
let path = ev.composedPath()
2023-05-24 11:54:26 +08:00
if (path[0] === ev.currentTarget && this.maskClose) {
2023-03-29 16:16:58 +08:00
this.closeDrawer()
}
2023-03-28 19:40:39 +08:00
})
2023-03-27 19:34:30 +08:00
}
render() {
2023-03-29 16:16:58 +08:00
let style = {}
if ((this.from === 'left' || this.from === 'right') && this.width) {
style = { width: this.width }
} else if ((this.from === 'top' || this.from === 'bottom') && this.height) {
style = { height: this.height }
}
2023-03-27 19:34:30 +08:00
return html`
2023-03-29 16:16:58 +08:00
<div
class="drawer"
style=${styleMap(style)}
ref="drawer"
#animation=${ANIMATION[this.from]}
>
2023-03-27 19:34:30 +08:00
<header class="header">
<span class="title"><slot name="title">${this.title}</slot></span>
<wc-icon name="close" @click=${this.closeDrawer}></wc-icon>
</header>
2023-03-29 16:16:58 +08:00
<main class="wrapper"><slot></slot></main>
2023-03-27 19:34:30 +08:00
</div>
`
}
}
Drawer.reg('drawer')