pinafore/routes/_components/compose/ComposeLengthGauge.html

50 lines
1.7 KiB
HTML
Raw Normal View History

2018-03-04 01:12:48 +01:00
<div class="compose-box-length-gauge {{shouldAnimate ? 'should-animate' : ''}} {{overLimit ? 'over-char-limit' : ''}}"
style="transform: scaleX({{lengthAsFractionDeferred || 0}});"
2018-02-27 06:50:03 +01:00
aria-hidden="true"
></div>
<style>
.compose-box-length-gauge {
grid-area: gauge;
margin: 0 0 5px 5px;
height: 2px;
background: var(--main-theme-color);
transform-origin: left;
}
.compose-box-length-gauge.should-animate {
transition: transform 0.2s linear;
}
.compose-box-length-gauge.over-char-limit {
background: var(--warning-color);
}
</style>
<script>
import { CHAR_LIMIT } from '../../_static/statuses'
import { mark, stop } from '../../_utils/marks'
2018-02-27 07:22:56 +01:00
import { store } from '../../_store/store'
import { scheduleIdleTask } from '../../_utils/scheduleIdleTask'
2018-02-27 06:50:03 +01:00
export default {
oncreate() {
let { lengthAsFraction } = this.get()
this.set({lengthAsFractionDeferred: lengthAsFraction})
2018-02-27 07:22:56 +01:00
// perf improvement for keyboard input latency
this.observe('lengthAsFraction', () => {
scheduleIdleTask(() => {
mark('set lengthAsFractionDeferred')
let { lengthAsFraction } = this.get() || {} // TODO: wtf svelte?
this.set({lengthAsFractionDeferred: lengthAsFraction})
stop('set lengthAsFractionDeferred')
2018-02-27 06:50:03 +01:00
requestAnimationFrame(() => this.set({shouldAnimate: true}))
})
}, {init: false})
2018-02-27 06:50:03 +01:00
},
2018-02-27 07:22:56 +01:00
store: () => store,
2018-02-27 06:50:03 +01:00
computed: {
lengthAsFraction: (length) => {
2018-02-27 06:50:03 +01:00
// We don't need to update the gauge for every decimal point, so round it to the nearest 0.02
let int = Math.round(Math.min(CHAR_LIMIT, length) / CHAR_LIMIT * 100)
return (int - (int % 2)) / 100
2018-02-27 06:50:03 +01:00
}
}
}
</script>