AnimXYZ Documentation

repository·master·Indexed 25 days ago

https://github.com/ingram-projects/animxyz

A composable animation library that uses CSS variables to create and customize animations without manual keyframes. It supports Vue, React, SCSS, and standard CSS, providing a core package (@animxyz/core) and dedicated framework components. The library features a modernization plan for v1.0, introducing the data-xyz attribute, CSS @property and @layer structures, and improved stagger indexing.

Tokens
14.3K
Snippets
36
Records
109
Agent score
81%

What's inside AnimXYZ

  1. Use Transform utilities and variables

    master

    Transform utilities and variables allow you to define the starting (.xyz-in) or ending (.xyz-out) CSS transform of an animating element. For example, using xyz="up" applies a translateY() to an element, moving it from above its normal position when animating in and to that same position when animating out.

    You can override any provided transform variables with custom values in your CSS or via inline styling for granular control. This is particularly useful if an element already has a transform applied (e.g., transform: translateX(-50%)) that you want to maintain during an AnimXYZ animation; in such cases, pass the existing value to the relevant CSS variable (e.g., --xyz-translate-x: -50%).

  2. Compose animations using utility combinations

    master

    AnimXYZ allows you to mix and match multiple animation utilities within a single xyz attribute to create complex animations without writing additional CSS. You can combine different types of transformations (e.g., movement, rotation, scaling) and timing controls.

    Example combinations:

    • xyz="fade up" (Fade and move up)
    • xyz="fade flip-up flip-left" (Fade with flip effects)
    • xyz="fade down-5 rotate-right-50% stagger" (Fade, move down, rotate, and stagger children)
    • xyz="fade up-100% flip-down flip-right-50% rotate-left-100% origin-bottom duration-10 stagger" (Complex multi-property animation)
  3. Control animation direction with Direction utilities and variables

    master

    Direction utilities and variables control the CSS animation-direction property.

    By default, AnimXYZ sets the direction to normal for in, appear, and enter variants, and reverse for the out variant. You can override these defaults using direction utilities, which is particularly useful for infinite animations (e.g., making an element grow and shrink indefinitely).

    To use a specific direction, add the utility to your xyz attribute, such as xyz="big iterate-infinite direction-alternate".

  4. Understand the AnimXYZ Modernization Plan and Release Tracks

    master

    The AnimXYZ project is undergoing a modernization process divided into two distinct release tracks to manage stability and breaking changes:

    • Track A (Safe Fixes): Focuses on 0.x patch and minor releases. This track is intended for bug fixes and non-breaking improvements.
    • Track B (Breaking Modernization): Focuses on the v1.0 major release. This track introduces breaking changes, such as moving from the xyz attribute to the data-xyz attribute and adopting CSS @property and @layer structures.

    Key architectural decisions for the upcoming v1.0 include:

    • Attribute Change: A clean break from xyz to data-xyz.
    • CSS Features: Full adoption of @property and @layer structures.
    • Stagger Indexing: Progressive enhancement using sibling-index() where supported, with an nth-child ladder as a fallback.
    • Testing: Moving to a model of Compile + snapshot tests + GitHub Actions (without browser visual tests).
  5. Control animation repetition with Iterate utilities and variables

    master

    The iterate utilities and variables control the CSS animation-iteration-count property. This allows you to specify how many times an animation should repeat, either a fixed number of times or indefinitely.

    Available Variables

    You can use the following CSS variables to control iteration counts for different animation phases:

    ScopeVariable
    Overall--xyz-iterate
    In (entering)--xyz-in-iterate
    Out (leaving)--xyz-out-iterate
    Appear--xyz-appear-iterate

    Default Value

    • --xyz-iterate-default: 1

    Usage Example

    You can apply these via the xyz attribute or by setting the CSS variables directly in your styles.

  6. Customize transform-origin with --xyz-origin

    master

    If the standard origin utilities do not provide the precision you need, you can override the origin using the --xyz-origin CSS variable. This allows for granular control using specific pixel or percentage values.

    Example: --xyz-origin: 50px 50px sets the origin to a point 50px down and 50px to the right from the top-left corner.

  7. Understand CSS variable inheritance in AnimXYZ

    master

    AnimXYZ CSS variables are inherited by child elements. An element with an active class will use its parent's CSS variables for its animations unless:

    1. The child specifically overrides the variable.
    2. The child uses an xyz attribute, which overrides all AnimXYZ variables for that specific element.

    Variables are categorized by their application scope:

    • Overall: Applies to all states.
    • In: Applies to the 'in' animation state.
    • Out: Applies to the 'out' animation state.
    • Appear: Applies to the 'appear' animation state.
  8. Use @property for typed animation dials

    master

    AnimXYZ v1.0 utilizes CSS @property to provide type safety and individually transitionable custom properties (dials). This allows for more robust animations and better browser integration.

    Key Constraints for Custom Properties

    When working with or extending the library's custom properties, note the following:

    • Inheritance: All registered properties must have inherits: true to support nested composition and stagger relays.
    • Initial Values: The initial-value for a registered property must be computationally independent. It cannot reference other AnimXYZ variables (e.g., it cannot be var(--xyz-*-default)).
    • Syntax Types: The library uses specific CSS types for different dials:
      • <time>: For duration, delay, and stagger.
      • <number>: For opacity and scale.
      • <length-percentage>: For translate x/y.
      • <length>: For translate-z and perspective.
      • <angle>: For rotate and skew.

    Browser Support

    The browser floor for these features is Baseline July 2024.

  9. Understand the @layer architecture and override contract

    master

    AnimXYZ v1.0 uses CSS Cascade Layers (@layer) to manage specificity and ensure predictable animation behavior without relying on !important.

    Layer Hierarchy

    The layers are declared in the following order (from lowest to highest priority):

    1. xyz.defaults: :root defaults and @property registrations.
    2. xyz.index: The nth-child ladder.
    3. xyz.utilities: The [data-xyz~='...'] selectors.
    4. xyz.triggers.in / xyz.triggers.out / xyz.triggers.appear: Mode-specific trigger classes.
    5. xyz.overrides: Handles paused, none, absolute, and prefers-reduced-motion.

    The Override Contract

    • User Styles: Unlayered user CSS will beat AnimXYZ styles by default.
    • Deliberate Overriding: To ensure your styles are overridden by AnimXYZ, place your styles in a layer declared before the xyz layers.
    • Sass Integration: If using Sass, you can control the layer name via $xyz-layer. Setting this to an empty string will emit unlayered styles (the escape hatch).
  10. Apply utilities to specific animation directions (In, Out, Appear)

    master

    You can target specific phases of an element's lifecycle by prepending the direction variant name to a utility. The available variants are:

    • In: For entering animations.
    • Out: For exiting animations.
    • Appear: For the initial appearance of the element.

    Example: xyz="fade up out-down appear-big duration-5 in-duration-10" applies a fade and up movement generally, but uses down when exiting, big when appearing, a 5 duration for appearing, and a 10 duration for entering.

  11. How Staggering works in AnimXYZ

    master

    Staggering increases the animation-delay for each element in a list so that their animations trigger sequentially (like dominoes).

    By default, AnimXYZ applies this staggered delay to the first 20 elements (or the last 20 if using stagger-rev) based on their nth-child index.

    Customizing Staggering:

    • More than 20 elements: If you need to stagger more than 20 elements or want to change the order, you can pass a custom index to each element using the --xyz-index or --xyz-index-rev variables.
    • Framework Components: If you use the provided Vue or React XyzTransitionGroup components, AnimXYZ automatically manages the --xyz-index and --xyz-index-rev variables for all elements within the group.
    • Layout Note: In Vue and React, because elements are added/removed during enter/exit animations, staggered elements that are not position: absolute may cause the group layout to shift as each element enters or exits in sequence.
  12. Apply perspective to 3D animations

    master

    To achieve a 3D effect in CSS animations, elements often require a perspective property. While you can apply this to a parent element manually (e.g., perspective: 500px), AnimXYZ provides utility classes to apply perspective specifically to an element only while it is animating.

    Note: Smaller perspective values result in a more pronounced 3D effect.