Inputs

SliderPro

FormKit Pro Quick Installation Guide 🚀

Introduction

The slider input is an advanced and highly configurable version of the native HTML range input. Some notable additions compared to the standard range input are:

  • Support for multi-value inputs.
  • Display tooltips to show selected values.
  • Show linked inputs for min and max values.
  • Render tick marks for value steps.
  • Add arbitrary tick marks.
  • Force selection to snap to tick marks.
  • Render bar chart data linked to selection values.

Slider Overview

7 mins

Basic example

<FormKit  type="slider"  label="A very basic slider"  help="Out of the box I'm not much different than a range input."/>
Out of the box I'm not much different than a range input.
  • 50

Native props

if you're familar with using the native HTML range input you'll find than many of the existing props you're familiar with work as expected with the slider input.

<FormKit  type="slider"  label="Native Props"  value="500"  min="100"  max="1000"  step="25"  help="Feels natural"/>
Feels natural
  • 500

Keyboard modifier keys

Even in its simplest form the slider input has a few tricks up its sleeve. Just like the native range input you can use your arrow keys to adjust the input value when it is focused — but with the slider input you can hold the shift key to increment by 10x and your system's modifer key (command or ctrl) to jump to the min / max values.

Keyboard number entry

Whenever you have a handle focused on a slider input you can type a value on your keyboard and after a slight debounce your slider handle will be set to the value you provided — or the closest available match.

Multi-value

To render two value handles on a slider input supply an array as the input's value. Control handles can be dragged through each other and your resulting array value will always be returned sorted in order from smallest to largest.

<FormKit  type="slider"  name="slider"  label="Multiple Values"  :value="[50, 350]"  min="0"  max="500"  help="I now render two control handles"/>
I now render two control handles
  • 50
  • 350

Customizing handles

Using the maxHandleInner (default) and minHandleInner slots you can customize the appearance of your slider handles. The maxHandleInner slot is used for single-value slider inputs.

<template>  <FormKit    type="slider"    label="A slider with styled marks and a custom handle"    value="50"    handle-class="!w-auto !h-auto"  >    <template #handleMaxInner>      <div class="p-2">        <svg xmlns="http://www.w3.org/2000/svg" height="1em" style="display: block;" viewBox="0 0 512 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M406.6 374.6l96-96c12.5-12.5 12.5-32.8 0-45.3l-96-96c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L402.7 224l-293.5 0 41.4-41.4c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-96 96c-12.5 12.5-12.5 32.8 0 45.3l96 96c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 288l293.5 0-41.4 41.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0z"/></svg>      </div>    </template>  </FormKit></template>
  • 50

Customizing tooltips

Tooltip visibility

By default slider tooltips show on hover and when a selection handle is being dragged. You can force tooltips into an on or off state by using the tooltip prop and providing a boolean value.

<FormKit  type="slider"  label="Default Tooltip Visibility"  value="25"  help="I show my tooltip on hover and drag"/><FormKit  type="slider"  label="Force Show Tooltip"  value="50"  tooltip="true"  help="I always show my tooltip"/><FormKit  type="slider"  label="Force Hide Tooltip"  value="75"  tooltip="false"  help="I never show my tooltip. I'm basically a native range input"/>
I show my tooltip on hover and drag
  • 25
I always show my tooltip
  • 50
I never show my tooltip. I'm basically a native range input

Tooltip formating

By providing a function to the tooltip-format prop you can customize the value of your slider tooltip. Your tooltip-format function will be provided two arguments:

value: The current value that would be displayed in the tooltip by default. handle: The current handle being affected by the funciton. Either min or max.

<FormKit  type="slider"  label="Default Tooltip Visibility"  :value="[60, 100]"  :tooltip-format="    (value, handle) => {      return `${handle === 'min' ? 'Min' : 'Max'}: ${value}`    }  "  tooltip="true"  help="I apply custom formatting to my tooltips"/>
I apply custom formatting to my tooltips
  • Min: 60
  • Max: 100

By using the tooltip-format prop you can introduce variety to your slider inputs — especially when combined with FormKit's section-key class prop system.

