animation Module API
The animation module provides functions for creating CSS animations, keyframes, and transitions for interactive user interfaces.
keyframes()
Creates CSS @keyframes rules and animation class from an animation configuration table.
Nixi.keyframes(animation)
Parameters:
animation- Table with animation properties (name, from, to, duration, timing, delay, count, direction)
Returns: string - CSS @keyframes and animation class rules
local fade = Nixi.keyframes({
name = "fadeIn",
from = { opacity = "0" },
to = { opacity = "1" },
duration = 300,
timing = "ease-out"
})
-- @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
-- .fadeIn { animation: fadeIn 300ms ease-out 0s 1 normal; }
Animation Table Properties
| Property | Type | Default | Description |
|---|---|---|---|
name | string | "unnamed" | Animation name |
from | table | {} | Starting CSS properties |
to | table | {} | Ending CSS properties |
duration | number | 300 | Duration in ms |
timing | string | "ease" | Timing function |
delay | number | 0 | Delay in ms |
count | number|"infinite" | 1 | Iteration count |
direction | string | "normal" | Animation direction |
transition()
Returns pre-built transition CSS for named transitions.
Nixi.transition(name)
Parameters:
name- Name of the pre-built transition
Returns: string - CSS keyframes and animation class
Nixi.transition("fade_in")
-- Returns fade-in keyframes CSS
Nixi.transition("bounce")
-- Returns bounce keyframes CSS
Available Transitions
| Name | Description | Default Duration |
|---|---|---|
fade_in | Fade in from invisible | 200ms |
fade_out | Fade out to invisible | 200ms |
slide_in | Slide in from top | 300ms |
slide_out | Slide out to bottom | 200ms |
bounce | Scale up and back | 150ms |
pulse | Infinite pulsing scale | 100ms |
shake | Horizontal shake | 100ms |
all_transitions()
Returns CSS for all built-in transitions.
Nixi.all_transitions()
Returns: string - CSS for all default transitions
local all = Nixi.all_transitions()
-- Returns CSS for fade_in, fade_out, slide_in, slide_out, bounce, pulse, shake
animate()
Adds an animation class to an HTML element string.
Nixi.animate(element, animation_name)
Parameters:
element- HTML stringanimation_name- Name of the animation class to add
Returns: string - Element with animation class added
local button = Nixi.button({ class = "btn" }, "Click")
local animated = Nixi.animate(button, "nixi-pulse")
-- <button class="nixi-pulse btn">Click</button>
Animation Properties Table
| Property | Values | Description |
|---|---|---|
animation-name | keyframes name | Name of the @keyframes rule |
animation-duration | time (ms, s) | How long the animation takes |
animation-timing-function | ease, linear, ease-in, ease-out, ease-in-out, cubic-bezier() | Speed curve |
animation-delay | time | Delay before animation starts |
animation-iteration-count | number, infinite | How many times to play |
animation-direction | normal, reverse, alternate, alternate-reverse | Direction of play |
animation-fill-mode | none, forwards, backwards, both | Style before/after animation |
animation-play-state | running, paused | Whether animation is running |
Transition Properties Table
| Property | Values | Description |
|---|---|---|
transition-property | property name, all, none | Property to transition |
transition-duration | time | How long transition takes |
transition-timing-function | ease, linear, ease-in, ease-out, ease-in-out, cubic-bezier() | Speed curve |
transition-delay | time | Delay before transition |
Complete Example
package.path = "src/?.lua;src/?/init.lua;" .. package.path
local Nixi = require("nixi.init")
local app = Nixi.new({ port = 3000 })
local styles = [[
<style>
]] .. Nixi.keyframes({
name = "fadeIn",
from = { opacity = "0", transform = "translateY(10px)" },
to = { opacity = "1", transform = "translateY(0)" },
duration = 500,
timing = "ease-out"
}) .. [[
]] .. Nixi.keyframes({
name = "slideIn",
from = { transform = "translateX(-50px)", opacity = "0" },
to = { transform = "translateX(0)", opacity = "1" },
duration = 400,
timing = "ease-out"
}) .. [[
.fade-in {
animation: fadeIn 500ms ease-out;
}
.slide-in {
animation: slideIn 400ms ease-out;
}
</style>
]]
app:get("/", function(ctx)
return Nixi.Layout({
title = "Animated Page",
body = Nixi.div({ class = "fade-in" },
Nixi.h1(nil, "Animated Components"),
Nixi.div({ class = "slide-in" },
Nixi.p(nil, "This element slides in!")
)
),
head = styles
})
end)
return app
Type Definitions
---@class Nixi.Animation
---@field name string
---@field from table
---@field to table
---@field duration number
---@field timing string
---@field delay number
---@field count number | "infinite"
---@field direction string
---@class Nixi
---@field keyframes fun(animation: Nixi.Animation): string
---@field transition fun(name: string): string
---@field all_transitions fun(): string
---@field animate fun(element: string, animation_name: string): string