🔥 Meet Our New Project: t0ggles - Your Ultimate Project Management Tool! 🔥

Konsta UI & Framework7

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

Create Framework7 Project

First, create a Framework7 React project using Framework7 CLI

Install Tailwind CSS & Konsta UI

Now in created Framework7 project, we need to install Tailwind CSS & Konsta UI:

npm i tailwindcss konsta

There is a postcss.config.js file in the project root, we need to add there tailwindcss plugin:

module.exports = {
  plugins: {
    tailwindcss: {}, // <- add this
    'postcss-preset-env': {},
  },
};

Create tailwind.config.js file and add it to the project root folder with the following content:

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

module.exports = konstaConfig({
  content: ['./src/**/*.{js,jsx,ts,tsx}', './src/index.html'],
  darkMode: 'class',
});

Include Tailwind CSS to your app's styles. Add the following code in the beginning of your src/css/app.[css|scss|less]:

@tailwind base;
@tailwind components;
@tailwind utilities;

KonstaProvider

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

We need to wrap main Framework7's App component with KonstaProvider in the src/components/app.jsx:

import React from 'react';
import { KonstaProvider } from 'konsta/react';
import { App, /* ... */ } from 'framework7-react';

const MyApp = () => {
  // Framework7 Parameters
  const f7params = {
    theme: 'ios',
    ...
  };

  return (
    // Wrap Framework7's App with KonstaProvider with theme="parent"
    <KonstaProvider theme="parent">
      <App {...f7params}>
        ...
      </App>
    </KonstaProvider>
  );
}

export default MyApp;

Usage

Framework7 has very powerful and flexible router, and to keep it work correctly we need to use Router-related components from Framework7: App, View, Page, Navbar, Toolbar.

The rest of components (if there is a Konsta UI alternative) can be taken from Konsta UI (konsta/react)

Example Page

Now when everything is set up, we can use Konsta UI React components in Framework7 page components.

For example, let's open src/pages/home.jsx and change it to the following:

import React from 'react';
// Use Page, Navbar, Toolbar from Framework7
import { Page, Navbar, Toolbar } from 'framework7-react';
// Konsta UI components
import { Block, Button, List, ListItem, Link, BlockTitle } from 'konsta/react';

const HomePage = () => (
  // Use Page, Navbar & Toolbar from Framework7
  <Page name="home">
    <Navbar title="My App" large />
    <Toolbar bottom>
      <Link toolbar>Left Link</Link>
      <Link toolbar>Right Link</Link>
    </Toolbar>
    {/* In page content we can use Konsta UI components  */}
    <Block strong>
      <p>
        Here is your Framework7 & 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>
);
export default HomePage;

As a result we should see the following page:

konsta-framework7

Dark Mode

Pay attention that Tailwind CSS requires class dark to be added to enable Dark mode, while Framework7 requires class theme-dark.

So if you use dark mode, make sure you set/remove both dark theme-dark classes on root element (preferably on <html> element).

Color Theme

Framework7 and Konsta UI use different sources for main theme color. While Framework7's theme color is configured via CSS custom properties, Konsta UI theme should be set in tailwind.config.js.

ColorFramework7 CSS VarKonsta UI Prop
primary--f7-theme-colorprimary
primary dark--f7-theme-color-shadeprimary-dark
primary light--f7-theme-color-tintprimary-light

Let's say our theme color is #ff6b22, we need to set the following:

In app.css (to configure Framework7 theme color):

:root {
  --f7-theme-color: #ff6b22;
  --f7-theme-color-rgb: 255, 107, 34;
  --f7-theme-color-shade: #f85200;
  --f7-theme-color-tint: #ff864b;
}

And in tailwind.config.js (to configure Konsta UI theme color):

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

module.exports = konstaConfig({
  content: ['./src/**/*.{js,jsx,ts,tsx}', './src/index.html'],
  darkMode: 'class',
  // extend primary color
  theme: {
    extend: {
      colors: {
        primary: {
          light: '#ff864b',
          DEFAULT: '#ff6b22',
          dark: '#f85200',
        },
      },
    },
  },
});
Code licensed under MIT.
2022 © Konsta UI by nolimits4web.