Skip to content

Commit b92a93c

Browse files
committed
Partial docs
1 parent b5b1107 commit b92a93c

File tree

4 files changed

+79
-19
lines changed

4 files changed

+79
-19
lines changed

README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Velvette
22
Making it easier to author CSS View Transitions
33

4+
For the docs go [here](https://noamr.github.io/velvette/)
5+
46
## Tl;dr
57
A small JS library that implements common patterns on top of CSS view-transitions:
68
* Useful temporary classes while the transition is active
@@ -262,8 +264,3 @@ To apply a `Velvette` configuration for cross-document view transitions:
262264
new Velvette(config).crossDocument();
263265
```
264266

265-
266-
267-
268-
## The API
269-

src/index.js

+75-12
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,40 @@ import { start } from "./transition.js";
77
import { init } from "./nav.js";
88

99
/**
10+
* Starts a {@link https://developer.mozilla.org/en-US/docs/Web/API/ViewTransition ViewTransition} with
11+
* additional params. Based on these params, Velvette applies temporary classes to the {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html document element},
12+
* generates {@link https://developer.mozilla.org/en-US/docs/Web/CSS/view-transition-name `view-transition-name`} properties
13+
* and adds temporary styles for {@link https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API#the_view_transition_process the `::view-transition-*` pseudo-elements}.
1014
*
1115
* @param {object} options
12-
* @param {() => void | Promise<void>} options.update
13-
* @param {string[]?} options.classes
14-
* @param {{[key: string]: Partial<CSSStyleDeclaration>}?} options.styles
15-
* @param {{[selector: string]: string}?} options.captures
16+
* @param {() => void | Promise<void>} options.update The operation that updates the DOM to the new state.
17+
* @param {string[]?} options.classes A list of temporary classes to be applied to the document element with a `vt-` prefix during the course of the transition.
18+
* @param {{[key: string]: Partial<CSSStyleDeclaration>}?} options.styles A map of styles, see {@link Config#styles}
19+
* @param {{[selector: string]: string}?} options.captures A map of captures, see {@link Config#captures}
1620
*
1721
* @returns {ViewTransition | null}
22+
*
23+
* @example
24+
* import {startViewTransition} from "velvette";
25+
* // This will return a ViewTransition, or a null if this browser doesn't support CSS View Transitions.
26+
* startViewTransition({
27+
* async update() {
28+
* // Make actual DOM update
29+
* },
30+
*
31+
* // This would apply `vt-slide-in` on the root element for the duration of the transition
32+
* classes: ["slide-in"],
33+
*
34+
* // This would capture any `li.item .box`, and would give it the `view-transition-name`
35+
* // corresponding to "box-" plus its ancestor `li.item`'s id.
36+
* // In addition, it would apply any style with a pseudo that matches '.any-box' to all the
37+
* // captured elements.
38+
* captures: { "li.item[:id] .box": "box-$(id).any-box" }
39+
*
40+
* // Would apply a blur during the transition, to the transition group generated
41+
* // for any capture that matches the ".any-box".
42+
* styles: {"::view-transition-group(.any-box)": {filter: "blur(1px)"}},
43+
* });
1844
*/
1945
export function startViewTransition(options) {
2046
if (!("startViewTransition" in document)) {
@@ -46,20 +72,43 @@ export class VelvetteEvent extends Event {
4672
}
4773
}
4874

75+
/**
76+
* Constructs an object that takes a {@link Config} param and can then perform view transitions
77+
* based on navigations. There are 3 ways to invoke a navigation-based transition:
78+
*
79+
* - Programatically, via {@link Velvette#startNavigation Velvette.startNavigation}
80+
* - Same-document: by {@link Velvette#intercept intercepting} a navigation using the {@link https://developer.mozilla.org/en-US/docs/Web/API/Navigation_API Navigation API}
81+
* - Cross-document: by attaching to the window's events {@link Velvette#crossDocument Velvette.crossDocument}. Note that this doesn't work in stable browsers yet.
82+
*
83+
* See {@link Config} for the different configuration options.
84+
*/
4985
export class Velvette {
5086
#internal;
51-
/** @param {Config} config */
87+
/** @param {Config} config The configuration object that defines how to handle each navigation */
5288
constructor(config) {
5389
this.#internal = init(config);
5490
}
5591

5692
/**
5793
* @param {object} navigation
58-
* @param {string} navigation.from
59-
* @param {string} navigation.to
60-
* @param {NavigationType} navigation.navigationType
61-
* @param {number} navigation.traverseDelta
62-
* @param {() => void | Promise<void>} update
94+
* @param {string} navigation.from The previous URL of the navigation
95+
* @param {string} navigation.to The next URL of the navigation
96+
* @param {NavigationType} navigation.navigationType The type of navigation (push/replace/reload/traverse)
97+
* @param {number} navigation.traverseDelta If this is a `traverse` navigation, the delta. E.g. -1 is "back".
98+
* @param {() => void | Promise<void>} update The operation that updates the DOM based on the navigation.
99+
*
100+
* @example
101+
*
102+
* const velvette = new Velvette(config);
103+
* link.addEventListener("click", event => {
104+
* velvette.startNavigation({
105+
* from: document.URL,
106+
* to: event.target.href,
107+
* navigationType: "push",
108+
* }, () => {
109+
* // Make actual DOM change.
110+
* });
111+
* });
63112
*/
64113
startNavigation(navigation, update) {
65114
const result = this.#internal.findMatchingNav(navigation);
@@ -73,9 +122,22 @@ export class Velvette {
73122
}
74123

75124
/**
76-
* @param {NavigateEvent} event
125+
* @param {NavigateEvent} event A `NavigateEvent`
77126
* @param {{handler: () => Promise<void>}} interceptor
78127
* @return {Promise<ViewTransition | null>}
128+
*
129+
* @example
130+
* const velvette = new Velvette(config);
131+
* window.navigation.addEventListener("navigate", event => {
132+
* if (shouldInterceptEvent(event)) {
133+
* // This would potentially start a transition based on the configuration.
134+
* velvette.intercept(event, {
135+
* async handler() {
136+
* // Make actual DOM change
137+
* }
138+
* });
139+
* });
140+
* }
79141
*/
80142
intercept(event, interceptor) {
81143
const nav = this.#internal.findMatchingNavForNavigateEvent(event);
@@ -124,7 +186,8 @@ export class Velvette {
124186
* @returns {void}
125187
*/
126188
const toggle = enabled => {
127-
styleRule.navigation = enabled ? "auto" : "none"};
189+
styleRule.navigation = enabled ? "auto" : "none"
190+
};
128191

129192
window.addEventListener("pagehide", () => toggle(true));
130193
window.addEventListener("pagereveal", event => {

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"files": ["src/index.js"],
2+
"files": ["src/index.js", "src/global.d.ts", "src/types.ts"],
33
"compilerOptions": {
44
/* Visit https://aka.ms/tsconfig to read more about this file */
55

typedoc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"entryPoints": ["src/index.js"],
2+
"entryPoints": ["src/index.js", "src/types.ts", "src/global.d.ts"],
33
"readme": "README.md",
44
"out": "./docs"
55
}

0 commit comments

Comments
 (0)