Merge branch 'master' of ssh://github.com/bd-js/wcui

master
chenjiajian 2023-04-10 14:06:39 +08:00
commit 9347e5a0f7
2 changed files with 124 additions and 31 deletions

15
src/drag/index.js Normal file
View File

@ -0,0 +1,15 @@
import { css, html, Component, nextTick, styleMap } from '@bd/core'
import Core from './core.js'
class Drag extends Component {
static props = {
overflow: false,
limit: 'window',
'disable-x': false,
'disable-y': false,
by: String
}
}
Drag.reg('drag')

View File

@ -34,6 +34,10 @@ class Layer extends Component {
this.#wrapped = !BUILDIN_TYPES.includes(v) this.#wrapped = !BUILDIN_TYPES.includes(v)
} }
}, },
left: { type: String, attribute: false },
right: { type: String, attribute: false },
top: { type: String, attribute: false },
bottom: { type: String, attribute: false },
fixed: false, fixed: false,
mask: false, mask: false,
'mask-close': false, 'mask-close': false,
@ -58,6 +62,14 @@ class Layer extends Component {
:host([type]) { :host([type]) {
display: flex; display: flex;
} }
:host([type='toast']),
:host([type='notify']),
:host([type='common']) {
.layer {
position: absolute;
}
}
.noselect { .noselect {
-webkit-touch-callout: none; -webkit-touch-callout: none;
user-select: none; user-select: none;
@ -70,7 +82,6 @@ class Layer extends Component {
css` css`
.layer { .layer {
overflow: hidden;
flex: 0 auto; flex: 0 auto;
position: relative; position: relative;
z-index: 65535; z-index: 65535;
@ -82,11 +93,6 @@ class Layer extends Component {
transition: opacity 0.2s ease-in-out, left 0.2s ease-in-out, transition: opacity 0.2s ease-in-out, left 0.2s ease-in-out,
right 0.2s ease-in-out, top 0.2s ease-in-out, bottom 0.2s ease-in-out; right 0.2s ease-in-out, top 0.2s ease-in-out, bottom 0.2s ease-in-out;
&.scale {
transform: scale(1.01);
transition: transform 0.1s linear;
}
&:active { &:active {
z-index: 65536; z-index: 65536;
} }
@ -129,9 +135,13 @@ class Layer extends Component {
} }
::slotted(&__toast) { ::slotted(&__toast) {
flex-shrink: 0;
flex: 1;
display: flex; display: flex;
align-items: center; align-items: center;
width: 300px; width: 300px;
min-height: 40px;
margin-bottom: 15px !important;
padding: 0 10px !important; padding: 0 10px !important;
border-radius: 3px; border-radius: 3px;
font-weight: normal; font-weight: normal;
@ -139,6 +149,9 @@ class Layer extends Component {
--size: 16px; --size: 16px;
color: var(--color-dark-1); color: var(--color-dark-1);
} }
::slotted(&__toast + &__toast) {
margin-top: 30px;
}
::slotted(&__toast.style-info) { ::slotted(&__toast.style-info) {
border: 1px solid #ebeef5; border: 1px solid #ebeef5;
@ -226,8 +239,7 @@ class Layer extends Component {
css` css`
:host([mask]) { :host([mask]) {
height: 100%; height: 100%;
background: rgba(255, 255, 255, 0.15); background: rgba(0, 0, 0, 0.2);
backdrop-filter: blur(5px);
} }
:host([type='alert']), :host([type='alert']),
@ -258,6 +270,7 @@ class Layer extends Component {
box-shadow: none; box-shadow: none;
&__content { &__content {
flex-direction: column;
min-height: 40px; min-height: 40px;
} }
} }
@ -322,6 +335,40 @@ class Layer extends Component {
} }
} }
#play() {
switch (this.type) {
case 'toast':
let elem = this.lastElementChild
elem._anim = elem.animate(
[
{ marginTop: '-30px', opacity: 0 },
{ marginTop: '0', opacity: 1 }
],
{
duration: 200,
fill: 'forwards'
}
)
setTimeout(() => {
elem._anim.reverse()
elem._anim.onfinish = _ => {
elem.remove()
}
if (this.children.length === 0) {
this.close()
toastInstance = null
}
}, 3000)
break
case 'notify':
break
default:
this.$refs.box.$animate()
break
}
}
mounted() { mounted() {
if (this.type === 'prompt') { if (this.type === 'prompt') {
this.$refs.input = this.firstElementChild this.$refs.input = this.firstElementChild
@ -337,15 +384,14 @@ class Layer extends Component {
if (path[0] === ev.currentTarget) { if (path[0] === ev.currentTarget) {
if (UNIQUE_TYPES.includes(this.type)) { if (UNIQUE_TYPES.includes(this.type)) {
this.$refs.box.classList.toggle('scale', true) return
setTimeout(_ => { }
this.$refs.box.classList.remove('scale')
}, 100) if (this['mask-close']) {
} else if (this['mask-close']) {
if (this.#wrapped === false) { if (this.#wrapped === false) {
this.#reject() this.#reject()
} }
this.close() this.$refs.box.$animate(true).then(_ => this.close())
} }
} }
}) })
@ -357,11 +403,19 @@ class Layer extends Component {
this.#toggleDrag() this.#toggleDrag()
this.$refs.box.$animate() this.#play()
} }
updated() { updated() {
this.$refs.box.$animate() this.#play()
}
moveTo(obj = {}) {
var css = ''
for (var k in obj) {
css += `${k}:${obj[k]};`
}
this.$refs.box.style.cssText += css
} }
show() { show() {
@ -442,8 +496,22 @@ class Layer extends Component {
} }
render() { render() {
let { type, mask, left, right, top, bottom } = this
let styles = ''
if (type === 'common' && mask === false) {
top = top || 0
}
styles = styleMap({ left, right, top, bottom })
return html` return html`
<div ref="box" #animation=${{ type: 'micro-bounce' }} class="layer"> <div
ref="box"
class="layer"
#animation=${{ type: 'micro-bounce' }}
style=${styles}
>
<div <div
class="layer__title noselect" class="layer__title noselect"
style=${styleMap({ display: !!this.title ? '' : 'none' })} style=${styleMap({ display: !!this.title ? '' : 'none' })}
@ -475,6 +543,7 @@ class Layer extends Component {
function layer(opt = {}) { function layer(opt = {}) {
let layDom = document.createElement('wc-layer') let layDom = document.createElement('wc-layer')
let { type = 'common', content = '' } = opt let { type = 'common', content = '' } = opt
let alreadyInTree = false
if (type === 'toast') { if (type === 'toast') {
opt = { opt = {
@ -485,18 +554,18 @@ function layer(opt = {}) {
} }
if (toastInstance) { if (toastInstance) {
toastInstance.close(true) // toastInstance.close(true)
layDom = toastInstance
alreadyInTree = true
} else {
toastInstance = layDom
} }
toastInstance = layDom layDom.top = '20px'
} else { } else {
layDom.mask = opt.mask layDom.mask = opt.mask
if (opt.btns === false) { if (opt.btns && opt.btns.length) {
layDom.btns = []
} else if (opt.btns && opt.btns.length) {
layDom.btns = opt.btns layDom.btns = opt.btns
} else {
layDom.btns = LANG_BTNS.concat()
} }
if (opt.intercept && typeof opt.intercept === 'function') { if (opt.intercept && typeof opt.intercept === 'function') {
@ -511,9 +580,6 @@ function layer(opt = {}) {
} }
/* 额外样式 */ /* 额外样式 */
layDom['mask-color'] = opt['mask-color']
layDom.blur = opt.blur
layDom.radius = opt.radius layDom.radius = opt.radius
layDom.background = opt.background layDom.background = opt.background
@ -545,8 +611,17 @@ function layer(opt = {}) {
layDom.fixed = !!opt.fixed layDom.fixed = !!opt.fixed
layDom.innerHTML = content if (alreadyInTree) {
document.body.appendChild(layDom) let tmp = document.createElement('template')
tmp.innerHTML = opt.content
layDom.appendChild(tmp.content.cloneNode(true))
layDom.updated()
} else {
layDom.innerHTML = content
document.body.appendChild(layDom)
}
return layDom.promise return layDom.promise
} }
@ -615,7 +690,10 @@ layer.notify = function (content) {
type: 'notify', type: 'notify',
title: '通知', title: '通知',
content, content,
blur: true, fixed: true,
mask: false,
top: 0,
right: 0,
from: { right: '-300px', top: 0 }, from: { right: '-300px', top: 0 },
to: { right: 0 } to: { right: 0 }
}) })
@ -640,7 +718,7 @@ layer.toast = function (txt, type = 'info') {
return this({ return this({
content: ` content: `
<div class="layer__content__toast style-${type}"> <div class="layer__content__toast style-${type}">
<wc-icon is="${ico}"></wc-icon> <wc-icon name="${ico}"></wc-icon>
<span class="toast-txt">${txt}</span> <span class="toast-txt">${txt}</span>
</div>`, </div>`,
type: 'toast' type: 'toast'