-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathNcRadioGroupButton.vue
More file actions
165 lines (140 loc) · 4.39 KB
/
NcRadioGroupButton.vue
File metadata and controls
165 lines (140 loc) · 4.39 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { createElementId } from '../../utils/createElementId.ts'
import { useNcFormBox } from '../NcFormBox/useNcFormBox.ts'
import { useInsideRadioGroup } from '../NcRadioGroup/useNcRadioGroup.ts'
const props = defineProps<{
/**
* Non visible label for accessibility when no `label` is passed.
*/
ariaLabel?: string
/**
* The visual label of the radio button
*/
label?: string
/**
* The value that should be assigned to the `modelValue` of the `NcRadioGroup`.
*/
value: string
/**
* Disabled state of the radio button
*/
disabled?: boolean
}>()
const labelId = createElementId()
const radioGroup = useInsideRadioGroup()
const { formBoxItemClass } = useNcFormBox()
onMounted(() => radioGroup!.value.register(true))
const isChecked = computed(() => radioGroup?.value.modelValue === props.value)
/**
* Handle updating the current model value
*/
function onUpdate() {
if (props.disabled) {
return
}
radioGroup!.value.onUpdate(props.value)
}
</script>
<template>
<div
:class="[{
[$style.radioGroupButton_active]: isChecked,
[$style.radioGroupButton_disabled]: disabled,
}, $style.radioGroupButton, formBoxItemClass]"
@click="onUpdate">
<div v-if="$scopedSlots.icon" :class="$style.radioGroupButton__icon">
<!-- @slot Optional icon slot -->
<slot name="icon" />
</div>
<div v-if="label" :id="labelId" :class="$style.radioGroupButton__label">
{{ label }}
</div>
<input
:aria-labelledby="label ? labelId : undefined"
:aria-label="label ? undefined : ariaLabel"
class="hidden-visually"
:checked="isChecked"
type="radio"
:disabled="disabled"
:value="value"
@input="onUpdate">
</div>
</template>
<style module lang="scss">
.radioGroupButton {
--radio-group-button--border-radius: var(--border-radius-small);
--radio-group-button--border-width: 1px;
--radio-group-button--color: var(--color-primary-element-light-text);
--radio-group-button--background-color: var(--color-primary-element-light);
--radio-group-button--background-color-hover: var(--color-primary-element-light-hover);
--radio-group-button--padding: 1px;
cursor: pointer;
color: var(--radio-group-button--color);
background-color: var(--radio-group-button--background-color);
transition: var(--animation-quick) background-color;
border: var(--radio-group-button--border-width) solid var(--radio-group-button--background-color-hover);
border-bottom-width: 2px;
border-radius: var(--radio-group-button--border-radius);
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
text-align: center;
min-height: var(--default-clickable-area);
// ensure that the content is centered because of uneven border
padding-block: var(--radio-group-button--padding) 0;
padding-inline: var(--radio-group-button--padding);
* {
cursor: pointer;
}
&:has(&__label) {
padding-inline: calc(var(--radio-group-button--padding) + var(--border-radius-element));
}
&:has(&__icon) {
padding-inline-start: var(--radio-group-button--padding);
}
&:hover:not(.radioGroupButton_disabled) {
background-color: var(--radio-group-button--background-color-hover);
}
&:focus-within {
--radio-group-button--border-width: 2px;
--radio-group-button--padding: 0px;
border: var(--radio-group-button--border-width) solid var(--color-main-text) !important;
outline: calc(var(--default-grid-baseline) / 2) var(--color-main-background);
}
}
.radioGroupButton_active {
--radio-group-button--color: var(--color-primary-element-text);
--radio-group-button--background-color: var(--color-primary-element);
--radio-group-button--background-color-hover: var(--color-primary-element-hover);
}
.radioGroupButton__label {
font-weight: var(--font-weight-element, bold);
}
.radioGroupButton_disabled {
filter: saturate($opacity_normal);
opacity: $opacity_disabled;
// Reset the cursor
cursor: default;
* {
cursor: default;
}
}
.radioGroupButton__icon {
--radio-group-button--icon-size: calc(var(--default-clickable-area) - 4px);
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: var(--radio-group-button--icon-size);
// make sure the icon is a bit smaller to account for border
* {
--default-clickable-area: var(--radio-group-button--icon-size);
}
}
</style>