|
| 1 | +<script setup lang="tsx"> |
| 2 | +/** @jsxImportSource vue */ |
| 3 | +import { AnimateNumber } from 'motion-plus-vue' |
| 4 | +import { motion } from 'motion-v' |
| 5 | +import { defineComponent, ref } from 'vue' |
| 6 | +
|
| 7 | +const monthlyPrice = 19 |
| 8 | +const yearlyPrice = 199 |
| 9 | +const isYearly = ref(true) |
| 10 | +
|
| 11 | +const Button = defineComponent({ |
| 12 | + props: { |
| 13 | + isSelected: { |
| 14 | + type: Boolean, |
| 15 | + required: true, |
| 16 | + }, |
| 17 | + onClick: { |
| 18 | + type: Function, |
| 19 | + required: true, |
| 20 | + }, |
| 21 | + }, |
| 22 | + setup(props, { slots }) { |
| 23 | + return () => ( |
| 24 | + <button onClick={props.onClick}> |
| 25 | + <motion.span |
| 26 | + initial={false} |
| 27 | + animate={{ |
| 28 | + color: props.isSelected ? 'var(--background)' : 'var(--text)', |
| 29 | + opacity: props.isSelected ? 1 : 0.5, |
| 30 | + }} |
| 31 | + > |
| 32 | + {slots.default?.()} |
| 33 | + </motion.span> |
| 34 | + {props.isSelected && ( |
| 35 | + <motion.div |
| 36 | + layoutId="selected" |
| 37 | + class="selected" |
| 38 | + style={{ borderRadius: 25, zIndex: 1 }} |
| 39 | + /> |
| 40 | + )} |
| 41 | + </button> |
| 42 | + ) |
| 43 | + }, |
| 44 | +}) |
| 45 | +</script> |
| 46 | + |
| 47 | +<template> |
| 48 | + <div class="price-switcher"> |
| 49 | + <AnimateNumber |
| 50 | + :format="{ |
| 51 | + style: 'currency', |
| 52 | + currency: 'USD', |
| 53 | + minimumFractionDigits: 0, |
| 54 | + maximumFractionDigits: 0, |
| 55 | + }" |
| 56 | + locales="en-US" |
| 57 | + :suffix="isYearly ? '/yr' : '/mo'" |
| 58 | + class="number" |
| 59 | + :transition="{ |
| 60 | + visualDuration: 0.6, |
| 61 | + type: 'spring', |
| 62 | + bounce: 0.25, |
| 63 | + opacity: { duration: 0.2, ease: 'linear' }, |
| 64 | + }" |
| 65 | + :value="isYearly ? yearlyPrice : monthlyPrice" |
| 66 | + /> |
| 67 | + <div class="switch"> |
| 68 | + <Button |
| 69 | + :is-selected="!isYearly" |
| 70 | + :on-click="() => isYearly = false" |
| 71 | + > |
| 72 | + Monthly |
| 73 | + </Button> |
| 74 | + <Button |
| 75 | + :is-selected="isYearly" |
| 76 | + :on-click="() => isYearly = true" |
| 77 | + > |
| 78 | + Yearly |
| 79 | + </Button> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | +</template> |
| 83 | + |
| 84 | +<style> |
| 85 | +.price-switcher { |
| 86 | + display: flex; |
| 87 | + flex-direction: column; |
| 88 | + align-items: center; |
| 89 | + gap: 20px; |
| 90 | +} |
| 91 | +
|
| 92 | +.number { |
| 93 | + font-size: 96px; |
| 94 | + letter-spacing: -0.04em; |
| 95 | + font-weight: 530; |
| 96 | + font-variation-settings: "opsz" 30, "wght" 530; |
| 97 | +} |
| 98 | +
|
| 99 | +.number-section-post { |
| 100 | + font-size: 32px; |
| 101 | + opacity: 0.5; |
| 102 | + position: relative; |
| 103 | + bottom: 15px; |
| 104 | + align-self: flex-end; |
| 105 | + margin-left: 5px; |
| 106 | + letter-spacing: -0.02em; |
| 107 | +} |
| 108 | +
|
| 109 | +.switch { |
| 110 | + display: flex; |
| 111 | + gap: 10px; |
| 112 | + padding: 6px; |
| 113 | + border-radius: 100px; |
| 114 | + background-color: rgba(255, 255, 255, 0.05); |
| 115 | +} |
| 116 | +
|
| 117 | +.switch button { |
| 118 | + position: relative; |
| 119 | + padding: 8px 12px; |
| 120 | + display: flex; |
| 121 | +} |
| 122 | +
|
| 123 | +.switch button span { |
| 124 | + z-index: 2; |
| 125 | + position: relative; |
| 126 | + color: var(--text); |
| 127 | + will-change: opacity; |
| 128 | + font-size: 13px; |
| 129 | + line-height: 1; |
| 130 | + font-variation-settings: "opsz" 20, "wght" 590; |
| 131 | +} |
| 132 | +
|
| 133 | +.switch .selected { |
| 134 | + position: absolute; |
| 135 | + top: 0; |
| 136 | + left: 0; |
| 137 | + right: 0; |
| 138 | + bottom: 0; |
| 139 | + background-color: #f5f5f5; |
| 140 | + will-change: transform; |
| 141 | +} |
| 142 | +</style> |
0 commit comments