api-client/packages/hoppscotch-ui/src/components/smart/Checkbox.vue

107 lines
2.3 KiB
Vue
Raw Normal View History

2021-11-15 14:55:40 +00:00
<template>
<div
2022-11-27 17:49:19 +00:00
class="inline-flex items-center justify-center transition cursor-pointer flex-nowrap group hover:text-secondaryDark"
2022-03-01 06:41:53 +00:00
role="checkbox"
:aria-checked="on"
@click="emit('change')"
2021-11-15 14:55:40 +00:00
>
<input
:id="checkboxID"
2021-11-15 14:55:40 +00:00
type="checkbox"
:name="name"
2022-12-03 07:31:47 +00:00
class="checkbox"
2021-11-15 14:55:40 +00:00
:checked="on"
@change="emit('change')"
2021-11-15 14:55:40 +00:00
/>
<label
:for="checkboxID"
2022-11-27 17:49:19 +00:00
class="pl-0 font-semibold truncate align-middle cursor-pointer"
2021-11-15 14:55:40 +00:00
>
<slot></slot>
</label>
</div>
</template>
<script lang="ts">
/*
This checkboxIDCounter is tracked in the global scope in order to ensure that each checkbox has a unique ID.
When we use this component multiple times on the same page, we need to ensure that each checkbox has a unique ID.
This is because the label's for attribute needs to match the checkbox's id attribute.
That's why we use a global counter that increments each time we use this component.
*/
let checkboxIDCounter = 564275
</script>
<script setup lang="ts">
// Unique ID for checkbox
const checkboxID = `checkbox-${checkboxIDCounter++}`
defineProps({
on: {
type: Boolean,
default: false,
2021-11-15 14:55:40 +00:00
},
name: {
type: String,
default: "checkbox",
},
2021-11-15 14:55:40 +00:00
})
const emit = defineEmits<{
(e: "change"): void
}>()
2021-11-15 14:55:40 +00:00
</script>
<style lang="scss" scoped>
2022-12-03 07:31:47 +00:00
.checkbox[type="checkbox"] {
@apply relative;
2021-11-15 14:55:40 +00:00
@apply appearance-none;
@apply hidden;
& + label {
@apply inline-flex items-center justify-center;
@apply cursor-pointer;
@apply relative;
2021-11-15 14:55:40 +00:00
&::before {
2022-12-03 07:31:47 +00:00
@apply border-2 border-divider;
2021-11-15 14:55:40 +00:00
@apply rounded;
@apply group-hover:border-accentDark;
2021-11-15 14:55:40 +00:00
@apply inline-flex;
@apply items-center;
@apply justify-center;
@apply text-transparent;
@apply h-4;
@apply w-4;
@apply mr-2;
2021-11-15 14:55:40 +00:00
@apply transition;
content: "";
}
&::after {
content: "";
border: solid;
border-width: 0 1.9px 1.9px 0;
height: 0.6rem;
width: 0.3rem;
left: 0.35rem;
position: absolute;
top: 2px;
transform: rotate(45deg);
opacity: 0;
2021-11-15 14:55:40 +00:00
}
}
&:checked + label::before {
@apply bg-accent;
@apply border-accent;
@apply text-accentContrast;
}
&:checked + label::after {
@apply text-accentContrast;
opacity: 1;
}
2021-11-15 14:55:40 +00:00
}
</style>