Introducing PrimeReact v11-alpha 🎉Discover Now

StyleClass

StyleClass manages CSS classes on a target element with enter/leave animations triggered by user interaction.

preview

Usage#

import { StyleClass } from 'primereact/styleclass';
<StyleClass
    as="button"
    selector="@next"
    enterFromClassName="hidden"
    enterActiveClassName="animate-scalein"
    leaveToClassName="hidden"
    leaveActiveClassName="animate-fadeout"
    hideOnOutsideClick
>
    Toggle
</StyleClass>
<div className="hidden">Content</div>

Examples#

Basic#

Toggle visibility of a target element with enter and leave animations.

basic-demo

Animation#

Custom CSS animations and built-in slide animations for enter and leave transitions.

Custom
animation-demo

Toggle Class#

Use toggleClassName for simple class toggling without enter/leave animations.

This panel is toggled instantly using toggleClassName without any enter/leave animations.

toggle-demo

Selector#

Different selector types to target elements: @next, @prev, and CSS selectors.

@next
@prev
CSS Selector
selector-demo

Selector#

The selector prop determines the target element. Built-in values:

  • @next — Next sibling element
  • @prev — Previous sibling element
  • @parent — Parent element
  • @grandparent — Grandparent element
  • Any CSS selector string (e.g. #my-panel)

Animation Classes#

StyleClass applies CSS classes in sequence to create enter and leave transitions.

Enter sequence:

  1. Remove enterFromClassName (e.g. hidden)
  2. Add enterActiveClassName (e.g. animate-scalein)
  3. On animation end: remove enterActiveClassName, add enterToClassName

Leave sequence:

  1. Remove leaveFromClassName
  2. Add leaveActiveClassName (e.g. animate-fadeout)
  3. On animation end: remove leaveActiveClassName, add leaveToClassName (e.g. hidden)

Outside Click#

Set hideOnOutsideClick to automatically hide the target when clicking outside the trigger and target elements.

API#

Hooks#

See useStyleClass for the headless hook API.

Accessibility#

StyleClass does not add ARIA attributes — it is a utility for CSS class manipulation. Add appropriate aria-expanded, aria-controls, and other attributes to the trigger and target elements as needed.