Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pf-v6-checkbox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@patternfly/elements': minor
---

Adds `<pf-v6-checkbox>`, a checkbox with a shadow label, description and body slots, indeterminate state, controlled nested checkboxes, and native form submission, validation, and reset.
1 change: 1 addition & 0 deletions docs/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import '@patternfly/elements/pf-v5-text-area/pf-v5-text-area.js';
import '@patternfly/elements/pf-v5-text-input/pf-v5-text-input.js';
import '@patternfly/elements/pf-v5-tile/pf-v5-tile.js';
import '@patternfly/elements/pf-v5-tooltip/pf-v5-tooltip.js';
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';

import '@patternfly/elements/pf-v6-avatar/pf-v6-avatar.js';
import '@patternfly/elements/pf-v6-background-image/pf-v6-background-image.js';
Expand Down
138 changes: 138 additions & 0 deletions elements/pf-v6-checkbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# pf-v6-checkbox

A **checkbox** is used to select a single item or multiple items, typically to
choose elements to perform an action or to reflect a binary setting.

## Usage

```html
<pf-v6-checkbox label="Checkbox"></pf-v6-checkbox>
```

### Checked

```html
<pf-v6-checkbox label="Checkbox" checked></pf-v6-checkbox>
```

### Required

```html
<pf-v6-checkbox label="Checkbox" required></pf-v6-checkbox>
```

### With an external label

The element is a form-associated custom element, so it works with a native
`<label for="id">`. Do not combine an external label with the `label` attribute.

```html
<pf-v6-checkbox id="choice" name="choice"></pf-v6-checkbox>
<label for="choice">Choice</label>
```

### Description and body

Use `slot="description"` for descriptive text and the default slot for custom
body content. Nested checkboxes placed in the default slot are controlled by
their parent.

```html
<pf-v6-checkbox label="Checkbox">
<span slot="description">Description text</span>
<span>Body content</span>
</pf-v6-checkbox>
```

### Indeterminate

`indeterminate` is a JavaScript-only property, matching the native
`<input type="checkbox">` API. While indeterminate, the element exposes
`aria-checked="mixed"`.

```html
<pf-v6-checkbox id="partial" label="Partially selected"></pf-v6-checkbox>
<script type="module">
document.getElementById('partial').indeterminate = true;
</script>
```

### Reversed label

```html
<pf-v6-checkbox label="Checkbox" label-position="start"></pf-v6-checkbox>
```

### Form association

`<pf-v6-checkbox>` submits its `value` (default `"on"`) when checked, and is
omitted from the form data when unchecked. Form reset restores the initial
`checked` state.

```html
<form>
<pf-v6-checkbox name="choice" value="yes" label="Choice"></pf-v6-checkbox>
</form>
```

## Divergences from React `Checkbox`

### Intentionally omitted

| React prop | Notes |
|---|---|
| `defaultChecked` | React-only uncontrolled pattern; the `checked` attribute is the initial state, and form reset restores it |
| `isLabelWrapped` | Not needed: clicking the shadow label always toggles the checkbox |
| `component` | Not applicable to a custom element |
| `ouiaId` / `ouiaSafe` | React testing props; not part of the web component API |

### Changed API

| React prop | Web component | Difference |
|---|---|---|
| `label` | `label` attribute | Rendered as a shadow label. External `<label for>` is also supported |
| `id` | `id` attribute | Native attribute |
| `name` | `name` attribute | Native attribute |
| `value` | `value` property/attribute | Defaults to `"on"` |
| `isChecked` / `checked` | `checked` attribute/property | Dropped `is-` prefix per web component convention |
| `isDisabled` | `disabled` | Dropped `is-` prefix per web component convention |
| `isRequired` | `required` | Dropped `is-` prefix per web component convention |
| `isValid` | `invalid` | Inverted boolean: set `invalid` to show the invalid state |
| `isChecked={null}` | `indeterminate` property | JavaScript-only property, like native checkboxes |
| `description` | `slot="description"` | Slotted content instead of a prop |
| `body` | Default slot | Slotted content instead of a prop |
| `labelPosition` | `label-position` | `"start"` or `"end"` (default) |
| `aria-label` | `accessible-label` attribute or native `aria-label` | `accessible-label` follows the PFE convention |
| `onChange` | `input` and `change` events | Native-style events |

### Added

| Web component API | Notes |
|---|---|
| External `<label>` support | Works with `<label for="id">` and labels wrapping the element |
| Form association | Participates in native form submission, validation, reset, and disabled fieldsets |
| Form reset | Restores the initial `checked` state |
| Controlled checkboxes | Nested checkboxes are toggled by their parent, which reflects the aggregate state |
| Cross-root ARIA | Uses ARIA IDL element references (`ariaLabelledByElements`, `ariaDescribedByElements`) so the shadow label and description are exposed to assistive technology |
| CSS shadow parts | `input`, `label`, `required`, `description`, and `body` |

## Accessibility

