Movatterモバイル変換


[0]ホーム

URL:


TW Elements Laravel integration

This article shows you how to integrate Laravel application with TW Elements. Free download, open source license.


Prerequisites

Before starting the project make sure to install the following utilities:


Creating a new Laravel application

Let's create a fresh Laravel application so that we can go through all the steps together.

Note: If you have trouble with installing composer and you got an error bound installation - trycomposer install --ignore-platform-req=ext-fileinfo orcomposer update --ignore-platform-reqs.

Step 1

Create new project.

                composer create-project laravel/laravel my-project    cd my-project

Step 2

Run the development server.

                php artisan serve

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/        ├── ...        ├── node_modules/               ├── public/          ├── resources/        │   ├── css/        │   │   └── app.css        │   ├── js/        │   │   ├── app.js        │   │   └── ...        │   └── views/        │       └── welcome.blade.php        ├── ...        ├── package-lock.json        ├── package.json        ├── ...        ├── postcss.config.js        ├── README.md        ├── tailwind.config.js        └── vite.config.js

Step 2

Add the paths to all of your template files in yourtailwind.config.js file.

                /** @type {import('tailwindcss').Config} */    module.exports = {      content: [        "./resources/**/*.blade.php",         "./resources/**/*.js",        "./resources/**/*.vue",        "./node_modules/tw-elements/js/**/*.js"      ],      theme: {        extend: {},      },      darkMode: "class",      plugins: [require("tw-elements/plugin.cjs")]    }

Step 3

Add the@tailwind directives for each of Tailwind’s layers to yourapp.css file.

                @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 4

Make sure your compiled CSS and JS are included in thehead inwelcome.blade.php file.

                @vite('resources/css/app.css')    @vite('resources/js/app.js')

Step 5

Install TW Elements.

                npm install tw-elements

Step 6

Import and initialize withinitTWE function components which are you intend to use and necessary functioninitTWE inapp.js. Additionally add HTML markup for components in thewelcome.blade.php file.

                import { Tooltip, initTWE } from "tw-elements";    initTWE({ Tooltip });
                ...    <main>      <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>    </main>    ...

Step 7

Start build process.

                npm run dev

And run the development server. Awesome! You're all set to dive into using TW Elements for your Laravel project. Have fun!

                php artisan serve

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 and initialize components.

                import { Tooltip } from 'tw-elements';    const myTooltip = new Tooltip(document.getElementById('my-tooltip'));
                ...    <main>      <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>    </main>    ...

Troubleshooting

Problem overview:

After initializing TWE components as described in the documentation you can got erros like(TWE Components name) is not defined orte is not defined.

Suggested solution:

Set variables or constants to the window scope.

                      const { Tooltip, initTWE } = await import("tw-elements");          window.iniTWE = initTWE;          window.Tooltip = Tooltip;          initTWE({ Tooltip });

Source GitHub Discussion #1839 #1737


Related resources

Tutorials:

Integrations :


[8]ページ先頭

©2009-2025 Movatter.jp