Skip to content

Commit

Permalink
fix: init logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjiaxuan committed May 28, 2023
1 parent 2df988a commit c2f809a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 49 deletions.
4 changes: 2 additions & 2 deletions playgrounds/vue3/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { reactive, ref, version, watch } from 'vue'
const numberRollRef = ref<InstanceType<typeof NumberRoll>>()
const options = reactive({
start: {
value: 123,
value: 132,
type: 'number',
},
end: {
Expand All @@ -19,7 +19,7 @@ const options = reactive({
type: 'number',
},
transitionDelay: {
value: '0',
value: '0.5s',
type: 'text',
},
transitionDuration: {
Expand Down
45 changes: 6 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ export default defineComponent({
.padStart(+props.totalLength, '0')
.split('')
.forEach((number, idx) => setItemTranslateY(idx, +number))
// isColorTransparent.value = false
console.log(itemTranslateYs.value)
}

const endNumberWithPadding = computed(() => props.end.toString().padStart(+props.totalLength, '0'))
Expand All @@ -94,7 +92,6 @@ export default defineComponent({
endNumberWithPadding.value.split('').forEach((number, idx) => setItemTranslateY(idx, +number))
}

const isColorTransparent = ref(true) // { color: 'transparent' }
const vueNumberRollRef = ref<HTMLElement | null>(null)
const itemHeightNumber = computed(() =>
(vueNumberRollRef.value?.children[0]?.children[0]?.children[0] as HTMLElement)?.offsetHeight ?? 0)
Expand All @@ -109,14 +106,29 @@ export default defineComponent({
}
}

const isInitialized = ref(false)
const stop = watch(itemHeightNumber, _itemHeightNumber => {
if (_itemHeightNumber > 0) {
stop()
init()
// void nextTick(() => window.requestAnimationFrame(() => isInitialized.value = true))
setTimeout(() => isInitialized.value = true)
}
})
watch([
itemHeightNumber,
() => props.start,
() => props.totalLength,
() => props.reverseRollDirection,
], init, { immediate: true })

onMounted(() => props.immediate && roll())
onMounted(() => {
if (props.immediate) {
const stop = watch(isInitialized, _isInitialized => {
stop()
_isInitialized && roll()
})
}
})

// These two methods were set at methods, working as an alias, for the lack of Intellisense.
expose({ roll, reset: init })
Expand All @@ -135,7 +147,7 @@ export default defineComponent({
itemTranslateYs.value.map((_, index) => h(
'li',
{
class: `${ props.itemClass } transition-property-transform box-border mt0 mb0`,
class: `${ props.itemClass } mt0 mb0`,
style: { height: itemHeightNumber.value.toString() + 'px' },
},
[
Expand All @@ -145,11 +157,11 @@ export default defineComponent({
class: 'flex-(~ justify-center items-center box-border)',
style: [
itemTranslateYs.value[index],
{
isInitialized.value ? {
'transition-duration': props.transitionDuration,
'transition-timing-function': props.transitionTimingFunction,
'transition-delay': props.transitionDelay,
},
} : {},
],
},
[ ...new Array<void>(10) ].map((_, index) => h(
Expand Down

0 comments on commit c2f809a

Please sign in to comment.