Flowbite + Vue
Vue.js는 반응성과 선언적 렌더링에 기반한 인기 있는 프론트엔드 라이브러리예요. Behance, Nintendo, Gitlab, Font Awesome 등 많은 웹사이트에서 사용하며 현대적인 웹 애플리케이션을 만들 수 있어요. Tailwind CSS와 Flowbite를 설치하면, Tailwind의 유틸리티 우선 접근 방식과 Flowbite의 인터랙티브 UI 컴포넌트(모달, 테이블, 드롭다운, 데이트피커 등)로 프로젝트를 더 빠르게 만들 수 있어요.
출처: 문서
본문
이 가이드를 따라 Vue 3과 Vite에서 Tailwind CSS v4와 Flowbite를 설치하는 법을 배워요.
Vue 프로젝트 만들기 (Create a Vue project)
터미널에서 다음 명령을 실행해 새 Vite 프로젝트를 만들어요.
npm create vite@latest flowbite-app -- --template vue
cd flowbite-app
Tailwind CSS 설치 (Install Tailwind CSS)
이 가이드는 많은 개선이 들어간 최신 Tailwind CSS v4를 사용해요.
터미널에서 Vite용 Tailwind CSS를 설치해요.
npm install tailwindcss @tailwindcss/vite --save
vite.config.js 파일 안에 Tailwind 플러그인을 추가해요.
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [
vue(),
tailwindcss()
],
})
style.css 파일 안에 tailwind 모듈을 가져와요.
@import "tailwindcss";
이제 Tailwind CSS와 Vue를 모두 설치했어요. 다음 단계로 진행하면 돼요.
Flowbite 설치 (Install Flowbite)
Flowbite의 UI 컴포넌트는 Vue와 Tailwind로 웹사이트를 만들 때 시간을 절약하게 해줘요. CSS와 JavaScript 의존성을 모두 설치하는 다음 단계를 따라가세요.
NPM으로 Flowbite를 의존성으로 설치해요.
npm install flowbite --save
메인 input.css CSS 파일에 Flowbite의 기본 테마 변수를 가져와요.
/* choose one of the following */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');
@import "flowbite/src/themes/default";
/* MINIMAL THEME
@import url('https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300..800;1,300..800&display=swap');
@import "flowbite/src/themes/minimal";
*/
/* ENTERPRISE THEME
@import url('https://fonts.googleapis.com/css2?family=Shantell+Sans:ital,wght@0,300..800;1,300..800&display=swap');
@import "flowbite/src/themes/enterprise";
*/
/* PLAYFUL THEME
@import url('https://fonts.googleapis.com/css2?family=Google+Sans+Code:ital,wght@0,300..800;1,300..800&display=swap');
@import "flowbite/src/themes/playful";
*/
/* MONO THEME
@import "flowbite/src/themes/mono";
*/
CSS에 Flowbite 플러그인 파일을 가져와요.
@plugin "flowbite/plugin";
CSS에 Flowbite의 소스 파일 경로를 구성해요.
@source "../node_modules/flowbite";
터미널에서 다음 명령을 실행해 로컬 개발 서버를 시작해요.
npm run dev
프로젝트를 빌드하려면 npm run build를 실행하면 돼요.
Flowbite 컴포넌트 (Flowbite components)
Vue 3, Tailwind CSS, Flowbite를 성공적으로 설치했다면 이제 Flowbite의 컴포넌트(네비게이션 바, 모달, 드롭다운 등)를 가져와 사용할 수 있어요.
예시로 Modal 컴포넌트를 사용해서 HTML 마크업을 Vue 3 템플릿 파일 안에 복사해봐요.
<template>
<div>
<div class="flex justify-center p-4">
<button id="button" data-modal-toggle="modal" data-modal-target="modal" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 mr-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Show modal</button>
</div>
<div id="modal" tabindex="-1" aria-hidden="true" class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full">
<div class="relative w-full max-w-2xl max-h-full">
<div class="relative bg-white rounded-lg shadow-sm dark:bg-gray-700">
<div class="flex items-start justify-between p-5 border-b rounded-t dark:border-gray-600">
<h3 class="text-xl font-semibold text-gray-900 lg:text-2xl dark:text-white">
Terms of Service
</h3>
<button id="closeButton" data-modal-hide="modal" type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
</button>
</div>
<!-- Modal body -->
<div class="p-6 space-y-6">
<p class="text-base leading-relaxed text-gray-500 dark:text-gray-400">
With less than a month to go before the European Union enacts new consumer privacy laws for its citizens, companies around the world are updating their terms of service agreements to comply.
</p>
<p class="text-base leading-relaxed text-gray-500 dark:text-gray-400">
The European Union's General Data Protection Regulation (G.D.P.R.) goes into effect on May 25 and is meant to ensure a common set of data rights in the European Union. It requires organizations to notify users as soon as possible of high-risk data breaches that could personally affect them.
</p>
</div>
<!-- Modal footer -->
<div class="flex items-center p-6 space-x-2 border-t border-gray-200 rounded-b dark:border-gray-600">
<button type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">I accept</button>
<button type="button" class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-blue-300 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600">Decline</button>
</div>
</div>
</div>
</div>
</div>
</template>
데이터 속성 (Data attributes)
Vue 3의 onMounted 생명주기 메서드를 기반으로 Flowbite 패키지에서 "init" 함수들을 가져오면 기본적으로 인터랙티브 컴포넌트를 동작하게 할 수 있어요.
다음은 Vue 3에서 데이터 속성을 기반으로 컴포넌트를 초기화하는 예시예요.
<script setup>
import { onMounted } from 'vue'
import { initFlowbite } from 'flowbite'
// initialize components based on data attribute selectors
onMounted(() => {
initFlowbite();
})
</script>
<template>
// Modal HTML markup with data attributes from Flowbite
</template>
혹은 컴포넌트를 각각 초기화할 때 사용할 수 있는 전체 함수 목록을 쓸 수도 있어요.
<script setup>
import { onMounted } from 'vue'
import {
initAccordions,
initCarousels,
initCollapses,
initDials,
initDismisses,
initDrawers,
initDropdowns,
initModals,
initPopovers,
initTabs,
initTooltips } from 'flowbite'
// initialize components based on data attribute selectors
onMounted(() => {
initAccordions();
initCarousels();
initCollapses();
initDials();
initDismisses();
initDrawers();
initDropdowns();
initModals();
initPopovers();
initTabs();
initTooltips();
})
</script>
이렇게 하면 Flowbite의 데이터 속성으로 모든 인터랙티브 컴포넌트가 동작하지만, 로드 속도를 최적화하려면 실제로 각 페이지에서 사용하는 컴포넌트만 초기화하는 걸 권장해요. 실제 동작은 스타터 가이드의 Events.vue 파일에서 확인할 수 있어요.
JavaScript API
Flowbite의 인터랙티브 컴포넌트를 Vue 3과 Tailwind CSS와 함께 사용하려면, Vue 파일 안에서 컴포넌트를 가져오고 Vue의 onMounted 메서드에 기반해 객체를 초기화하면 돼요. 다음은 Modal 컴포넌트를 사용하는 예시예요.
<script setup>
import { onMounted } from 'vue'
import { Modal } from 'flowbite'
onMounted(() => {
const $buttonElement = document.querySelector('#button');
const $modalElement = document.querySelector('#modal');
const $closeButton = document.querySelector('#closeButton');
const modalOptions = {
backdropClasses: 'bg-gray-900/50 dark:bg-gray-900/80 fixed inset-0 z-40'
}
if ($modalElement) {
const modal = new Modal($modalElement, modalOptions);
$buttonElement.addEventListener('click', () => modal.toggle());
$closeButton.addEventListener('click', () => modal.hide());
// programmatically show
// modal.show();
}
})
</script>
보시다시피 Flowbite 패키지에서 Modal 컴포넌트를 가져와 onMounted 메서드로 이벤트 리스너를 설정했어요. 이렇게 하면 템플릿이 로드된 뒤에 동작하며, Vue Router 같은 도구로 뷰를 전환할 때도 잘 동작해요.
<template> 태그 안에 넣어야 하는 HTML 마크업은 다음과 같아요.
<template>
<div class="flex justify-center p-4">
<button id="button" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 mr-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Show modal</button>
</div>
<div id="modal" tabindex="-1" aria-hidden="true" class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full">
<div class="relative w-full max-w-2xl max-h-full">
<!-- Modal content -->
<div class="relative bg-white rounded-lg shadow-sm dark:bg-gray-700">
<!-- Modal header -->
<div class="flex items-start justify-between p-5 border-b rounded-t dark:border-gray-600">
<h3 class="text-xl font-semibold text-gray-900 lg:text-2xl dark:text-white">
Terms of Service
</h3>
<button id="closeButton" type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
</button>
</div>
<!-- Modal body -->
<div class="p-6 space-y-6">
<p class="text-base leading-relaxed text-gray-500 dark:text-gray-400">
With less than a month to go before the European Union enacts new consumer privacy laws for its citizens, companies around the world are updating their terms of service agreements to comply.
</p>
<p class="text-base leading-relaxed text-gray-500 dark:text-gray-400">
The European Union's General Data Protection Regulation (G.D.P.R.) goes into effect on May 25 and is meant to ensure a common set of data rights in the European Union. It requires organizations to notify users as soon as possible of high-risk data breaches that could personally affect them.
</p>
</div>
<!-- Modal footer -->
<div class="flex items-center p-6 space-x-2 border-t border-gray-200 rounded-b dark:border-gray-600">
<button type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">I accept</button>
<button type="button" class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-blue-300 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600">Decline</button>
</div>
</div>
</div>
</div>
</template>
Flowbite API에서 사용 가능한 메서드와 옵션은 이 문서의 각 페이지에서 해당되는 곳의 "JavaScript Behaviour" 섹션을 아래로 스크롤하면 확인할 수 있어요.
Vue 3 스타터 프로젝트 (Vue 3 Starter Project)
이 가이드를 기반으로 GitHub에 스타터 프로젝트도 만들었어요. Vue 3과 Tailwind CSS 구성으로 Flowbite의 모든 사용 가능한 컴포넌트를 사용하는 방법을 보여줘요.
Flowbite Vue 라이브러리 (Flowbite Vue Library)
Vue 3 환경 안에서 사용하도록 특별히 만들어진 전용 Flowbite Vue 라이브러리 작업을 시작했어요. 출시되면 Vue 3에서 우리 컴포넌트를 사용하는 권장 방식이 될 거예요.