Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mikromike's avatar

Filament3, toggle component, form jump after state updated.

Hello

I have simple Filament toggle component in form.

   Toggle::make('internal')
             ->label('Sisäinen domaini')
          //->reactive()
           ->live()
          ->default(true)
          ->onColor('success')
          ->offColor('danger')
          ->afterStateUpdated(function (Forms\Components\Toggle $component, $set, $state) {
            $set('label', $state ? 'text when set ON' : 'text when set OFF');
          }),

here is second part:

        Group::make()
            ->schema([
                    TextInput::make('label')
                      ->label(fn ($get) => $get('label'))
                     ->reactive()
                    ->disabled()
                   ->default('Some default text')
              ])
                ->extraAttributes(['style' => 'height: 30px;']),
  1. Problem is each time when toggle state has changed, whole form jumps little bit. I had chat with AI Larry couple hours, no help.

Done so far:

  • added app.css :

       [x-cloak] { display: none !important; }
    

with and without "!important ".

  • changed ->extraAttributes(['style' => 'height: 30px;']), height and weight, no help.

Any ideas ?, Thanks Mika.

0 likes
4 replies
LaryAI's avatar
Level 58

The issue you're experiencing with the form "jumping" when the toggle state changes is likely due to the re-rendering of the form components. This can happen when the state is updated and the form is re-evaluated, causing a slight visual shift.

Here are a few steps you can take to mitigate this issue:

  1. Minimize Re-renders: Ensure that only the necessary components are re-rendered when the state changes. This can be done by isolating the state changes to specific components.

  2. CSS Adjustments: Sometimes, adding CSS transitions or ensuring consistent heights and widths can help smooth out the visual changes.

  3. Debounce State Updates: Debouncing the state updates can help reduce the frequency of re-renders.

Here is a revised version of your code with some adjustments:

Toggle::make('internal')
    ->label('Sisäinen domaini')
    ->live()
    ->default(true)
    ->onColor('success')
    ->offColor('danger')
    ->afterStateUpdated(function (Forms\Components\Toggle $component, $set, $state) {
        $set('label', $state ? 'text when set ON' : 'text when set OFF');
    }),

Group::make()
    ->schema([
        TextInput::make('label')
            ->label(fn ($get) => $get('label'))
            ->reactive()
            ->disabled()
            ->default('Some default text')
    ])
    ->extraAttributes(['style' => 'height: 30px;']),

Additionally, you can add some CSS to ensure smoother transitions:

/* Add this to your app.css */
.toggle-container {
    transition: all 0.3s ease-in-out;
}

And then apply this class to your form components:

Toggle::make('internal')
    ->label('Sisäinen domaini')
    ->live()
    ->default(true)
    ->onColor('success')
    ->offColor('danger')
    ->afterStateUpdated(function (Forms\Components\Toggle $component, $set, $state) {
        $set('label', $state ? 'text when set ON' : 'text when set OFF');
    })
    ->extraAttributes(['class' => 'toggle-container']),

Group::make()
    ->schema([
        TextInput::make('label')
            ->label(fn ($get) => $get('label'))
            ->reactive()
            ->disabled()
            ->default('Some default text')
    ])
    ->extraAttributes(['class' => 'toggle-container', 'style' => 'height: 30px;']),

By adding the toggle-container class and the CSS transition, you should see a smoother update without the form "jumping" as much. If the issue persists, you might need to look into more specific CSS adjustments or further isolate the state changes to minimize re-renders.

tisuchi's avatar
tisuchi
Best Answer
Level 70

@mikromike Instead of using ->live() for the Toggle component, try using ->reactive() in combination with a more fine-tuned Livewire update method.

for example:

Toggle::make('internal')
    ->label('Sisäinen domaini')
    ->reactive() // Use reactive instead of live for more granular updates
    ->default(true)
    ->onColor('success')
    ->offColor('danger')
    ->afterStateUpdated(function (Forms\Components\Toggle $component, $set, $state) {
        $set('label', $state ? 'text when set ON' : 'text when set OFF');
    }),
mikromike's avatar

@tisuchi , tried both, still same problem, also updated as AI Larry's suggest.

1 like
mikromike's avatar

@tisuchi, found the problem, it was my browser, tested in other pc, did not have any problem.

1 like

Please or to participate in this conversation.