Debian Codegraph @

www/src/components/CDNSnippet.astro

raw
1 ---
2 import pkg from '../../../package.json';
3 import integrity from '../data/integrity.json';
4
5 const CDN_URL = `https://cdn.jsdelivr.net/npm/htmx.org@${pkg.version}/dist/htmx.min.js`;
6 const INTEGRITY = integrity.min;
7 ---
8
9 <!-- CDN one-liner: click anywhere to copy full <script> tag -->
10 <button
11 class="cdn-copy"
12 title="Copy CDN script tag"
13 data-cdn-url={CDN_URL}
14 data-integrity={INTEGRITY}
15 _={`on click
16 set url to my @data-cdn-url
17 set integrity to my @data-integrity
18 set tag to '<script src="' + url + '" integrity="' + integrity + '" crossorigin="anonymous"></script>'
19 writeText(tag) on navigator.clipboard
20 add .icon-\\[radix-icons--check\\] to first <i/> in me
21 remove .icon-\\[radix-icons--copy\\] from first <i/> in me
22 set @title of me to 'Copied!'
23 add @data-copied
24 wait 2s
25 add .icon-\\[radix-icons--copy\\] to first <i/> in me
26 remove .icon-\\[radix-icons--check\\] from first <i/> in me
27 set @title of me to 'Copy to clipboard'
28 remove @data-copied
29 end
30 on copy(event)
31 halt the event
32 set url to my @data-cdn-url
33 set integrity to my @data-integrity
34 set tag to '<script src="' + url + '" integrity="' + integrity + '" crossorigin="anonymous"></script>'
35 call event.clipboardData.setData('text/plain', tag)`}
36 >
37 <!-- Script tag with truncated URL display -->
38 <code
39 ><span class="tag">&lt;script</span
40 ><span class="attr"> src=</span
41 ><span class="val">"…/htmx.min.js"</span
42 ><span class="tag">&gt;&lt;/script&gt;</span
43 ></code>
44
45 <!-- Copy icon -->
46 <span class="icon-wrap">
47 <i class="icon-[radix-icons--copy]" aria-hidden="true"></i>
48 </span>
49
50 </button>
51
52 <style>
53 .cdn-copy {
54 display: flex;
55 align-items: center;
56 border: 1px solid light-dark(var(--color-neutral-200), var(--color-neutral-800));
57 border-radius: var(--radius-lg);
58 background: light-dark(var(--color-white), var(--color-neutral-950));
59 padding: 0.375rem 0.375rem 0.375rem 0.75rem;
60 cursor: pointer;
61 outline: none;
62 transition: border-color 0.15s, background 0.15s;
63
64 @media (min-width: 640px) {
65 padding: 0.5rem 0.5rem 0.5rem 1rem;
66 }
67
68 @media (hover: hover) {
69 &:hover {
70 border-color: light-dark(var(--color-neutral-400), var(--color-neutral-600));
71 background: light-dark(var(--color-neutral-50), var(--color-neutral-900));
72 }
73 }
74
75 &:is(:focus-visible, :active) {
76 border-color: light-dark(var(--color-neutral-400), var(--color-neutral-600));
77 background: light-dark(var(--color-neutral-50), var(--color-neutral-900));
78 }
79
80 code {
81 font-family: var(--font-mono);
82 font-size: 0.625rem;
83 line-height: 1.25rem;
84 white-space: nowrap;
85
86 @media (min-width: 640px) { font-size: 0.75rem; }
87 }
88
89 .tag { color: light-dark(var(--color-blue-600), var(--color-blue-400)); }
90 .attr { color: light-dark(var(--color-neutral-600), var(--color-neutral-400)); }
91 .val { color: light-dark(var(--color-emerald-600), var(--color-emerald-400)); }
92 }
93
94 .icon-wrap {
95 position: relative;
96 flex-shrink: 0;
97 margin-left: 0.375rem;
98 width: 1.5rem;
99 height: 1.5rem;
100 display: flex;
101 align-items: center;
102 justify-content: center;
103 border-radius: var(--radius-sm);
104 color: light-dark(var(--color-neutral-400), var(--color-neutral-600));
105 transition: color 0.15s;
106
107 @media (min-width: 640px) {
108 margin-left: 0.5rem;
109 width: 1.75rem;
110 height: 1.75rem;
111 }
112
113 i {
114 width: 0.875rem;
115 height: 0.875rem;
116
117 @media (min-width: 640px) {
118 width: 1rem;
119 height: 1rem;
120 }
121 }
122
123 /* "Copied!" tooltip */
124 &::before {
125 content: 'Copied!';
126 position: absolute;
127 bottom: 100%;
128 left: 50%;
129 transform: translateX(-50%);
130 margin-bottom: 0.25rem;
131 padding: 0.25rem 0.5rem;
132 font-size: 0.75rem;
133 font-weight: 600;
134 color: light-dark(white, black);
135 background: light-dark(var(--color-neutral-900), var(--color-neutral-100));
136 border-radius: var(--radius-sm);
137 white-space: nowrap;
138 opacity: 0;
139 pointer-events: none;
140 transition: opacity 0.15s;
141 }
142 }
143
144 /* Interact: icon color */
145 @media (hover: hover) {
146 .cdn-copy:hover .icon-wrap {
147 color: light-dark(black, white);
148 }
149 }
150
151 .cdn-copy:is(:focus-visible, :active) .icon-wrap {
152 color: light-dark(black, white);
153 }
154
155 /* Copied state: show tooltip */
156 .cdn-copy[data-copied] .icon-wrap::before {
157 opacity: 1;
158 }
159 </style>