<FormKit  type="slider"  label="About how large should the spacing be?"  help="This is a rough estimate."  :value="[25, 75]"  :tooltip-format="(value, handle) => (handle === 'min' ? '👉' : '👈')"  tooltip-class="large"/><FormKit  type="slider"  label="What do you think of FormKit?"  help="be honest."  value="80"  :tooltip-format="    (value) => {      if (value < 25) {        return '😡'      } else if (value < 50) {        return '😐'      } else if (value < 75) {        return '😀'      } else if (value < 100) {        return '😍'      } else if (value === 100) {        return '🦄'      }    }  "  tooltip="true"  :fill-class="emotionClass"  tooltip-class="large"/>
This is a rough estimate.
  • 👉
  • 👈
be honest.
  • 😍

Linked number inputs

Sometimes a range-style input is more useful for users if they're also able to directly enter numeric values in linked fields. You can reveal number inputs that are bound to the values of the slider range handles by setting the show-input prop to true.

<FormKit  type="slider"  value="33"  label="A single slider with a visible input"  help="I have a single linked input"  show-input/><FormKit  type="slider"  :value="[33, 66]"  label="A slider with multiple inputs"  show-input/>
I have a single linked input
  • 33
  • 33
  • 66

Linked input attributes

The linked number inputs are FormKit inputs. You can apply props to them using the provided props:

  • input-attrs: will bind your provided object to both the min and max inputs.
  • min-input-attrs: Object to bind to the linked min input.
  • max-input-attrs: Object to bind to the linked max input.
<FormKit  type="slider"  max="1000"  :value="[300, 800]"  label="Linked input attrs"  show-input  :input-attrs="{    sectionsSchema: {      prefix: {        $el: 'span',        children: '&dollar;',      },    },  }"  :min-input-attrs="{    label: 'Minimum Price',  }"  :max-input-attrs="{    label: 'Maximum Price',  }"/>
  • 300
  • 800
$
$

Marks

Basic usage

The slider input supports the rendering of marks on the input's track. By setting the marks property to true marks will be rendered at every steps interval.

<FormKit  type="slider"  label="I show marks on my track"  help="By default, marks are rendered at every step interval."  value="50"  step="10"  marks/>
By default, marks are rendered at every step interval.
  • 50

With labels

You can enable labels for your marks by setting the mark-labels prop to true.

<FormKit  type="slider"  label="I show marks on my track with labels"  help="Labels can be enabled via prop."  value="50"  step="10"  marks  mark-labels/>
Labels can be enabled via prop.
0102030405060708090100
  • 50

Custom marks

By supplying an array of objects to the marks prop instead of a boolean you can place arbirtary marks on the slider's track. Mark objects shold have the folloing properties:

  • at: The position (value) on the track where the mark should render.
  • label: The label to render for the mark if the mark-labels prop is enabled.
  • class: A string of classes to apply to the mark element.
  • labelClasses: A string of classes to apply to the mark label element.
<FormKit  type="slider"  label="I render custom marks"  value="50"  :marks="[    { at: 0, label: '0°C' },    { at: 20, label: '20°C' },    { at: 37, label: '37°C' },    { at: 100, label: '100°C' },  ]"  mark-labels/>
0°C20°C37°C100°C
  • 50

Snap to marks

When supplying custom marks you may want to force the selection to snap to the provided marks. This can be achieved with the snap-to-marks prop. When snap-to-marks is true keyboard events will jump to the next closest mark and entered values through keyboard or linked inputs will snap to the nearest available mark.

<FormKit  type="slider"  label="I render custom marks"  value="37"  :marks="[    { at: 0, label: '0°C' },    { at: 20, label: '20°C' },    { at: 37, label: '37°C' },    { at: 100, label: '100°C' },  ]"  mark-labels  snap-to-marks/>
0°C20°C37°C100°C
  • 37

Styling marks

You can apply custom classes to your marks and mark labels by using the class and labelClasses properties on your mark objects.

