Update v4.2 - New components, 10+ framework guides, and quality improvements. Visit Changelog

Tailwind CSS Copy Markup Plugin API

Headless, unstyled copy markup plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.

JavaScript Typescript

Installation

To get started, install Copy Markup plugin via npm, else you can skip this step if you are already using Preline UI as a package.

  1. Install the plugin

    Install @preline/copy-markup via npm

    Terminal
                              
                                npm i @preline/copy-markup
                              
                            
  2. Add the plugin CSS

    @import the plugin's CSS file into your Tailwind CSS file.

    main.css
                              
                                @import "tailwindcss";
                                
                                /* @preline/copy-markup */
                                /* [!code highlight:1] */
                                @import "./node_modules/@preline/copy-markup/theme.css";
                              
                            
  3. Add the plugin JavaScript

    Include the JavaScript <script> that powers the interactive elements near the end of your </body> tag:

    HTML
                              
                            

    Additional Initialization Options

    Use the non-auto entry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.

    HTML (non-auto)
                              
                            

    Via bundler

    When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module.

    JavaScript (auto)
                              
                                import "@preline/copy-markup";
                              
                            
    JavaScript (non-auto)
                              
                                import HSCopyMarkup from "@preline/copy-markup/non-auto";
                                
                                HSCopyMarkup.autoInit();
                                
                                // Or initialize a specific element manually
                                const el = document.querySelector("#copy-markup");
                                if (el) new HSCopyMarkup(el);
                              
                            

Example

Add input fields.

Basic usage

Prefer to create your own style? Here is a completely unstylized example.

HTML
                      
                        <div id="hs-wrapper-for-copy">
                          <input id="hs-content-for-copy" type="text" class="py-3 px-4 block w-full border-stone-200 rounded-lg text-sm text-stone-800 focus:z-10 dark:bg-neutral-800 dark:border-neutral-700 dark:text-neutral-200 dark:placeholder:text-neutral-500" placeholder="Enter Name">
                        </div>

                        <button type="button" data-hs-copy-markup='{
                            "targetSelector": "#hs-content-for-copy",
                            "wrapperSelector": "#hs-wrapper-for-copy",
                            "limit": 3
                          }' id="hs-copy-content">
                          Add Name
                        </button>
                      
                    

Data Options

Name Description Options Default value
data-hs-copy-markup Activate a Copy Markup by specifying on an element. Should be added to the button (trigger).
:targetSelector Specifies the target element to copy. The value must be a valid selector. string
:wrapperSelector Specifies which element should be used for copying. The value must be a valid selector. string
:limit Specifies how many items you can copy until the button is disabled. number null

Methods

The HSCopyMarkup object is contained within the global window object

Method Description
Public methods
delete(target) Remove the element associated to the target.
  • target should be a Node
destroy() Destroys the instance, removes generated markup (if any), removes added classes and attributes.
Static methods
HSCopyMarkup.getInstance(target, isInstance) Returns the element associated to the target.
  • target should be a Node or string (valid selector)
  • isInstance boolean. Returns the instance instead of Node if true

Open item (public method).

JavaScript
                      
                        const copyMarkup = new HSCopyMarkup(document.querySelector('#copy-markup'));
                        const deleteBtn = document.querySelector('#delete-btn');

                        deleteBtn.addEventListener('click', () => {
                          copyMarkup.delete(document.querySelector('#copy-markup-item-1'));
                        });
                      
                    

Open item (mixed).

JavaScript
                      
                        const { element } = HSCopyMarkup.getInstance('#copy-markup', true);
                        const deleteBtn = document.querySelector('#delete-btn');

                        deleteBtn.addEventListener('click', () => {
                          element.delete(document.querySelector('#copy-markup-item-1'));
                        });
                      
                    

Events

Method Description Returned value
on:copy Called when target element was copied. Copied element
on:delete Called when target element was deleted. Deleted element

Call any function on copy example.

JavaScript
                      
                        const el = HSCopyMarkup.getInstance('#copy-markup');

                        el.on('copy', (copiedElement) => {...});
                      
                    

Ready to use Components

Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.

Copy Markup

Explore ready-to-use Tailwind CSS examples built with Preline UI.

View Copy Markup examples

Copy Markup

© 2026 Preline Labs.