Skip to content

Commit 1ef38be

Browse files
committed
feat(transition): change transition api to reflect the new types
1 parent 091e6bb commit 1ef38be

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/lib/components/RouteTransition.svelte

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@
99
1010
const { children, key, id, transition }: { children: Snippet; id: string; key: any | any[]; transition: TransitionProps } = $props();
1111
12-
let firstRender = true;
13-
const skipFirst = $derived<TransitionProps['skipFirst']>(transition?.skipFirst ?? false);
14-
const skipTransition = (skip = skipFirst) => {
12+
let firstRender = $state(true);
13+
const first = $derived<TransitionProps['first']>(transition?.first ?? true);
14+
const skipTransition = () => {
1515
if (!firstRender) return false;
1616
firstRender = false;
17-
return skip === true;
17+
return first === false;
1818
};
1919
20-
const isTransitionFunction = (skip?: TransitionProps['skipFirst']): skip is TransitionFunction => typeof skip === 'function' && !!skip;
21-
const isTransitionObject = (skip?: TransitionProps['skipFirst']): skip is TransitionWithProps => typeof skip === 'object' && !!skip?.use;
20+
const isTransitionFunction = (skip?: TransitionProps['first']): skip is TransitionFunction => typeof skip === 'function' && !!skip;
21+
const isTransitionObject = (skip?: TransitionProps['first']): skip is TransitionWithProps => typeof skip === 'object' && !!skip?.use;
2222
2323
const _in = $derived<TransitionFunction>(((node, props, options) => {
2424
if (skipTransition()) return;
25-
if (firstRender && isTransitionFunction(skipFirst)) return skipFirst;
26-
if (firstRender && isTransitionObject(skipFirst)) return skipFirst.use;
25+
if (firstRender && isTransitionFunction(first)) return first;
26+
if (firstRender && isTransitionObject(first)) return first.use;
2727
return transition?.in?.(node, props, options);
2828
}) as TransitionFunction);
2929
const _out = $derived<TransitionFunction>(((node, props, options) => {
30-
if (firstRender && skipFirst) return;
30+
if (firstRender && first) return;
3131
return transition?.out?.(node, props, options);
3232
}) as TransitionFunction);
3333
3434
const _inParams = $derived.by(() => {
35-
if (firstRender && isTransitionObject(skipFirst) && skipFirst.props) return skipFirst.props;
35+
if (firstRender && isTransitionObject(first) && first.props) return first.props;
3636
return transition?.params?.in ?? {};
3737
});
3838
const _outParams = $derived(transition?.params?.out ?? {});

src/lib/models/component.model.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ export type TransitionDiscardFunction = (entry: MutationRecord, index: number, e
3232
export interface TransitionProps<
3333
T extends { in?: TransitionParams; out?: TransitionParams; first?: TransitionParams } = { in?: TransitionParams; out?: TransitionParams; first?: TransitionParams },
3434
> {
35-
/**
36-
* Skip the first enter transition.
37-
* This is useful when the first route load is fast and the transition is not needed.
38-
* @default false
39-
*/
40-
skipFirst?: boolean | TransitionFunction<T['first']> | TransitionWithProps<TransitionParams>;
4135
/**
4236
* If `true`, the transition will be updated on any route change.
4337
* By default, the transition is only triggered when the component changes to avoid unnecessary mounting and unmounting.
@@ -58,6 +52,18 @@ export interface TransitionProps<
5852
* Transition to use when navigating away from the current route.
5953
*/
6054
out?: TransitionFunction<T['out']>;
55+
/**
56+
* The enter transition to use when the route is first loaded.
57+
* This is useful when the first route load is fast and the transition is not needed.
58+
*
59+
* If `false`, no transition will be applied on the first route load.
60+
* If `true`, the transition will be applied.
61+
* If a function is provided, it will be called with the `in` transition parameters.
62+
* If a transition object is provided, it will be used as the transition function and parameters.
63+
*
64+
* @default true
65+
*/
66+
first?: boolean | TransitionFunction<T['first']> | TransitionWithProps<TransitionParams>;
6167
/**
6268
* Transition parameters to be passed to the transition functions.
6369
*/

src/lib/utils/transition.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export const transition: TransitionProps<{ in?: ScaleFreezeParams; out?: ScaleFr
88
in: scaleFreeze,
99
out: scaleFreeze,
1010
params: { in: { delay: 400 } },
11-
skipFirst: { use: scaleFreeze, props: { delay: 100 } },
11+
first: { use: scaleFreeze, props: { delay: 100 } },
1212
};

0 commit comments

Comments
 (0)