<!-- https://abap2ui5.github.io/docs/cookbook/browser_interaction/clipboard -->

# Clipboard

Copy arbitrary text to the user's clipboard with the frontend event `client->cs_event-clipboard_copy`. The text travels as the first `t_arg` entry and is passed to the browser's `navigator.clipboard.writeText` API, which performs the copy entirely in the browser.

```abap
CLASS z2ui5_cl_sample_clipboard DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.
    DATA mv_text TYPE string.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS z2ui5_cl_sample_clipboard IMPLEMENTATION.
  METHOD z2ui5_if_app~main.

    CASE abap_true.

      WHEN client->check_on_init( ).
        mv_text = `Hello from abap2UI5`.

        DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
            )->ele( n = `View` ns = `mvc`
                )->a( n = `xmlns`     v = `sap.m`
                )->a( n = `xmlns:mvc` v = `sap.ui.core.mvc`

                )->ele( `Page`
                    )->tag( `Input`
                        )->a( n = `value` v = client->_bind( mv_text )
                    )->tag( `Button`
                        )->a( n = `text`  v = `copy to clipboard`
                        )->a( n = `press` v = client->_event( `COPY` ) ).

        client->view_display( view->stringify( ) ).

      WHEN client->check_on_event( `COPY` ).
        client->follow_up_action(
            val   = client->cs_event-clipboard_copy
            t_arg = VALUE #( ( mv_text ) ) ).
        client->message_toast_display( `copied` ).

    ENDCASE.

  ENDMETHOD.
ENDCLASS.
```

## Copy the App State URL

To share the current app state instead of a custom string, compose the link with `client->app_state_get_href( )` and hand it to the same `clipboard_copy` action — see [App State](../event_navigation/navigation/app_state.md).

::: warning
The browser's Clipboard API requires HTTPS (or `localhost`). On plain HTTP the call is silently ignored.
:::

<!-- samples:start (generated by scripts/link-samples.mjs — do not edit) -->

## Working Samples

Complete apps from the [sample catalog](https://github.com/abap2UI5/samples/blob/main/SAMPLES.md)
that use what this page describes. Each is a single class — pull the repository with
[abapGit](https://abapgit.org) and start it with `?app_start=<class>`.

| Sample | Class |
|---|---|
| Copy to Clipboard (A) | [`Z2UI5_CL_SMP_APP_325`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_325.clas.abap) |
| Local and Session Storage (A,C) | [`Z2UI5_CL_SMP_APP_327`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_327.clas.abap) |

<!-- samples:end -->
