@create-figma-plugin/ui
Version:
Production-grade Preact components that replicate the Figma UI design
32 lines • 1.69 kB
JavaScript
import { h } from 'preact';
import { useCallback } from 'preact/hooks';
import { IconCheck16 } from '../../icons/icon-16/icon-check-16.js';
import { createClassName } from '../../utilities/create-class-name.js';
import { createComponent } from '../../utilities/create-component.js';
import { noop } from '../../utilities/no-op.js';
import styles from './checkbox.module.css';
export const Checkbox = createComponent(function ({ children, disabled = false, onChange = noop, onKeyDown = noop, onValueChange = noop, propagateEscapeKeyDown = true, value, ...rest }, ref) {
const handleChange = useCallback(function (event) {
onChange(event);
const newValue = event.currentTarget.checked === true;
onValueChange(newValue);
}, [onChange, onValueChange]);
const handleKeyDown = useCallback(function (event) {
onKeyDown(event);
if (event.key === 'Escape') {
if (propagateEscapeKeyDown === false) {
event.stopPropagation();
}
event.currentTarget.blur();
}
}, [onKeyDown, propagateEscapeKeyDown]);
return (h("label", { class: createClassName([
styles.checkbox,
disabled === true ? styles.disabled : null
]) },
h("input", { ...rest, ref: ref, checked: value === true, class: styles.input, disabled: disabled === true, onChange: handleChange, onKeyDown: handleKeyDown, tabIndex: 0, type: "checkbox" }),
h("div", { class: styles.box }, value === true ? (h("div", { class: styles.checkIcon },
h(IconCheck16, null))) : null),
h("div", { class: styles.children }, children)));
});
//# sourceMappingURL=checkbox.js.map