-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
196 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
components/molecules/VCarouselProgress/Default.stories.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script setup lang="ts"> | ||
const index = ref(0) | ||
</script> | ||
|
||
<template> | ||
<NuxtStory> | ||
<VCarouselProgress | ||
v-model:index="index" | ||
:snap-length="3" | ||
/> | ||
</NuxtStory> | ||
</template> |
94 changes: 94 additions & 0 deletions
94
components/molecules/VCarouselProgress/VCarouselProgress.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<script lang="ts" setup> | ||
import type { ThemeProps } from '~/composables/use-theme' | ||
const props = defineProps<{ | ||
index: number | ||
snapLength: number | ||
} & ThemeProps>() | ||
const { themeClass } = useTheme({ props }) | ||
const root = ref<HTMLElement | null>(null) | ||
const thumb = ref<HTMLElement | null>(null) | ||
onMounted(() => { | ||
setIndicatorWidth() | ||
setIndicatorPosition(props.index) | ||
}) | ||
watch(() => props.snapLength, () => { | ||
setIndicatorWidth() | ||
setIndicatorPosition(props.index) | ||
}, { flush: 'post' }) | ||
watch(() => props.index, (newIndex) => { | ||
setIndicatorPosition(newIndex) | ||
}) | ||
function setIndicatorWidth() { | ||
if (!root.value?.offsetWidth || !thumb.value) return // can be undefined in SSR | ||
const value = (root.value?.offsetWidth / Math.max(props.snapLength, 1) / root.value?.offsetWidth) * 100 | ||
thumb.value?.style.setProperty('--v-carousel-controls-thumb-width', value + '%') | ||
} | ||
function setIndicatorPosition(index: number) { | ||
if (!root.value || !thumb.value) return | ||
const percent = index / Math.max(props.snapLength - 1, 1) | ||
const translate = percent * (root.value?.offsetWidth - thumb.value?.getBoundingClientRect().width) | ||
thumb.value.style.translate = translate + 'px 0 ' | ||
} | ||
</script> | ||
|
||
<template> | ||
<div | ||
ref="root" | ||
:class="[$style.root, themeClass]" | ||
> | ||
<div | ||
ref="thumb" | ||
:class="$style.thumb" | ||
/> | ||
</div> | ||
</template> | ||
|
||
<style lang="scss" module> | ||
@use 'assets/scss/functions/rem' as *; | ||
@use 'assets/scss/mixins/include-media' as *; | ||
@use 'assets/scss/mixins/theme' as *; | ||
.root { | ||
position: relative; | ||
overflow: hidden; | ||
width: rem(48); | ||
height: rem(2); | ||
flex-shrink: 0; | ||
@include theme-variants('colors-control'); | ||
&::before { | ||
position: absolute; | ||
background-color: var(--theme-colors-control-content-disabled, #ccc); | ||
content: ''; | ||
inset: 0; | ||
} | ||
@include media('>=lg') { | ||
width: rem(64); | ||
} | ||
} | ||
.thumb { | ||
position: relative; | ||
width: clamp(#{rem(22)}, var(--v-carousel-controls-thumb-width), 100%); | ||
height: 100%; | ||
background-color: var(--theme-colors-control-content, currentColor); | ||
content: ''; | ||
transform-origin: left; | ||
transition: 0.2s linear, 0.5s; | ||
transition-property: translate, width; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { Ref } from 'vue' | ||
|
||
export interface VCarouselControlsOptions { | ||
displayNumbers?: boolean | ||
snapLength: Ref<number> | ||
index: Ref<number> | ||
} | ||
|
||
function formatValue(n: number) { | ||
return (n < 9 ? '0' : '') + (n + 1) | ||
} | ||
|
||
export function useCarouselControls(options: VCarouselControlsOptions) { | ||
const { t } = useI18n() | ||
|
||
const numbersOutput = computed(() => { | ||
if (!options.displayNumbers) return | ||
|
||
return `${formatValue(toValue(options.index))} / ${formatValue(toValue(options.snapLength) - 1)}` | ||
}) | ||
|
||
function onButtonClicked(event: Event) { | ||
const el = event.currentTarget as HTMLButtonElement | ||
|
||
if (el.name === 'next') options.index.value = toValue(options.index) + 1 | ||
else if (el.name === 'previous') options.index.value = toValue(options.index) - 1 | ||
} | ||
|
||
const isCarouselDraggable = computed(() => { | ||
const length = toValue(options.snapLength) | ||
return !!length && length > 1 | ||
}) | ||
|
||
const nextDisabled = computed(() => { | ||
return (toValue(options.index) === toValue(options.snapLength) - 1) || !toValue(options.snapLength) | ||
}) | ||
|
||
const nextButtonAttrs = computed(() => { | ||
return { | ||
disabled: nextDisabled.value, | ||
name: 'next', | ||
iconName: 'arrow-right', | ||
ariaLabel: t('carousel.next_slide_aria'), | ||
onClick: onButtonClicked, | ||
} | ||
}) | ||
|
||
const previousDisabled = computed(() => toValue(options.index) === 0) | ||
const prevButtonAttrs = computed(() => { | ||
return { | ||
disabled: previousDisabled.value, | ||
name: 'previous', | ||
iconName: 'arrow-left', | ||
ariaLabel: t('carousel.previous_slide_aria'), | ||
onClick: onButtonClicked, | ||
} | ||
}) | ||
|
||
return { numbersOutput, onButtonClicked, nextButtonAttrs, prevButtonAttrs, previousDisabled, nextDisabled, isCarouselDraggable } | ||
} |