Command palette

@yasinsensoy/web

Web · Components

EffectLayer

Client layer of effects.

22 exports

Live example

Preview

NewPro

Card

Defaults come from UIProvider.
Hover, click.
Revenue
$128,400
12% increase
Warning
Alert effects are managed from the same place.
AK
How long does shipping take?
1-3 business days.
How do I return an item?
My account → Orders.

Code

import { UIProvider, Button } from "@yasinsensoy/web";

// app/appearance.ts
import { defineAppearance } from "@yasinsensoy/web";

export const appearance = defineAppearance({
  defaults: {
    button: { effect: "ripple", shape: "rounded", variant: "solid" },
    card: { variant: "elevated", effect: ["tilt", "glow"] },
    badge: { effect: "shine" },
    alert: { effect: "lift" }, stat: { effect: "lift" }, avatar: { effect: "lift" },
    checkbox: { effect: "pulse" }, switch: { effect: "pulse" }, tabs: { effect: "pulse" },
    field: { effect: "glow", variant: "outline" }, // every form field
    accordion: { effect: "ripple" }, menu: { effect: "ripple" }, sidebar: { effect: "ripple" },
  },
  effects: {
    sparkle: { press: (el, { x, y }) => sprinkleStars(el, x, y) },
  },
  variants: {
    button: { neon: { extends: "outline", className: "border-lime-400 bg-black text-lime-300" } },
    card: { neon: "border-lime-400/60 shadow-[0_0_32px] shadow-lime-400/20" },
    field: { glass: { extends: "filled", className: "bg-primary/5 backdrop-blur" } },
  },
});

// app/layout.tsx
<UIProvider appearance={appearance}>{children}</UIProvider>

// ui.d.ts
declare module "@yasinsensoy/web" {
  interface Register { appearance: typeof import("./app/appearance").appearance }
}

// A component's own prop always wins:
<Button effect="none">Efektsiz</Button>

Events

  1. Events appear here when you interact with the preview.

Effects on your own element: getEffects

My own box

Import

import {
  EffectLayer,
  EffectParts,
  getEffects,
  mergeAppearance,
  defineAppearance,
  getAppearance,
} from "@yasinsensoy/web";

EffectLayer

componentClient component

Client layer of effects. Placed inside the element with effects and binds listeners to its parent (the owner of the effect); also works inside server components (Card, Badge):

  • Pointer tracking (glow, tilt, custom effects with pointer: true): writes the pointer position to the --ui-effect-x/y (pixels) and --ui-effect-px/py (-1…1 relative to the center) variables. Light and tilt are drawn entirely in CSS; React doesn't re-render. Writes are reduced to one per frame and, since layers only change translate/transform, they are composited without painting. No listeners are bound on touch screens; the element doesn't move while scrolling.
  • Press (ripple, pulse, bounce, custom press): plays from the pressed point on mouse/touch press and from the center on keyboard. Doesn't play on disabled elements or with the "reduce motion" preference.
  • Custom setup (setup): called when the element appears and cleaned up when it is removed.

To add effects to your own component, give the element data-effect and put this inside it; or use EffectParts.

The measured box (getBoundingClientRect) changes slightly while the element rotates, so the position is computed from offsetWidth/Height, which rotation doesn't affect, and the box center. That way tilt doesn't feed back into the pointer position and jitter.

EffectLayer({ effects }: EffectLayerProps): JSX.Element

EffectParts

componentServer component

Decoration layers placed inside an element with effects: the rotating beam (beam), the shine (shine) and, where needed, the pointer/press layer (EffectLayer). The element itself must carry the data-effect attribute; see getEffects.

EffectParts({ effects, appearance }: EffectPartsProps): JSX.Element | null

getEffects

function

Adds the library's effects to your own element or component. Also works in server components. The element must be positioned (relative); effect layers take the element's corner radius.

getEffects(effect: EffectInput, { component, fallback, appearance }?: GetEffectsOptions): EffectsResult

Examples

const fx = getEffects(["tilt", "glow"]);
<div {...fx.props} className={cn("relative rounded-xl border p-6", fx.props.className)}>
  {fx.parts}
  Content
</div>

Merges two appearance settings; next wins.

mergeAppearance(base: AppearanceConfig, next: AppearanceConfig | undefined): AppearanceConfig

Defines the app's appearance settings and registers them app-wide: component defaults, custom effects and custom variants. Returns the same object; pass it to the UIProvider appearance prop and register it with the Register interface so the names are type-checked.

