Clipboard

This component provides examples of how to use the clipboard functionality with different target elements.

Note Regarding Clipboard Feature:

Please note that clipboard functionality is only available in secure contexts (HTTPS) and may vary in support across different browsers. Ensure your website is served over HTTPS to utilize this feature reliably. Always provide alternative methods for users if clipboard operations are not supported in their browser environment.

  1. Simple Usage
   <button class="button clip-b" clip-b-d="Copy Text"></button>
   <!-- This button copies the text "Copy Text" when clicked. -->
  1. Target Element Usage
    - Example with input element:
     <input class="input" value="Copy Text">
     <button class="button clip-b" clip-b-t=".input"></button>
     <!-- This button copies the value of the input element with the class "input" when clicked. -->
     <div class="brave">HELLO 👋</div>
     <button class="button clip-b" clip-b-t=".brave" clip-b-v="textContent"></button>
     <!-- This button copies the text content of the div element with the class "brave" when clicked. -->
  1. Each Target Element with Index
    - Example with code elements:
     <pre type="html"><code>
        <h1>code 1</h1>
     </code></pre>
     <button class="button clip-b" clip-b-t="code" clip-b-e="index" clip-b-v="innerHTML"></button>
     <!-- This button copies the inner HTML of the code element corresponding to its index when clicked. -->

     <pre type="html"><code>
        <h1>code 2</h1>
     </code></pre>
     <button class="button clip-b" clip-b-t="code" clip-b-e="index" clip-b-v="innerHTML"></button>
     <!-- This button copies the inner HTML of the next code element corresponding to its index when clicked. -->

<pre type="html"><code>
        <h1>code 3</h1>
     </code></pre>
     <button class="button clip-b" clip-b-t="code" clip-b-e="index" clip-b-v="innerHTML"></button>
     <!-- This button copies the inner HTML of the next code element corresponding to its index when clicked. -->

Note

clip-b Class

The clip-b class is a fundamental component of the clipboard functionality within HTML documents. It enables the creation of buttons that, when clicked, facilitate the copying of specified text content to the clipboard. This class acts as a marker for identifying elements that trigger clipboard operations.

Syntax:

<button class="button clip-b"></button>

Attributes:

Usage:

  <button class="button clip-b" clip-b-d="Copy Text"></button>

This button, when clicked, will copy the text "Copy Text" to the clipboard.

  <button class="button clip-b" clip-b-t=".target-element" clip-b-v="textContent"></button>

This button will copy the text content of the element with the class target-element when clicked.

clip-b-d Attribute

The clip-b-d attribute is utilized in conjunction with the clip-b class to specify the data to be copied to the clipboard when the associated button is clicked. This attribute allows developers to define the content directly within the HTML markup, simplifying the implementation of basic clipboard functionality without the need for additional scripting.

Syntax:

<button class="button clip-b" clip-b-d="data"></button>

Attributes:

Usage:

  <button class="button clip-b" clip-b-d="Copy Text"></button>

This button will copy the text "Copy Text" to the clipboard when clicked.

  <button class="button clip-b" clip-b-d="<?php echo $dynamicText; ?>"></button>

Additional Notes:

Example:

<!-- Simple clipboard button with specified text -->
<button class="button clip-b" clip-b-d="Click to Copy"></button>

<!-- Dynamic content generation using server-side scripting -->
<?php
    $dynamicText = "Dynamic Text Content";
?>
<button class="button clip-b" clip-b-d="<?php echo $dynamicText; ?>"></button>

In summary, the clip-b-d attribute facilitates the easy implementation of basic clipboard functionality by allowing developers to specify the data to be copied directly within the HTML markup. This attribute is versatile and can be used with both static and dynamically generated content to provide users with a seamless copying experience.

clip-b-t Attribute

The clip-b-t attribute is an essential attribute associated with the clipboard component (clip-b). It specifies the target element(s) from which text content will be copied when the clipboard button is clicked. This attribute plays a crucial role in determining the source of the text data to be copied to the clipboard.

Syntax:

<button class="button clip-b" clip-b-t="targetSelector"></button>

Attributes:

Usage:

  <button class="button clip-b" clip-b-t=".input"></button>

This will copy the text content from the input field with the class input when the button is clicked.

  <button class="button clip-b" clip-b-t="p"></button>

This will copy text content from all <p> elements on the page.

Additional Notes:

Example:

<!-- Copy text from a specific paragraph -->
<p id="paragraph1">This is the text to be copied.</p>
<button class="button clip-b" clip-b-t="#paragraph1"></button>


<p>This is paragraph 2.</p>
<button class="button clip-b" clip-b-t="p"></button>

In summary, the clip-b-t attribute allows you to specify the target element(s) whose text content will be copied to the clipboard when the associated clipboard button is clicked. It provides flexibility in selecting the desired source of text data for copying.

clip-b-v Attribute

The clip-b-v attribute is an essential part of the clipboard functionality provided by the clip-b class. It allows developers to specify the content that will be copied to the clipboard when the associated button is clicked. This attribute provides flexibility in selecting the portion of content from the target element(s) to be included in the clipboard operation.

