Skip to content

Commit 47109aa

Browse files
committed
readme
1 parent 3659c9d commit 47109aa

1 file changed

Lines changed: 74 additions & 2 deletions

File tree

README.md

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @cardstack/view-transitions
22

3-
[Short description of the addon.]
3+
Ember utilities for triggering native View Transition API animations.
44

55
## Compatibility
66

@@ -15,7 +15,79 @@ ember install @cardstack/view-transitions
1515

1616
## Usage
1717

18-
[Longer description of how to use the addon in apps.]
18+
You will need to learn about the [View Transition API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API) to make use of this library. This library provides some small utilities that make it convenient to use View Transitions in Ember apps.
19+
20+
### Animatable values
21+
22+
```ts
23+
export function animatable<T>(
24+
initialValue: T,
25+
opts?: { equals?: (a: T, b: T) => boolean },
26+
): Animatable<T>
27+
28+
export interface Animatable<T> {
29+
readonly current: T;
30+
set(newValue: T): void;
31+
}
32+
```
33+
34+
Creates a value that triggers a ViewTransition whenever it changes. `current` does not update instantly when you call `set`, instead `current` is allowed to update after the ViewTransition has already snapshotted the "before" state for the View Transition.
35+
36+
```gts
37+
import { animatable } from '@cardstack/view-transitions';
38+
39+
const expanded = animatable(false);
40+
41+
function toggleExpanded() {
42+
expanded.set(!expanded.current);
43+
}
44+
45+
<template>
46+
<button {{on "click" toggleExpanded}}>Toggle</button>
47+
{{#if expanded.current}}
48+
<ExpandedContentHere />
49+
{{else}}
50+
<CollapsedContentHere />
51+
{{/if}}
52+
</template>
53+
```
54+
55+
Animatable values are also necessarily `tracked`, for full interoperability with the rest of Ember. You can consume them like any other tracked state, and re-renders will happen automatically, with the added bonus that the DOM state before and after the re-render will be captured by the browser and can be smoothly animated.
56+
57+
### viewTransitionName
58+
59+
A modifier that sets the `view-transition-name` CSS property. Useful for programmatically assigning unique names based on model IDs, etc. Accepts any number of strings or numbers and concatenates them to form the view-transition-name.
60+
61+
```ts
62+
viewTransitionName: FunctionBasedModifier<{
63+
Args: {
64+
Positional: (string | number)[];
65+
};
66+
Element: HTMLElement;
67+
}>;
68+
```
69+
70+
```gts
71+
import { viewTransitionName } from '@cardstack/view-transitions';
72+
<template>
73+
{{#each @products as |product|}}
74+
<div {{viewTransitionName "product" product.id}}>
75+
{{product.title}}
76+
</div>
77+
{{/each}}
78+
</template>
79+
```
80+
81+
The critical thing to understand about the `view-transition-name` CSS property is that it must be unique on the whole page. It can appear in both the "old" and "new" states of the DOM, but can't appear simultaneously more than once. It controls which elements get captured (think: "screenshotted") during the ViewTransition, and how the old and new element correlate.
82+
83+
## Demo App
84+
85+
This repo contains a demo app. To run it:
86+
87+
```sh
88+
pnpm install
89+
pnpm start
90+
```
1991

2092
## Contributing
2193

0 commit comments

Comments
 (0)