The host element is the semantic checkbox: it has `role="checkbox"` and exposes
`aria-checked`, `aria-disabled`, `aria-required`, and `aria-invalid` through
`ElementInternals`. The shadow `<input type="checkbox">` is a visual affordance
only and is hidden from assistive technology.

- <kbd>Tab</kbd> and <kbd>Shift</kbd> + <kbd>Tab</kbd> move focus between the
checkbox and other focusable elements.
- <kbd>Space</kbd> toggles the checkbox. <kbd>Enter</kbd> does not.
- Clicking the visible label toggles the checkbox, and the label has a pointer
cursor to show that it can be clicked.
- A checkbox without a visible label must have an accessible name. Use
`accessible-label`, `aria-label`, or `aria-labelledby`.
- The `description` slot is exposed as the checkbox's accessible description
automatically.

`ShadowRoot.referenceTarget` is not needed for label association: the host
element is the labeled control, so a native `<label for="id">` is associated
through `ElementInternals.labels` without forwarding to the shadow input.
9 changes: 9 additions & 0 deletions elements/pf-v6-checkbox/demo/controlled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<pf-v6-checkbox id="controlled-check-1" name="check1" label="Parent CheckBox">
<pf-v6-checkbox id="controlled-check-2" name="check2" label="Child CheckBox 1"></pf-v6-checkbox>
<pf-v6-checkbox id="controlled-check-3" name="check3" label="Child CheckBox 2"></pf-v6-checkbox>
</pf-v6-checkbox>
<pf-v6-checkbox id="controlled-check-4" name="check4" label="Controlled CheckBox"></pf-v6-checkbox>

<script type="module">
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';
</script>
6 changes: 6 additions & 0 deletions elements/pf-v6-checkbox/demo/disabled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<pf-v6-checkbox id="disabled-check-1" label="Disabled CheckBox 1" disabled checked></pf-v6-checkbox>
<pf-v6-checkbox id="disabled-check-2" label="Disabled CheckBox 2" disabled></pf-v6-checkbox>

<script type="module">
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';
</script>
6 changes: 6 additions & 0 deletions elements/pf-v6-checkbox/demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<pf-v6-checkbox id="uncontrolled-check-1" label="Uncontrolled CheckBox 1"></pf-v6-checkbox>
<pf-v6-checkbox id="uncontrolled-check-2" label="Uncontrolled CheckBox 2"></pf-v6-checkbox>

<script type="module">
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';
</script>
8 changes: 8 additions & 0 deletions elements/pf-v6-checkbox/demo/required.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<pf-v6-checkbox id="required-check"
name="required-check"
label="Required checkbox"
required></pf-v6-checkbox>

<script type="module">
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';
</script>
8 changes: 8 additions & 0 deletions elements/pf-v6-checkbox/demo/reversed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<pf-v6-checkbox id="checkbox-reversed"
name="checkbox-reversed"
label-position="start"
label="Reversed checkbox example"></pf-v6-checkbox>

<script type="module">
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';
</script>
7 changes: 7 additions & 0 deletions elements/pf-v6-checkbox/demo/standalone.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<pf-v6-checkbox id="standalone-check"
name="standalone-check"
accessible-label="Standalone input"></pf-v6-checkbox>

<script type="module">
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';
</script>
7 changes: 7 additions & 0 deletions elements/pf-v6-checkbox/demo/with-body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<pf-v6-checkbox id="body-check-1" label="Checkbox with body">
<span>This is where custom content goes.</span>
</pf-v6-checkbox>

<script type="module">
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';
</script>
8 changes: 8 additions & 0 deletions elements/pf-v6-checkbox/demo/with-description-and-body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<pf-v6-checkbox id="description-body-check" label="Checkbox with description and body">
<span slot="description">Single-tenant cloud service hosted and managed by Red Hat that offers high-availability enterprise-grade clusters in a virtual private cloud on AWS or GCP.</span>
<span>This is where custom content goes.</span>
</pf-v6-checkbox>

<script type="module">
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';
</script>
7 changes: 7 additions & 0 deletions elements/pf-v6-checkbox/demo/with-description.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<pf-v6-checkbox id="description-check-1" label="Checkbox with description">
<span slot="description">Single-tenant cloud service hosted and managed by Red Hat that offers high-availability enterprise-grade clusters in a virtual private cloud on AWS or GCP.</span>
</pf-v6-checkbox>

<script type="module">
import '@patternfly/elements/pf-v6-checkbox/pf-v6-checkbox.js';
</script>
147 changes: 147 additions & 0 deletions elements/pf-v6-checkbox/docs/pf-v6-checkbox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{% renderOverview %}
<pf-v6-checkbox label="Checkbox"></pf-v6-checkbox>
{% endrenderOverview %}

{% band header="Usage" %}
### Controlled

Nested `<pf-v6-checkbox>` elements are controlled by their parent. The parent
toggles all children when clicked, and becomes indeterminate when only some
children are checked.

