Sources
A collapsed list of the citations behind an answer, each with a title, its host and an optional favicon.
Installation
Copy and paste the following code into your project.
Update the import paths to match your project structure.
Anatomy
Sources start closed. The trigger counts the items on its own and reads "Used 2 sources", so the answer keeps its shape and the citations sit one click away.
Composition
Use the following composition to build
Sources:Open by default
Pass
defaultOpen when the citations are the point, for example in a research view. Each item derives its host from the href. Favicons are off by default, because fetching them from a third party leaks which pages the answer cited and trips a strict CSP. Pass getFavicon on the root to turn them on, either with your own endpoint or with the bundled googleFavicon helper, which is what the demos use. favicon on an item overrides that image or removes it with null.Labels
label on the trigger receives the count and returns the text, which is where translations go. Give the trigger children instead to replace the label entirely. When the number of items is not the number of citations, say because the list is paginated, set count on the root. Set it too when you wrap Sources.Item in your own component, since the automatic count only sees items placed directly in the tree.API Reference
Sources
| Prop | Type | Default | Description |
|---|---|---|---|
defaultOpen | boolean | false | Open state when uncontrolled |
open | boolean | - | Controlled open state |
onOpenChange | (open: boolean) => void | - | Called when the open state changes |
count | number | - | Number shown in the trigger, defaults to items |
getFavicon | (host: string) => string | null | - | Builds the favicon URL for each item, off when left out |
className | string | - | Additional CSS classes |
Sources.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
label | (count: number) => string | Used N sources | Builds the label from the count |
children | ReactNode | - | Replaces the label when given |
className | string | - | Additional CSS classes |
Sources.Content
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |
Sources.Item
| Prop | Type | Default | Description |
|---|---|---|---|
href | string | - | Link to the source, opens in a new tab |
title | string | - | Text of the citation |
favicon | string | null | from getFavicon | Image next to the title, null hides it |
children | ReactNode | - | Replaces the title when given |
className | string | - | Additional CSS classes |
sourceHost
sourceHost(href) returns the hostname of a URL without its www. prefix, and the input unchanged when it is not a URL.Found something to improve?
Notice a bug, typo, or missing detail on this page? Help us make the documentation better by opening a GitHub issue.