@wonderflow/react-components
Version:
UI components from Wonderflow's Wanda design system
36 lines (35 loc) • 2.13 kB
JavaScript
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 { forwardRef, 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: 17,
};
export const Toggle = forwardRef(({ className, disabled, id, label, dimension = 'regular', onChange, ...otherProps }, forwardedRef) => {
const seedID = useUIDSeed();
const fieldID = useMemo(() => id ?? seedID('toggle'), [id, seedID]);
const refWrapper = useRef(null);
const wrapper = useSize(refWrapper);
const isAlignCenter = useMemo(() => wrapper?.height === inputSize[dimension], [wrapper, dimension]);
return (_jsxs(Stack, { as: "span", direction: "row", columnGap: 8, vAlign: isAlignCenter ? 'center' : 'start', fill: false, ref: refWrapper, "data-outer-element": "Wrapper", children: [_jsx("input", { type: "checkbox", disabled: disabled, "aria-disabled": disabled, "data-control-dimension": dimension, "data-inner-element": "Toggle", onChange: onChange, className: clsx(styles.Toggle, className), ref: forwardedRef, id: fieldID, ...otherProps }), label && (_jsx(Text, { as: "label", "aria-disabled": disabled, "data-inner-element": "Label", className: styles.Label, variant: dimension === 'small' ? 'body-2' : 'body-1', htmlFor: fieldID, title: otherProps?.title, children: label }))] }));
});
Toggle.displayName = 'Toggle';