2019-08-25 05:23:43 +01:00
|
|
|
<span class="length-indicator {overLimit ? 'over-char-limit' : ''}"
|
2022-12-10 18:40:37 +00:00
|
|
|
aria-live={lengthVerbosity}
|
|
|
|
aria-atomic='true'
|
2019-08-25 05:23:43 +01:00
|
|
|
{style}
|
|
|
|
>{lengthToDisplayDeferred}</span>
|
|
|
|
<style>
|
|
|
|
.length-indicator {
|
|
|
|
color: var(--length-indicator-color);
|
|
|
|
font-size: 1.3em;
|
|
|
|
}
|
|
|
|
|
|
|
|
.length-indicator.over-char-limit {
|
|
|
|
color: var(--warning-color);
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
2021-07-05 04:19:04 +01:00
|
|
|
import { mark, stop } from '../_utils/marks.js'
|
|
|
|
import { store } from '../_store/store.js'
|
2019-08-25 05:23:43 +01:00
|
|
|
import { observe } from 'svelte-extras'
|
2021-07-05 04:19:04 +01:00
|
|
|
import { throttleTimer } from '../_utils/throttleTimer.js'
|
2019-08-25 05:23:43 +01:00
|
|
|
const updateDisplayedLength = process.browser && throttleTimer(requestAnimationFrame)
|
|
|
|
|
2022-12-10 18:40:37 +00:00
|
|
|
// How many chars within the limit to start warning at
|
|
|
|
const WARN_THRESHOLD = 10
|
|
|
|
|
2019-08-25 05:23:43 +01:00
|
|
|
export default {
|
|
|
|
oncreate () {
|
|
|
|
const { lengthToDisplay } = this.get()
|
|
|
|
this.set({ lengthToDisplayDeferred: lengthToDisplay })
|
|
|
|
// perf improvement for keyboard input latency
|
|
|
|
this.observe('lengthToDisplay', () => {
|
|
|
|
updateDisplayedLength(() => {
|
|
|
|
mark('set lengthToDisplayDeferred')
|
|
|
|
const { lengthToDisplay } = this.get()
|
|
|
|
this.set({ lengthToDisplayDeferred: lengthToDisplay })
|
|
|
|
stop('set lengthToDisplayDeferred')
|
|
|
|
})
|
|
|
|
}, { init: false })
|
|
|
|
},
|
|
|
|
data: () => ({
|
|
|
|
lengthToDisplayDeferred: 0,
|
|
|
|
style: ''
|
|
|
|
}),
|
|
|
|
store: () => store,
|
|
|
|
computed: {
|
|
|
|
lengthToDisplay: ({ length, max }) => max - length,
|
2022-12-10 18:40:37 +00:00
|
|
|
lengthVerbosity: ({ lengthToDisplayDeferred }) => {
|
|
|
|
// When approaching the limit, notify screen reader users
|
|
|
|
if (lengthToDisplayDeferred > WARN_THRESHOLD) {
|
|
|
|
return 'off'
|
2019-08-25 05:23:43 +01:00
|
|
|
} else {
|
2022-12-10 18:40:37 +00:00
|
|
|
return 'polite'
|
2019-08-25 05:23:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
observe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|