Server components (Card, Badge…) can't read context; they take the settings from this registry. In Next.js App Router, import the settings file in the root layout (server-side registration) and pass it to the provider (client side). Custom effects containing functions (press, setup) can't be passed from server to client; if you have such effects, also import the file in your client provider file.

defineAppearance(appearance: A): A

Examples

// app/appearance.ts
import { defineAppearance } from "@yasinsensoy/web";

export const appearance = defineAppearance({
  defaults: {
    button: { effect: "ripple", shape: "pill" },
    card: { variant: "elevated", effect: ["lift", "glow"] },
  },
  effects: {
    neon: { className: "hover:shadow-[0_0_24px] hover:shadow-primary/50" },
  },
  variants: {
    button: { neon: "bg-black text-lime-300 border border-lime-400" },
    card: { brand: { extends: "gradient", className: "border-violet-400/40" } },
  },
});

// app/layout.tsx (server)
import { appearance } from "./appearance";
<UIProvider appearance={appearance}>…</UIProvider>

// ui.d.ts — so names are type-checked:
declare module "@yasinsensoy/web" {
  interface Register { appearance: typeof import("./app/appearance").appearance }
}

<Button variant="neon" effect={["ripple", "neon"]}>Start</Button>

Returns the app-wide appearance settings. For server components and code outside components; in client components use useAppearance, which also sees nested providers.

getAppearance(): AppearanceConfig

Props of the EffectLayer component.

NameTypeDescription
effectsstringEffects to run, space-separated (e.g. "tilt glow ripple").

Props of the EffectParts component.

NameTypeDescription
effectsreadonly string[]Effects to run.
appearance?AppearanceConfigSettings from which custom effects are read. Defaults to the app-wide registry (getAppearance).

getEffects options.

NameTypeDescription
component?EffectComponentComponent key whose default is read from UIProvider appearance.defaults (e.g. "card").
fallback?EffectInputEffect to use when there is neither a prop nor a provider default.
appearance?AppearanceConfigSettings. In a client component pass useAppearance(); otherwise the app-wide registry is used.

EffectsResult

interface

Result of getEffects.

NameTypeDescription
effectsreadonly string[]Effects to run (none and empties filtered out).
props{ "data-effect"?: string; className?: string; }Attributes to spread on the element: data-effect and custom effect classes.
partsReactNodeDecoration layers to place inside the element.

Effect prop; a name or an array of names. @internal

type EffectInput = string | readonly string[] | undefined | null | false

The library's built-in effects. Each effect works on every component with an effect prop (Button, Card, Badge, Alert, all form fields, Tabs, Accordion, Menu, Sidebar, Dialog, Popover, Toaster, Image, Gallery…; full list in EffectComponent) and can be combined as an array (["tilt", "glow", "ripple"]).

Played on press:

  • scale: sinks in slightly; CSS only.
  • ripple: a wave spreading from the pressed point.
  • pulse: a ring that expands outward from the element and fades.
  • bounce: springs in and bounces back.

Active on hover, keyboard focus or while active:

  • lift: the element rises slightly and its shadow grows.
  • border: the border turns the accent color.
  • beam: a light beam rotating around the border.
  • glow: a light following the pointer; the background and border glow where the pointer is.
  • tilt: the element tilts in 3D toward the pointer's corner.
  • shine: a diagonal shine passes over the element (on press on touch screens).
  • none: no effect; turns off the UIProvider appearance default.

Colors come from the element's accent color (--ui-effect-accent, the text color by default; tone on Card). With the "reduce motion" preference moving effects don't play; glow and tilt stay still on touch screens so they don't interfere with scrolling.

type BuiltinEffect = "scale" | "ripple" | "pulse" | "bounce" | "lift" | "border" | "beam" | "glow" | "tilt" | "shine" | "none"

Effect name: one of the built-in effects (BuiltinEffect) or a custom effect added with defineAppearance and registered with Register.

type EffectName = BuiltinEffect | CustomEffectName

Type of the effect prop: a single effect or effects that run together. When omitted, the component's UIProvider appearance.defaults setting is used, otherwise the component's own default. "none" turns them all off.

type EffectProp = EffectName | readonly EffectName[]

Examples

<Button effect="ripple">Save</Button>
<Card effect={["tilt", "glow"]}>…</Card>
<Badge effect="shine">New</Badge>

Names of a custom variant in the registry. C is the component key ("button", "card"…). never when Register isn't extended.

type CustomVariant = [RegisteredAppearance] extends [never] ? never : RegisteredAppearance extends { variants: infer V; } ? C extends keyof V ? Extract<keyof V[C], string> : never : never

