Skip to content

Commit f050ba3

Browse files
committed
feat(transition): adds custom first transition
1 parent 795b309 commit f050ba3

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/lib/components/RouteTransition.svelte

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import type { TransitionFunction } from '@dvcol/svelte-utils/transition';
2+
import type { TransitionFunction, TransitionWithProps } from '@dvcol/svelte-utils/transition';
33
import type { Snippet } from 'svelte';
44
55
import type { TransitionProps } from '~/models/component.model.js';
@@ -10,23 +10,31 @@
1010
const { children, key, id, transition }: { children: Snippet; id: string; key: any | any[]; transition: TransitionProps } = $props();
1111
1212
let firstRender = true;
13-
const skipFirst = $derived<boolean>(transition?.skipFirst ?? true);
13+
const skipFirst = $derived<TransitionProps['skipFirst']>(transition?.skipFirst ?? false);
1414
const skipTransition = (skip = skipFirst) => {
15-
if (!firstRender || !skip) return false;
15+
if (!firstRender) return false;
1616
firstRender = false;
17-
return true;
17+
return skip === true;
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;
22+
2023
const _in = $derived<TransitionFunction>(((node, props, options) => {
2124
if (skipTransition()) return;
25+
if (firstRender && isTransitionFunction(skipFirst)) return skipFirst;
26+
if (firstRender && isTransitionObject(skipFirst)) return skipFirst.use;
2227
return transition?.in?.(node, props, options);
2328
}) as TransitionFunction);
2429
const _out = $derived<TransitionFunction>(((node, props, options) => {
2530
if (firstRender && skipFirst) return;
2631
return transition?.out?.(node, props, options);
2732
}) as TransitionFunction);
2833
29-
const _inParams = $derived(transition?.params?.in ?? {});
34+
const _inParams = $derived.by(() => {
35+
if (firstRender && isTransitionObject(skipFirst) && skipFirst.props) return skipFirst.props;
36+
return transition?.params?.in ?? {};
37+
});
3038
const _outParams = $derived(transition?.params?.out ?? {});
3139
3240
const _containerProps = $derived(transition?.props?.container);

src/lib/models/component.model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Values } from '@dvcol/common-utils/common/class';
2-
import type { TransitionFunction } from '@dvcol/svelte-utils/transition';
2+
import type { TransitionFunction, TransitionProps as TransitionParams, TransitionWithProps } from '@dvcol/svelte-utils/transition';
33
import type { Snippet } from 'svelte';
44
import type { HTMLAttributes } from 'svelte/elements';
55

@@ -30,14 +30,14 @@ export interface RouterContextProps<Name extends RouteName = any> {
3030
export type TransitionDiscardFunction = (entry: MutationRecord, index: number, entries: MutationRecord[]) => boolean;
3131

3232
export interface TransitionProps<
33-
T extends { in?: Record<string, any>; out?: Record<string, any> } = { in?: Record<string, any>; out?: Record<string, any> },
33+
T extends { in?: TransitionParams; out?: TransitionParams; first?: TransitionParams } = { in?: TransitionParams; out?: TransitionParams; first?: TransitionParams },
3434
> {
3535
/**
3636
* Skip the first enter transition.
3737
* This is useful when the first route load is fast and the transition is not needed.
38-
* @default true
38+
* @default false
3939
*/
40-
skipFirst?: boolean;
40+
skipFirst?: boolean | TransitionFunction<T['first']> | TransitionWithProps<TransitionParams>;
4141
/**
4242
* If `true`, the transition will be updated on any route change.
4343
* By default, the transition is only triggered when the component changes to avoid unnecessary mounting and unmounting.

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: true,
11+
skipFirst: { use: scaleFreeze, props: { delay: 100 } },
1212
};

0 commit comments

Comments
 (0)