const styledMarks = [  {    at: 0,    label: '0',    class: 'tiny',    labelClass: 'red',  },  {    at: 25,    label: '25',    class: 'small',    labelClass: 'orange',  },  {    at: 50,    label: '50',    class: 'normal',    labelClass: 'yellow',  },  {    at: 75,    label: '75',    class: 'large',    labelClass: 'blue-green',  },  {    at: 100,    label: '100',    class: 'xlarge',    labelClass: 'green',  },]
0255075100
  • 90

Scaling slider values

By default the slider input scales values linearly across its entire range. However, you can use the provided scaling-function or intervals props to change this behavior.

Using functions

The scaling-function prop accepts 3 values:

  • The string linear — this is the default behavior
  • The string log which will apply a logarithmic scaling function to your slider
  • An object which consits of two functions named forward and reverse which will apply your own scaling logic.
scaling-functions
scaling-utils
<FormKit  v-model="scalingValue"  type="slider"  label="Scaling: Linear"  scaling-function="linear"  v-bind="sharedProps"/><FormKit  v-model="scalingValue"  type="slider"  label="Scaling: Logarithmic"  scaling-function="log"  v-bind="sharedProps"/><FormKit  v-model="scalingValue"  type="slider"  label="Scaling: Custom Quadratic"  :scaling-function="customQuad"  v-bind="sharedProps"/><FormKit  v-model="scalingValue"  type="slider"  label="Scaling: Custom Sine Wave"  :scaling-function="customSine"  v-bind="sharedProps"/>
export const customQuad = {  forward: (value, min, max) => {    return Math.sqrt(value / max) * max  },  reverse: (percentage, min, max) => {    return percentage ** 2 / max  },}export const customSine = {  forward: (value, min, max) => {    return max * (Math.asin(value / 50 - 1) / Math.PI + 0.5)  },  reverse: (percentage, min, max) => {    return 50 * (Math.sin((Math.PI * (percentage - max / 2)) / max) + 1)  },}

Using Intervals

The intervals prop accepts an array of object where each object defines an interval range on the slider. Each obect contains:

  • value: The point on the slider at which the interval begins
  • step: The step size that should be used within the interval

Your last interval will cover the range extending from your specified value to the max prop value on your slider. If your lowest interval does not begin at the minimum possible value of your slider then an interval covering the range from your min slider prop to your first interval will be created for you. Its step value will be the value of the step prop on your slider input.

It's important to note that the visual representation of intervals on a slider track is based on the total number of possible steps within a given interval and not the interval's raw numeric value.

<script setup>const intervals = [  { value: 0, step: 10 },  { value: 100, step: 50 },  { value: 800, step: 100 },]const marks = [  { at: 0, label: '0' },  { at: 100, label: '100' },  { at: 800, label: '800' },  { at: 3000, label: '3000' },]</script><template>  <FormKit    type="slider"    label="Scaling: Interval"    :value="100"    :intervals="intervals"    :marks="marks"    mark-labels    min="0"    max="3000"  /></template>
01008003000
  • 100

Chart

You can render a bar chart above the slider with arbitrary values by suppling an array to the chart prop. The chart array has a similar structure to the marks array. Each nested object should contain:

  • at: The position (value) on the track where the bar for the chart should render.
  • value: A numeric value that will be used to calculate the relative height of the bar in the chart.

The slider input will iterate over you chart array and determine the largest value contained in the array, and then divide all other values by that largest value to determine the relative heights for rendering the bars of the chart.

<FormKit  type="slider"  label="I render a bar chart above my track"  value="30"  :chart="[    { at: 0, value: 10 },    { at: 10, value: 15 },    { at: 20, value: 20 },    { at: 30, value: 25 },    { at: 40, value: 30 },    { at: 50, value: 35 },    { at: 60, value: 30 },    { at: 70, value: 25 },    { at: 80, value: 15 },    { at: 90, value: 5 },  ]"/>
  • 30

By combining the provided props for the slider component you can create a robust input to cover a variety of usecases. Here is an example for a price-range slider.