{% htmlexample %}
<pf-v6-checkbox id="controlled-check-1" name="check1" label="Parent CheckBox">
<pf-v6-checkbox id="controlled-check-2" name="check2" label="Child CheckBox 1"></pf-v6-checkbox>
<pf-v6-checkbox id="controlled-check-3" name="check3" label="Child CheckBox 2"></pf-v6-checkbox>
</pf-v6-checkbox>
<pf-v6-checkbox id="controlled-check-4" name="check4" label="Controlled CheckBox"></pf-v6-checkbox>
{% endhtmlexample %}

### Uncontrolled

Uncontrolled checkboxes manage their own state. Set the `checked` attribute to
change the initial state.

{% htmlexample %}
<pf-v6-checkbox id="uncontrolled-check-1" label="Uncontrolled CheckBox 1"></pf-v6-checkbox>
<pf-v6-checkbox id="uncontrolled-check-2" label="Uncontrolled CheckBox 2"></pf-v6-checkbox>
{% endhtmlexample %}

### Reversed

Use `label-position="start"` to display the label before the checkbox.

{% htmlexample %}
<pf-v6-checkbox id="checkbox-reversed"
name="checkbox-reversed"
label-position="start"
label="Reversed checkbox example"></pf-v6-checkbox>
{% endhtmlexample %}

### Disabled

{% htmlexample %}
<pf-v6-checkbox id="disabled-check-1" label="Disabled CheckBox 1" disabled checked></pf-v6-checkbox>
<pf-v6-checkbox id="disabled-check-2" label="Disabled CheckBox 2" disabled></pf-v6-checkbox>
{% endhtmlexample %}

### Checkbox with description

Use `slot="description"` for descriptive text.

{% htmlexample %}
<pf-v6-checkbox id="description-check-1" label="Checkbox with description">
<span slot="description">Single-tenant cloud service hosted and managed by Red Hat that offers high-availability enterprise-grade clusters in a virtual private cloud on AWS or GCP.</span>
</pf-v6-checkbox>
{% endhtmlexample %}

### Checkbox with body

Use the default slot for custom body content.

{% htmlexample %}
<pf-v6-checkbox id="body-check-1" label="Checkbox with body">
<span>This is where custom content goes.</span>
</pf-v6-checkbox>
{% endhtmlexample %}

### Checkbox with description and body

Use both the `description` slot and the default slot.

{% htmlexample %}
<pf-v6-checkbox id="description-body-check" label="Checkbox with description and body">
<span slot="description">Single-tenant cloud service hosted and managed by Red Hat that offers high-availability enterprise-grade clusters in a virtual private cloud on AWS or GCP.</span>
<span>This is where custom content goes.</span>
</pf-v6-checkbox>
{% endhtmlexample %}

### Standalone input

A checkbox without a visible label must have an accessible name.

{% htmlexample %}
<pf-v6-checkbox id="standalone-check"
name="standalone-check"
accessible-label="Standalone input"></pf-v6-checkbox>
{% endhtmlexample %}

### Required input

A required checkbox displays an indicator and participates in constraint
validation.

{% htmlexample %}
<pf-v6-checkbox id="required-check"
name="required-check"
label="Required checkbox"
required></pf-v6-checkbox>
{% endhtmlexample %}
{% endband %}

### Accessibility
The host element is the semantic checkbox: it has `role="checkbox"` and exposes
its checked, disabled, required, and invalid states to assistive technology.
The shadow `<input>` is a visual affordance only and is hidden from assistive
technology.

#### Keyboard interaction

| Key | Function |
| --- | -------- |
| <kbd>Tab</kbd> | Moves focus to the next checkbox or focusable element. |
| <kbd>Shift</kbd> + <kbd>Tab</kbd> | Moves focus to the previous checkbox or focusable element. |
| <kbd>Space</kbd> | Toggles the checkbox. No other key toggles it. |

#### Labeling

Use the `label` attribute for a visible label. Clicking the label toggles the
checkbox, and the label has a pointer cursor to show that it can be clicked.
The label text is also the checkbox's accessible name. To label the checkbox
with an external `<label for="id">` instead, give the checkbox an `id` and omit
the `label` attribute; do not combine the two.

A checkbox without a visible label needs an accessible name. Use
`accessible-label`, or the native `aria-label` or `aria-labelledby`
attributes.

{% htmlexample %}
<pf-v6-checkbox accessible-label="Select all rows"></pf-v6-checkbox>
{% endhtmlexample %}

When the `description` slot is populated, the element exposes the description
as the checkbox's accessible description. `required` and `invalid` add
`aria-required` and `aria-invalid`, and a disabled checkbox is removed from the
page's tab order.

{% renderSlots %}{% endrenderSlots %}

{% renderAttributes %}{% endrenderAttributes %}

{% renderMethods %}{% endrenderMethods %}

{% renderEvents %}{% endrenderEvents %}

{% renderCssCustomProperties %}{% endrenderCssCustomProperties %}

{% renderCssParts %}{% endrenderCssParts %}
Loading
Loading