|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {focusRingClasses} from '@material/web/labs/gb/components/focus/focus-ring.js'; |
| 8 | +import { |
| 9 | + rippleClasses, |
| 10 | + setupRipple, |
| 11 | +} from '@material/web/labs/gb/components/ripple/ripple.js'; |
| 12 | +import {PSEUDO_CLASSES} from '@material/web/labs/gb/components/shared/pseudo-classes.js'; |
| 13 | +import {AttributePart} from 'lit'; |
| 14 | +import {Directive, directive, DirectiveParameters} from 'lit/directive.js'; |
| 15 | +import {classMap, type ClassInfo} from 'lit/directives/class-map.js'; |
| 16 | + |
| 17 | +/** Checkbox classes. */ |
| 18 | +export const CHECKBOX_CLASSES = { |
| 19 | + checkbox: 'checkbox', |
| 20 | + invalid: PSEUDO_CLASSES.invalid, |
| 21 | + hover: PSEUDO_CLASSES.hover, |
| 22 | + focus: PSEUDO_CLASSES.focus, |
| 23 | + active: PSEUDO_CLASSES.active, |
| 24 | + checked: PSEUDO_CLASSES.checked, |
| 25 | + indeterminate: PSEUDO_CLASSES.indeterminate, |
| 26 | + disabled: PSEUDO_CLASSES.disabled, |
| 27 | +} as const; |
| 28 | + |
| 29 | +/** The state provided to the `checkboxClasses()` function. */ |
| 30 | +export interface CheckboxClassesState { |
| 31 | + /** Emulates `:invalid`. */ |
| 32 | + invalid?: boolean; |
| 33 | + /** Emulates `:hover`. */ |
| 34 | + hover?: boolean; |
| 35 | + /** Emulates `:focus`. */ |
| 36 | + focus?: boolean; |
| 37 | + /** Emulates `:active`. */ |
| 38 | + active?: boolean; |
| 39 | + /** Emulates `:checked`. */ |
| 40 | + checked?: boolean; |
| 41 | + /** Emulates `:indeterminate`. */ |
| 42 | + indeterminate?: boolean; |
| 43 | + /** Emulates `:disabled`. */ |
| 44 | + disabled?: boolean; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Returns the checkbox classes to apply to an element based on the given state. |
| 49 | + * |
| 50 | + * @param state The state of the checkbox. |
| 51 | + * @return An object of class names and truthy values if they apply. |
| 52 | + */ |
| 53 | +export function checkboxClasses({ |
| 54 | + invalid = false, |
| 55 | + hover = false, |
| 56 | + focus = false, |
| 57 | + active = false, |
| 58 | + checked = false, |
| 59 | + indeterminate = false, |
| 60 | + disabled = false, |
| 61 | +}: CheckboxClassesState = {}): ClassInfo { |
| 62 | + return { |
| 63 | + ...rippleClasses(), |
| 64 | + ...focusRingClasses(), |
| 65 | + [CHECKBOX_CLASSES.checkbox]: true, |
| 66 | + [CHECKBOX_CLASSES.checked]: checked, |
| 67 | + [CHECKBOX_CLASSES.indeterminate]: indeterminate, |
| 68 | + [CHECKBOX_CLASSES.disabled]: disabled, |
| 69 | + [CHECKBOX_CLASSES.invalid]: invalid, |
| 70 | + [CHECKBOX_CLASSES.hover]: hover, |
| 71 | + [CHECKBOX_CLASSES.focus]: focus, |
| 72 | + [CHECKBOX_CLASSES.active]: active, |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Sets up checkbox functionality for the given element. |
| 78 | + * |
| 79 | + * @param checkbox The element on which to set up checkbox functionality. |
| 80 | + * @param opts Setup options, supports a cleanup `signal`. |
| 81 | + */ |
| 82 | +export function setupCheckbox( |
| 83 | + checkbox: HTMLElement, |
| 84 | + opts?: {signal?: AbortSignal}, |
| 85 | +): void { |
| 86 | + setupRipple(checkbox, opts); |
| 87 | +} |
| 88 | + |
| 89 | +/** The state provided to the `checkbox()` directive. */ |
| 90 | +export interface CheckboxDirectiveState extends CheckboxClassesState { |
| 91 | + /** Additional classes to apply to the element. */ |
| 92 | + classes?: ClassInfo; |
| 93 | +} |
| 94 | + |
| 95 | +class CheckboxDirective extends Directive { |
| 96 | + private element?: HTMLElement; |
| 97 | + private cleanup?: AbortController; |
| 98 | + |
| 99 | + render(state: CheckboxDirectiveState) { |
| 100 | + return classMap({ |
| 101 | + ...(state.classes || {}), |
| 102 | + ...checkboxClasses(state), |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + override update( |
| 107 | + {element}: AttributePart, |
| 108 | + [state]: DirectiveParameters<this>, |
| 109 | + ) { |
| 110 | + if (element !== this.element) { |
| 111 | + this.element = element as HTMLElement; |
| 112 | + this.cleanup?.abort(); |
| 113 | + this.cleanup = new AbortController(); |
| 114 | + setupCheckbox(this.element, {signal: this.cleanup.signal}); |
| 115 | + } |
| 116 | + |
| 117 | + return this.render(state); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * A Lit directive that adds checkbox styling and functionality to its element. |
| 123 | + * |
| 124 | + * @example |
| 125 | + * ```ts |
| 126 | + * html`<input type="checkbox" class="${checkbox()}">`; |
| 127 | + * ``` |
| 128 | + */ |
| 129 | +export const checkbox = directive(CheckboxDirective); |
0 commit comments