This Svelte creative template is designed to streamline the development of sophisticated, animation-rich web experiences. It provides a powerful, structured API to create and manage complex animations, including seamless page-to-page transitions, without compromising layout integrity. The template intelligently handles client-side resource loading, coordinating it with animations for a polished and performant user experience.
Built with a modern stack including Svelte 5, TypeScript, Tailwind CSS, and GSAP, and pre-configured for deployment on Cloudflare Workers.
[!NOTE] This template is currently under active development. Features may change, and new APIs will be added. Integration with Threlte for 3D graphics is planned for a future release.
git clone https://github.com/qtnrtnd/svelte-creative-template.git
cd svelte-creative-template
yarn install
yarn dev
src/lib
)The src/lib
directory contains the core logic of the template.
gsap.ts
This file initializes the GSAP library and registers the following plugins, making them available for use throughout the application:
ScrollTrigger
ScrollSmoother
Flip
SplitText
src/lib/animation
)This is the heart of the template's animation capabilities.
TweenTransition
src/lib/animation/core/TweenTransition.svelte.ts
intro
, outro
, and idle
states.prevent
, invalidate
, restart
.inViewport
option).introstart
, introend
, etc.).TweenTransition.from()
.Crossfade
src/lib/animation/core/Crossfade.ts
Crossfade.create()
to generate a [send, receive]
tuple of transition functions.send
transition to the outgoing element and the receive
transition to the incoming element.key
.TweenTransitionAdapterConfig
src/lib/animation/core/TweenTransitionAdapterConfig.ts
TweenTransition
or a custom animation function and produces a TransitionConfig
object that Svelte can use, bridging the gap between GSAP's imperative API and Svelte's declarative transitions.src/lib/states
)AppContext
src/lib/states/core/AppContext.svelte.ts
initial
: true
only during the initial application load.swapping
: true
when a page transition is active.frozen
: A state to disable user interactions (e.g., during a critical animation).scrollPaused
: A state to disable scrolling.frozen
is true.InterfaceContext
src/lib/states/core/InterfaceContext.svelte.ts
swapOffset
to maintain visual positioning of elements during a page swap.src/lib/preload
)PreloadContext
src/lib/preload/core/PreloadContext.ts
usePreload
hook provides a component-specific interface to preloadImage()
. The context handles responsive images (<picture>
) and ensures the correct image variant is loaded based on the viewport.src/lib/layout
)LayoutContext
src/lib/layout/core/LayoutContext.svelte.ts
fixed
: A reactive set for snippets that should be rendered in a 'fixed' container, useful for elements that need to persist across page transitions or remain fixed in the viewport.src/lib/portal
)PortalContext
src/lib/portal/core/PortalContext.svelte.ts
Portal
component. Its primary role is to track the durations of outgoing animations to determine how long a portal's content should be "kept alive" during a page transition, ensuring that animations can complete before the element is removed from the DOM.Portal.svelte
src/lib/suspense
)SuspenseContext
src/lib/suspense/core/SuspenseContext.svelte.ts
suspendTasks
: Awaits promises.suspendState
: Suspends based on a boolean function.onReveal
/onSuspend
: Hooks for suspense state changes.Suspense.svelte
src/lib/scroller
)src/lib/scroller/utils/context.ts
gsap.ScrollSmoother
instance.createScrollerContext(options)
: Creates a ScrollSmoother
instance and registers it in Svelte's context. Should be called once in a high-level layout component.useScroller()
: Retrieves the ScrollSmoother
instance from the context.src/lib/hooks
)This directory provides a powerful event system and a set of predefined hooks for key application lifecycle events.
Hook
Classsrc/lib/hooks/core/Hook.ts
Hook.create()
to create a new hook.src/lib/hooks/utils/functions.ts
onResize
: Dispatched when the window is resized.onScroll
: Dispatched on scroll, providing the ScrollSmoother
instance.beforeSwap
, onSwap
, afterSwap
: A series of hooks that fire during page transitions, providing access to the leaving and entering page elements.onRemove
: Fires a callback when a specific DOM element is removed.beforeRefresh
, onRefresh
: Fire before and after a ScrollTrigger
instance is refreshed.src/lib/helpers
)A collection of utility functions and Svelte attachments to simplify common tasks.
src/lib/helpers/utils/functions.ts
debounce(fn, delay)
: Creates a debounced version of a function.fromClient(create, cleanup)
: Executes a function only on the client-side.mergeCls(...inputs)
: Merges Tailwind CSS classes safely.setParams(params)
: Updates URL search parameters without a full navigation.once(fn)
: Creates a function that can only be called once.observer
Attachmentsrc/lib/helpers/utils/attachments.ts
{@attach ...}
) factory for creating ResizeObserver
, MutationObserver
, and IntersectionObserver
instances declaratively.src/lib/components
)This template includes a set of utility components to handle common patterns.
Page.svelte
: A wrapper for page content that orchestrates page-level transitions using the AppContext
and the onSwap
hooks.Portal.svelte
: Renders its children into a different part of the DOM. It uses PortalContext
to manage the lifecycle of its content, especially during page transitions.Suspense.svelte
: Manages loading states by wrapping asynchronous operations. It provides a SuspenseContext
to its children, allowing them to register tasks and control the suspense boundary.Template.svelte
: A core component that likely orchestrates the main application layout and initializes the core contexts.Anchor.svelte
: An enhanced anchor tag.Fixed.svelte
: A component to place content into the LayoutContext
's fixed
area.OverflowMarquee.svelte
: Creates a scrolling marquee effect for overflowing content.Picture.svelte
: A component for responsive images that integrates with the PreloadContext
for optimized loading.The package.json
file includes the following scripts:
yarn dev
: Starts the Vite development server.yarn build
: Builds the application for production.yarn preview
: Builds the app and previews the production build locally using Wrangler.yarn deploy
: Deploys the application to Cloudflare Workers.yarn check
: Runs svelte-check
to type-check your Svelte components.yarn lint
: Lints the codebase using ESLint and Prettier.yarn format
: Formats the entire codebase with Prettier.yarn cf-typegen
: Generates TypeScript types for your Cloudflare Worker bindings.