Skip to content

Commit 3cdd410

Browse files
authored
Add copyable element
1 parent dcd80bf commit 3cdd410

15 files changed

Lines changed: 196 additions & 14 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A collection of unstyled, headless custom elements designed to make building ric
55
## Overview
66

77
**Hyperkit**
8-
Version: 0.0.1
8+
Version: 0.0.2
99
[Github](https://github.com/hyperlaunch/hyperkit)
1010

1111
**Headless Elements, Supercharged UIs**

packages/docs/markdoc.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ export default defineMarkdocConfig({
110110
"./src/astro-components/Examples/FieldsetRepeater/TransitionFieldsetRepeater.astro",
111111
),
112112
},
113+
copyable: {
114+
render: component("./src/astro-components/Examples/Copyable.astro"),
115+
},
113116
},
114117
extends: [
115118
shiki({

packages/docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hyperkitxyz/docs",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"dependencies": {
55
"@fontsource/poppins": "^5.1.0",
66
"@hyperkitxyz/elements": "workspace:*",

packages/docs/src/assets/robot.png

-4.13 MB
Binary file not shown.
1.14 MB
Loading
1.3 MB
Loading

packages/docs/src/astro-components/ComponentNavItem.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ type Props = {
44
};
55
66
const { href } = Astro.props;
7-
const active = Astro.url.pathname === href;
7+
const normalize = (str: string) => str.replace(/^\/+|\/+$/g, "");
8+
const active = normalize(Astro.url.pathname) === normalize(href);
89
---
910

1011
<li class="pb-2 last:pb-0">
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<hyperkit-copyable value="https://www.hyperkit.xyz/">
2+
<button
3+
class="group px-4 py-2 bg-white text-xs text-gray-800 rounded shadow hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-all duration-200"
4+
>
5+
<span class="group-data-[copied]:hidden">Copy</span>
6+
<span class="hidden group-data-[copied]:block">Copied</span>
7+
</button>
8+
</hyperkit-copyable>
9+
<script>
10+
import "@hyperkitxyz/elements/copyable.ts";
11+
</script>

packages/docs/src/astro-components/Layout.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const pageTitle = [title, name].filter(Boolean).join(" — ");
7373
<span
7474
class="px-2 py-1 text-xs font-medium shadow-sm rounded-full bg-zinc-300 dark:bg-zinc-700 text-zinc-800 dark:text-zinc-200"
7575
>
76-
0.0.1
76+
0.0.2
7777
</span>
7878
</h1>
7979

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
name: Copyable
3+
tagline: A button that allows easy copying of text to the clipboard.
4+
thumbnail: "./thumbnails/modal.png"
5+
---
6+
7+
The `<hyperkit-copyable />` element provides an easy way to copy specified text to the user's clipboard. It wraps a button element, which, when clicked, copies the provided text value to the clipboard. Additionally, it fires a `copied` event when the action is successful and sets a `data-copied` attribute on the button.
8+
9+
{% example hero=true %}{% copyable / %}{% /example %}
10+
11+
## Usage
12+
13+
Import the JS:
14+
```js
15+
import "@hyperkitxyz/elements/copyable";
16+
```
17+
18+
Tag:
19+
```html
20+
<hyperkit-copyable value="Text to copy">
21+
<button>Copy</button>
22+
</hyperkit-copyable>
23+
```
24+
25+
### Options
26+
27+
{% table %}
28+
* Attribute
29+
* Value
30+
*
31+
---
32+
* `value`
33+
* String _(required)_
34+
* Specifies the text that will be copied to the clipboard when the button is clicked.
35+
---
36+
{% /table %}
37+
38+
### Children
39+
40+
```html
41+
<hyperkit-copyable value="Text to copy">
42+
<button>Copy</button>
43+
</hyperkit-copyable>
44+
```
45+
46+
#### `<button />`
47+
* The button that triggers the copy action when clicked.
48+
* Can be styled according to your needs and can contain any text or content.
49+
50+
## JavaScript API
51+
52+
The `hyperkit-copyable` element provides a simple JavaScript API to interact with its functionality and listen for events.
53+
54+
### Listening for the `copied` Event
55+
56+
The element emits a `copied` event whenever the text is successfully copied to the clipboard. You can listen for this event to respond to the copy action:
57+
58+
```js
59+
const copyable = document.querySelector("hyperkit-copyable");
60+
61+
copyable.on("copied", (event) => {
62+
console.log("Copied value:", event.detail.value);
63+
});
64+
```
65+
66+
## Examples
67+
68+
### Styling the button
69+
70+
This example shows how you can use the data-copied attribute to toggle the visible text when the button is used.
71+
72+
{% example %}{% copyable / %}{% /example %}
73+
74+
```html
75+
<hyperkit-copyable value="https://www.hyperkit.xyz/">
76+
<button
77+
class="group px-4 py-2 bg-white text-xs text-gray-800 rounded shadow hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-all duration-200"
78+
>
79+
<span class="group-data-[copied]:hidden">Copy</span>
80+
<span class="hidden group-data-[copied]:block">Copied</span>
81+
</button>
82+
</hyperkit-copyable>
83+
```

0 commit comments

Comments
 (0)