| 1 | /** |
| 2 | * <v-clicks/> click animations component |
| 3 | * |
| 4 | * Learn more: https://sli.dev/guide/animations.html#click-animation |
| 5 | */ |
| 6 | |
| 7 | import type { VNode, VNodeArrayChildren } from 'vue' |
| 8 | import { toArray } from '@antfu/utils' |
| 9 | import { Comment, createVNode, defineComponent, h, isVNode, resolveDirective, Text, withDirectives } from 'vue' |
| 10 | import { normalizeSingleAtValue } from '../composables/useClicks' |
| 11 | import VClickGap from './VClickGap.vue' |
| 12 | |
| 13 | const listTags = ['ul', 'ol'] |
| 14 | const RE_WHITESPACE_OR_COMMA = /[\s,]+/ |
| 15 | |
| 16 | /** |
| 17 | * Positive depth from a `lists` or `tables` prop. |
| 18 | * Returns `null` when the prop is omitted so the legacy single-child behavior stays intact. |
| 19 | * |
| 20 | * @param value - Raw prop value. Empty or non-positive values mean "not set". |
| 21 | * @returns The depth, or `null` when the feature is off. |
| 22 | */ |
| 23 | function positiveDepth(value: number | string | undefined): number | null { |
| 24 | if (value == null || value === '') |
| 25 | return null |
| 26 | const depth = +value |
| 27 | if (Number.isNaN(depth) || depth <= 0) |
| 28 | return null |
| 29 | return depth |
| 30 | } |
| 31 | |
| 32 | export default defineComponent({ |
| 33 | props: { |
| 34 | depth: { |
| 35 | type: [Number, String], |
| 36 | default: 1, |
| 37 | }, |
| 38 | /** |
| 39 | * Reveal list items among other blocks, this many levels deep. |
| 40 | * `1` is each top-level item. `2` also reveals nested lists. |
| 41 | */ |
| 42 | lists: { |
| 43 | type: [Number, String], |
| 44 | default: undefined, |
| 45 | }, |
| 46 | /** |
| 47 | * Reveal table content among other blocks, this many levels deep. |
| 48 | * `1` is each body row. `2` is each body cell. |
| 49 | */ |
| 50 | tables: { |
| 51 | type: [Number, String], |
| 52 | default: undefined, |
| 53 | }, |
| 54 | every: { |
| 55 | type: [Number, String], |
| 56 | default: 1, |
| 57 | }, |
| 58 | at: { |
| 59 | type: [Number, String], |
| 60 | default: '+1', |
| 61 | }, |
| 62 | hide: { |
| 63 | type: Boolean, |
| 64 | default: false, |
| 65 | }, |
| 66 | /** |
| 67 | * @deprecated use `animation` instead |
| 68 | */ |
| 69 | fade: { |
| 70 | type: Boolean, |
| 71 | default: false, |
| 72 | }, |
| 73 | animation: { |
| 74 | type: [String, Array<string>], |
| 75 | default: undefined, |
| 76 | }, |
| 77 | handleSpecialElements: { |
| 78 | type: Boolean, |
| 79 | default: true, |
| 80 | }, |
| 81 | }, |
| 82 | render() { |
| 83 | const every = +this.every |
| 84 | const at = normalizeSingleAtValue(this.at) |
| 85 | const isRelative = typeof at === 'string' |
| 86 | |
| 87 | let elements = this.$slots.default?.() |
| 88 | |
| 89 | if (at == null || !elements) { |
| 90 | return elements |
| 91 | } |
| 92 | |
| 93 | const click = resolveDirective('click')! |
| 94 | |
| 95 | const applyDirective = (node: VNode, value: number | string) => { |
| 96 | const modifiers: Record<string, boolean> = { |
| 97 | hide: this.hide, |
| 98 | } |
| 99 | if (typeof this.animation === 'string') { |
| 100 | this.animation.split(RE_WHITESPACE_OR_COMMA).forEach((a) => { |
| 101 | if (a) |
| 102 | modifiers[a] = true |
| 103 | }) |
| 104 | } |
| 105 | else if (Array.isArray(this.animation)) { |
| 106 | this.animation.forEach(a => modifiers[a] = true) |
| 107 | } |
| 108 | if (this.fade) |
| 109 | modifiers.fade = true |
| 110 | |
| 111 | return withDirectives(node, [ |
| 112 | [ |
| 113 | click, |
| 114 | value, |
| 115 | '', |
| 116 | modifiers, |
| 117 | ], |
| 118 | ]) |
| 119 | } |
| 120 | |
| 121 | const openAllTopLevelSlots = <T extends VNodeArrayChildren | VNode[]>(children: T): T => { |
| 122 | return children.flatMap((i) => { |
| 123 | if (isVNode(i) && typeof i.type === 'symbol' && Array.isArray(i.children)) |
| 124 | return openAllTopLevelSlots(i.children) |
| 125 | else |
| 126 | return [i] |
| 127 | }) as T |
| 128 | } |
| 129 | |
| 130 | elements = openAllTopLevelSlots(toArray(elements)) |
| 131 | |
| 132 | let globalIdx = 1 |
| 133 | let execIdx = 0 |
| 134 | |
| 135 | const reserveClick = () => +at + Math.ceil(globalIdx++ / every) - 1 |
| 136 | |
| 137 | /** |
| 138 | * Directive value for a reserved click index. |
| 139 | * Call it after the node's children are mapped: Vue mounts children first, |
| 140 | * so relative deltas must follow that order. |
| 141 | */ |
| 142 | const clickValueFor = (thisShowIdx: number) => { |
| 143 | const delta = thisShowIdx - execIdx |
| 144 | execIdx = thisShowIdx |
| 145 | return isRelative |
| 146 | ? delta >= 0 |
| 147 | ? `+${delta}` |
| 148 | : `${delta}` |
| 149 | : thisShowIdx |
| 150 | } |
| 151 | |
| 152 | const nextClickValue = () => clickValueFor(reserveClick()) |
| 153 | |
| 154 | const mapSubList = (children: VNodeArrayChildren, depth: number, maxDepth: number): VNodeArrayChildren => { |
| 155 | const vNodes = openAllTopLevelSlots(children).map((i) => { |
| 156 | if (!isVNode(i)) |
| 157 | return i |
| 158 | if (listTags.includes(i.type as string) && Array.isArray(i.children)) { |
| 159 | // eslint-disable-next-line ts/no-use-before-define |
| 160 | const vNodes = mapChildren(i.children, depth + 1, maxDepth) |
| 161 | return h(i, {}, vNodes) |
| 162 | } |
| 163 | return h(i) |
| 164 | }) |
| 165 | return vNodes |
| 166 | } |
| 167 | |
| 168 | const componentDepth = +this.depth |
| 169 | const mapChildren = (children: VNodeArrayChildren, depth = 1, maxDepth = componentDepth): VNodeArrayChildren => { |
| 170 | const vNodes = openAllTopLevelSlots(children).map((i) => { |
| 171 | if (!isVNode(i) || i.type === Comment) |
| 172 | return i |
| 173 | |
| 174 | // Reserve the index before recursing, so a parent shows before its nested items. |
| 175 | const thisShowIdx = reserveClick() |
| 176 | |
| 177 | let vNode |
| 178 | if (depth < maxDepth && Array.isArray(i.children)) |
| 179 | vNode = h(i, {}, mapSubList(i.children, depth, maxDepth)) |
| 180 | else |
| 181 | vNode = h(i) |
| 182 | |
| 183 | return applyDirective(vNode, clickValueFor(thisShowIdx)) |
| 184 | }) |
| 185 | return vNodes |
| 186 | } |
| 187 | |
| 188 | const expandTable = (tableNode: VNode, maxDepth: number) => { |
| 189 | if (!Array.isArray(tableNode.children)) |
| 190 | return applyDirective(h(tableNode), nextClickValue()) |
| 191 | |
| 192 | return h(tableNode, {}, tableNode.children.map((i) => { |
| 193 | if (!isVNode(i)) |
| 194 | return i |
| 195 | // Header rows stay visible. Body rows (or their cells) take the clicks. |
| 196 | if (i.type === 'tbody' && Array.isArray(i.children)) { |
| 197 | if (maxDepth >= 2) { |
| 198 | const rows = openAllTopLevelSlots(i.children).map((row) => { |
| 199 | if (!isVNode(row) || row.type !== 'tr' || !Array.isArray(row.children)) |
| 200 | return row |
| 201 | return h(row, {}, mapChildren(row.children, 1, 1)) |
| 202 | }) |
| 203 | return h(i, {}, rows) |
| 204 | } |
| 205 | return h(i, {}, mapChildren(i.children, 1, 1)) |
| 206 | } |
| 207 | return h(i) |
| 208 | })) |
| 209 | } |
| 210 | |
| 211 | const isBlankText = (node: VNode) => |
| 212 | node.type === Text && (typeof node.children !== 'string' || node.children.trim() === '') |
| 213 | |
| 214 | const mapMixed = (children: VNodeArrayChildren, listsDepth: number | null, tablesDepth: number | null): VNodeArrayChildren => { |
| 215 | return openAllTopLevelSlots(children).map((i) => { |
| 216 | if (!isVNode(i) || i.type === Comment || isBlankText(i)) |
| 217 | return i |
| 218 | |
| 219 | if (listsDepth != null && listTags.includes(i.type as string) && Array.isArray(i.children)) |
| 220 | return h(i, {}, mapChildren(i.children, 1, listsDepth)) |
| 221 | |
| 222 | if (tablesDepth != null && i.type === 'table') |
| 223 | return expandTable(i, tablesDepth) |
| 224 | |
| 225 | return applyDirective(h(i), nextClickValue()) |
| 226 | }) |
| 227 | } |
| 228 | |
| 229 | const lastGap = () => createVNode(VClickGap, { |
| 230 | size: +at + Math.ceil((globalIdx - 1) / every) - 1 - execIdx, |
| 231 | }) |
| 232 | |
| 233 | if (this.handleSpecialElements) { |
| 234 | const listsDepth = positiveDepth(this.lists) |
| 235 | const tablesDepth = positiveDepth(this.tables) |
| 236 | const only = elements.length === 1 |
| 237 | ? elements[0] |
| 238 | : null |
| 239 | |
| 240 | // A lone list or table keeps the previous behavior unless lists/tables asks for it. |
| 241 | if (only && listsDepth == null && listTags.includes(only.type as string) && Array.isArray(only.children)) { |
| 242 | return h(only, {}, [ |
| 243 | ...mapChildren(only.children), |
| 244 | lastGap(), |
| 245 | ]) |
| 246 | } |
| 247 | |
| 248 | if (only && tablesDepth == null && only.type === 'table' && Array.isArray(only.children)) { |
| 249 | return h(only, {}, only.children.map((i) => { |
| 250 | if (!isVNode(i)) { |
| 251 | return i |
| 252 | } |
| 253 | else if (i.type === 'tbody' && Array.isArray(i.children)) { |
| 254 | return h(i, {}, [ |
| 255 | ...mapChildren(i.children), |
| 256 | lastGap(), |
| 257 | ]) |
| 258 | } |
| 259 | else { |
| 260 | return h(i) |
| 261 | } |
| 262 | })) |
| 263 | } |
| 264 | |
| 265 | if (listsDepth != null || tablesDepth != null) { |
| 266 | return [ |
| 267 | ...mapMixed(elements, listsDepth, tablesDepth), |
| 268 | lastGap(), |
| 269 | ] |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return [ |
| 274 | ...mapChildren(elements), |
| 275 | lastGap(), |
| 276 | ] |
| 277 | }, |
| 278 | }) |
| 279 |