Syntax:

<button class="button clip-b" clip-b-v="value"></button>

Attributes:

Usage:

Additional Notes:

Example:

<!-- Copying text content from a paragraph -->
<p class="paragraph">Paragraph Content</p>
<button class="button clip-b" clip-b-t=".paragraph" clip-b-v="textContent"></button>

<!-- Copying inner HTML content from a div -->
<div class="content">Text Content to Copy</div>
<button class="button clip-b" clip-b-t=".content" clip-b-v="innerHTML"></button>

In summary, the clip-b-v attribute enhances the clipboard functionality by allowing developers to specify the value or property of the target element(s) to be copied to the clipboard. This attribute enables customization and flexibility in selecting the content for the clipboard operation, catering to various use cases and requirements.

clip-b-e Attribute

The clip-b-e attribute is an integral part of the clipboard functionality facilitated by the clip-b class. It specifies the method by which target elements are selected and their content is extracted for copying to the clipboard when the associated button is clicked. This attribute offers developers the flexibility to define the selection process based on specific requirements.

Syntax:

<button class="button clip-b" clip-b-e="method"></button>

Attributes:

Usage:

Additional Notes:

Example:

<!-- Selecting target element by index -->
<p class="paragraph">Paragraph 1</p>
<button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>
<p class="paragraph">Paragraph 2</p>
<button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>

In summary, the clip-b-e attribute allows developers to define how target elements are selected and content is extracted for clipboard operations. By providing various selection methods, developers can create tailored clipboard functionality to suit different use cases and user interactions.

The clip-b-e attribute defines the method for selecting target elements and extracting content for clipboard operations. It is important to understand how it functions correctly to avoid unintended behavior in clipboard functionality.

Let's break down the examples provided:

  1. This is wrong:
   <input value="Paragraph First">
   <button class="button clip-b" clip-b-t="input"></button>
   <p class="paragraph">Paragraph 1</p>
   <button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>
   <p class="paragraph">Paragraph 2</p>
   <button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>

In this example, the input field is separate from the paragraph elements, and the clipboard button targets the input instead of the paragraphs. This may not produce the desired clipboard behavior, as the input is not associated with the paragraph content.

  1. But this is fine:
   <input class="paragraph" value="Paragraph First">
   <button class="button clip-b" clip-b-t=".paragraph"></button>
   <p class="paragraph">Paragraph 1</p>
   <button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>
   <p class="paragraph">Paragraph 2</p>
   <button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>

In this corrected version, the input field is part of the paragraph elements, and the clipboard button targets the paragraph elements correctly, ensuring that the clipboard operation includes the intended content.

  1. This is wrong:
   <input class="paragraph" value="Paragraph First">
   <p class="paragraph">Paragraph 1</p>
   <p class="paragraph">Paragraph 2</p> 
   <button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>
   <button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>
   <button class="button clip-b" clip-b-t=".paragraph"></button>

In this example, the clipboard buttons with clip-b-e="index" are targeting specific paragraph elements, but the last button without clip-b-e does not have a specific target. This may result in inconsistent behavior in the clipboard operation.

  1. But this is fine:
   <input class="paragraph" value="Paragraph First">
   <p class="paragraph">Paragraph 1</p>
   <p class="paragraph">Paragraph 2</p> 
   <button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="value"></button>
   <button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>
   <button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b  -v="textContent"></button>

In this corrected version, all clipboard buttons target the paragraph elements correctly, and each button specifies the content to be copied to the clipboard using clip-b-v, ensuring consistent behavior across all buttons.

In summary, when using clip-b-e, ensure that each clipboard button has a clear and consistent target element, and specify the content to be copied accurately using clip-b-v to avoid unintended behavior in clipboard operations.

Clipboard Event Handling

The following documentation provides an example of how to handle events related to clipboard operations triggered by the clip-b class. This example demonstrates how to listen for clipboard events such as successful copying, copying completion, and error handling.

HTML Markup:

<!-- Event -->
<h1>Copy paragraph</h1>
<p class="paragraph">Paragraph Content</p>

<button class="button clip-b" clip-b-t=".paragraph" clip-b-e="index" clip-b-v="textContent"></button>

JavaScript Code:

<script>
    // Selecting elements
    var change =  document.querySelector("h1");
    var btn = document.querySelector(".clip-b");

    // Event listener for successful clipboard copy
    btn.addEventListener("clip-success", function() {
        change.textContent = "Copied!";
    });

    // Event listener for completion of clipboard operation
    btn.addEventListener("clip-after", function() {
        change.textContent = "Copy paragraph";
    });

    // Event listener for clipboard operation error
    btn.addEventListener("clip-error", function() {
        console.log("Error copying text to clipboard");
    });
</script>

Description:

This documentation provides a basic example of how to handle clipboard events in a web application using the clip-b class and corresponding event listeners. Developers can further customize event handling based on their specific requirements and user experience considerations.