Clipboard
View as MarkdownA button that copies a value to the clipboard, with an optional input that shows it.
Features
Permalink to heading "Features"- Copies text to the clipboard
- Switches to a copied state as visual feedback
- Custom duration of the copied state with
timeout(3000 ms by default)
<primitives:clipboard.root>
<primitives:clipboard.label />
<primitives:clipboard.control>
<primitives:clipboard.input />
<primitives:clipboard.trigger>
<primitives:clipboard.indicator />
</primitives:clipboard.trigger>
</primitives:clipboard.control>
</primitives:clipboard.root>
Installation
Permalink to heading "Installation"typo3 ui:add clipboard
Please copy the files manually from GitHub into your project.
Read more about installing Components and Primitives.
Examples
Permalink to heading "Examples"Copy button on its own
Permalink to heading "Copy button on its own"Leave out the input for a copy button on its own. The demo copies a share link.
Custom timeout
Permalink to heading "Custom timeout"Set timeout in milliseconds to change how long the copied state stays. The button in the demo reads "Copied for 5 seconds!" for timeout="5000".
Localization
Permalink to heading "Localization"The trigger labels for the idle and the copied state ship in the language file and follow the site language. Pass translations with one string per state to override them for one clipboard.
<f:variable
name="clipboardTranslations"
value="{
triggerLabelIdle: '{f:translate(key: \'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:clipboard.copy\')}',
triggerLabelCopied: '{f:translate(key: \'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:clipboard.copied\')}'
}"
/>
<ui:clipboard.root value="SUMMER24" translations="{clipboardTranslations}"> ... </ui:clipboard.root>
A client script passes a triggerLabel function of the copied state instead of the two strings.
API Reference
Permalink to heading "API Reference"The following tables cover the available props of the Fluid Primitives. For a full list of available client side props and methods, see the Zag.js Machine API.
clipboard.root
Permalink to heading "clipboard.root"Provides the clipboard state and shared context for the composed parts. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
value | stringThe value to copy to the clipboard. | Yes | - |
timeout | integerThe timeout for the copied state in milliseconds. | No | 3000 |
translations | arrayLocalized clipboard trigger labels, `triggerLabelIdle` and `triggerLabelCopied`. A client script can pass a `triggerLabel` function of the copied state instead. Use `f:translate` for per-template localization overrides when needed. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
rootId | stringThe root ID of the component, used for hydration and identification. | No | - |
ids | arrayThe IDs of of the component parts for composition. | No | [] |
autoMount | booleanWhether the client initializes the component on its own. Set it to false to mount it yourself with `mount()` | No | true |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-clipboard-root | <uid> |
data-copied | Present when copied state is true |
clipboard.label
Permalink to heading "clipboard.label"Labels the clipboard input. Renders a <label> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-clipboard-label | <uid> |
data-copied | Present when copied state is true |
clipboard.control
Permalink to heading "clipboard.control"Groups the input and trigger into a single layout container. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-clipboard-control | <uid> |
data-copied | Present when copied state is true |
clipboard.input
Permalink to heading "clipboard.input"Displays the text value that can be copied. Renders an <input> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-clipboard-input | <uid> |
data-copied | Present when copied state is true |
data-readonly | Present when read-only |
clipboard.trigger
Permalink to heading "clipboard.trigger"Copies the current value to the clipboard when activated. Renders a <button> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-clipboard-trigger | <uid> |
data-copied | Present when copied state is true |
clipboard.indicator
Permalink to heading "clipboard.indicator"Displays UI for a specific clipboard state like idle or copied. Renders a <span> element.
| Name | Description | Required | Default |
|---|---|---|---|
state | Enum\ClipboardIndicatorStateThe indicator state to render. | Yes | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Machine JavaScript API
Permalink to heading "Machine JavaScript API"| Name | Type | Description |
|---|---|---|
copied | boolean | Whether the value has been copied to the clipboard |
value | string | The value to be copied to the clipboard |
setValue | (value: string) => void | Set the value to be copied to the clipboard |
copy | VoidFunction | Copy the value to the clipboard |
getIndicatorState | (props: IndicatorProps) => IndicatorState | Returns the state of the indicator |