suiss/ui ยท Pattern
Preview
A link preview that shows a hover card on desktop and a drawer on mobile.
Preview is an open-source pattern from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Preview reveals extra information about a link, such as a profile or page summary, without navigating. On desktop it renders a suiss/ui HoverCard that opens when the trigger is hovered or focused. On mobile (below 48rem, via useIsMobile), where hover is not available, it renders a Drawer with a swipe handle that opens when the trigger is tapped.
Use it for supplementary content that people can skip. Keep essential information and actions out of the preview, because the hover card opens only on hover or focus.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
-
Add the
cnhelper. -
Add the pieces it builds on: Drawer, Hover Card,
use-mobile. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a Preview:
Examples
With close button
Set showCloseButton to add a close button to the mobile drawer. PreviewContent then adds end padding so content does not sit under it.
With close label
Set closeLabel to use a Done text button for the mobile close button in place of the icon.
Attached
The mobile drawer floats inset from the screen edges by default. Set floating to false so it sits on the screen edges.
Accessibility
- On desktop the trigger is a link; the card opens on hover and on keyboard focus, and closes when the pointer leaves.
- On mobile the trigger is a button that opens the drawer.
- The trigger shows a focus ring when focused with the keyboard.
API Reference
Preview
The root that chooses HoverCard or Drawer and holds the open state.
| Prop | Type | Default |
|---|---|---|
open | boolean | - |
defaultOpen | boolean | - |
onOpenChange | (open: boolean) => void | - |
floating | boolean | true |
children | React.ReactNode | - |
Open state is uncontrolled by default; use defaultOpen, or open with onOpenChange to control it. Place PreviewTrigger and PreviewContent inside Preview; the parts throw when used outside it. floating has no effect on desktop.
PreviewTrigger
The link-styled element that opens the preview.
| Prop | Type | Default |
|---|---|---|
href | string | - |
Also accepts the props of React.HTMLAttributes<HTMLElement>.
Also accepts className, children, id and aria-label.
On desktop the trigger is a link that uses href. On mobile it is a button that opens the drawer, so the link target is not followed.
PreviewContent
The card or drawer that holds the preview.
| Prop | Type | Default |
|---|---|---|
showCloseButton | boolean | - |
closeLabel | string | - |
Also accepts align, alignOffset, side and sideOffset from HoverCardContent, which position the desktop card, and the props of React.HTMLAttributes<HTMLElement>.
Also accepts align, alignOffset, side and sideOffset from HoverCardContent, which position the desktop card, and className, style, children, id and aria-label.
showCloseButton and closeLabel apply only to the mobile drawer. On mobile, className applies to an inner padded wrapper. Each part sets a data-slot, such as preview-trigger or preview-content, for styling.
Web preview
PreviewTrigger renders inline in the primary color and fades when pressed. It opens a Drawer that floats above the screen edges by default and has a swipe handle.
Use it to show a summary of a person, place or item from a mention or link without leaving the screen.
Installation
- Install the following dependencies:
- Copy and paste the following code into
components/patterns/preview.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
- Copy and paste the following code into
components/patterns/preview.tsx:
- Update the import paths to match your project setup.
- Copy and paste the following code into
components/patterns/preview.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Default
Web preview
With close button
Shows a close button with showCloseButton.
Web preview
With close label
Labels the close button Done with closeLabel.
Web preview
Attached
Sets floating to false so the drawer sits on the screen edges.
Web preview
The React Native tab shows 1 more example.
Accessibility
- Give
PreviewContentanaccessibilityLabelthat names what is previewed.
API Reference
Preview
The root drawer, floating by default.
| Prop | Type | Default |
|---|---|---|
open | boolean | - |
defaultOpen | boolean | - |
onOpenChange | (open: boolean) => void | - |
floating | boolean | true |
children | React.ReactNode | - |
Put a PreviewTrigger and a PreviewContent inside Preview. The preview can be uncontrolled or controlled with open, defaultOpen and onOpenChange.
PreviewTrigger
The link-styled element that opens the preview.
Also accepts the props of React.ComponentProps<typeof DrawerTrigger>.
A string or number child is wrapped in Text.
PreviewContent
The drawer content with padding for the preview.
| Prop | Type | Default |
|---|---|---|
className | string | - |
Also accepts the props of React.ComponentProps<typeof DrawerContent>.
It pads its children and adds end padding when a close button is shown, so content does not sit under it.
.preview
| Parameter | Type | Default |
|---|---|---|
isPresented | Binding<Bool> | - |
arrowEdge | Edge | .top |
content | () -> Content | - |
PreviewContent
The drawer content with padding for the preview.
| Parameter | Type | Default |
|---|---|---|
showCloseButton | Bool | false |
closeLabel | LocalizedStringKey? | nil |
width | CGFloat? | 256 |
content | () -> Content | - |
Preview
The root drawer, floating by default.
| Parameter | Type | Default |
|---|---|---|
open | Boolean | - |
onOpenChange | (Boolean) -> Unit | - |
floating | Boolean | true |
content | @Composable () -> Unit | - |
PreviewTrigger
The link-styled element that opens the preview.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable BoxScope.() -> Unit | - |
text | String | - |
Overloads: PreviewTrigger(content), PreviewTrigger(text).
PreviewContent
The drawer content with padding for the preview.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
showCloseButton | Boolean | false |
closeLabel | String? | null |
content | @Composable ColumnScope.() -> Unit | - |