Konsta UI & Framework7

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

Create Framework7 Project

First, create a Framework7 Svelte 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 folowing content:

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

module.exports = konstaConfig({
  content: ['./src/**/*.{js,jsx,ts,tsx,svelte}', './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.svelte:

<script>
  import { KonstaProvider } from 'konsta/svelte';
  import { App /* ... */ } from 'framework7-svelte';

  const f7parms = {
    theme: 'ios',
    // ...
  };
</script>

<!-- Wrap Framework7's App with KonstaProvider with theme="parent" -->
<KonstaProvider theme="parent">
  <App {...f7params}> ... </App>
</KonstaProvider>

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/svelte)

Example Page

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

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

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

<!-- 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 class="flex space-x-4">
    <Button>Button 1</Button>
    <Button>Button 2</Button>
  </Block>
</Page>

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 (version 6) requires class theme-dark. Framework7 version 7 requires same dark class for dark mode.

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.
2021 © Konsta UI by nolimits4web.