LightBulbSwitch.vue 1.1 KB
<script lang="ts" setup>
  import on from '/@/assets/icons/light-bulb-on.svg';
  import off from '/@/assets/icons/light-bulb-off.svg';

  const props = defineProps<{
    value?: boolean;
  }>();

  const emit = defineEmits(['update:value', 'change']);

  const handleChange = (event: Event) => {
    const _value = (event.target as HTMLInputElement).checked;
    emit('update:value', _value);
    emit('change', _value);
  };
</script>

<template>
  <label class="light-bulb-switch">
    <input :value="props.value" :checked="props.value" type="checkbox" @change="handleChange" />
    <div class="light-bulb__on"> <img :src="on" alt="开" /> </div>
    <div class="light-bulb__off"> <img :src="off" alt="关" /> </div>
  </label>
</template>

<style scoped lang="less">
  .light-bulb-switch {
    input[type='checkbox'] {
      display: none;
    }

    input:checked ~ .light-bulb__off {
      display: none;
    }

    input:not(:checked) ~ .light-bulb__on {
      display: none;
    }

    .light-bulb__on > img,
    .light-bulb__off > img {
      width: 48px;
      height: 48px;
      cursor: pointer;
    }
  }
</style>