Nitro

Features

Transitions

Transitions animate elements as they enter and leave the page. Add nitro:transition to an element inside an @if or a @foreach, and Nitro animates it when it appears and when it goes.

On this page

To animate an element, add nitro:transition to an element that an @if or a @foreach renders. The element animates as it enters. When it leaves, Nitro keeps it in the page until its animation ends. Without a name, the transition is a built-in fade and scale that needs no CSS.

Entering, leaving and moving live, in this page View source ↓
  • Ada
  • Grace
  • Alan

Try adding and removing names, then shuffle the list. The rows that stay slide to their new places, thanks to .move. When you switch tabs, the old tab leaves before the new one enters, thanks to .out-in. When you open the panel, it uses the built-in fade and scale, growing from its top edge.

The component

Browser + server
<?php

namespace App\Nitro\Components;

use Nitro\Component;

class TransitionDemo extends Component
{
    public array $people = ['Ada', 'Grace', 'Alan'];

    public array $waiting = ['Katherine', 'Margaret', 'Edsger', 'Barbara', 'Donald'];

    public bool $open = false;

    public string $tab = 'list';

    public function add(): void
    {
        if ($this->waiting !== []) {
            $this->people[] = array_shift($this->waiting);
        }
    }

    public function remove(string $name): void
    {
        $this->people = array_values(array_filter($this->people, fn ($person) => $person !== $name));
        $this->waiting[] = $name;
    }

    public function shuffle(): void
    {
        shuffle($this->people);
    }
}

Using the built-in transition

The built-in transition fades and scales the element. Modifiers let you change how it animates:

Blade
@if ($open)
    <div class="panel" nitro:transition.duration.200ms.origin.top>...</div>
@endif
Modifier Effect
.opacity , .scale Fade only, or scale only (both by default)
.duration.300ms The duration, 150ms by default
.origin.top The transform origin: top , bottom , left , right or center
.in , .out Animate only when entering, or only when leaving
.appear Animate on the component's first render in the browser too
.out-in , .in-out For an element that replaces another ( @if and @else , on both): the old one leaves before the new one enters, or after it
.move On the rows of a @foreach : the rows that stay slide to their new places when the list changes

Writing your own transition

When you give a transition a name, your CSS classes decide what moves and for how long. This works the same way as Vue's transitions:

Blade
<p nitro:transition="fade">...</p>
CSS
.fade-enter-active, .fade-leave-active { transition: opacity .3s ease; }
.fade-enter-from, .fade-leave-to { opacity: 0; }
.fade-move { transition: transform .3s; }

Animating lists and tabs

Give each row of a list a nitro:key, so Nitro can follow it as the list changes. When two elements replace each other, put .out-in on both of them.

Blade
@foreach ($people as $person)
    <li nitro:key="{{ $person }}" nitro:transition.move.duration.250ms>{{ $person }}</li>
@endforeach

@if ($tab === 'list')
    <section nitro:transition.out-in>...</section>
@else
    <section nitro:transition.out-in>...</section>
@endif

When nothing animates

Nitro doesn't animate the server's HTML, elements in a hidden tab, or anything for visitors who have asked their system for reduced motion.

Next steps