All files / show / mod.ts

100.00% Branches 6/6
100.00% Lines 24/24
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
x8
x8
x8
x15
x16
x16
x15
x15
x18
x18
x18
x18
x18
x19
x19
x18
x18
x18
x15
x8
 
 
x8



























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

/** `*show` directive. */
export const _show = {
  name: "*show",
  phase: Phase.DISPLAY,
  default: "true",
  async execute(renderer, element, { attributes: [attribute], ...options }) {
    if (!renderer.isHtmlElement(element)) {
      return
    }
    const result = Boolean(await renderer.evaluate(element, attribute.value || this.default, options))
    if (result) {
      element.style.removeProperty("display")
      if (!element.style.length) {
        element.removeAttribute("style")
      }
      if (renderer.window.getComputedStyle(element).display === "none") {
        element.style.setProperty("display", "initial", "important")
      }
    } else {
      element.style.setProperty("display", "none", "important")
    }
  },
} as Directive & { default: NonNullable<Directive["default"]> }

/** Default exports. */
export default _show