mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
2.4 KiB
120 lines
2.4 KiB
2 years ago
|
<script lang="ts" setup>
|
||
|
type FlipTrigger = 'hover' | 'click' | { duration: number }
|
||
|
|
||
|
interface Props {
|
||
|
triggers?: FlipTrigger[]
|
||
2 years ago
|
duration?: number
|
||
2 years ago
|
}
|
||
|
|
||
|
const props = withDefaults(defineProps<Props>(), {
|
||
|
triggers: () => ['click'] as FlipTrigger[],
|
||
2 years ago
|
duration: 800,
|
||
2 years ago
|
})
|
||
|
|
||
|
let flipped = $ref(false)
|
||
|
let hovered = $ref(false)
|
||
|
let flipTimer = $ref<NodeJS.Timer | null>(null)
|
||
|
|
||
|
onMounted(() => {
|
||
|
const duration = props.triggers.reduce((dur, trigger) => {
|
||
|
if (typeof trigger !== 'string') {
|
||
|
dur = trigger.duration
|
||
|
}
|
||
|
|
||
|
return dur
|
||
|
}, 0)
|
||
|
|
||
|
if (duration > 0) {
|
||
|
flipTimer = setInterval(() => {
|
||
|
if (!hovered) {
|
||
|
flipped = !flipped
|
||
|
}
|
||
|
}, duration)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
onBeforeUnmount(() => {
|
||
|
if (flipTimer) {
|
||
|
clearInterval(flipTimer)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
function onHover(isHovering: boolean) {
|
||
|
hovered = isHovering
|
||
|
|
||
|
if (props.triggers.find((trigger) => trigger === 'hover')) {
|
||
|
flipped = isHovering
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function onClick() {
|
||
|
if (props.triggers.find((trigger) => trigger === 'click')) {
|
||
|
flipped = !flipped
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
let isFlipping = $ref(false)
|
||
|
|
||
|
watch($$(flipped), () => {
|
||
|
isFlipping = true
|
||
|
|
||
|
setTimeout(() => {
|
||
|
isFlipping = false
|
||
|
}, props.duration / 2)
|
||
|
})
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<div class="flip-card" @click="onClick" @mouseover="onHover(true)" @mouseleave="onHover(false)">
|
||
2 years ago
|
<div
|
||
|
class="flipper"
|
||
|
:style="{ '--flip-duration': `${props.duration || 800}ms`, 'transform': flipped ? 'rotateY(180deg)' : '' }"
|
||
|
>
|
||
|
<div
|
||
|
class="front"
|
||
|
:style="{ 'pointer-events': flipped ? 'none' : 'auto', 'opacity': !isFlipping ? (flipped ? 0 : 100) : flipped ? 100 : 0 }"
|
||
|
>
|
||
2 years ago
|
<slot name="front" />
|
||
|
</div>
|
||
2 years ago
|
<div
|
||
|
class="back"
|
||
|
:style="{ 'pointer-events': flipped ? 'auto' : 'none', 'opacity': !isFlipping ? (flipped ? 100 : 0) : flipped ? 0 : 100 }"
|
||
|
>
|
||
2 years ago
|
<slot name="back" />
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<style lang="scss" scoped>
|
||
|
.flip-card {
|
||
|
background-color: transparent;
|
||
|
perspective: 1000px;
|
||
|
}
|
||
|
|
||
|
.flipper {
|
||
2 years ago
|
--flip-duration: 800ms;
|
||
|
|
||
2 years ago
|
position: relative;
|
||
|
width: 100%;
|
||
|
height: 100%;
|
||
|
text-align: center;
|
||
2 years ago
|
transition: all ease-in-out;
|
||
|
transition-duration: var(--flip-duration);
|
||
2 years ago
|
transform-style: preserve-3d;
|
||
|
}
|
||
|
|
||
|
.front,
|
||
|
.back {
|
||
|
position: absolute;
|
||
|
width: 100%;
|
||
|
height: 100%;
|
||
|
-webkit-backface-visibility: hidden;
|
||
|
backface-visibility: hidden;
|
||
|
}
|
||
|
|
||
|
.back {
|
||
|
transform: rotateY(180deg);
|
||
|
}
|
||
|
</style>
|