UNPKG

@wonderflow/react-components

Version:

UI components from Wonderflow's Wanda design system

43 lines (42 loc) 2.67 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; /* * Copyright 2022-2023 Wonderflow Design Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { useSize } from 'ahooks'; import clsx from 'clsx'; import { domAnimation, LazyMotion, m } from 'framer-motion'; import { forwardRef, useEffect, useMemo, useRef, } from 'react'; import { useUIDSeed } from 'react-uid'; import { Stack, Text } from '../..'; import * as styles from './selection-controls.module.css'; const inputSize = { regular: 24, small: 16, }; export const Checkbox = forwardRef(({ className, disabled, dimension = 'regular', onChange, indeterminate, id, label, hidden, ...otherProps }, forwardedRef) => { const ref = useRef(forwardedRef); const seedID = useUIDSeed(); const fieldID = useMemo(() => id ?? seedID('checkbox'), [id, seedID]); const refWrapper = useRef(null); const wrapper = useSize(refWrapper); const isAlignCenter = useMemo(() => wrapper?.height === inputSize[dimension], [wrapper, dimension]); useEffect(() => { if (ref.current) { ref.current.indeterminate = indeterminate; } }, [indeterminate]); return (_jsxs(Stack, { as: "span", direction: "row", columnGap: 8, vAlign: isAlignCenter ? 'center' : 'start', fill: false, ref: refWrapper, "data-outer-element": "Wrapper", children: [_jsx(LazyMotion, { features: domAnimation, strict: true, children: _jsx(m.span, { className: clsx(styles.InputWrapper, className), whileTap: { scale: 1.15 }, transition: { duration: 0.3, ease: 'backOut' }, "data-radio-control": hidden, children: _jsx("input", { type: "checkbox", disabled: disabled, "aria-disabled": disabled, "data-control-dimension": dimension, "data-inner-element": "Checkbox", onChange: onChange, className: styles.CheckboxInput, ref: ref, hidden: hidden, id: fieldID, ...otherProps }) }) }), label && (_jsx(Text, { as: "label", "aria-disabled": disabled, "data-inner-element": "Label", className: styles.Label, htmlFor: fieldID, variant: dimension === 'small' ? 'body-2' : 'body-1', title: otherProps?.title, children: label }))] })); }); Checkbox.displayName = 'Checkbox';