EffectPress

interface

Info given to a press effect.

NameTypeDescription
xnumberHorizontal position of the pressed point relative to the element (pixels). Center on keyboard.
ynumberVertical position of the pressed point relative to the element (pixels). Center on keyboard.
eventEventEvent that started the press: pointerdown or a keyboard click.

Custom effect definition. One or more of three ways can be used together:

  • CSS only: className is added to the element (Tailwind classes or your own CSS). Also works in server components. The element also carries the effect name in its data-effect attribute; you can select it in CSS with [data-effect~="neon"].
  • JavaScript on press: press is called on every press (mouse, touch or keyboard). Use element.animate() for animation.
  • Free setup: setup is called once when the element appears; bind your own listeners and return the cleanup function.

Definitions containing functions can't be passed from server to client; define them in a client file (see defineAppearance).

NameTypeDescription
className?stringClasses added to the element.
pointer?booleanWrites the pointer position to the --ui-effect-x/y (pixels) and --ui-effect-px/py (-1…1) variables; while the pointer is over it the element carries data-pointer. Works only on mouse devices.
press?(element: HTMLElement, press: EffectPress) => voidCalled on every press. Not called with the "reduce motion" preference.
setup?(element: HTMLElement) => undefined | (() => void)Called when the element appears; may return a cleanup function.

Examples

const effects = {
  neon: { className: "hover:shadow-[0_0_24px] hover:shadow-primary/60" },
  confetti: {
    press: (el, { x, y }) => burstConfetti(el, x, y),
  },
} satisfies Record<string, EffectDefinition>;

Custom variant definition. A string is classes directly; an object can derive from a built-in variant and take separate classes per color (tone).

When given under a built-in variant's name (e.g. button: { solid: "…" }), the classes are added on top of that variant.

type VariantEntry = string | { /** Built-in variant to base on (e.g. `"outline"`). When omitted only the base layout classes remain. */ extends?: string; /** Classes added for every color. */ className?: string; /** Classes added per color (`tone`). */ tones?: Partial<Record<Tone, string>>; }

Examples

variants: {
  button: {
    neon: "bg-black text-lime-300 border border-lime-400 hover:bg-lime-400/10",
    glassy: { extends: "outline", className: "backdrop-blur bg-background/60" },
    brand: { extends: "solid", tones: { primary: "bg-violet-600 hover:bg-violet-700" } },
  },
  card: { neon: "border-lime-400 shadow-[0_0_24px] shadow-lime-400/30" },
}

Keys of components that take custom variants.

  • button: Button, IconButton, ButtonGroup and everything that uses buttons (Dialog, Form buttons…).
  • field: all form fields (Input, Textarea, Select, NumberInput, DatePicker…) and their *Field wrappers.
  • The others are the component of the same name (tableOfContents → TableOfContents, otpInput → OtpInput).
type VariantComponent = "button" | "card" | "badge" | "alert" | "stat" | "kbd" | "tabs" | "emptyState" | "field" | "accordion" | "stepper" | "otpInput" | "fab" | "timeline" | "tableOfContents" | "radio" | "countdown"

Default appearance settings of a component.

NameTypeDescription
variant?stringDefault variant name: built-in or added with variants.
effect?EffectName | (string & {}) | readonly (EffectName | (string & {}))[]Default effect or effects.
tone?ToneDefault color (if the component supports it).
size?SizeDefault size (if the component supports it).
shape?stringDefault corner shape (buttons only: rounded, pill, sharp).

Keys of components that take effects; AppearanceConfig.defaults is given with these keys.

type EffectComponent = VariantComponent | "avatar" | "checkbox" | "switch" | "collapsible" | "sidebar" | "navigationMenu" | "breadcrumb" | "menu" | "treeView" | "rating" | "colorSwatch" | "kanban" | "notificationCenter" | "fileUpload" | "image" | "gallery" | "dialog" | "sheet" | "popover" | "hoverCard" | "toast"

The app's appearance settings: component defaults, custom effects and custom variants. Defined with defineAppearance and passed to the UIProvider appearance prop.

NameTypeDescription
defaults?Partial<Record<EffectComponent, ComponentDefaults>>Default variant, effect, tone, size, shape per component. A prop given to the component always wins.
effects?Readonly<Record<string, EffectDefinition>>Custom effects: name → definition. See EffectDefinition.
variants?Partial<Record<VariantComponent, Readonly<Record<string, VariantEntry>>>>Custom variants per component: name → classes. See VariantEntry.