htmgo/htmgo-site/ui/copy.go
maddalax 35877a1b2e
New Docs (#63)
* scripting enhancements

* tests

* cleanup / tests

* new docs wip

* add more docs

* more updates

* add caching docs

* add sse docs

* more docs

* sidebar, and fix navigation blocks

* remove old docs

* set proper meta

* fixes
2024-10-30 13:27:42 -05:00

33 lines
791 B
Go

package ui
import (
"fmt"
"github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/js"
)
func CopyButton(selector string, classes ...string) *h.Element {
classes = append(classes, "flex p-2 bg-slate-800 text-white cursor-pointer items-center")
return h.Div(
h.Class(classes...),
h.Text("Copy"),
h.OnClick(
// language=JavaScript
js.EvalJs(fmt.Sprintf(`
if(!navigator.clipboard) {
return;
}
let text = document.querySelector("%s").innerText;
navigator.clipboard.writeText(text);
self.innerText = "Copied!";
setTimeout(() => {
self.innerText = "Copy";
}, 1000);
`, selector)),
),
)
}
func AbsoluteCopyButton(selector string) *h.Element {
return CopyButton(selector, "absolute top-0 right-0 rounded-bl-md")
}