简化space组件;完成slider组件重构

master
yutent 2023-11-24 11:42:47 +08:00
parent 8461d75a69
commit a3f4b72ebd
2 changed files with 56 additions and 36 deletions

View File

@ -4,7 +4,7 @@
* @date 2023/04/28 16:14:10 * @date 2023/04/28 16:14:10
*/ */
import { css, html, Component, range } from 'wkit' import { css, html, Component, bind, unbind, range, offset } from 'wkit'
class Slider extends Component { class Slider extends Component {
static props = { static props = {
@ -38,6 +38,7 @@ class Slider extends Component {
} }
}, },
vertical: false, vertical: false,
range: false,
disabled: false disabled: false
} }
@ -53,7 +54,6 @@ class Slider extends Component {
display: flex; display: flex;
align-items: center; align-items: center;
width: 100%; width: 100%;
min-height: 32px;
} }
.container { .container {
@ -83,9 +83,9 @@ class Slider extends Component {
// vertical // vertical
css` css`
:host([vertical]) { :host([vertical]) {
width: auto; width: 32px;
height: 100%; height: 100%;
align-items: flex-end; justify-content: center;
.container { .container {
width: var(--line-width); width: var(--line-width);
@ -127,7 +127,6 @@ class Slider extends Component {
position: absolute; position: absolute;
left: 0; left: 0;
top: calc((32px - var(--line-width)) / -2); top: calc((32px - var(--line-width)) / -2);
z-index: 2;
width: 32px; width: 32px;
height: 32px; height: 32px;
padding: 7px; padding: 7px;
@ -187,6 +186,23 @@ class Slider extends Component {
} }
} }
} }
:host([hide-thumb]) {
.thumb::before {
display: none;
}
}
`,
// disabled
css`
:host([disabled]) {
cursor: not-allowed;
opacity: 0.6;
.thumb::before {
cursor: not-allowed;
transform: unset;
}
}
` `
] ]
@ -224,18 +240,44 @@ class Slider extends Component {
} }
#seek(ev) { #seek(ev) {
let { clientWidth, clientHeight } = ev.target let { clientWidth: w, clientHeight: h } = ev.target
let { min, max, step } = this let { min, max, step } = this
let val = 0 let val = 0
if (this.vertical) { if (this.vertical) {
val = (clientHeight - ev.offsetY) / clientHeight val = (h - ev.y) / h
} else { } else {
val = ev.offsetX / clientWidth val = ev.x / w
} }
val *= this.#len val *= this.#len
val += min val += min
this.value = this.#fix(val) this.value = this.#fix(val)
this.$emit('input')
}
#mousedown(ev) {
let target = ev.currentTarget
let doc = document
let opts = { once: true }
let { left, top } = offset(target)
let { scrollLeft, scrollTop } = document.documentElement
let _x = left - scrollLeft
let _y = top - scrollTop
if (this.disabled) {
return
}
this.#seek({ target, x: ev.x - _x, y: ev.y - _y })
let callback = bind(doc, 'mousemove', ({ x, y }) => {
x -= _x
y -= _y
this.#seek({ target, x, y })
})
bind(doc, 'mouseup', _ => unbind(doc, 'mousemove', callback), opts)
} }
get #activeStyle() { get #activeStyle() {
@ -256,17 +298,13 @@ class Slider extends Component {
let n = this.hasAttribute('show-stops') ? this.#stops : 1 let n = this.hasAttribute('show-stops') ? this.#stops : 1
return html` return html`
<main class="container"> <main class="container" @mousedown=${this.#mousedown}>
<div class="slider-bar"> <div class="slider-bar">
${range(1, n).map(i => { ${range(1, n).map(i => {
return html`<i style="left:${i * this.#point}%"></i>` return html`<i style="left:${i * this.#point}%"></i>`
})} })}
</div> </div>
<div <div class="slider-bar" style=${this.#activeStyle}></div>
class="slider-bar"
style=${this.#activeStyle}
@click=${this.#seek}
></div>
<div class="thumb" style=${this.#thumbStyle}> <div class="thumb" style=${this.#thumbStyle}>
<mark class="value">${this.value}</mark> <mark class="value">${this.value}</mark>

View File

@ -8,18 +8,17 @@ import { css, html, Component } from 'wkit'
class Space extends Component { class Space extends Component {
static styles = css` static styles = css`
@use 'sass:math';
:host { :host {
display: block; display: block;
} }
.container { .container {
--gaps: var(--wc-space-gap, 12px);
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
align-items: center; align-items: center;
width: 100%; width: 100%;
padding: 6px 0; padding: calc(var(--gaps) / 2) 0;
gap: 12px; gap: var(--gaps);
} }
:host([vertical]) { :host([vertical]) {
.container { .container {
@ -31,27 +30,10 @@ class Space extends Component {
justify-content: space-between; justify-content: space-between;
} }
} }
$gaps: (
's': 4px,
'm': 8px,
'l': 12px,
'xl': 16px,
'xxl': 20px,
'xxxl': 24px
);
@loop $k, $v in $gaps {
:host([gap='#{$k}']) {
.container {
padding: math.div($v, 2) 0;
gap: $v;
}
}
}
` `
render() { render() {
return html`<div class="container"><slot /></div>` return html`<div class="container"><slot></slot></div>`
} }
} }