All files / ref / mod.ts

100.00% Branches 4/4
100.00% Lines 22/22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
x8
 
 
 
x8
x8
x32
x8
x8
 
 
x8
x8
x8
x8
x8
x40
x255
x51
x8
x8
x42
x58
x120
x14
x8
 
 
x8



























// Imports
import { type Directive, Phase } from "@mizu/internal/engine"
export type * from "@mizu/internal/engine"

/** `*ref` typings. */
export const typings = {
  modifiers: {
    raw: { type: Boolean, enforce: true },
  },
} as const

/** `*ref` directive. */
export const _ref = {
  name: "*ref",
  phase: Phase.REFERENCE,
  typings,
  setup(_, __, { state }) {
    if (!state.$refs) {
      return { state: { $refs: {} } }
    }
  },
  async execute(renderer, element, { attributes: [attribute], state, ...options }) {
    const parsed = renderer.parseAttribute(attribute, this.typings, { modifiers: true })
    const name = parsed.modifiers.raw ? attribute.value : await renderer.evaluate(element, attribute.value, { state, ...options })
    return { state: { $refs: { ...state.$refs as Record<PropertyKey, unknown>, [`${name}`]: element } } }
  },
} as Directive<null, typeof typings>

/** Default exports. */
export default _ref