beta This component version is ready to be used but is still in active development.

Checkbox form

An input that shows the site visitor if the option is selected or not.

github location npm version

Usage

Checkboxes can be used when a site visitor to select one or more options from a list in a form.

When to use

Use the .vf-form__checkbox when you want the site visitor to select 0, 1 or multiple options from the predefined list of checkboxes.

When not to use

Do not use the .vf-form__checkbox when you need a site visitor to only pick one option from the available selection. For this use case you should use the vf-radio button component.

Implementation

A vf-form__checkbox must be accompanied by a vf-form__label inside a vf-form__item with the vf-form__item--checkbox variant.

You can also use the vf-form__helper to add some more descriptive, explanitory text under the vf-form__label. See the examples for the correct markup.

The .vf-form__checkbox needs to grouped into a vf-form__fieldset and using the vf-form__label to help the site visitor understand what they are choosing for.

Generally the vf-form__fieldset will use the vf-stack layout component to stack the vf-form__checkbox on top of each other. You can set these to be inline by using the vf-cluster layout component as well. See the examples for the correct markup.

Variants

Nunjucks syntax

Depending on your environment you'll want to use render or include. As a rule of thumb: server-side use include, precompiled browser use render. If you're using vf-eleventy you should use include.

Using include

You'll need to pass a context object from your code or Yaml file (example), as well as the path to the Nunjucks template. Nunjucks' include is an abstraction of render and provides some additional portability.


{% set context fromYourYamlFile %}
- or -
{% set context = {
  "component-type": "form",
  "checkbox_legend": {
    "legend__text": "Pick your favourite(s)."
  },
  "required": true,
  "checkbox_label": {
    "form__label": "By selecting this checkbox you agree you like pizza.",
    "form__label_for": "commit-to-newsletters"
  },
  "checkbox_group": "agreement",
  "checkbox_value": "pizza-agreement"
}
 %}
{% include "../path_to/vf-form__checkbox/vf-form__checkbox.njk" %}
                

Using render

This approach is best for bare-bones Nunjucks environments, such as precompiled templates with the Nunjucks slim runtime where include is not be available.

{% render '@vf-form__checkbox', {
  "component-type": "form",
  "checkbox_legend": {
    "legend__text": "Pick your favourite(s)."
  },
  "required": true,
  "checkbox_label": {
    "form__label": "By selecting this checkbox you agree you like pizza.",
    "form__label_for": "commit-to-newsletters"
  },
  "checkbox_group": "agreement",
  "checkbox_value": "pizza-agreement"
} %}
                
HTML
<div class="vf-form__item vf-form__item--checkbox">
  <input type="checkbox" name="agreement" value="pizza-agreement" id="commit-to-newsletters" class="vf-form__checkbox">
  <label for="commit-to-newsletters" class="vf-form__label">By selecting this checkbox you agree you like pizza.</label>
</div>
              
Pick your favourite(s).

Some text to help with things

You didn't pick Strawberries?

Nunjucks syntax

Depending on your environment you'll want to use render or include. As a rule of thumb: server-side use include, precompiled browser use render. If you're using vf-eleventy you should use include.

Using include

You'll need to pass a context object from your code or Yaml file (example), as well as the path to the Nunjucks template. Nunjucks' include is an abstraction of render and provides some additional portability.


