summaryrefslogtreecommitdiff
path: root/packages/frontend/src/components/form/checkbox.vue
blob: 347ef1cc64ac20d09ba78b26959f4067c068fe85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<template>
<div
	class="ziffeoms"
	:class="{ disabled, checked }"
>
	<input
		ref="input"
		type="checkbox"
		:disabled="disabled"
		@keydown.enter="toggle"
	>
	<span ref="button" v-adaptive-border v-tooltip="checked ? i18n.ts.itsOn : i18n.ts.itsOff" class="button" @click.prevent="toggle">
		<i class="check ti ti-check"></i>
	</span>
	<span class="label">
		<!-- TODO: 無名slotの方は廃止 -->
		<span @click="toggle"><slot name="label"></slot><slot></slot></span>
		<p class="caption"><slot name="caption"></slot></p>
	</span>
</div>
</template>

<script lang="ts" setup>
import { toRefs, Ref } from 'vue';
import * as os from '@/os';
import Ripple from '@/components/MkRipple.vue';
import { i18n } from '@/i18n';

const props = defineProps<{
	modelValue: boolean | Ref<boolean>;
	disabled?: boolean;
}>();

const emit = defineEmits<{
	(ev: 'update:modelValue', v: boolean): void;
}>();

let button = $ref<HTMLElement>();
const checked = toRefs(props).modelValue;
const toggle = () => {
	if (props.disabled) return;
	emit('update:modelValue', !checked.value);

	if (!checked.value) {
		const rect = button.getBoundingClientRect();
		const x = rect.left + (button.offsetWidth / 2);
		const y = rect.top + (button.offsetHeight / 2);
		os.popup(Ripple, { x, y, particle: false }, {}, 'end');
	}
};
</script>

<style lang="scss">
.ziffeoms {
	position: relative;
	display: flex;
	transition: all 0.2s ease;

	> * {
		user-select: none;
	}

	> input {
		position: absolute;
		width: 0;
		height: 0;
		opacity: 0;
		margin: 0;
	}

	> .button {
		position: relative;
		display: inline-flex;
		flex-shrink: 0;
		margin: 0;
		box-sizing: border-box;
		width: 23px;
		height: 23px;
		outline: none;
		background: var(--panel);
		border: solid 1px var(--panel);
		border-radius: 4px;
		cursor: pointer;
		transition: inherit;

		> .check {
			margin: auto;
			opacity: 0;
			color: var(--fgOnAccent);
			font-size: 13px;
			transform: scale(0.5);
			transition: all 0.2s ease;
		}
	}

	&:hover {
		> .button {
			border-color: var(--inputBorderHover) !important;
		}
	}

	> .label {
		margin-left: 12px;
		margin-top: 2px;
		display: block;
		transition: inherit;
		color: var(--fg);

		> span {
			display: block;
			line-height: 20px;
			cursor: pointer;
			transition: inherit;
		}

		> .caption {
			margin: 8px 0 0 0;
			color: var(--fgTransparentWeak);
			font-size: 0.85em;

			&:empty {
				display: none;
			}
		}
	}

	&.disabled {
		opacity: 0.6;
		cursor: not-allowed;
	}

	&.checked {
		> .button {
			background-color: var(--accent) !important;
			border-color: var(--accent) !important;

			> .check {
				opacity: 1;
				transform: scale(1);
			}
		}
	}
}
</style>