--- url: /reference/items/ViewTransition/examples.md --- # Examples ## Togglable Click the Enter and Leave buttons to toggle the element. The animation runs as a native view transition, defined entirely in the `::view-transition-old()` / `::view-transition-new()` CSS. In a browser without the View Transitions API, the element toggles instantly. :::code-group ```js import { Base, createApp } from '@studiometa/js-toolkit'; import { ViewTransition } from '@studiometa/ui'; class Togglable extends ViewTransition { static config = { name: 'Togglable', }; } class App extends Base { static config = { name: 'App', refs: ['enterBtn', 'leaveBtn'], components: { Togglable, }, }; onEnterBtnClick() { this.$children.Togglable[0].enter(); } onLeaveBtnClick() { this.$children.Togglable[0].leave(); } } export default createApp(App); ``` ::: ## Triggering with the `Action` component `ViewTransition` exposes the same `enter()`, `leave()` and `toggle()` methods as [`Transition`](/reference/items/Transition/), so the [`Action` component](/reference/items/Action/) can drive it declaratively. No custom `onClick` handlers are required. Here the buttons target the `Togglable` instance and call its methods directly through `data-option-effect`. :::code-group ```js import { registerComponent } from '@studiometa/js-toolkit'; import { Action, ViewTransition } from '@studiometa/ui'; class Togglable extends ViewTransition { static config = { name: 'Togglable', }; } registerComponent(Action); registerComponent(Togglable); ``` :::