<FormKit  type="slider"  label="Choose your price range"  :value="[800, 1200]"  :tooltip-format="(v) => `&dollar;${v}`"  tooltip="true"  min="0"  max="2000"  show-input  step="25"  :input-attrs="{    floatingLabel: true,    prefixIcon: 'dollar',  }"  :min-input-attrs="{    label: 'Minimum',  }"  :max-input-attrs="{    label: 'Maximum',  }"  :marks="[    { at: 0, label: '$0' },    { at: 500, label: '$500' },    { at: 1000, label: '$1000' },    { at: 1500, label: '$1500' },    { at: 2000, label: '$2000' },  ]"  mark-labels  :chart="chartData"/>

Props & Attributes

PropType Default Description
chartarraynoneAn array of objects containing at and value keys. Renders a relative-height bar-chart above the slider track.
input-attrsobject{}Applies custom attributes to the nested linked FormKit inputs for both min and max values.
intervalsarraynoneAn array of interval values consisting of value and step used to define differing scaling for different sections of a slider track.
marksboolean || arrayfalseWhen true, renders marks on the track at every step value. When an Array of objects with at and label keys, renders a mark on the track at every object's at value.
mark-labelsbooleanfalseEnables rendering of labels belowe marks on track
maxnumber100The maximum possible value of the slider range.
max-input-attrsobject{}Applies custom attributes to the nested linked FormKit input for the max value.
minnumber0The minimum possible value of the slider range.
min-input-attrsobject{}Applies custom attributes to the nested linked FormKit input for the min value.
prefixstringnoneCharacters that will always appear at the beginning of the input.
scaling-functionstring || objectlinearDefines scaling behavior for values on the rendered slider track using either a preset value string or a set of custom functions.
show-inputbooleanfalseRenders FormKit type="number" input(s) that are linked to the slider's value(s)
snap-to-marksbooleanfalseWhen enabled, forces user selection to snap to marks
stepnumber1The minimum value at which the slider value can increment / decrement.
suffixstringnoneCharacters that will always appear at the end of the input.
tooltipbooleannoneWhen set with a boolean value, forces the display of the slider's handle tooltip(s).
tooltip-formatfunctionnoneA function that is provided the value and handle for each slider handle which should return a string.
Show Universal props
configObject{}Configuration options to provide to the input’s node and any descendent node of this input.
delayNumber20Number of milliseconds to debounce an input’s value before the commit hook is dispatched.
dirtyBehaviorstringtouchedDetermines how the "dirty" flag of this input is set. Can be set to touched or comparetouched (the default) is more performant, but will not detect when the form is once again matching its initial state.
errorsArray[]Array of strings to show as error messages on this field.
helpString''Text for help text associated with the input.
idStringinput_{n}The unique id of the input. Providing an id also allows the input’s node to be globally accessed.
ignoreBooleanfalsePrevents an input from being included in any parent (group, list, form etc). Useful when using inputs for UI instead of actual values.
indexNumberundefinedAllows an input to be inserted at the given index if the parent is a list. If the input’s value is undefined, it inherits the value from that index position. If it has a value it inserts it into the lists’s values at the given index.
labelString''Text for the label element associated with the input.
nameStringinput_{n}The name of the input as identified in the data object. This should be unique within a group of fields.
parentFormKitNodecontextualBy default the parent is a wrapping group, list or form — but this props allows explicit assignment of the parent node.
prefix-iconString''Specifies an icon to put in the prefixIcon section.
preserveBooleanfalsePreserves the value of the input on a parent group, list, or form when the input unmounts.
preserve-errorsBooleanfalseBy default errors set on inputs using setErrors are automatically cleared on input, setting this prop to true maintains the error until it is explicitly cleared.
sections-schemaObject{}An object of section keys and schema partial values, where each schema partial is applied to the respective section.
suffix-iconString''Specifies an icon to put in the suffixIcon section.
typeStringtextThe type of input to render from the library.
validationString, Array[]The validation rules to be applied to the input.
validation-visibilityStringblurDetermines when to show an input's failing validation rules. Valid values are blur, dirty, and live.
validation-labelString{label prop}Determines what label to use in validation error messages, by default it uses the label prop if available, otherwise it uses the name prop.
validation-rulesObject{}Additional custom validation rules to make available to the validation prop.
valueAnyundefinedSeeds the initial value of an input and/or its children. Not reactive. Can seed entire groups (forms) and lists..

