| 1 | import type { ClicksInfo, RawAtValue } from '@slidev/types' |
| 2 | import type { PropType, VNode } from 'vue' |
| 3 | import { injectLocal } from '@vueuse/core' |
| 4 | import { cloneVNode, Comment, computed, defineComponent, Fragment, h, isVNode, nextTick, onMounted, onUnmounted, shallowRef, Text, Transition } from 'vue' |
| 5 | import { |
| 6 | CLASS_VCLICK_ANIMATION_PREFIX, |
| 7 | CLASS_VCLICK_CURRENT, |
| 8 | CLASS_VCLICK_HIDDEN, |
| 9 | CLASS_VCLICK_PRIOR, |
| 10 | CLASS_VCLICK_TARGET, |
| 11 | injectionClicksContext, |
| 12 | injectionFrontmatter, |
| 13 | } from '../constants' |
| 14 | import { configs } from '../env' |
| 15 | import { makeId } from '../logic/utils' |
| 16 | |
| 17 | const RE_WHITESPACE_OR_COMMA = /[\s,]+/ |
| 18 | |
| 19 | function flattenChildren(children: unknown): VNode[] { |
| 20 | const result: VNode[] = [] |
| 21 | for (const child of Array.isArray(children) |
| 22 | ? children |
| 23 | : [children]) { |
| 24 | if (Array.isArray(child)) { |
| 25 | result.push(...flattenChildren(child)) |
| 26 | } |
| 27 | else if (isVNode(child)) { |
| 28 | if (child.type === Fragment) |
| 29 | result.push(...flattenChildren(child.children)) |
| 30 | else if (child.type !== Comment) |
| 31 | result.push(child) |
| 32 | } |
| 33 | else if (child != null && typeof child !== 'boolean') { |
| 34 | result.push(h(Text, String(child))) |
| 35 | } |
| 36 | } |
| 37 | return result |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Renders its slot only while its click is shown, so the content is mounted |
| 42 | * on the click and unmounted when it's hidden again. |
| 43 | * |
| 44 | * Each element is mounted with the `slidev-vclick-hidden` class (`opacity: 0`, plus the |
| 45 | * animation preset) and the class is removed on the next frame, so it animates in the same |
| 46 | * way as with the plain `v-click` directive. When it's hidden again, the content is unmounted |
| 47 | * on the click, and its element is kept with the class added back until the transition has finished. |
| 48 | * |
| 49 | * Used by `<v-click mount>`, `<v-after mount>` and the `.mount` modifier of the click directives. |
| 50 | * |
| 51 | * @internal |
| 52 | */ |
| 53 | export const VClickMount = defineComponent({ |
| 54 | name: 'VClickMount', |
| 55 | props: { |
| 56 | at: { |
| 57 | type: [Number, String, Array, Boolean] as PropType<RawAtValue>, |
| 58 | default: '+1', |
| 59 | }, |
| 60 | hide: { |
| 61 | type: Boolean, |
| 62 | default: false, |
| 63 | }, |
| 64 | /** |
| 65 | * Animation presets, e.g. `"fade up"`. Defaults to the `clickAnimation` frontmatter / config. |
| 66 | */ |
| 67 | animation: { |
| 68 | type: [String, Array] as PropType<string | string[]>, |
| 69 | default: undefined, |
| 70 | }, |
| 71 | wrapText: { |
| 72 | type: Function as PropType<(text: VNode) => VNode>, |
| 73 | default: (text: VNode) => h('span', text), |
| 74 | }, |
| 75 | }, |
| 76 | setup(props, { slots }) { |
| 77 | const clicks = injectLocal(injectionClicksContext, undefined)?.value |
| 78 | const frontmatter = injectLocal(injectionFrontmatter, undefined) |
| 79 | const id = makeId() |
| 80 | |
| 81 | // `undefined` until the click is registered, `null` when there is no click to wait for |
| 82 | const info = shallowRef<ClicksInfo | null | undefined>(undefined) |
| 83 | // The content that is already shown when the slide is rendered isn't animated, |
| 84 | // the same as the plain `v-click` directive. |
| 85 | const animate = shallowRef(false) |
| 86 | |
| 87 | // Register in `onMounted`, which runs in the same order as the `mounted` hook |
| 88 | // of the `v-click` directive, so relative values (`'+1'`) resolve the same way. |
| 89 | onMounted(() => { |
| 90 | if (!clicks) { |
| 91 | info.value = null |
| 92 | return |
| 93 | } |
| 94 | const resolved = clicks.calculate(props.at) |
| 95 | clicks.register(id, resolved) |
| 96 | info.value = resolved |
| 97 | nextTick(() => { |
| 98 | animate.value = true |
| 99 | }) |
| 100 | }) |
| 101 | |
| 102 | onUnmounted(() => { |
| 103 | clicks?.unregister(id) |
| 104 | }) |
| 105 | |
| 106 | const isShown = computed(() => { |
| 107 | const resolved = info.value |
| 108 | if (resolved === undefined) |
| 109 | return false |
| 110 | if (resolved === null) |
| 111 | return true |
| 112 | return props.hide |
| 113 | ? !resolved.isActive.value |
| 114 | : resolved.isActive.value |
| 115 | }) |
| 116 | |
| 117 | const animationClasses = computed(() => { |
| 118 | let animations: string[] |
| 119 | if (props.animation != null) { |
| 120 | animations = Array.isArray(props.animation) |
| 121 | ? props.animation |
| 122 | : props.animation.split(RE_WHITESPACE_OR_COMMA) |
| 123 | } |
| 124 | else { |
| 125 | animations = (frontmatter?.clickAnimation || configs.clickAnimation || '').split(RE_WHITESPACE_OR_COMMA) |
| 126 | } |
| 127 | return animations.filter(Boolean).map(a => `${CLASS_VCLICK_ANIMATION_PREFIX}${a}`) |
| 128 | }) |
| 129 | |
| 130 | return () => { |
| 131 | const shown = isShown.value |
| 132 | const resolved = info.value |
| 133 | const current = !!resolved && resolved.isCurrent.value |
| 134 | const classes = [ |
| 135 | CLASS_VCLICK_TARGET, |
| 136 | ...animationClasses.value, |
| 137 | { |
| 138 | [CLASS_VCLICK_CURRENT]: current, |
| 139 | [CLASS_VCLICK_PRIOR]: !!resolved && resolved.isActive.value && !current, |
| 140 | }, |
| 141 | ] |
| 142 | // The slot is rendered even while hidden, so the transitions stay in place and |
| 143 | // can animate the elements out before they are unmounted. |
| 144 | // Rendering the slot only creates vnodes, nothing is mounted until it's shown. |
| 145 | const children = flattenChildren(slots.default?.()) |
| 146 | // One transition per element, so each of them is animated on its own, |
| 147 | // like the plain `v-click` directive does on every element. |
| 148 | return h(Fragment, children.map((child, idx) => h( |
| 149 | Transition, |
| 150 | { |
| 151 | key: child.key ?? `__vclick_mount_${idx}`, |
| 152 | css: animate.value, |
| 153 | enterFromClass: CLASS_VCLICK_HIDDEN, |
| 154 | enterActiveClass: '', |
| 155 | enterToClass: '', |
| 156 | leaveFromClass: '', |
| 157 | leaveActiveClass: '', |
| 158 | leaveToClass: CLASS_VCLICK_HIDDEN, |
| 159 | }, |
| 160 | () => shown |
| 161 | ? cloneVNode(child.type === Text |
| 162 | ? props.wrapText(child) |
| 163 | : child, { class: classes }) |
| 164 | : null, |
| 165 | ))) |
| 166 | } |
| 167 | }, |
| 168 | }) |
| 169 |