{% set context fromYourYamlFile %}
- or -
{% set context = {
  "component-type": "form",
  "checkbox_legend": {
    "legend__text": "Pick your favourite(s)."
  },
  "item_01": {
    "checkbox_value": "apples",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Apples",
      "form__label_for": "fruit_01"
    }
  },
  "item_02": {
    "checkbox_value": "bums",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Bananas",
      "form__label_for": "fruit_02"
    },
    "checkbox_helper": {
      "helper_text": "Some text to help with things"
    }
  },
  "item_03": {
    "checkbox_value": "mangos",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Mangos",
      "form__label_for": "fruit_03"
    }
  },
  "item_04": {
    "checkbox_value": "oranges",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Oranges",
      "form__label_for": "fruit_04"
    }
  },
  "item_05": {
    "checkbox_value": "pears",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Pears",
      "form__label_for": "fruit_05"
    },
    "disabled": true
  },
  "item_06": {
    "checkbox_value": "strawberries",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Strawberries",
      "form__label_for": "fruit_06",
      "required": true
    },
    "invalid": true,
    "checkbox_helper": {
      "error": true,
      "helper_text": "You didn't pick Strawberries?"
    }
  }
}
 %}
{% include "../path_to/vf-form__checkbox/vf-form__checkbox.njk" %}
                

Using render

This approach is best for bare-bones Nunjucks environments, such as precompiled templates with the Nunjucks slim runtime where include is not be available.

{% render '@vf-form__checkbox', {
  "component-type": "form",
  "checkbox_legend": {
    "legend__text": "Pick your favourite(s)."
  },
  "item_01": {
    "checkbox_value": "apples",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Apples",
      "form__label_for": "fruit_01"
    }
  },
  "item_02": {
    "checkbox_value": "bums",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Bananas",
      "form__label_for": "fruit_02"
    },
    "checkbox_helper": {
      "helper_text": "Some text to help with things"
    }
  },
  "item_03": {
    "checkbox_value": "mangos",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Mangos",
      "form__label_for": "fruit_03"
    }
  },
  "item_04": {
    "checkbox_value": "oranges",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Oranges",
      "form__label_for": "fruit_04"
    }
  },
  "item_05": {
    "checkbox_value": "pears",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Pears",
      "form__label_for": "fruit_05"
    },
    "disabled": true
  },
  "item_06": {
    "checkbox_value": "strawberries",
    "checkbox_group": "fruits",
    "checkbox_label": {
      "form__label": "Strawberries",
      "form__label_for": "fruit_06",
      "required": true
    },
    "invalid": true,
    "checkbox_helper": {
      "error": true,
      "helper_text": "You didn't pick Strawberries?"
    }
  }
} %}
                
HTML
<fieldset class="vf-form__fieldset vf-stack vf-stack--400">
  <legend class="vf-form__legend">Pick your favourite(s).</legend>


  <div class="vf-form__item vf-form__item--checkbox">
    <input type="checkbox" name="fruits" value="apples" id="fruit_01" class="vf-form__checkbox">
    <label for="fruit_01" class="vf-form__label">Apples</label>
  </div>


  <div class="vf-form__item vf-form__item--checkbox">
    <input type="checkbox" name="fruits" value="bums" id="fruit_02" class="vf-form__checkbox">
    <label for="fruit_02" class="vf-form__label">Bananas</label>

    <p class="vf-form__helper">Some text to help with things</p>
  </div>


  <div class="vf-form__item vf-form__item--checkbox">
    <input type="checkbox" name="fruits" value="mangos" id="fruit_03" class="vf-form__checkbox">
    <label for="fruit_03" class="vf-form__label">Mangos</label>
  </div>


  <div class="vf-form__item vf-form__item--checkbox">
    <input type="checkbox" name="fruits" value="oranges" id="fruit_04" class="vf-form__checkbox">
    <label for="fruit_04" class="vf-form__label">Oranges</label>
  </div>


  <div class="vf-form__item vf-form__item--checkbox">
    <input type="checkbox" name="fruits" value="pears" id="fruit_05" class="vf-form__checkbox" disabled>
    <label for="fruit_05" class="vf-form__label">Pears</label>
  </div>


  <div class="vf-form__item vf-form__item--checkbox">
    <input type="checkbox" name="fruits" value="strawberries" id="fruit_06" class="vf-form__checkbox vf-form__checkbox--invalid">
    <label for="fruit_06" class="vf-form__label | vf-form__label--required">Strawberries<span class="vf-u-sr-only">field is required.</span>
      <svg class="vf-icon vf-icon--asterick" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
        <title>asterick</title>
        <path d="M23.555,8.729a1.505,1.505,0,0,0-1.406-.98H16.062a.5.5,0,0,1-.472-.334L13.405,1.222a1.5,1.5,0,0,0-2.81,0l-.005.016L8.41,7.415a.5.5,0,0,1-.471.334H1.85A1.5,1.5,0,0,0,.887,10.4l5.184,4.3a.5.5,0,0,1,.155.543L4.048,21.774a1.5,1.5,0,0,0,2.31,1.684l5.346-3.92a.5.5,0,0,1,.591,0l5.344,3.919a1.5,1.5,0,0,0,2.312-1.683l-2.178-6.535a.5.5,0,0,1,.155-.543l5.194-4.306A1.5,1.5,0,0,0,23.555,8.729Z" />
      </svg>
    </label>

    <p class="vf-form__helper vf-form__helper--error">You didn't pick Strawberries?</p>
  </div>

