Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/components/NcCheckboxRadioSwitch/NcCheckboxContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
:checked="isChecked"
:loading="loading">
<NcLoadingIcon v-if="loading" />
<NcIconToggleSwitch
v-else-if="isSwitchType"
:checked="isChecked"
:size="iconSize"
inline />
<component
:is="checkboxRadioIconElement"
v-else-if="!buttonVariant"
Expand Down Expand Up @@ -62,8 +67,7 @@ import CheckboxMarked from 'vue-material-design-icons/CheckboxMarked.vue'
import MinusBox from 'vue-material-design-icons/MinusBox.vue'
import RadioboxBlank from 'vue-material-design-icons/RadioboxBlank.vue'
import RadioboxMarked from 'vue-material-design-icons/RadioboxMarked.vue'
import ToggleSwitch from 'vue-material-design-icons/ToggleSwitch.vue'
import ToggleSwitchOff from 'vue-material-design-icons/ToggleSwitchOff.vue'
import NcIconToggleSwitch from '../NcIconToggleSwitch/NcIconToggleSwitch.vue'
import NcLoadingIcon from '../NcLoadingIcon/index.ts'

export const TYPE_CHECKBOX = 'checkbox'
Expand All @@ -76,6 +80,7 @@ export default {

components: {
NcLoadingIcon,
NcIconToggleSwitch,
},

props: {
Expand Down Expand Up @@ -176,6 +181,10 @@ export default {
return this.type === TYPE_BUTTON
},

isSwitchType() {
return this.type === TYPE_SWITCH
},

/**
* Returns the proper Material icon depending on the select case
*
Expand All @@ -189,14 +198,6 @@ export default {
return RadioboxBlank
}

// Switch
if (this.type === TYPE_SWITCH) {
if (this.isChecked) {
return ToggleSwitch
}
return ToggleSwitchOff
}

// Checkbox
if (this.indeterminate) {
return MinusBox
Expand Down Expand Up @@ -242,6 +243,7 @@ export default {
&-radio:not(&--button-variant) &__icon,
&-switch:not(&--button-variant) &__icon {
margin-block: calc((var(--default-clickable-area) - 2 * var(--default-grid-baseline) - var(--icon-height)) / 2) auto;
line-height: 0;
}

&-checkbox:not(&--button-variant) &__icon--has-description,
Expand Down
17 changes: 2 additions & 15 deletions src/components/NcFormBoxSwitch/NcFormBoxSwitch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
<script setup lang="ts">
import type { Slot } from 'vue'

import { mdiToggleSwitch, mdiToggleSwitchOff } from '@mdi/js'
import { watch } from 'vue'
import NcFormBoxItem from '../NcFormBox/NcFormBoxItem.vue'
import NcIconSvgWrapper from '../NcIconSvgWrapper/NcIconSvgWrapper.vue'
import NcIconToggleSwitch from '../NcIconToggleSwitch/NcIconToggleSwitch.vue'
import { createElementId } from '../../utils/createElementId.ts'

/** Switch toggle model value */
Expand Down Expand Up @@ -80,11 +79,7 @@ watch(modelValue, () => {
role="switch"
:aria-describedby="descriptionId"
:disabled>
<NcIconSvgWrapper
:path="modelValue ? mdiToggleSwitch : mdiToggleSwitchOff"
:class="$style.formBoxSwitch__icon"
:size="34 /* --default-clickable-area */"
inline />
<NcIconToggleSwitch :checked="modelValue" inline />
</template>
</NcFormBoxItem>
</template>
Expand All @@ -103,14 +98,6 @@ input.formBoxSwitch__input {
height: auto;
cursor: inherit;
}

.formBoxSwitch__icon {
color: var(--color-text-maxcontrast);
}

input:checked + .formBoxSwitch__icon {
color: var(--color-primary-element);
}
</style>

<docs>
Expand Down
66 changes: 66 additions & 0 deletions src/components/NcIconToggleSwitch/NcIconToggleSwitch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->

<script lang="ts">
const svg = `<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 12">
<path d="M17,1H7A5,5 0 0,0 2,6 5,5 0 0,0 7,11H17A5,5 0 0,0 22,6 5,5 0 0,0 17,1Z" />
<circle
cy="6"
r="3"
fill="var(--color-main-background)" />
</svg>`
</script>

<script setup lang="ts">
import { computed } from 'vue'
import NcIconSvgWrapper from '../NcIconSvgWrapper/NcIconSvgWrapper.vue'

const {
checked,
size = 34,
inline = false,
} = defineProps<{
/** Whether the toggle switch is checked */
checked: boolean
/** Size of the icon */
size?: number
/** NcIconSvgWrapper's inline prop */
inline?: boolean
}>()

const color = computed(() => checked ? 'var(--color-primary-element)' : 'var(--color-text-maxcontrast)')
const cx = computed(() => checked ? 'calc(17 / 24 * 100%)' : 'calc(7 / 24 * 100%)')
</script>

<template>
<NcIconSvgWrapper
:class="$style.iconToggleSwitch"
:svg
:size
:inline />
</template>

<style module lang="scss">
.iconToggleSwitch {
color: v-bind(color);
transition: color var(--animation-quick) ease;

svg {
/* Unlike other icons, this icon is not a square */
height: auto !important;
}

circle {
cx: v-bind(cx);
transition: cx var(--animation-quick) ease;
}
}
</style>

<docs>
Private component
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe expose it just like NcLoadingIcon?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, existing components should cover all the cases of toggle switches. Loading icon, in contrast, is used directly in most of the cases because we don't have any alternatives like loading states of containers/buttons/inputs.

</docs>
1 change: 1 addition & 0 deletions styleguide.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ module.exports = async () => {
'src/components/NcTextArea/*.vue',
'src/components/NcUserBubble/NcUserBubbleDiv.vue',
'src/components/NcFormBox/NcFormBoxItem.vue',
'src/components/NcIconToggleSwitch/NcIconToggleSwitch.vue',
],
sections: [
{
Expand Down
Loading