FormKit ships robust and accessible markup — but with no assumptions about your
desired styles. There is an optional base theme (as seen in these docs)
called Genesis
that you can use in your projects.
To load genesis
via CDN, supply it to the theme
property of your defaultConfig
.
...
defaultConfig({
theme: 'genesis' // will load from CDN and inject into document head
})
...
To install Genesis, first install the @formkit/themes
package.
npm install @formkit/themes
Then in your main.js
(wherever you boot Vue up) include the Genesis theme.css
(this assumes you are using a build tool like Vite, Webpack, Snowpack, or Nuxt):
import '@formkit/themes/genesis'
For styling purposes some attributes are automatically added to and removed from the outer
section of all FormKit inputs:
data-type
— The type of input, text
, select
, checkbox
etc.data-multiple
— For inputs that accept the multiple
attribute, this will be appended when the input has the multiple attribute (like the select
input).data-disabled
— Present when an input is disabled.data-complete
— Present when the input is "complete". Intended to be used for styling the input when a user has completed filling out the input (like a green checkmark). Read about context.state.complete
for information what conditions cause this to be true
.data-invalid
— Present when the input has failing validation rules and the messages for the failing rules are visible.data-errors
— Present when the input has explicitly set errors.You can use the above attributes to easily provide realtime visual feedback for users filling out your forms:
Most users will want to apply their own styles and classes to FormKit's provided markup. FormKit provides numerous methods to apply classes for your project.
Classes can be modified for all sections using any of the following methods (from highest to lowest specificity):
{section-key}-class
props. (most specific)classes
prop.classes
configuration option.rootClasses
configuration function. (least specific)The classes follow a strict hierarchy. Initially, classes are produced by the rootClasses
function. They can then be modified by the classes
configuration option, then by the classes
prop, and finally by the {section-key}-class
prop. At each of these stages classes can be appended, reset, or selectively modified.
To append a class, simply return the string you want to append, or provide an object of classes with boolean values — true
properties will be appended:
Classes produced by all earlier hierarchy steps can be completely removed by providing a special (not rendered) class $reset
in either string format or object format:
Classes produced by an earlier step in the class hierarchy can be selectively removed by providing an object with the value false
for the class you want to remove or by providing a class name to a {section-key}-class
prop that starts with $remove:
and matches an existing class in the class list. This includes removing formkit's default formkit-
prefixed classes:
In addition to the four methods listed above, more generalized overrides are also available, like overriding an input’s schema, using the classes
node hook, or utilizing slots:
The simplest way to modify the classes of an element inside a FormKit input is via the {section-key}-class
props. To add a class to a specific section element, like label
, you simply add the label-class
prop:
The classes prop is similar to the section-key class prop except it allows setting classes on all sections at the same time:
The classes configuration option is similar to the classes prop, except it applies to all inputs the configuration is applied to. FormKit's unique configuration system allows for you to apply classes globally on your project or just inputs within a certain group or form:
FormKit ships with a helper function called generateClasses
included in @formkit/themes
.
The generateClasses
function takes a javascript object keyed by input type with values of a sub-object keyed by ${sectionKey}
with values of strings. With this function you can quickly apply class lists to sections within inputs based on a given inputs' type.
rootClasses
is a configuration function that is responsible
for producing the default classes for each element. This function already has a default value which produces all the default classes (like formkit-outer
and formkit-label
) that ship with FormKit — so replacing this single function allows you to easily replace all initial classes. This makes it an ideal candidate for writing custom themes when using utility frameworks like Tailwind.
The rootClasses
function is passed 2 arguments (respectively):
label
or input
).The function will be called once for each section and it must return an object of classes with boolean values.
While typical usage of rootClasses
is at the global config level to apply
classes to your entire project - you can also use it with the config
prop to override
a specific form or input within your project with a class list computed from the logic
within your provided function:
Because rootClasses
is a configuration option, you can apply it per input, per group, or globally.
In addition to modifying classes via config or props on a <FormKit>
component, you can use the same techniques within schema:
Within schema, you can also modify the classes of an element inside an input via the {section-key}Class
properties. For example, to add a class to the label section, you can add the labelClass
property:
{
$formkit: 'text',
name: 'email',
// adds 'appended-class' to the "label" section
labelClass: 'appended-class'
},
Much like the classes prop on a <FormKit>
component, you can modify the class list for any section of an input with the classes
property on a schema node:
{
$formkit: 'text',
name: 'email',
// modifies classes on both the "outer" and "inner" sections of this input
classes: {
outer: 'new-outer-class',
inner: {
$reset: true, // resets classes on the "inner" section
'new-inner-class': true
}
},
},
Since config is passed down to descendant inputs, you can alter classes via config on a parent, such as a form
, list
, or a group
, and this will affect all descendants to any depth:
If you have not already, add Tailwind CSS to your project following their installation instructions
FormKit provides a Tailwind version of the Genesis theme you can use as a starting point in your own project. To use this pre-made Tailwind theme copy the following theme into a file (something like tailwind-theme.js
) in your project.
Now, import your Tailwind theme into your formkit.config
file and add it to your project's configuration.
You will need to import the generateClasses
helper function from the @formkit/themes
package as well as the supporting icons from the @formkit/icons
package.
npm install @formkit/themes @formkit/icons
// formkit.config.js
import { generateClasses } from '@formkit/themes'
import { genesisIcons } from '@formkit/icons'
import myTailwindTheme from './tailwind-theme.js' // change to your theme's path
export default {
icons: {
...genesisIcons,
},
config: {
classes: generateClasses(myTailwindTheme),
},
}
Next, add the path to your theme to your tailwind.config
file's content array — this is required so that Tailwind knows which classes you're using in your project.
Additionally you should add the FormKitVariants
plugin to your tailwind.config.js
from the @formkit/themes
package in order to make use of the FormKit-provided variants such as formkit-invalid:
in your project.
// tailwind.config.js
const FormKitVariants = require('@formkit/themes/tailwindcss')
module.exports = {
content: [
...
'./tailwind-theme.js',
],
plugins: [FormKitVariants],
}
Want to create your own Tailwind theme? We've written a guide walking through the process using both inline class props as well as using the FormKitVariants
plugin and the generateClasses
helper function from @formkit/themes
to create a global Tailwind theme.
The guide concludes with a complete reproduction of the FormKit Genesis CSS theme written in Tailwind.