Kosorikōsori alpha

Label

Renders an accessible label associated with controls.

Installation

Install the primitive

Install the @radix-ui/react-label package.

npm install @radix-ui/react-label

Copy-paste the component

Copy and paste the component code in a .tsx file.

'use client';
 
import type { VariantProps } from 'tailwind-variants';
import { forwardRef } from 'react';
import { Root } from '@radix-ui/react-label';
import { clsx } from 'clsx/lite';
import { tv } from 'tailwind-variants';
 
const labelStyles = tv({
  base: clsx(
    'text-sm font-medium leading-none',
    'peer-disabled:cursor-not-allowed peer-disabled:text-grey-solid',
  ),
});
 
type LabelRef = React.ElementRef<typeof Root>;
type LabelRadixProps = React.ComponentPropsWithoutRef<typeof Root>;
type LabelVariants = VariantProps<typeof labelStyles>;
type LabelProps = LabelRadixProps & LabelVariants;
 
/**
 * Label component that provides a label for form elements.
 *
 * @param {LabelProps} props - The props for the Label component.
 *
 * @example
 * <Label htmlFor='email'>Your email address</Label>
 *
 * @see {@link https://dub.sh/ui-label Label Docs} for further information.
 */
 
export const Label = forwardRef<LabelRef, LabelProps>(
  ({ className, ...props }, ref) => (
    <Root ref={ref} className={labelStyles({ class: className })} {...props} />
  ),
);
 
Label.displayName = Root.displayName;

Update import paths

Update the @kosori/ui import paths to fit your project structure, for example, using ~/components/ui.

Usage

import { Label } from '~/components/ui/label';
<Label htmlFor='email'>Your email address</Label>

On this page