Sections & slot data

You can target a specific section of an input using that section's "key", allowing you to modify that section's classes, HTML (via :sections-schema, or content (via slots)). Read more about sections here.

Choose your price range
Select available locations by price
📊
25
10
90
90
10
90
You must provide a rating.
Section-key Description
chartA container for the chart bar elements.
chartBarAn individual bar for the chart element.
fillThe fill for the selected range on the slider input.
handleMaxThe maximum value selection handle. This is the default handle for single-value slider inputs.
handleMinThe minimum value selection handle.
handlesA container for the track selection handles
linkedValuesA wrapper for the linked inputs for the slider’s minimum and maximum values.
markAn individual mark on the track.
markLabelThe label for an individual mark on the track.
marksA container for all of the marks on the track
maxValueThe linked FormKit number input for the slider’s maximum value. If the slider only has a single value this is rendered as a direct descendant of the sliderInner section.
minValueThe linked FormKit number input for the slider’s minimum value.
sliderInnerThe interior div that contains the slider input content.
trackA wrapper that contains all of the track markup for the slider input.
trackInnerAn interior wrapper for styling purposes that contains all of the track markup excluding the chart.
trackWrapperA wrapper that contains all of the track markup excluding the chart.
tooltipMinThe tooltip for the minimum value selection handle
tooltipMaxThe tooltip for the maximum value selection handle
Show Universal section keys
outerThe outermost wrapping element.
wrapperA wrapper around the label and input.
labelThe label of the input.
prefixHas no output by default, but allows content directly before an input element.
prefixIconAn element for outputting an icon before the prefix section.
innerA wrapper around the actual input element.
suffixHas no output by default, but allows content directly after an input element.
suffixIconAn element for outputting an icon after the suffix section.
inputThe input element itself.
helpThe element containing help text.
messagesA wrapper around all the messages.
messageThe element (or many elements) containing a message — most often validation and error messages.

Accessibility

All FormKit inputs are designed with the following accessibility considerations in mind. Help us continually improve accessibility for all by filing accessibility issues here:

  • Semantic markup
  • ARIA attributes
  • Keyboard accessibility
  • Focus indicators
  • Color contrast with the provided theme
  • Accessible labels, help text, and errors

Accessibility attributes

Section KeyAttributeValueDescription
maxValuetabindex-1Prioritizes keyboard focus order by setting it to -1 when input is disabled.
minValuetabindex-1Prioritizes keyboard focus order by setting it to -1 when input is disabled.
handleMaxtabindex-1 or 0Prioritizes keyboard focus order by setting it to -1 when input is disabled and 0 when its not.
handleMintabindex-1 or 0Prioritizes keyboard focus order by setting it to -1 when input is disabled and 0 when its not.
Universal Accessibility Attributes
labellabelforAssociates a label to an input element. Users can click on the label to focus the input or to toggle between states.
inputinputdisabledDisables an HTML element, preventing user interaction and signaling a non-interactive state.
aria-describedbyAssociates an element with a description, aiding screen readers.
aria-requiredAdded when input validation is set to required.
iconiconforLinks icon to input element when icon in rendered as a label.

Keyboard Interactions

Keyboard EventDescription
Adjust the input value of the active handle up by the current step.
Adjust the input value of the active handle up by the current step.
Adjust the input value of the active handle down by the current step.
Adjust the input value of the active handle down by the current step.
Shift+Adjust the input value of the active handle up by 10x the current step.
Shift+Adjust the input value of the active handle up by 10x the current step.
Shift+Adjust the input value of the active handle down by 10x the current step.
Shift+Adjust the input value of the active handle down by 10x the current step.
Page UpAdjust the input value of the active handle up by 10x the current step.
Page DownAdjust the input value of the active handle down by 10x the current step.
HomeSet the input value of the active handle to the min value.
EndSet the input value of the active handle to the max value.
Universal Keyboard Events
TabMoves the focus to the next focusable element on the page.
Shift+TabMoves the focus to the previous focusable element on the page.