Documentation / Hooks & utilities
Utilities
The small building blocks the components rest on, exported for your own widgets.
cn(...classes)joins classes, skipping falsy values (false,null,undefined,""), and resolves conflicting Tailwind classes withtailwind-merge: of two classes that set the same thing, the one written last wins, whatever the order of the stylesheet. That is what lets a component'sclassNamereplace its own classes. A class under a variant (hover:,dark:) only replaces one under the same variants.getFocusableElements(container)lists the keyboard-reachable elements, in document order;isFocusableandisElementVisibletest a single element (disabled,tabindex="-1",hidden,inert,aria-hidden,display: none…).FOCUSABLE_SELECTORis the starting selector.hideOthers(targets)makes everything that is neither an ancestor nor a descendant of the targetsinert+aria-hidden, and returns the function that restores the previous state (reference counted: calls nest). TheINERT_IGNORE_ATTRIBUTEattribute (data-synertech-inert-ignore) exempts an element.registerFloatingLayer(layer, getOwner)declares a floating layer (menu, popover) attached outside the dialog but owned by a trigger: a dialog keeps it interactive if that trigger lives inside it.findOwnedLayers(targets)finds those layers.
Examples
Conditional classes with cn
False conditions drop out; the caller’s className comes last.
import { useState } from "react";
import { cn } from "@synertech/ui";
function Tag({ active, className }: { active: boolean; className?: string }) {
return (
<span
className={cn(
"rounded-full px-3 py-1 text-sm",
active ? "bg-blue-600 text-white" : "bg-gray-100 text-gray-900",
className
)}
>
Étiquette
</span>
);
}
export function ConditionalClasses() {
const [active, setActive] = useState(false);
return (
<button type="button" onClick={() => setActive((value) => !value)}>
<Tag active={active} className={active ? "shadow" : undefined} />
</button>
);
}The caller’s class wins
Of two classes that set the same background, cn keeps the last one: the one passed in className replaces the tag’s own, and the rest stays.
import { cn } from "@synertech/ui";
function Tag({ active, className }: { active: boolean; className?: string }) {
return (
<span
className={cn(
"rounded-full px-3 py-1 text-sm",
active ? "bg-blue-600 text-white" : "bg-gray-100 text-gray-900",
className
)}
>
Étiquette
</span>
);
}
export function CallerClassWins() {
return (
<div className="flex gap-2">
<Tag active={false} />
<Tag
active={false}
className="bg-amber-100 text-gray-900 dark:bg-amber-900 dark:text-amber-100"
/>
</div>
);
}First field to fill in
getFocusableElements skips disabled fields: focus goes to the first field that can actually be reached.
import { useRef } from "react";
import { Button, getFocusableElements } from "@synertech/ui";
export function FocusFirstField() {
const formRef = useRef<HTMLFormElement>(null);
return (
<form ref={formRef} className="space-y-2">
<input
disabled
defaultValue="Client n° 4821"
className="block rounded-md border px-3 py-2 text-sm"
/>
<input
placeholder="Objet de la demande"
className="block rounded-md border px-3 py-2 text-sm"
/>
<Button
type="button"
onClick={() => formRef.current && getFocusableElements(formRef.current)[0]?.focus()}
>
Commencer la saisie
</Button>
</form>
);
}Testing an element with isFocusable
Useful to decide where to send focus when the original element has been disabled.
import { useRef, useState } from "react";
import { Button, isFocusable } from "@synertech/ui";
export function CheckFocusable() {
const buttonRef = useRef<HTMLButtonElement>(null);
const [disabled, setDisabled] = useState(false);
const [result, setResult] = useState("");
return (
<div className="flex items-center gap-3">
<button
ref={buttonRef}
type="button"
disabled={disabled}
className="rounded-md border px-3 py-1.5 text-sm"
>
Cible
</button>
<Button size="sm" onClick={() => setDisabled((value) => !value)}>
{disabled ? "Activer" : "Désactiver"}
</Button>
<Button
size="sm"
onClick={() =>
setResult(
buttonRef.current && isFocusable(buttonRef.current) ? "focalisable" : "non focalisable"
)
}
>
Tester
</Button>
<span className="text-sm text-gray-600">{result}</span>
</div>
);
}Spotlighting an element with hideOthers
A tutorial isolates a block: the rest of the page becomes inert, except the region marked with INERT_IGNORE_ATTRIBUTE.
import { useEffect, useRef, useState } from "react";
import { Button, INERT_IGNORE_ATTRIBUTE, cn, hideOthers } from "@synertech/ui";
export function Spotlight() {
const [active, setActive] = useState(false);
const targetRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!active || !targetRef.current) return;
return hideOthers(targetRef.current);
}, [active]);
return (
<div className="space-y-3">
<Button onClick={() => setActive(true)}>Lancer la visite guidée</Button>
<p {...{ [INERT_IGNORE_ATTRIBUTE]: "" }} aria-live="polite" className="text-sm text-gray-600">
{active ? "Étape 1 sur 3" : ""}
</p>
<div
ref={targetRef}
className={cn("rounded-lg border p-4", active && "ring-4 ring-blue-400")}
>
<p className="text-sm">Créez votre premier projet ici.</p>
{active && (
<Button size="sm" className="mt-2" onClick={() => setActive(false)}>
Terminer
</Button>
)}
</div>
</div>
);
}Floating layer in a modal
A palette attached to <body> falls outside the modal: registerFloatingLayer ties it to its button, and the modal keeps it interactive and inside its focus trap.
import { useEffect, useRef, useState } from "react";
import { Button, Modal, registerFloatingLayer } from "@synertech/ui";
const COLORS = ["#ef4444", "#22c55e", "#3b82f6"];
export function FloatingLayerInModal() {
const [open, setOpen] = useState(false);
const [pickerOpen, setPickerOpen] = useState(false);
const [color, setColor] = useState(COLORS[0]);
const triggerRef = useRef<HTMLButtonElement>(null);
useEffect(() => {
if (!pickerOpen) return;
const layer = document.createElement("div");
layer.className = "fixed right-8 top-8 z-[60] flex gap-2 rounded-md bg-white p-2 shadow-lg";
for (const swatch of COLORS) {
const button = document.createElement("button");
button.type = "button";
button.ariaLabel = swatch;
button.className = "h-6 w-6 rounded-full";
button.style.background = swatch;
button.onclick = () => {
setColor(swatch);
setPickerOpen(false);
};
layer.append(button);
}
document.body.append(layer);
const unregister = registerFloatingLayer(layer, () => triggerRef.current);
return () => {
unregister();
layer.remove();
};
}, [pickerOpen]);
return (
<>
<Button onClick={() => setOpen(true)}>Personnaliser</Button>
<Modal
isOpen={open}
onClose={() => setOpen(false)}
title="Couleur du thème"
showSubmit={false}
>
<button
ref={triggerRef}
type="button"
onClick={() => setPickerOpen((value) => !value)}
className="flex items-center gap-2 text-sm"
>
<span className="h-5 w-5 rounded-full" style={{ background: color }} /> Choisir une
couleur
</button>
</Modal>
</>
);
}API reference
Functions
cn
FunctionJoins class names, skipping falsy values, and resolves the Tailwind classes that set the same thing: the one written last wins, whatever the order of the stylesheet. cn("px-4 bg-white", className) with className="bg-red-600" gives px-4 bg-red-600. A class under a variant (hover:, dark:, md:) only replaces one under the same variants, and a class Tailwind does not know is kept as it is.
import { cn } from "@synertech/ui";cn(...classes: (string | undefined | null | false)[]): stringfindOwnedLayers
FunctionRegistered layers whose owner lives inside one of targets, or inside a layer that does.
Ownership is followed from layer to layer. The entry that opens a submenu sits in its menu, and that menu is portalled next to the dialog, not inside it: looked up one step only, the menu belonged to the dialog and its submenu to nobody, so the submenu went inert with the page behind.
import { findOwnedLayers } from "@synertech/ui";findOwnedLayers(targets: Element[]): Element[]getFocusableElements
FunctionEvery focusable descendant of container, in DOM order.
Positive tabindex values are not re-ordered: mixing them with the natural order is an anti-pattern and the sequence stays predictable this way.
import { getFocusableElements } from "@synertech/ui";getFocusableElements(container: HTMLElement): HTMLElement[]hideOthers
FunctionHides every element that is neither an ancestor nor a descendant of one of the targets (typically the dialog and, when it must stay clickable, its overlay).
import { hideOthers } from "@synertech/ui";hideOthers(targets: Element | Element[]): () => voidReturns a function restoring the previous state (idempotent).
isElementVisible
FunctionWalks up from element and returns false as soon as an ancestor hides it (hidden, inert, aria-hidden, display: none, visibility: hidden).
Layout based checks (offsetParent, getClientRects) are deliberately not used: they always report "invisible" in jsdom, which would make the focus trap untestable.
import { isElementVisible } from "@synertech/ui";isElementVisible(element: HTMLElement): booleanisFocusable
FunctionTrue when element can be reached with the Tab key.
import { isFocusable } from "@synertech/ui";isFocusable(element: HTMLElement): booleanregisterFloatingLayer
FunctionDeclares layer as a floating layer owned by getOwner().
The owner is resolved lazily, because a portal is usually mounted before the trigger it belongs to.
import { registerFloatingLayer } from "@synertech/ui";registerFloatingLayer(layer: Element, getOwner: () => Element | null): () => voidReturns a function unregistering the layer.
Constants
FOCUSABLE_SELECTOR
ConstantSelector matching every element that can potentially receive focus.
import { FOCUSABLE_SELECTOR } from "@synertech/ui";const FOCUSABLE_SELECTOR: stringINERT_IGNORE_ATTRIBUTE
ConstantOpt-out attribute for elements that must stay live (toasts, aria-live regions).
import { INERT_IGNORE_ATTRIBUTE } from "@synertech/ui";const INERT_IGNORE_ATTRIBUTE: "data-synertech-inert-ignore"