Konsta UI & Nuxt

Let's see how to use Konsta UI Vue components with Nuxt.

You can start from forking this Konsta Nuxt 3 Starter repository or follow the guide below.

Because Konsta UI comes with components only for Vue.js v3, it compatible only with Nuxt 3 as well.

Create Nuxt Project

First, create a Nuxt project

Install Tailwind CSS

We can follow official Tailwind CSS installation Guide

Install all required dependencies

npm install -D tailwindcss postcss@latest autoprefixer@latest sass
npx tailwindcss init

Create postcss.config.js file with the following content:

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
};

Enable Post CSS and konsta transpilation in your nuxt.config.js:

import { defineNuxtConfig } from 'nuxt';

export default defineNuxtConfig({
  build: {
    transpile: ['konsta'],
    postcss: {
      postcssOptions: require('./postcss.config.js'),
    },
  },
});

Tailwind CSS Styles

Create a assets/globals.css file with the following content to include Tailwind CSS:

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700;900&display=swap');
@tailwind base;
@tailwind components;
@tailwind utilities;

Install Konsta UI

Now in created Nuxt project, we need to install Konsta UI:

npm i konsta

And in your tailwind.config.js file we should extend config with Konsta UI's config:

// import konstaConfig config
const konstaConfig = require('konsta/config');

// wrap config with konstaConfig config
module.exports = konstaConfig({
  content: [
    './components/*.{js,ts,jsx,vue}',
    './pages/*.{js,ts,jsx,vue}',
  ],
  darkMode: 'media', // or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
});

App Component

Now we need to setup main App component so we can set some global parameters (like theme).

We need to wrap whole app with App in the ./app.vue:

<template>
  <!-- Wrap our app with App component -->
  <k-app theme="ios">
    <NuxtPage />
  </k-app>
</template>

<script>
  import { kApp } from 'konsta/vue';
  import './assets/main.scss';

  export default {
    components: {
      kApp,
    },
  };
</script>

Example Page

Now when everything is set up, we can use Konsta UI Vue components in our Nuxt pages.

For example, let's open pages/index.vue and change it to the following:

<template>
  <k-page>
    <k-navbar title="My App" />

    <k-block strong>
      <p>Here is your Nuxt & Konsta UI app. Let's see what we have here.</p>
    </k-block>
    <k-block-title>Navigation</k-block-title>
    <k-list>
      <k-list-item href="/about/" title="About" />
      <k-list-item href="/form/" title="Form" />
    </k-list>

    <k-block strong class="flex space-x-4">
      <k-button>Button 1</k-button>
      <k-button>Button 2</k-button>
    </k-block>
  </k-page>
</template>
<script>
  // Konsta UI components
  import {
    kPage,
    kNavbar,
    kBlock,
    kButton,
    kList,
    kListItem,
    kLink,
    kBlockTitle,
  } from 'konsta/vue';

  export default {
    components: {
      kPage,
      kNavbar,
      kBlock,
      kButton,
      kList,
      kListItem,
      kLink,
      kBlockTitle,
    },
  };
</script>

As a result we should see the following page:

konsta-next
Code licensed under MIT.
2022 © Konsta UI by nolimits4web.