Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new SvelteKit application
Let's create a fresh SvelteKit application so that we can go through all the steps together.
Step 1
Create new project.
SvelteKit
app: App template - Skeleton project
,
Type checking - JS with JSDocs comments
, Additional options -
ESLint
.
npm create svelte@latest my-project
cd my-project
npm install
Step 2
Run the development server.
npm run dev
Installing and configuring Tailwind CSS and TW Elements
Step 1
Install Tailwind CSS.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
File structure
If you have followed all instructions mentioned earlier, your file structure should look like this:
my-project/
├── .svelte-kit/
├── node_modules/
├── src/
│ ├── lib/
│ ├── routes/
│ │ └── +page.svelte
│ ├── app.d.ts
│ └── app.html
├── static/
├── ...
├── package-lock.json
├── package.json
├── postcss.config.js
├── ...
├── svelte.config.js
├── tailwind.config.js
└── vite.config.js
Step 2
Add the paths to all of your template files in your
tailwind.config.js
file.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{html,js,svelte,ts}",
"./node_modules/tw-elements/js/**/*.js",
],
theme: {
extend: {},
},
darkMode: "class",
plugins: [require("tw-elements/plugin.cjs")],
};
Step 3
In the svelte.config.js
file, include
vitePreprocess
to enable the handling of
style
blocks as PostCSS.
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter()
},
preprocess: vitePreprocess()
};
export default config;
Step 4
Create app.css
file in src
directory and add the
@tailwind
directives.
@import url("https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900&display=swap");
@tailwind base;
@layer base {
html {
@apply text-surface;
@apply bg-white;
}
html.dark {
@apply text-neutral-50;
@apply bg-body-dark;
}
}
@tailwind components;
@tailwind utilities;
p {
@apply leading-[1.6];
}
Step 5
Create a new file named +layout.svelte
in the
./src/routes
directory and import the recently created
app.css
file.
<script>
import "../app.css";
</script>
<slot></slot>
Step 6
Install TW elements.
npm install tw-elements
Step 7
Import components which are you intend to use and necessary function
initTWE
. Initialize initTWE
in a lifecycle method.
<script>
import { onMount } from "svelte";
onMount(async () => {
const { Tooltip, initTWE } = await import("tw-elements");
initTWE({ Tooltip });
});
</script>
<div class="mt-16 flex justify-center">
<p class="text-lg">
Hover the link to see the
<a
href="#"
class="text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600"
data-twe-toggle="tooltip"
title="Hi! I'm tooltip"
>tooltip</a
>
</p>
</div>
Step 8
Start the app and see if everything's fine. Awesome! You're all set to dive into using TW Elements for your SvelteKit project. Have fun!
npm run dev
Initializing via JS
By default all components have autoinit which means they are initialized by data attributes. But if you want to make init by JavaScript - there is also possibility to do that.
Step 1
Import library and initialize components in lifecycle method.
<script>
import { onMount } from "svelte";
import { Tooltip } from "tw-elements";
onMount(async () => {
const { Tooltip } = await import("tw-elements");
const myTooltip = new Tooltip(document.getElementById("my-tooltip"));
});
</script>
<div class="mt-16 flex justify-center">
<p class="text-lg">
Hover the link to see the
<a
id="my-tooltip"
href="#"
class="text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600"
title="Hi! I'm tooltip">
tooltip
</a>
</p>
</div>