Перейти к содержимому

Типографика

Диагональный текст

Анимированный текст с проходящей градиентной полосой и поддержкой циклической смены строк.


Диагональный текст

Компонент для акцентного текста с проходящей градиентной полосой. Поддерживает одиночную строку, массив строк, задержку, длительность, циклическое воспроизведение и callback завершения прохода.

Превью

Мы создаём сайты
<script lang="ts" module>
  export const DEFAULT_SWEEP_COLORS = ["#ffffff", "#c4b5fd", "#ffffff"];
  export const DEFAULT_BASE_COLOR = "currentColor";
  export const DEFAULT_DURATION = 1200;
  export const DEFAULT_DELAY = 0;
  export const DEFAULT_LOOP_DELAY = 0;
  export const DEFAULT_BAND_RATIO = 0.22;
  export const ENTER_DURATION = 350;
  export const EXIT_DURATION = 250;
  export const SWAP_SHIFT = 8;
  export const SWAP_EASING = "cubic-bezier(0.22, 1, 0.36, 1)";
  export const SWEEP_EASING = "cubic-bezier(0.4, 0, 0.2, 1)";

  export type IDiaText = {
    text: string | string[];
    sweepColors?: string[];
    baseColor?: string;
    duration?: number;
    delay?: number;
    loop?: boolean;
    loopDelay?: number;
    bandRatio?: number;
    autoPlay?: boolean;
    textStyle?: string;
    style?: string;
    class?: string;
    textClass?: string;
    /** Optional continuation revealed when the text is hovered. */
    revealText?: string;
    /** Reveal revealText on pointer hover/focus. */
    revealOnHover?: boolean;
    onSweepEnd?: (finishedIndex: number) => void;
  };
</script>

<script lang="ts">
  import { onDestroy } from "svelte";

  let {
    text,
    sweepColors = DEFAULT_SWEEP_COLORS,
    baseColor = DEFAULT_BASE_COLOR,
    duration = DEFAULT_DURATION,
    delay = DEFAULT_DELAY,
    loop = false,
    loopDelay = DEFAULT_LOOP_DELAY,
    bandRatio = DEFAULT_BAND_RATIO,
    autoPlay = true,
    textStyle = "",
    style = "",
    class: className = "",
    textClass = "",
    revealText = "",
    revealOnHover = false,
    onSweepEnd,
  }: IDiaText = $props();

  const texts = $derived<string[]>(Array.isArray(text) ? [...text] : [text]);
  const textKey = $derived(texts.join(""));
  const isMulti = $derived(texts.length > 1);

  let index = $state(0);
  let cycle = $state(0);
  let width = $state(0);
  let hovered = $state(false);

  let contentEl: HTMLElement | undefined = $state();
  let sweepEl: HTMLElement | undefined = $state();
  let timer: ReturnType<typeof setTimeout> | undefined;

  let previousTextKey = "";
  $effect(() => {
    if (previousTextKey !== textKey) {
      previousTextKey = textKey;
      index = 0;
    }
  });

  const band = $derived(width * bandRatio);
  const strip = $derived(width + band);
  const ready = $derived(width > 0);
  const label = $derived(texts[index] ?? "");

  const gradient = $derived.by(() => {
    if (!ready) return "none";
    const start = (width / strip) * 100;
    const colors = [...sweepColors, "transparent"];
    const step = (100 - start) / (colors.length - 1);
    const stops = colors
      .map((c, i) => `${c} ${(start + step * i).toFixed(2)}%`)
      .join(", ");
    return `linear-gradient(90deg, ${baseColor} 0%, ${baseColor} ${start.toFixed(2)}%, ${stops})`;
  });

  function commitNext() {
    index = (index + 1) % texts.length;
    cycle += 1;
  }

  function handleSweepEnd(finishedIndex: number) {
    onSweepEnd?.(finishedIndex);
    if (!loop) return;

    timer = setTimeout(() => {
      if (!isMulti) {
        cycle += 1;
        return;
      }
      const exit = contentEl?.animate(
        [
          { opacity: 1, transform: "translateY(0)" },
          { opacity: 0, transform: `translateY(${-SWAP_SHIFT}px)` },
        ],
        { duration: EXIT_DURATION, easing: SWAP_EASING, fill: "forwards" },
      );
      if (!exit) return commitNext();
      exit.finished.then(commitNext).catch(() => undefined);
    }, loopDelay);
  }

  $effect(() => {
    if (!ready || !autoPlay || !sweepEl) return;

    const playing = index;

    if (matchMedia("(prefers-reduced-motion: reduce)").matches) {
      onSweepEnd?.(playing);
      return;
    }

    const animations: Animation[] = [];

    if (isMulti && contentEl) {
      animations.push(
        contentEl.animate(
          [
            { opacity: 0, transform: `translateY(${SWAP_SHIFT}px)` },
            { opacity: 1, transform: "translateY(0)" },
          ],
          { duration: ENTER_DURATION, easing: SWAP_EASING, fill: "both" },
        ),
      );
    }

    const sweep = sweepEl.animate(
      [
        { backgroundPositionX: "calc(var(--travel) * -1)" },
        { backgroundPositionX: "0px" },
      ],
      { duration, delay, easing: SWEEP_EASING, fill: "both" },
    );
    sweep.id = String(cycle);
    animations.push(sweep);

    sweep.finished.then(() => handleSweepEnd(playing)).catch(() => undefined);

    return () => {
      clearTimeout(timer);
      animations.forEach((a) => a.cancel());
    };
  });

  onDestroy(() => clearTimeout(timer));
