summaryrefslogtreecommitdiffhomepage
path: root/packages/ui/src/components/text-shimmer.tsx
blob: 6ee4ef4020f10853a5024274b4fff845adeea5de (plain)
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
31
32
33
34
35
36
import { For, createMemo, type ValidComponent } from "solid-js"
import { Dynamic } from "solid-js/web"

export const TextShimmer = <T extends ValidComponent = "span">(props: {
  text: string
  class?: string
  as?: T
  active?: boolean
  stepMs?: number
  durationMs?: number
}) => {
  const chars = createMemo(() => Array.from(props.text))
  const active = () => props.active ?? true

  return (
    <Dynamic
      component={props.as || "span"}
      data-component="text-shimmer"
      data-active={active()}
      class={props.class}
      aria-label={props.text}
      style={{
        "--text-shimmer-step": `${props.stepMs ?? 45}ms`,
        "--text-shimmer-duration": `${props.durationMs ?? 1200}ms`,
      }}
    >
      <For each={chars()}>
        {(char, index) => (
          <span data-slot="text-shimmer-char" aria-hidden="true" style={{ "--text-shimmer-index": `${index()}` }}>
            {char}
          </span>
        )}
      </For>
    </Dynamic>
  )
}