</fieldset>
              
Pick your favourite(s).
Nunjucks syntax

Depending on your environment you'll want to use render or include. As a rule of thumb: server-side use include, precompiled browser use render. If you're using vf-eleventy you should use include.

Using include

You'll need to pass a context object from your code or Yaml file (example), as well as the path to the Nunjucks template. Nunjucks' include is an abstraction of render and provides some additional portability.


{% set context fromYourYamlFile %}
- or -
{% set context = {
  "component-type": "form",
  "checkbox_legend": {
    "legend__text": "Pick your favourite(s)."
  },
  "inline": true,
  "item_01": {
    "checkbox_value": "walking",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Walking",
      "form__label_for": "travel_01"
    },
    "checkbox_helper": {
      "helper_text": "Some text to help with things"
    }
  },
  "item_02": {
    "checkbox_value": "bike",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Bike",
      "form__label_for": "travel_02"
    }
  },
  "item_03": {
    "checkbox_value": "car",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Car",
      "form__label_for": "travel_03"
    }
  },
  "item_04": {
    "checkbox_value": "bus",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Bus",
      "form__label_for": "travel_04"
    }
  },
  "item_05": {
    "checkbox_value": "train",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Train",
      "form__label_for": "travel_05"
    },
    "disabled": true
  },
  "item_06": {
    "required": true,
    "checkbox_value": "airplane",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Airplane",
      "form__label_for": "travel_06",
      "required": true
    },
    "invalid": true
  }
}
 %}
{% include "../path_to/vf-form__checkbox/vf-form__checkbox.njk" %}
                

Using render

This approach is best for bare-bones Nunjucks environments, such as precompiled templates with the Nunjucks slim runtime where include is not be available.

{% render '@vf-form__checkbox', {
  "component-type": "form",
  "checkbox_legend": {
    "legend__text": "Pick your favourite(s)."
  },
  "inline": true,
  "item_01": {
    "checkbox_value": "walking",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Walking",
      "form__label_for": "travel_01"
    },
    "checkbox_helper": {
      "helper_text": "Some text to help with things"
    }
  },
  "item_02": {
    "checkbox_value": "bike",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Bike",
      "form__label_for": "travel_02"
    }
  },
  "item_03": {
    "checkbox_value": "car",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Car",
      "form__label_for": "travel_03"
    }
  },
  "item_04": {
    "checkbox_value": "bus",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Bus",
      "form__label_for": "travel_04"
    }
  },
  "item_05": {
    "checkbox_value": "train",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Train",
      "form__label_for": "travel_05"
    },
    "disabled": true
  },
  "item_06": {
    "required": true,
    "checkbox_value": "airplane",
    "checkbox_group": "travel",
    "checkbox_label": {
      "form__label": "Airplane",
      "form__label_for": "travel_06",
      "required": true
    },
    "invalid": true
  }
} %}
                
