ui/src/modal/popconfirm.js

236 lines
5.1 KiB
JavaScript
Raw Normal View History

2023-04-28 18:33:40 +08:00
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2023/03/21 16:14:10
2023-04-28 18:33:40 +08:00
*/
2023-11-22 18:19:43 +08:00
import {
css,
html,
Component,
bind,
unbind,
styleMap,
offset,
outsideClick,
clearOutsideClick
} from 'wkit'
2023-11-24 15:50:48 +08:00
import '../base/button.js'
2023-04-28 18:33:40 +08:00
const DEFAULT_TIPS = '请确认你的操作!'
2023-05-04 16:15:42 +08:00
class PopConfirm extends Component {
static props = {
2023-11-22 18:19:43 +08:00
title: 'str!',
confirmButtonType: 'str!primary'
2023-04-28 18:33:40 +08:00
}
static styles = [
css`
:host {
display: inline-flex;
}
.container {
2023-04-28 18:33:40 +08:00
position: relative;
}
.tooltip {
display: none;
2023-05-04 16:15:42 +08:00
position: fixed;
z-index: 9;
justify-content: center;
2023-05-04 16:15:42 +08:00
align-items: center;
2023-11-22 18:19:43 +08:00
max-width: 260px;
min-width: 32px;
2023-11-22 18:19:43 +08:00
padding: 8px 12px 12px;
border-radius: 3px;
2023-11-22 18:19:43 +08:00
font-size: var(--wc-popconfirm-font, 14px);
background: var(--wc-popconfirm-background, #fff);
color: var(--wc-popconfirm-color, var(--color-dark-1));
box-shadow: 0 0 3px var(--wc-popconfirm-shadow, rgba(0, 0, 0, 0.3));
2023-05-04 16:15:42 +08:00
word-break: break-all;
-webkit-user-select: none;
user-select: none;
&::after {
position: absolute;
display: block;
width: 8px;
height: 8px;
border-radius: 2px;
2023-11-22 18:19:43 +08:00
background: var(--wc-popconfirm-background, #fff);
content: '';
transform: rotate(45deg);
2023-11-22 18:19:43 +08:00
box-shadow: -1px -1px 0 var(--wc-popconfirm-shadow, rgba(0, 0, 0, 0.1));
2023-05-04 16:15:42 +08:00
}
&[placement='left-top'] {
&::after {
right: 16px;
bottom: -4px;
2023-11-22 18:19:43 +08:00
box-shadow: 1px 1px 0
var(--wc-popconfirm-shadow, rgba(0, 0, 0, 0.1));
}
}
&[placement='left-bottom'] {
&::after {
right: 16px;
top: -4px;
}
}
&[placement='right-top'] {
&::after {
left: 16px;
bottom: -4px;
2023-11-22 18:19:43 +08:00
box-shadow: 1px 1px 0
var(--wc-popconfirm-shadow, rgba(0, 0, 0, 0.1));
}
}
&[placement='right-bottom'] {
&::after {
left: 16px;
top: -4px;
}
2023-05-04 16:15:42 +08:00
}
2023-04-28 18:33:40 +08:00
}
2023-11-22 18:19:43 +08:00
`,
css`
.title {
display: flex;
align-items: center;
gap: 6px;
padding: 8px 0;
--wc-icon-size: 16px;
wc-icon {
flex-shrink: 0;
color: var(--wc-popconfirm-icon-color, var(--color-red-1));
}
}
:host([hide-icon]) {
.title wc-icon {
display: none;
}
}
.actions {
display: flex;
justify-content: flex-end;
gap: 4px;
margin-top: 8px;
wc-button {
min-width: 40px;
height: 20px;
font-size: 12px;
&:first-child {
--wc-button-border-color: none;
}
}
}
2023-04-28 18:33:40 +08:00
`
]
2023-05-04 16:15:42 +08:00
2023-11-22 18:19:43 +08:00
#poped = false
#click(ev) {
let { left, top } = offset(this)
let placement = 'left'
let styles = { display: 'block' }
2023-11-22 18:19:43 +08:00
let st = document.documentElement.scrollTop
if (this.#poped) {
return
}
2023-05-04 16:15:42 +08:00
2023-11-22 18:19:43 +08:00
if (left < 260 || (left > 260 && window.innerWidth - left > 260)) {
placement = 'right'
styles.left = left + 'px'
2023-05-04 16:15:42 +08:00
} else {
2023-11-22 18:19:43 +08:00
let right = window.innerWidth - left + this.clientWidth
styles.right = right + 'px'
2023-05-04 16:15:42 +08:00
}
2023-11-22 18:19:43 +08:00
if (top < 160) {
top += 8 + this.clientHeight
placement += '-bottom'
styles.top = top + 'px'
2023-05-04 16:15:42 +08:00
} else {
let bottom = window.innerHeight - top + 8
placement += '-top'
styles.bottom = bottom + 'px'
2023-11-22 18:19:43 +08:00
if (st > 0) {
styles.transform = `translateY(-${st}px)`
}
2023-05-04 16:15:42 +08:00
}
2023-11-22 18:19:43 +08:00
this.#poped = true
//
this.$refs.tips.setAttribute('placement', placement)
this.$refs.tips.style.cssText = styleMap(styles)
this.$refs.tips.$animate()
2023-05-04 16:15:42 +08:00
}
2023-11-22 18:19:43 +08:00
#close() {
if (this.#poped) {
this.#poped = false
this.$refs.tips.$animate(true)
}
}
#confirm() {
this.$emit('confirm')
this.#close()
}
mounted() {
2023-11-22 18:19:43 +08:00
this._outFn = outsideClick(this, _ => this.#close())
this._scrollFn = bind(document, 'scroll', ev => {
if (this.#poped) {
let st = document.documentElement.scrollTop
this.$refs.tips.style.transform = `translateY(-${st}px)`
}
})
}
unmounted() {
unbind(document, 'scroll', this._scrollFn)
clearOutsideClick(this._outFn)
2023-05-04 16:15:42 +08:00
}
2023-04-28 18:33:40 +08:00
render() {
return html`
<main class="container">
2023-11-22 18:19:43 +08:00
<div class="wrapper" @click=${this.#click}>
<slot></slot>
</div>
<div class="tooltip" ref="tips" #animation=${{}}>
2023-11-22 18:19:43 +08:00
<header class="title">
<wc-icon name="info"></wc-icon>
${this.title || DEFAULT_TIPS}
</header>
<footer class="actions">
<wc-button @click=${this.#close}>取消</wc-button>
<wc-button
solid
type=${this.confirmButtonType || 'primary'}
@click=${this.#confirm}
>确定</wc-button
>
</footer>
<i class="trigon"></i>
2023-05-04 16:15:42 +08:00
</div>
</main>
2023-04-28 18:33:40 +08:00
`
}
}
PopConfirm.reg('popconfirm')