You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A small SolidJS utility to animate the presence of an element. Inspired by & directly forked from [`use-presence`](https://www.npmjs.com/package/use-presence).
13
-
14
-
### The problem
15
-
16
-
There are two problems that you have to solve when animating the presence of an element:
17
-
18
-
1. During enter animations, you have to render an initial state where the element is hidden and only after the latest state has propagated to the DOM, you can can animate the final state that the element should animate towards.
19
-
2. Exit animations are a bit tricky in SolidJS, since this typically means that a component unmounts. However when the component has already unmounted, you can't animate it anymore. A workaround is often to keep the element mounted, but that keeps unnecessary elements around and can hurt accessibility, as hidden interactive elements might still be focusable.
20
-
21
-
### This solution
22
-
23
-
This utility provides a lightweight solution where the animating element is only mounted the minimum of time, while making sure the animation is fully visible to the user. The rendering is left to the user to support all kinds of styling solutions.
The first argument of `createPresence` is a signal accessor of arbitrary type. This allows you to use it with any kind of data, not just booleans. This is useful if you want to animate between different items. If you utilize the returned `mountedItem` property, you can get the data which should be currently mounted regardless of the animation state
When an elements visibilty tied to a signal, the elements gets mounted and unmounted abruptly, not permitting to apply any css transitions.
129
123
130
124
```ts
131
-
function createPresence<TItem>(
132
-
item:Accessor<TItem|undefined>,
133
-
options:Options,
134
-
):PresenceResult<TItem>;
135
-
136
-
typeOptions= {
137
-
/** Duration in milliseconds used both for enter and exit transitions. */
138
-
transitionDuration:MaybeAccessor<number>;
139
-
/** Duration in milliseconds used for enter transitions (overrides `transitionDuration` if provided). */
140
-
enterDuration:MaybeAccessor<number>;
141
-
/** Duration in milliseconds used for exit transitions (overrides `transitionDuration` if provided). */
142
-
exitDuration:MaybeAccessor<number>;
143
-
/** Opt-in to animating the entering of an element if `isVisible` is `true` during the initial mount. */
144
-
initialEnter?:boolean;
145
-
};
125
+
<Showwhen={show()}>
126
+
<div>HelloWorld!</div>
127
+
</Show>
128
+
```
146
129
147
-
typePresenceResult<TItem> = {
148
-
/** Should the component be returned from render? */
149
-
isMounted:Accessor<boolean>;
150
-
/** The item that is currently mounted. */
151
-
mountedItem:Accessor<TItem|undefined>;
152
-
/** Should the component have its visible styles applied? */
153
-
isVisible:Accessor<boolean>;
154
-
/** Is the component either entering or exiting currently? */
155
-
isAnimating:Accessor<boolean>;
156
-
/** Is the component entering currently? */
157
-
isEntering:Accessor<boolean>;
158
-
/** Is the component exiting currently? */
159
-
isExiting:Accessor<boolean>;
160
-
};
130
+
`createPresence` creates a derived signal, through which we transition from `false` to `true` in two steps, `entering` and `entered`, and again from `true` to `false` in two steps `exiting` and `exited`.
131
+
132
+
```ts
133
+
const [show, setShow] =createSignal(true);
134
+
135
+
const state =createPresence(show, {
136
+
duration: 500,
137
+
initialTransition: true,
138
+
});
139
+
```
140
+
141
+
This allows us to apply enter and exit transitions on an element but first we need to hand over the control of the element's visibilty to the `state`:
142
+
143
+
```ts
144
+
const isMounted = () =>state() !=='exited';
145
+
<Showwhen={isMounted()}>
146
+
<div>HelloWorld!</div>
147
+
</Show>
161
148
```
162
149
163
-
## Demo
150
+
`createPresence` returns a derived signal with five states, three of them being resting states, `initial`, `entered`, `exited` and two of them being transitioning states, `entering` and `exiting`.
164
151
165
-
Demo can be seen [here](https://stackblitz.com/edit/presence-demo).
Element is mounted at `initial` state. This state is short-lived since `entering` is flushed immediately via event loop however it can be used to set css properties.
157
+
158
+
When `show` is set to `true`, component gets mounted at `initial` state which changes to `entering` almost immediately and remain there for the duration of `500ms`, then moves to `entered` state and remain there indefinitely.
159
+
160
+
This allow us to transition from a property loaded with `entering` state to the one loaded with `entered` state:
168
161
169
-
See [CHANGELOG.md](./CHANGELOG.md)
162
+
```ts
163
+
<div
164
+
style={{
165
+
transition: 'all .5s linear',
166
+
167
+
...(state() ==='entering'&& {
168
+
color: 'green',
169
+
}),
170
+
171
+
...(state() ==='entered'&& {
172
+
color: 'red',
173
+
}),
174
+
}}
175
+
>
176
+
HelloWorld!
177
+
</div>
178
+
```
170
179
171
-
## Related
180
+
Now, when `show` is set to `false`, the `div` elemement does not disappear immediately but waits for the duration of `exiting` state. In other words, we extended element's presence in the DOM for `500ms` which allows us to apply exit transitions:
0 commit comments