Skip to content

Line Shadow Text

A text component with a moving diagonal line shadow.

Installation

Copy and paste the following code into your project:

vue
<script setup lang="ts">
import { computed } from "vue";
import { cn } from "@/lib/utils";

interface LineShadowTextProps {
  class?: string;
  text: string;
  shadowColor?: string;
  as?: keyof HTMLElementTagNameMap;
}

const props = withDefaults(defineProps<LineShadowTextProps>(), {
  shadowColor: "black",
  as: "span",
});

const style = computed(() => ({
  "--shadow-color": props.shadowColor,
}));
</script>

<template>
  <component
    :is="props.as"
    :style="style"
    :data-text="props.text"
    :class="cn('line-shadow-text relative z-0 inline-flex', props.class)"
  >
    {{ props.text }}
  </component>
</template>

<style scoped>
.line-shadow-text::after {
  content: attr(data-text);
  position: absolute;
  top: 0.04em;
  left: 0.04em;
  z-index: -10;
  background-image: linear-gradient(
    45deg,
    transparent 45%,
    var(--shadow-color) 45%,
    var(--shadow-color) 55%,
    transparent 0
  );
  background-size: 0.06em 0.06em;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: line-shadow 15s linear infinite;
}

@keyframes line-shadow {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 100% -100%;
  }
}
</style>

If you prefer to keep the keyframes in your Tailwind config instead of a scoped <style> block, add the following animation to your tailwind.config.js:

tailwind.config.js
js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      animation: {
        "line-shadow": "line-shadow 15s linear infinite",
      },
      keyframes: {
        "line-shadow": {
          "0%": { "background-position": "0 0" },
          "100%": { "background-position": "100% -100%" },
        },
      },
    },
  },
};

Usage

vue
<script setup lang="ts">
import LineShadowText from "@/components/spark-ui/line-shadow-text/line-shadow-text.vue";
</script>

<template>
  <LineShadowText text="Fast" shadow-color="black" />
</template>

Props

PropTypeDefaultDescription
textstring-The text to display with shadow effect
shadowColorstring"black"The color of the moving line shadow
askeyof HTMLElementTagNameMap"span"The HTML element to render the text as
classstring-Additional classes to merge onto the tag

Released under the MIT License.