[ Getting started ]

Usage

How Reusify components take props, handle state, forward refs and accept your own styles.

Importing#

Everything is a named export from the package root. Unused components are tree-shaken from your bundle.

import { Button, Input, Modal, useDisclosure, useToast } from 'reusify-ui';

Props follow HTML#

Components accept the attributes of the element they render, so onClick, disabled, type, aria-* and data-* all work as you expect. Reusify only adds props for things HTML can't do, like variant, loading or label.

<Button variant="outline" size="sm" type="submit" aria-label="Save draft" data-testid="save">
  Save draft
</Button>

<Input label="Email" type="email" name="email" autoComplete="email" required />

Controlled & uncontrolled#

Stateful components (Tabs, Accordion, Switch, RadioGroup) work both ways. Pass defaultValue and let the component manage itself, or pass value + onChange to control it from your state.

// Uncontrolled
<Tabs defaultValue="overview" items={items} />

// Controlled
const [tab, setTab] = useState('overview');
<Tabs value={tab} onChange={setTab} items={items} />

Refs & form libraries#

Inputs, buttons and other form controls forward refs to the native element. Use them with React Hook Form, Formik or plain useRef.

const { register } = useForm();

<Input label="Name" {...register('name', { required: true })} />

Customizing a single component#

Every component accepts className (and style). For a one-off change, add your own class or Tailwind utilities. For app-wide changes, use theme variables instead (see Theming).

<Button className="rounded-full px-6">Pill button</Button>

<Card style={{ '--ru-primary': '#e11d48' }}>
  {/* everything inside uses rose as the primary color */}
</Card>
Without a cascade layer (see Installation), a utility and a Reusify class have the same specificity, and the one loaded later wins. Add Tailwind's important modifier (rounded-full! in v4, !rounded-full in v3) if you need to force it.

Composition#

Components are designed to be combined. A typical settings card:

<Card>
  <CardHeader title="Notifications" subtitle="Choose what we email you about." />
  <CardBody>
    <Switch label="Product updates" defaultChecked />
    <Switch label="Weekly digest" />
  </CardBody>
  <CardFooter>
    <Button variant="secondary">Cancel</Button>
    <Button onClick={() => toast.success('Preferences saved')}>Save</Button>
  </CardFooter>
</Card>