Gradient Text
gradient-textuiApplies a CSS gradient as text color via background-clip: text. Supports preset gradients (sunset, ocean, forest, fire, candy, aurora, rainbow, gold, neon, grape), custom from/to colors with a direction, a fully custom CSS gradient string, and an animated mode that shifts the gradient.
Installation
$pnpm dlx shadcn@latest add https://uipkge.dev/r/lit/gradient-text.json$npx shadcn@latest add https://uipkge.dev/r/lit/gradient-text.json$yarn dlx shadcn@latest add https://uipkge.dev/r/lit/gradient-text.json$bunx shadcn@latest add https://uipkge.dev/r/lit/gradient-text.jsonManifest:
https://uipkge.dev/r/lit/gradient-text.jsonInstalls to:src/uip/components/gradient-text/Examples
Loading interactive previews…
Loading interactive previews…
Props
| Name | Type / Values | Default | Required |
|---|---|---|---|
preset | GradientPreset | — | optional |
from | string | — | optional |
to | string | — | optional |
direction | Direction | to right | optional |
gradient | string | — | optional |
animated | boolean | false | optional |
animation-duration | number | 4 | optional |
Schema
Type aliases from this item's source — use them to shape the data you pass in.
HTMLElementTagNameMapinterface HTMLElementTagNameMap {
'uip-gradient-text': UipGradientText
}
}npm dependencies
Includes
Files installed (2)
src/uip/components/gradient-text/gradient-text.ts2.6 kB
import { LitElement, css, html } from 'lit' import { tailwind } from '../../lib/styles' import { ThemeController } from '../../lib/theme' import { gradientTextPresets, type GradientPreset } from './gradient-text.variants' const gradientKeyframes = css` @media (prefers-reduced-motion: no-preference) { @keyframes gradient-text-shift { 0% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0% 50%; } } } ` type Direction = | 'to right' | 'to left' | 'to top' | 'to bottom' | 'to top right' | 'to top left' | 'to bottom right' | 'to bottom left' /** * <uip-gradient-text> — renders text with a CSS gradient fill. */ export class UipGradientText extends LitElement { static styles = [ tailwind, gradientKeyframes, css` :host { display: inline-block; } `, ] static properties = { preset: { reflect: true }, from: { reflect: true }, to: { reflect: true }, direction: { reflect: true }, gradient: { reflect: true }, animated: { type: Boolean, reflect: true }, animationDuration: { type: Number, attribute: 'animation-duration', reflect: true }, } preset?: GradientPreset from?: string to?: string direction: Direction = 'to right' gradient?: string animated = false animationDuration = 4 constructor() { super() new ThemeController(this) } connectedCallback() { super.connectedCallback() this.setAttribute('data-uipkge', '') this.setAttribute('data-slot', 'gradient-text') } willUpdate() { if (this.preset) this.setAttribute('data-preset', this.preset) if (this.animated) this.setAttribute('data-animated', 'true') } render() { let g = this.gradient if (!g && this.preset) g = gradientTextPresets[this.preset] if (!g && this.from && this.to) g = `linear-gradient(${this.direction}, ${this.from}, ${this.to})` if (!g) g = 'linear-gradient(to right, var(--primary), var(--primary))' const style = `background-image: ${g}; background-clip: text; -webkit-background-clip: text; color: transparent; -webkit-text-fill-color: transparent; ${ this.animated ? `background-size: 200% 200%; animation: gradient-text-shift ${this.animationDuration}s ease infinite;` : '' }` return html`<span part="base" data-slot="gradient-text" class="inline-block" style=${style}><slot></slot></span>` } } customElements.get('uip-gradient-text') || customElements.define('uip-gradient-text', UipGradientText) declare global { interface HTMLElementTagNameMap { 'uip-gradient-text': UipGradientText } }src/uip/components/gradient-text/gradient-text.variants.ts1 kB
import type { VariantProps } from 'class-variance-authority' import { cva } from 'class-variance-authority' export const gradientTextPresets = { sunset: 'linear-gradient(to right, #ff7e5f, #feb47b)', ocean: 'linear-gradient(to right, #2193b0, #6dd5ed)', forest: 'linear-gradient(to right, #11998e, #38ef7d)', fire: 'linear-gradient(to right, #f12711, #f5af19)', candy: 'linear-gradient(to right, #f857a6, #ff8a5c)', aurora: 'linear-gradient(to right, #00c6ff, #0072ff, #00c6ff)', rainbow: 'linear-gradient(to right, #ff0000, #ff7f00, #ffff00, #00ff00, #0000ff, #4b0082, #9400d3)', gold: 'linear-gradient(to right, #bf953f, #fcf6ba, #b38728, #fbf5b7, #aa771c)', neon: 'linear-gradient(to right, #00f260, #0575e6)', grape: 'linear-gradient(to right, #6a11cb, #2575fc)', } as const export type GradientPreset = keyof typeof gradientTextPresets export const gradientTextVariants = cva('inline-block', { variants: {}, defaultVariants: {}, }) export type GradientTextVariants = VariantProps<typeof gradientTextVariants>
Raw manifest:https://uipkge.dev/r/lit/gradient-text.json