Konsta UI & Next.js

Let's see how to use Konsta UI React components with Next.js.

Create Next.js Project

First, create a Next.js project using create-next-app

Install Tailwind CSS

We can follow official Tailwind CSS with Next.js Guide

Install Konsta UI

Now in created Next.js 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: [
    './pages/**/*.{js,ts,jsx,tsx}',
    './components/**/*.{js,ts,jsx,tsx}',
  ],
  darkMode: false, // or '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 pages/_app.js:

import { App } from 'konsta/react';
import '../styles/globals.css';

function MyApp({ Component, pageProps }) {
  return (
    // Wrap our app with App component
    <App theme="ios">
      <Component {...pageProps} />
    </App>
  );
}

export default MyApp;

Example Page

Now when everything is set up, we can use Konsta UI React components in our Next.js pages.

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

// Konsta UI components
import {
  Page,
  Navbar,
  Block,
  Button,
  List,
  ListItem,
  Link,
  BlockTitle,
} from 'konsta/react';

export default function Home() {
  return (
    <Page>
      <Navbar title="My App" />

      <Block strong>
        <p>
          Here is your Next.js & Konsta UI app. Let's see what we have here.
        </p>
      </Block>
      <BlockTitle>Navigation</BlockTitle>
      <List>
        <ListItem href="/about/" title="About" />
        <ListItem href="/form/" title="Form" />
      </List>

      <Block strong className="flex space-x-4">
        <Button>Button 1</Button>
        <Button>Button 2</Button>
      </Block>
    </Page>
  );
}

As a result we should see the following page:

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