Add focus ring to toggle switch

Signed-off-by: Phillip Rak <rak.phillip@gmail.com>
This commit is contained in:
Phillip Rak 2025-02-05 14:12:46 -07:00
parent c7a998910e
commit f5d9f9bd54
1 changed files with 36 additions and 2 deletions

View File

@ -1,5 +1,5 @@
<script lang="ts">
import { defineComponent } from 'vue';
import { defineComponent, onMounted, onBeforeUnmount, useTemplateRef } from 'vue';
type StateType = boolean | 'true' | 'false' | undefined;
@ -33,6 +33,29 @@ export default defineComponent({
emits: ['update:value'],
setup() {
const switchChrome = useTemplateRef<HTMLElement>('switchChrome');
const focus = () => {
switchChrome.value?.classList.add('focus');
};
const blur = () => {
switchChrome.value?.classList.remove('focus');
};
const switchInput = useTemplateRef<HTMLInputElement>('switchInput');
onMounted(() => {
switchInput.value?.addEventListener('focus', focus);
switchInput.value?.addEventListener('blur', blur);
});
onBeforeUnmount(() => {
switchInput.value?.removeEventListener('focus', focus);
switchInput.value?.removeEventListener('blur', blur);
});
},
data() {
return { state: false as StateType };
},
@ -64,6 +87,7 @@ export default defineComponent({
>{{ offLabel }}</span>
<label class="switch hand">
<input
ref="switchInput"
type="checkbox"
role="switch"
:checked="state"
@ -71,7 +95,10 @@ export default defineComponent({
@input="toggle(null)"
@keydown.enter="toggle(null)"
>
<span class="slider round" />
<span
ref="switchChrome"
class="slider round"
/>
</label>
<span
class="label no-select hand"
@ -121,6 +148,13 @@ $toggle-height: 16px;
background-color: var(--checkbox-disabled-bg);
-webkit-transition: .4s;
transition: .4s;
&.focus {
@include focus-outline;
outline-offset: 2px;
-webkit-transition: 0s;
transition: 0s;
}
}
.slider:before {