HTML
<fieldset class="vf-form__fieldset | vf-stack vf-stack--400">
  <legend class="vf-form__legend">Pick your favourite(s).</legend>
  <div class="vf-cluster vf-cluster--400">
    <div class="vf-cluster__inner">

      <div class="vf-form__item vf-form__item--checkbox">
        <input type="checkbox" name="travel" value="walking" id="travel_01" class="vf-form__checkbox">
        <label for="travel_01" class="vf-form__label">Walking</label>
      </div>


      <div class="vf-form__item vf-form__item--checkbox">
        <input type="checkbox" name="travel" value="bike" id="travel_02" class="vf-form__checkbox">
        <label for="travel_02" class="vf-form__label">Bike</label>
      </div>


      <div class="vf-form__item vf-form__item--checkbox">
        <input type="checkbox" name="travel" value="car" id="travel_03" class="vf-form__checkbox">
        <label for="travel_03" class="vf-form__label">Car</label>
      </div>


      <div class="vf-form__item vf-form__item--checkbox">
        <input type="checkbox" name="travel" value="bus" id="travel_04" class="vf-form__checkbox">
        <label for="travel_04" class="vf-form__label">Bus</label>
      </div>


      <div class="vf-form__item vf-form__item--checkbox">
        <input type="checkbox" name="travel" value="train" id="travel_05" class="vf-form__checkbox" disabled>
        <label for="travel_05" class="vf-form__label">Train</label>
      </div>


      <div class="vf-form__item vf-form__item--checkbox">
        <input type="checkbox" name="travel" value="airplane" id="travel_06" class="vf-form__checkbox vf-form__checkbox--invalid">
        <label for="travel_06" class="vf-form__label | vf-form__label--required">Airplane<span class="vf-u-sr-only">field is required.</span>
          <svg class="vf-icon vf-icon--asterick" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <title>asterick</title>
            <path d="M23.555,8.729a1.505,1.505,0,0,0-1.406-.98H16.062a.5.5,0,0,1-.472-.334L13.405,1.222a1.5,1.5,0,0,0-2.81,0l-.005.016L8.41,7.415a.5.5,0,0,1-.471.334H1.85A1.5,1.5,0,0,0,.887,10.4l5.184,4.3a.5.5,0,0,1,.155.543L4.048,21.774a1.5,1.5,0,0,0,2.31,1.684l5.346-3.92a.5.5,0,0,1,.591,0l5.344,3.919a1.5,1.5,0,0,0,2.312-1.683l-2.178-6.535a.5.5,0,0,1,.155-.543l5.194-4.306A1.5,1.5,0,0,0,23.555,8.729Z" />
          </svg>
        </label>
      </div>

    </div>
  </div>
</fieldset>
              

Examples

Installation info

This component is distributed with npm. After installing npm, you can install the .vf-form__checkbox with this command.

$ yarn add --dev @visual-framework/vf-form__checkbox

Sass/CSS

The source files included are written in Sass(scss). You can point your Sass include-path at your node_modules directory and import it like this.

@import "@visual-framework/vf-form__checkbox/index.scss";

Make sure you import Sass requirements along with the modules. You can use a project boilerplate or the vf-sass-starter

Changelog

Changelog

3.0.0-alpha.0

  • reworks the inputs to make more use of nunjucks
  • restyles the inputs to match latest design direction
  • adds better documentation

2.0.2

  • Fixes CSS to match stylelint rules.
  • https://github.com/visual-framework/vf-core/pull/1405

2.0.1

  • changes any set- style functions to cleaner version

2.0.0

  • Changes hover/focus interaction.
  • Adds inline example.
  • https://github.com/visual-framework/vf-core/pull/1228/

1.1.0

  • the vf-form__checkbox is now used in more than forms so we have added some 'context' styling for when it is in a form

1.0.0

  • moves vf-form__checkbox--inline into it's own .njk file
  • tidies up alignment of label and checkbox

Assets



File system location: components/vf-form__checkbox

Find an issue on this page? Propose a change or discuss it.