Use With
Setting up Twind for seamless integration in a SvelteKit project.
- 📝 Inspect the example
- 📓 Consult the API reference
- 📜 Read the changelog
🤝 Compatibility
@twind/with-sveltekit | @sveltejs/kit |
---|---|
>=1.1.0 <1.2.0 | >=1.0.0 <2 |
>=1.0.0-next.38 <1.1.0 | >=1.0.0-next.391 <1 |
>=1.0.0-next.1 <1.0.0-next.38 | >=1.0.0-next.100 <1.0.0-next.391 |
📦 Installation
-
Install from npm
@twind/core
and@twind/with-sveltekit
are available on npm and need to be installed together.shnpm install @twind/core @twind/with-sveltekit
-
Define the configuration
Using an extra file,
twind.config.js
, allows some tools like IntelliSense to find your configuration.twind.config.js jsimport { defineConfig } from '@twind/core' export default defineConfig({ /* @twind/with-sveltekit will use hashed class names in production by default * If you don't want this, uncomment the next line */ // hash: false, })
-
Load Twind in the root layout
install
creates and registers a twind instance that will generate the styles. This allows third-party packages to importtw
from the twind package and get the same instance.src/routes/+layout.js jsimport install from '@twind/with-sveltekit' import config from '../../twind.config' install(config) // Optional: add a load function
-
Enable Twind in hooks
Enable server-side rendering of all the styles that are used within the HTML and sending them to the client.
src/hooks.server.js jsimport handleTwind from '@twind/with-sveltekit/hooks' export const handle = handleTwind()
If you have other handlers use the
sequence
helper:jsimport { sequence } from '@sveltejs/kit/hooks' export const handle = sequence(handleTwind(), ...otherHandlers)
-
Optional: Install and configure the recommended presets
@twind/preset-autoprefix
and@twind/preset-tailwind
.Install the presets
All presets are available on npm.
shnpm install @twind/preset-autoprefix @twind/preset-tailwind
Configure the presets
Each preset must be added to the
presets
array in the configuration.twind.config.js jsimport { defineConfig } from '@twind/core' import presetAutoprefix from '@twind/preset-autoprefix' import presetTailwind from '@twind/preset-tailwind' export default defineConfig({ presets: [presetAutoprefix(), presetTailwind()], })
-
Start using Twind in your svelte component
Start using Twind classes to style your content.
html<h1 class="text-3xl font-bold underline">Hello world!</h1>