</script>

<span
  class="dia-text {className}"
  role="group"
  {style}
  onmouseenter={() => (hovered = true)}
  onmouseleave={() => (hovered = false)}
  onfocusin={() => (hovered = true)}
  onfocusout={() => (hovered = false)}
>
  <span class="sizer {textClass}" style={textStyle} bind:clientWidth={width}>{label}</span>

  {#if ready}
    <span
      class="content"
      aria-hidden="true"
      bind:this={contentEl}
      style:opacity={isMulti ? 0 : 1}
    >
      <span
        class="sweep {textClass}"
        bind:this={sweepEl}
        style="{textStyle}; --strip: {strip}px; --travel: {strip}px; background-image: {gradient};"
      >{label}</span>
    </span>
  {/if}

  {#if revealOnHover && revealText}
    <span
      class="reveal"
      class:visible={hovered}
      aria-hidden={!hovered}
    >{revealText}</span>
  {/if}
</span>

<style>
  .dia-text {
    position: relative;
    display: inline-flex;
    align-items: center;
  }

  .sizer {
    display: inline-block;
    white-space: nowrap;
    opacity: 0;
  }

  .content {
    position: absolute;
    inset: 0;
    pointer-events: none;
    display: flex;
    align-items: center;
  }

  .sweep {
    display: inline-block;
    white-space: nowrap;
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
    background-clip: text;
    background-repeat: no-repeat;
    background-size: var(--strip) 100%;
    background-position-x: calc(var(--travel) * -1);
  }

  .reveal {
    display: inline-block;
    max-width: 0;
    margin-left: 0;
    overflow: hidden;
    opacity: 0;
    transform: translateX(-0.35rem);
    white-space: nowrap;
    color: var(--foreground-muted);
    transition:
      max-width 420ms cubic-bezier(0.22, 1, 0.36, 1),
      margin-left 420ms cubic-bezier(0.22, 1, 0.36, 1),
      opacity 220ms ease,
      transform 420ms cubic-bezier(0.22, 1, 0.36, 1);
  }

  .reveal.visible {
    max-width: 40rem;
    margin-left: 0.65em;
    opacity: 1;
    transform: translateX(0);
  }

  @media (prefers-reduced-motion: reduce) {
    .content {
      opacity: 1 !important;
    }
    .sweep {
      background-position-x: 0 !important;
    }
    .reveal {
      transition: none;
    }
  }
</style>
<script lang="ts" module>
  export const DEFAULT_SWEEP_COLORS = ["#ffffff", "#c4b5fd", "#ffffff"];
  export const DEFAULT_BASE_COLOR = "currentColor";
  export const DEFAULT_DURATION = 1200;
  export const DEFAULT_DELAY = 0;
  export const DEFAULT_LOOP_DELAY = 0;
  export const DEFAULT_BAND_RATIO = 0.22;
  export const ENTER_DURATION = 350;
  export const EXIT_DURATION = 250;
  export const SWAP_SHIFT = 8;
  export const SWAP_EASING = "cubic-bezier(0.22, 1, 0.36, 1)";
  export const SWEEP_EASING = "cubic-bezier(0.4, 0, 0.2, 1)";

  export type IDiaText = {
    text: string | string[];
    sweepColors?: string[];
    baseColor?: string;
    duration?: number;
    delay?: number;
    loop?: boolean;
    loopDelay?: number;
    bandRatio?: number;
    autoPlay?: boolean;
    textStyle?: string;
    style?: string;
    class?: string;
    textClass?: string;
    /** Optional continuation revealed when the text is hovered. */
    revealText?: string;
    /** Reveal revealText on pointer hover/focus. */
    revealOnHover?: boolean;
    onSweepEnd?: (finishedIndex: number) => void;
  };
</script>

<script lang="ts">
  import { onDestroy } from "svelte";

  let {
    text,
    sweepColors = DEFAULT_SWEEP_COLORS,
    baseColor = DEFAULT_BASE_COLOR,
    duration = DEFAULT_DURATION,
    delay = DEFAULT_DELAY,
    loop = false,
    loopDelay = DEFAULT_LOOP_DELAY,
    bandRatio = DEFAULT_BAND_RATIO,
    autoPlay = true,
    textStyle = "",
    style = "",
    class: className = "",
    textClass = "",
    revealText = "",
    revealOnHover = false,
    onSweepEnd,
  }: IDiaText = $props();

  const texts = $derived<string[]>(Array.isArray(text) ? [...text] : [text]);
  const textKey = $derived(texts.join(""));
  const isMulti = $derived(texts.length > 1);

  let index = $state(0);
  let cycle = $state(0);
  let width = $state(0);
  let hovered = $state(false);

  let contentEl: HTMLElement | undefined = $state();
  let sweepEl: HTMLElement | undefined = $state();
  let timer: ReturnType<typeof setTimeout> | undefined;

  let previousTextKey = "";
  $effect(() => {
    if (previousTextKey !== textKey) {
      previousTextKey = textKey;
      index = 0;
    }
  });

  const band = $derived(width * bandRatio);
  const strip = $derived(width + band);
  const ready = $derived(width > 0);
  const label = $derived(texts[index] ?? "");

  const gradient = $derived.by(() => {
    if (!ready) return "none";
    const start = (width / strip) * 100;
    const colors = [...sweepColors, "transparent"];
    const step = (100 - start) / (colors.length - 1);
    const stops = colors
      .map((c, i) => `${c} ${(start + step * i).toFixed(2)}%`)
      .join(", ");
    return `linear-gradient(90deg, ${baseColor} 0%, ${baseColor} ${start.toFixed(2)}%, ${stops})`;
  });

  function commitNext() {
    index = (index + 1) % texts.length;
    cycle += 1;
  }

  function handleSweepEnd(finishedIndex: number) {
    onSweepEnd?.(finishedIndex);
    if (!loop) return;

    timer = setTimeout(() => {
      if (!isMulti) {
        cycle += 1;
        return;
      }
      const exit = contentEl?.animate(
        [
          { opacity: 1, transform: "translateY(0)" },
          { opacity: 0, transform: `translateY(${-SWAP_SHIFT}px)` },
        ],
        { duration: EXIT_DURATION, easing: SWAP_EASING, fill: "forwards" },
      );
      if (!exit) return commitNext();
      exit.finished.then(commitNext).catch(() => undefined);
    }, loopDelay);
  }

  $effect(() => {
    if (!ready || !autoPlay || !sweepEl) return;

    const playing = index;

    if (matchMedia("(prefers-reduced-motion: reduce)").matches) {
      onSweepEnd?.(playing);
      return;
    }

    const animations: Animation[] = [];

    if (isMulti && contentEl) {
      animations.push(
        contentEl.animate(
          [
            { opacity: 0, transform: `translateY(${SWAP_SHIFT}px)` },
            { opacity: 1, transform: "translateY(0)" },
          ],
          { duration: ENTER_DURATION, easing: SWAP_EASING, fill: "both" },
        ),
      );
    }

    const sweep = sweepEl.animate(
      [
        { backgroundPositionX: "calc(var(--travel) * -1)" },
        { backgroundPositionX: "0px" },
      ],
      { duration, delay, easing: SWEEP_EASING, fill: "both" },
    );
    sweep.id = String(cycle);
    animations.push(sweep);

    sweep.finished.then(() => handleSweepEnd(playing)).catch(() => undefined);

    return () => {
      clearTimeout(timer);
      animations.forEach((a) => a.cancel());
    };
  });

  onDestroy(() => clearTimeout(timer));
</script>

<span
  class="dia-text {className}"
  role="group"
  {style}
  onmouseenter={() => (hovered = true)}
  onmouseleave={() => (hovered = false)}
  onfocusin={() => (hovered = true)}
  onfocusout={() => (hovered = false)}
>
  <span class="sizer {textClass}" style={textStyle} bind:clientWidth={width}>{label}</span>

  {#if ready}
    <span
      class="content"
      aria-hidden="true"
      bind:this={contentEl}
      style:opacity={isMulti ? 0 : 1}
    >
      <span
        class="sweep {textClass}"
        bind:this={sweepEl}
        style="{textStyle}; --strip: {strip}px; --travel: {strip}px; background-image: {gradient};"
      >{label}</span>
    </span>
  {/if}

  {#if revealOnHover && revealText}
    <span
      class="reveal"
      class:visible={hovered}
      aria-hidden={!hovered}
    >{revealText}</span>
  {/if}
</span>

<style>
  .dia-text {
    position: relative;
    display: inline-flex;
    align-items: center;
  }

  .sizer {
    display: inline-block;
    white-space: nowrap;
    opacity: 0;
  }

  .content {
    position: absolute;
    inset: 0;
    pointer-events: none;
    display: flex;
    align-items: center;
  }

  .sweep {
    display: inline-block;
    white-space: nowrap;
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
    background-clip: text;
    background-repeat: no-repeat;
    background-size: var(--strip) 100%;
    background-position-x: calc(var(--travel) * -1);
  }

  .reveal {
    display: inline-block;
    max-width: 0;
    margin-left: 0;
    overflow: hidden;
    opacity: 0;
    transform: translateX(-0.35rem);
    white-space: nowrap;
    color: var(--foreground-muted);
    transition:
      max-width 420ms cubic-bezier(0.22, 1, 0.36, 1),
      margin-left 420ms cubic-bezier(0.22, 1, 0.36, 1),
      opacity 220ms ease,
      transform 420ms cubic-bezier(0.22, 1, 0.36, 1);
  }

  .reveal.visible {
    max-width: 40rem;
    margin-left: 0.65em;
    opacity: 1;
    transform: translateX(0);
  }

  @media (prefers-reduced-motion: reduce) {
    .content {
      opacity: 1 !important;
    }
    .sweep {
      background-position-x: 0 !important;
    }
    .reveal {
      transition: none;
    }
  }
</style>

Использование

<script lang="ts">
	import DiaText from '$lib/components/ui-registry/DiaText.svelte';
</script>

<p>
	Мы создаём <DiaText text="цифровые продукты" />
</p>
<script lang="ts">
	import DiaText from '$lib/components/ui-registry/DiaText.svelte';
</script>

<p>
	Мы создаём <DiaText text="цифровые продукты" />
</p>

Циклическая смена

<DiaText
	text={['сайты', 'приложения', 'инструменты']}
	loop
	duration={1400}
	delay={200}
	loopDelay={800}
	sweepColors={['#ffffff', '#8b5cf6', '#ffffff']}
	baseColor="currentColor"
/>
<DiaText
	text={['сайты', 'приложения', 'инструменты']}
	loop
	duration={1400}
	delay={200}
	loopDelay={800}
	sweepColors={['#ffffff', '#8b5cf6', '#ffffff']}
	baseColor="currentColor"
/>

Стилизация шрифта

Шрифт и размер наследуются от родителя. Для точной настройки используйте textClass или textStyle — они применяются и к измеряющему, и к анимируемому тексту, поэтому размеры совпадают.

<DiaText
	text="Заголовок"
	textClass="text-4xl font-bold"
	textStyle="letter-spacing: -0.02em"
/>
<DiaText
	text="Заголовок"
	textClass="text-4xl font-bold"
	textStyle="letter-spacing: -0.02em"
/>

Пропсы

Пропс Тип По умолчанию Описание
text string string[] — Текст или массив текстов
sweepColors string[] ['#ffffff', '#c4b5fd', '#ffffff'] Цвета проходящей полосы
baseColor string currentColor Базовый цвет текста
duration number 1200 Длительность прохода в мс
delay number 0 Задержка перед проходом в мс
loop boolean false Повторять анимацию и переключать строки
loopDelay number 0 Пауза между проходами в мс
bandRatio number 0.22 Относительная ширина градиентной полосы
autoPlay boolean true Запускать анимацию автоматически
class string '' Класс корневого элемента
textClass string '' Класс текста
style string '' Inline-стили корневого элемента
textStyle string '' Inline-стили текста
onSweepEnd (index: number) => void — Callback после завершения прохода