Files
mastodon-sakyey/app/javascript/mastodon/components/form_fields/toggle_field.tsx

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2026-01-23 13:20:56 +01:00
import type { ComponentPropsWithoutRef, CSSProperties } from 'react';
import { forwardRef } from 'react';
import classNames from 'classnames';
import type { CommonFieldWrapperProps } from './form_field_wrapper';
import { FormFieldWrapper } from './form_field_wrapper';
2026-01-23 13:20:56 +01:00
import classes from './toggle.module.css';
type Props = Omit<ComponentPropsWithoutRef<'input'>, 'type'> & {
size?: number;
};
export const ToggleField = forwardRef<
HTMLInputElement,
Props & CommonFieldWrapperProps
>(({ id, label, hint, status, required, ...otherProps }, ref) => (
2026-01-23 13:20:56 +01:00
<FormFieldWrapper
label={label}
hint={hint}
required={required}
status={status}
2026-01-23 13:20:56 +01:00
inputId={id}
inputPlacement='inline-end'
2026-01-23 13:20:56 +01:00
>
{(inputProps) => <Toggle {...otherProps} {...inputProps} ref={ref} />}
2026-01-23 13:20:56 +01:00
</FormFieldWrapper>
));
ToggleField.displayName = 'ToggleField';
export const Toggle = forwardRef<HTMLInputElement, Props>(
2026-01-23 13:20:56 +01:00
({ className, size, ...otherProps }, ref) => (
<span className={classes.wrapper}>
2026-01-23 13:20:56 +01:00
<input
{...otherProps}
type='checkbox'
className={classes.input}
ref={ref}
/>
<span
className={classNames(classes.toggle, className)}
style={
{ '--diameter': size ? `${size}px` : undefined } as CSSProperties
}
hidden
/>
</span>
2026-01-23 13:20:56 +01:00
),
);
Toggle.displayName = 'Toggle';