Skip to content

Commit 94ba655

Browse files
committed
Bespoke toggle button component
1 parent 6d33ca1 commit 94ba655

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<!--
2+
- ToggleButton.vue
3+
-
4+
- Description-
5+
-
6+
- This program is free software: you can redistribute it and/or modify
7+
- it under the terms of the GNU General Public License as published by
8+
- the Free Software Foundation, either version 3 of the License, or
9+
- (at your option) any later version.
10+
-
11+
- This program is distributed in the hope that it will be useful,
12+
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14+
- GNU General Public License for more details.
15+
-
16+
- You should have received a copy of the GNU General Public License
17+
- along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
-
19+
- @link https://www.librenms.org
20+
-
21+
- @copyright 2025 Tony Murray
22+
- @author Tony Murray <murraytony@gmail.com>
23+
-->
24+
25+
<template>
26+
<label
27+
class="tw:relative tw:inline-block tw:w-20 tw:h-10 tw:align-middle"
28+
:class="{ 'tw:opacity-60 tw:cursor-not-allowed': disabled }"
29+
>
30+
<input
31+
type="checkbox"
32+
class="tw:absolute tw:opacity-0 tw:w-0 tw:h-0 tw:peer"
33+
:name="name"
34+
:checked="checked"
35+
:required="required"
36+
:disabled="disabled"
37+
@change="onChange"
38+
/>
39+
<span
40+
class="tw:absolute tw:cursor-pointer tw:inset-0 tw:bg-gray-300 tw:dark:bg-gray-600 tw:transition-colors tw:duration-200 tw:rounded-full tw:peer-checked:bg-green-500 tw:dark:peer-checked:bg-green-400 tw:*:absolute tw:*:content-empty tw:*:size-8 tw:*:top-1 tw:*:left-1 tw:*:bg-white tw:*:transition-transform tw:*:duration-200 tw:*:rounded-full tw:*:shadow-md tw:peer-checked:*:translate-x-10"
41+
:class="{ 'tw:cursor-not-allowed': disabled }"
42+
aria-hidden="true"
43+
>
44+
<span></span>
45+
</span>
46+
</label>
47+
</template>
48+
49+
<script>
50+
export default {
51+
name: 'ToggleButton',
52+
props: {
53+
modelValue: { type: Boolean, default: undefined },
54+
value: { type: Boolean, default: undefined }, // legacy
55+
name: { type: String, default: undefined },
56+
required: { type: Boolean, default: false },
57+
disabled: { type: Boolean, default: false },
58+
// legacy prop from vue-js-toggle-button, ignored but accepted to prevent warnings
59+
sync: { type: [Boolean, String], default: undefined },
60+
},
61+
emits: ['update:modelValue', 'change'],
62+
computed: {
63+
checked() {
64+
// Prefer v-model if provided, otherwise fall back to legacy value
65+
if (this.modelValue !== undefined) {
66+
return !!this.modelValue;
67+
}
68+
return !!this.value;
69+
},
70+
},
71+
methods: {
72+
onChange(e) {
73+
const newVal = e.target.checked;
74+
// v-model support
75+
this.$emit('update:modelValue', newVal);
76+
// backward compatible change event structure
77+
this.$emit('change', { value: newVal });
78+
},
79+
},
80+
};
81+
</script>

0 commit comments

Comments
 (0)