|
1 | 1 | <template>
|
2 | 2 | <button
|
3 |
| - class="hoverClass relative h-12 w-12 duration-300 before:absolute before:inset-0 before:-z-10 before:scale-0 before:rounded-full before:bg-silver-500 before:opacity-0 before:transition-all before:ease-in themeSkyDark:before:bg-silver-500/25" |
| 3 | + class="buttonClass relative h-12 w-12 before:absolute before:inset-0 before:-z-10 before:scale-0 before:rounded-full before:bg-silver-500 before:opacity-0 before:transition-all before:ease-in themeSkyDark:before:bg-silver-500/25" |
4 | 4 | :class="{
|
5 | 5 | activeClass: active,
|
| 6 | + // Mobile touch transition class is only applied for buttons that are not |
| 7 | + // toggled via `active` |
| 8 | + [transitionClass]: active === null, |
6 | 9 | }"
|
| 10 | + @click="onClickButton" |
| 11 | + @transitionend="transitionClass = 'transitionOut'" |
7 | 12 | >
|
8 | 13 | <slot></slot>
|
9 | 14 | </button>
|
|
12 | 17 | <script setup lang="ts">
|
13 | 18 | withDefaults(
|
14 | 19 | defineProps<{
|
15 |
| - active?: boolean; |
| 20 | + active?: boolean | null; |
16 | 21 | }>(),
|
17 | 22 | {
|
18 |
| - active: false, |
| 23 | + active: null, |
19 | 24 | },
|
20 | 25 | );
|
| 26 | +
|
| 27 | +//#region Mobile touch transition |
| 28 | +const transitionClass = ref<"transitionIn" | "transitionOut">("transitionOut"); |
| 29 | +function onClickButton(e: MouseEvent) { |
| 30 | + const el = e.currentTarget; |
| 31 | + if (el instanceof HTMLElement) { |
| 32 | + // Trigger DOM reflow to reset animation before finished. |
| 33 | + el.style.animation = "none"; |
| 34 | + el.offsetHeight; |
| 35 | + el.style.animation = ""; |
| 36 | + transitionClass.value = "transitionIn"; |
| 37 | + } |
| 38 | +} |
| 39 | +//#endregion |
21 | 40 | </script>
|
22 | 41 |
|
23 | 42 | <style lang="postcss" scoped>
|
24 |
| -.activeClass, |
25 |
| -.hoverClass:hover { |
| 43 | +/** Applied when button is active */ |
| 44 | +.activeClass { |
26 | 45 | @apply before:scale-100 before:opacity-100 before:ease-out;
|
27 | 46 | }
|
| 47 | +
|
| 48 | +/** Use normal :hover behaviour on desktop */ |
| 49 | +@media (hover: hover) and (pointer: fine) { |
| 50 | + .buttonClass:hover { |
| 51 | + @apply before:scale-100 before:opacity-100 before:ease-out; |
| 52 | + } |
| 53 | +} |
| 54 | +
|
| 55 | +/** Target devices that don't support hover */ |
| 56 | +@media not (hover: hover) { |
| 57 | + .buttonClass.transitionIn { |
| 58 | + @apply before:scale-100 before:opacity-100 before:ease-in; |
| 59 | + } |
| 60 | +
|
| 61 | + .buttonClass.transitionOut { |
| 62 | + /** Add delay before transitioning out */ |
| 63 | + @apply before:scale-0 before:opacity-0 before:delay-75 before:ease-out; |
| 64 | + } |
| 65 | +} |
28 | 66 | </style>
|
0 commit comments