|
| 1 | +# transition function (for custom components) |
| 2 | + |
| 3 | +This is a function that is useful to write custom components or extend the components with animations. |
| 4 | + |
| 5 | +## Usage |
| 6 | +```js |
| 7 | +import { transition } from "tailwindcss-stimulus-components" |
| 8 | +import { Controller } from "@hotwired/stimulus" |
| 9 | + |
| 10 | +class CustomController extends Controller { |
| 11 | + static targets = ['content'] |
| 12 | + |
| 13 | + showContent() { |
| 14 | + transition(this.contentTarget, true, this.defaultOptions()) |
| 15 | + } |
| 16 | + |
| 17 | + hideContent() { |
| 18 | + transition(this.contentTarget, false, this.defaultOptions()) |
| 19 | + } |
| 20 | + |
| 21 | + defaultOptions() { |
| 22 | + return { |
| 23 | + enter: this.hasEnterClass ? this.enterClass : 'transition ease-out duration-100', |
| 24 | + enterFrom: this.hasEnterFromClass ? this.enterFromClass : 'transform opacity-0 scale-95', |
| 25 | + enterTo: this.hasEnterToClass ? this.enterToClass : 'transform opacity-100 scale-100', |
| 26 | + leave: this.hasLeaveClass ? this.leaveClass : 'transition ease-in duration-75', |
| 27 | + leaveFrom: this.hasLeaveFromClass ? this.leaveFromClass : 'transform opacity-100 scale-100', |
| 28 | + leaveTo: this.hasLeaveToClass ? this.leaveToClass : 'transform opacity-0 scale-95', |
| 29 | + toggleClass: this.hasToggleClass ? this.toggleClass : 'hidden', |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +application.register('custom', CustomController) |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +The default animation attributes can be put in a controller like this so they can optionnaly be overwritten |
| 39 | +on an element by element basis |
| 40 | + |
| 41 | +```html |
| 42 | +<div class="inline-block relative cursor-pointer" data-controller="custom" > |
| 43 | + <span class="underline" data-action="custom#showContent">Show</span> |
| 44 | + <span class="underline" data-action="custom#hideContent">Hide</span> |
| 45 | + <div class="hidden absolute left-0 bottom-7 w-max bg-white border border-gray-200 shadow rounded p-2" |
| 46 | + data-custom-target="content" |
| 47 | + |
| 48 | + data-custom-enter="transition ease-out duration-100" |
| 49 | + data-custom-enter-from="transform opacity-0 scale-95" |
| 50 | + data-custom-enter-to="transform opacity-100 scale-100" |
| 51 | + data-custom-leave="transition ease-in duration-75" |
| 52 | + data-custom-leave-from="transform opacity-100 scale-100" |
| 53 | + data-custom-leave-to="transform opacity-0 scale-95-0" |
| 54 | + data-toggle="hidden" |
| 55 | + > |
| 56 | + Content |
| 57 | + </div> |
| 58 | +</div> |
| 59 | +``` |
0 commit comments