Skip to content

Commit

Permalink
feat(Toggle): add loading prop (#1546)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
  • Loading branch information
aminmokhtari94 and benjamincanac committed Mar 25, 2024
1 parent 224ec3c commit e1e05af
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
13 changes: 13 additions & 0 deletions docs/content/2.components/toggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ excludedProps:
---
::

### Loading :u-badge{label="New" class="align-middle ml-2 !rounded-full" variant="subtle"}

Use the `loading` prop to show a loading icon and disable the Toggle.

Use the `loading-icon` prop to set a different icon or change it globally in `ui.toggle.default.loadingIcon`. Defaults to `i-heroicons-arrow-path-20-solid`.

::component-card
---
props:
loading: true
---
::

### Disabled

Use the `disabled` prop to disable the Toggle.
Expand Down
35 changes: 31 additions & 4 deletions src/runtime/components/forms/Toggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
:id="inputId"
v-model="active"
:name="name"
:disabled="disabled"
:disabled="disabled || loading"
:class="switchClass"
v-bind="attrs"
>
<span :class="containerClass">
<span v-if="onIcon" :class="[active ? ui.icon.active : ui.icon.inactive, ui.icon.base]" aria-hidden="true">
<span v-if="loading" :class="[ui.icon.active, ui.icon.base]" aria-hidden="true">
<UIcon :name="loadingIcon" :class="loadingIconClass" />
</span>
<span
v-if="!loading && onIcon"
:class="[active ? ui.icon.active : ui.icon.inactive, ui.icon.base]"
aria-hidden="true"
>
<UIcon :name="onIcon" :class="onIconClass" />
</span>
<span v-if="offIcon" :class="[active ? ui.icon.inactive : ui.icon.active, ui.icon.base]" aria-hidden="true">
<span
v-if="!loading && offIcon"
:class="[active ? ui.icon.inactive : ui.icon.active, ui.icon.base]"
aria-hidden="true"
>
<UIcon :name="offIcon" :class="offIconClass" />
</span>
</span>
Expand Down Expand Up @@ -58,6 +69,10 @@ export default defineComponent({
type: Boolean,
default: false
},
loading: {
type: Boolean,
default: false
},
onIcon: {
type: String,
default: () => config.default.onIcon
Expand All @@ -66,6 +81,10 @@ export default defineComponent({
type: String,
default: () => config.default.offIcon
},
loadingIcon: {
type: String,
default: () => config.default.loadingIcon
},
color: {
type: String as PropType<ToggleColor>,
default: () => config.default.color,
Expand Down Expand Up @@ -137,6 +156,13 @@ export default defineComponent({
)
})
const loadingIconClass = computed(() => {
return twJoin(
ui.value.icon.size[props.size],
color.value && ui.value.icon.loading.replaceAll('{color}', color.value)
)
})
provideUseId(() => useId())
return {
Expand All @@ -150,7 +176,8 @@ export default defineComponent({
switchClass,
containerClass,
onIconClass,
offIconClass
offIconClass,
loadingIconClass
}
}
})
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/ui.config/forms/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ export default {
'2xl': 'h-6 w-6'
},
on: 'text-{color}-500 dark:text-{color}-400',
off: 'text-gray-400 dark:text-gray-500'
off: 'text-gray-400 dark:text-gray-500',
loading: 'animate-spin text-{color}-500 dark:text-{color}-400'
},
default: {
onIcon: null,
offIcon: null,
loadingIcon: 'i-heroicons-arrow-path-20-solid',
color: 'primary',
size: 'md'
}
}
}

0 comments on commit e1e05af

Please sign in to comment.